@@ -143,6 +143,7 @@ Add these to the top of your `weather.py`:
143143
144144``` python
145145from typing import Any
146+
146147import httpx
147148from mcp.server.fastmcp import FastMCP
148149
@@ -163,27 +164,25 @@ Next, let's add our helper functions for querying and formatting the data from t
163164``` python
164165async def make_nws_request (url : str ) -> dict[str , Any] | None :
165166 """ Make a request to the NWS API with proper error handling."""
166- headers = {
167- " User-Agent" : USER_AGENT ,
168- " Accept" : " application/geo+json"
169- }
170- async with httpx.AsyncClient(follow_redirects = True ) as client:
167+ headers = {" User-Agent" : USER_AGENT , " Accept" : " application/geo+json" }
168+ async with httpx.AsyncClient() as client:
171169 try :
172170 response = await client.get(url, headers = headers, timeout = 30.0 )
173171 response.raise_for_status()
174172 return response.json()
175173 except Exception :
176174 return None
177175
176+
178177def format_alert (feature : dict ) -> str :
179178 """ Format an alert feature into a readable string."""
180179 props = feature[" properties" ]
181180 return f """
182- Event: { props.get(' event' , ' Unknown' )}
183- Area: { props.get(' areaDesc' , ' Unknown' )}
184- Severity: { props.get(' severity' , ' Unknown' )}
185- Description: { props.get(' description' , ' No description available' )}
186- Instructions: { props.get(' instruction' , ' No specific instructions provided' )}
181+ Event: { props.get(" event" , " Unknown" )}
182+ Area: { props.get(" areaDesc" , " Unknown" )}
183+ Severity: { props.get(" severity" , " Unknown" )}
184+ Description: { props.get(" description" , " No description available" )}
185+ Instructions: { props.get(" instruction" , " No specific instructions provided" )}
187186"""
188187```
189188
@@ -211,13 +210,14 @@ async def get_alerts(state: str) -> str:
211210 alerts = [format_alert(feature) for feature in data[" features" ]]
212211 return " \n ---\n " .join(alerts)
213212
213+
214214@mcp.tool ()
215215async def get_forecast (latitude : float , longitude : float ) -> str :
216216 """ Get weather forecast for a location.
217217
218218 Args:
219- latitude: Latitude of the location (recommended: up to 4 decimal places)
220- longitude: Longitude of the location (recommended: up to 4 decimal places)
219+ latitude: Latitude of the location
220+ longitude: Longitude of the location
221221 """
222222 # First get the forecast grid endpoint
223223 points_url = f " { NWS_API_BASE } /points/ { latitude} , { longitude} "
@@ -238,10 +238,10 @@ async def get_forecast(latitude: float, longitude: float) -> str:
238238 forecasts = []
239239 for period in periods[:5 ]: # Only show next 5 periods
240240 forecast = f """
241- { period[' name' ]} :
242- Temperature: { period[' temperature' ]} ° { period[' temperatureUnit' ]}
243- Wind: { period[' windSpeed' ]} { period[' windDirection' ]}
244- Forecast: { period[' detailedForecast' ]}
241+ { period[" name" ]} :
242+ Temperature: { period[" temperature" ]} ° { period[" temperatureUnit" ]}
243+ Wind: { period[" windSpeed" ]} { period[" windDirection" ]}
244+ Forecast: { period[" detailedForecast" ]}
245245"""
246246 forecasts.append(forecast)
247247
@@ -255,7 +255,8 @@ Finally, let's initialize and run the server:
255255``` python
256256def main ():
257257 # Initialize and run the server
258- mcp.run(transport = ' stdio' )
258+ mcp.run(transport = " stdio" )
259+
259260
260261if __name__ == " __main__" :
261262 main()
0 commit comments