11using ModelContextProtocol . Server ;
22using System . ComponentModel ;
3- using System . Net . Http . Headers ;
3+ using System . Net . Http . Json ;
44using System . Text . Json ;
55
66namespace QuickstartWeatherServer . Tools ;
@@ -10,75 +10,44 @@ public static class WeatherTools
1010{
1111 [ McpServerTool , Description ( "Get weather alerts for a US state." ) ]
1212 public static async Task < string > GetAlerts (
13+ HttpClient client ,
1314 [ Description ( "The US state to get alerts for." ) ] string state )
1415 {
15- using HttpClient client = GetWeatherClient ( ) ;
16-
17- var response = await client . GetAsync ( $ "/alerts/active/area/{ state } ") ;
18-
19- if ( ! response . IsSuccessStatusCode )
20- {
21- return "Failed to retrieve alerts." ;
22- }
23-
24- var json = await response . Content . ReadAsStringAsync ( ) ;
25- var jsonElement = JsonSerializer . Deserialize < JsonElement > ( json ) ;
16+ var jsonElement = await client . GetFromJsonAsync < JsonElement > ( $ "/alerts/active/area/{ state } ") ;
2617 var alerts = jsonElement . GetProperty ( "features" ) . EnumerateArray ( ) ;
2718
2819 if ( ! alerts . Any ( ) )
2920 {
3021 return "No active alerts for this state." ;
3122 }
3223
33- // Process the alerts and return a formatted string
34- var alertMessages = new List < string > ( ) ;
35- foreach ( var alert in alerts )
24+ return string . Join ( "\n --\n " , alerts . Select ( alert =>
3625 {
3726 JsonElement properties = alert . GetProperty ( "properties" ) ;
38- alertMessages . Add ( $ """
27+ return $ """
3928 Event: { properties . GetProperty ( "event" ) . GetString ( ) }
4029 Area: { properties . GetProperty ( "areaDesc" ) . GetString ( ) }
4130 Severity: { properties . GetProperty ( "severity" ) . GetString ( ) }
4231 Description: { properties . GetProperty ( "description" ) . GetString ( ) }
4332 Instruction: { properties . GetProperty ( "instruction" ) . GetString ( ) }
44- """ ) ;
45- }
46- return string . Join ( "\n ---\n " , alertMessages ) ;
33+ """ ;
34+ } ) ) ;
4735 }
4836
4937 [ McpServerTool , Description ( "Get weather forecast for a location." ) ]
5038 public static async Task < string > GetForecast (
39+ HttpClient client ,
5140 [ Description ( "Latitude of the location." ) ] double latitude ,
5241 [ Description ( "Longitude of the location." ) ] double longitude )
5342 {
54- using HttpClient client = GetWeatherClient ( ) ;
55- var response = await client . GetAsync ( $ "/points/{ latitude } ,{ longitude } ") ;
56- if ( ! response . IsSuccessStatusCode )
57- {
58- return "Failed to retrieve forecast." ;
59- }
60-
61- var json = await response . Content . ReadAsStringAsync ( ) ;
62- var jsonElement = JsonSerializer . Deserialize < JsonElement > ( json ) ;
43+ var jsonElement = await client . GetFromJsonAsync < JsonElement > ( $ "/points/{ latitude } ,{ longitude } ") ;
6344 var periods = jsonElement . GetProperty ( "properties" ) . GetProperty ( "periods" ) . EnumerateArray ( ) ;
64- // Process the forecast and return a formatted string
65- var forecastMessages = new List < string > ( ) ;
66- foreach ( var period in periods )
67- {
68- forecastMessages . Add ( $ """
69- { period . GetProperty ( "name" ) . GetString ( ) }
70- Temperature: { period . GetProperty ( "temperature" ) . GetInt32 ( ) } °F
71- Wind: { period . GetProperty ( "windSpeed" ) . GetString ( ) } { period . GetProperty ( "windDirection" ) . GetString ( ) }
72- Forecast: { period . GetProperty ( "detailedForecast" ) . GetString ( ) }
73- """ ) ;
74- }
75- return string . Join ( "\n ---\n " , forecastMessages ) ;
76- }
7745
78- private static HttpClient GetWeatherClient ( )
79- {
80- var client = new HttpClient ( ) { BaseAddress = new Uri ( "https://api.weather.gov" ) } ;
81- client . DefaultRequestHeaders . UserAgent . Add ( new ProductInfoHeaderValue ( "weather-tool" , "1.0" ) ) ;
82- return client ;
46+ return string . Join ( "\n ---\n " , periods . Select ( period => $ """
47+ { period . GetProperty ( "name" ) . GetString ( ) }
48+ Temperature: { period . GetProperty ( "temperature" ) . GetInt32 ( ) } °F
49+ Wind: { period . GetProperty ( "windSpeed" ) . GetString ( ) } { period . GetProperty ( "windDirection" ) . GetString ( ) }
50+ Forecast: { period . GetProperty ( "detailedForecast" ) . GetString ( ) }
51+ """ ) ) ;
8352 }
8453}
0 commit comments