1+ using  ModelContextProtocol . Server ; 
2+ using  System . ComponentModel ; 
3+ using  System . Net . Http . Headers ; 
4+ using  System . Text . Json ; 
5+ 
6+ namespace  QuickstartWeatherServer . Tools ; 
7+ 
8+ [ McpServerToolType ] 
9+ public  static   class  WeatherTools 
10+ { 
11+     [ McpServerTool ,  Description ( "Get weather alerts for a US state." ) ] 
12+     public  static   async  Task < string >  GetAlerts ( 
13+         [ Description ( "The US state to get alerts for." ) ]  string  state ) 
14+     { 
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 ) ; 
26+         var  alerts  =  jsonElement . GetProperty ( "features" ) . EnumerateArray ( ) ; 
27+ 
28+         if  ( ! alerts . Any ( ) ) 
29+         { 
30+             return  "No active alerts for this state." ; 
31+         } 
32+ 
33+         // Process the alerts and return a formatted string 
34+         var  alertMessages  =  new  List < string > ( ) ; 
35+         foreach  ( var  alert  in  alerts ) 
36+         { 
37+             JsonElement  properties  =  alert . GetProperty ( "properties" ) ; 
38+             alertMessages . Add ( $ """ 
39+                     Event: { properties . GetProperty ( "event" ) . GetString ( ) }  
40+                     Area: { properties . GetProperty ( "areaDesc" ) . GetString ( ) }  
41+                     Severity: { properties . GetProperty ( "severity" ) . GetString ( ) }  
42+                     Description: { properties . GetProperty ( "description" ) . GetString ( ) }  
43+                     Instruction: { properties . GetProperty ( "instruction" ) . GetString ( ) }  
44+                     """  ) ; 
45+         } 
46+         return  string . Join ( "\n ---\n " ,  alertMessages ) ; 
47+     } 
48+ 
49+     [ McpServerTool ,  Description ( "Get weather forecast for a location." ) ] 
50+     public  static   async  Task < string >  GetForecast ( 
51+         [ Description ( "Latitude of the location." ) ]  double  latitude , 
52+         [ Description ( "Longitude of the location." ) ]  double  longitude ) 
53+     { 
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 ) ; 
63+         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+     } 
77+ 
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 ; 
83+     } 
84+ } 
0 commit comments