1
+ using ModelContextProtocol ;
1
2
using ModelContextProtocol . Server ;
2
3
using System . ComponentModel ;
3
4
using System . Net . Http . Json ;
6
7
namespace QuickstartWeatherServer . Tools ;
7
8
8
9
[ McpServerToolType ]
9
- public static class WeatherTools
10
+ public sealed class WeatherTools
10
11
{
11
12
[ McpServerTool , Description ( "Get weather alerts for a US state." ) ]
12
13
public static async Task < string > GetAlerts (
13
14
HttpClient client ,
14
15
[ Description ( "The US state to get alerts for." ) ] string state )
15
16
{
16
- var jsonElement = await client . GetFromJsonAsync < JsonElement > ( $ "/alerts/active/area/{ state } ") ;
17
+ using var jsonDocument = await client . ReadJsonDocumentAsync ( $ "/alerts/active/area/{ state } ") ;
18
+ var jsonElement = jsonDocument . RootElement ;
17
19
var alerts = jsonElement . GetProperty ( "features" ) . EnumerateArray ( ) ;
18
20
19
21
if ( ! alerts . Any ( ) )
@@ -40,7 +42,8 @@ public static async Task<string> GetForecast(
40
42
[ Description ( "Latitude of the location." ) ] double latitude ,
41
43
[ Description ( "Longitude of the location." ) ] double longitude )
42
44
{
43
- var jsonElement = await client . GetFromJsonAsync < JsonElement > ( $ "/points/{ latitude } ,{ longitude } ") ;
45
+ using var jsonDocument = await client . ReadJsonDocumentAsync ( $ "/points/{ latitude } ,{ longitude } ") ;
46
+ var jsonElement = jsonDocument . RootElement ;
44
47
var periods = jsonElement . GetProperty ( "properties" ) . GetProperty ( "periods" ) . EnumerateArray ( ) ;
45
48
46
49
return string . Join ( "\n ---\n " , periods . Select ( period => $ """
0 commit comments