11from typing import Any
2+
23import httpx
34from mcp .server .fastmcp import FastMCP
45
910NWS_API_BASE = "https://api.weather.gov"
1011USER_AGENT = "weather-app/1.0"
1112
13+
1214async def make_nws_request (url : str ) -> dict [str , Any ] | None :
1315 """Make a request to the NWS API with proper error handling."""
14- headers = {
15- "User-Agent" : USER_AGENT ,
16- "Accept" : "application/geo+json"
17- }
16+ headers = {"User-Agent" : USER_AGENT , "Accept" : "application/geo+json" }
1817 async with httpx .AsyncClient () as client :
1918 try :
2019 response = await client .get (url , headers = headers , timeout = 30.0 )
@@ -23,17 +22,19 @@ async def make_nws_request(url: str) -> dict[str, Any] | None:
2322 except Exception :
2423 return None
2524
25+
2626def format_alert (feature : dict ) -> str :
2727 """Format an alert feature into a readable string."""
2828 props = feature ["properties" ]
2929 return f"""
30- Event: { props .get (' event' , ' Unknown' )}
31- Area: { props .get (' areaDesc' , ' Unknown' )}
32- Severity: { props .get (' severity' , ' Unknown' )}
33- Description: { props .get (' description' , ' No description available' )}
34- Instructions: { props .get (' instruction' , ' No specific instructions provided' )}
30+ Event: { props .get (" event" , " Unknown" )}
31+ Area: { props .get (" areaDesc" , " Unknown" )}
32+ Severity: { props .get (" severity" , " Unknown" )}
33+ Description: { props .get (" description" , " No description available" )}
34+ Instructions: { props .get (" instruction" , " No specific instructions provided" )}
3535"""
3636
37+
3738@mcp .tool ()
3839async def get_alerts (state : str ) -> str :
3940 """Get weather alerts for a US state.
@@ -53,6 +54,7 @@ async def get_alerts(state: str) -> str:
5354 alerts = [format_alert (feature ) for feature in data ["features" ]]
5455 return "\n ---\n " .join (alerts )
5556
57+
5658@mcp .tool ()
5759async def get_forecast (latitude : float , longitude : float ) -> str :
5860 """Get weather forecast for a location.
@@ -80,18 +82,20 @@ async def get_forecast(latitude: float, longitude: float) -> str:
8082 forecasts = []
8183 for period in periods [:5 ]: # Only show next 5 periods
8284 forecast = f"""
83- { period [' name' ]} :
84- Temperature: { period [' temperature' ]} °{ period [' temperatureUnit' ]}
85- Wind: { period [' windSpeed' ]} { period [' windDirection' ]}
86- Forecast: { period [' detailedForecast' ]}
85+ { period [" name" ]} :
86+ Temperature: { period [" temperature" ]} °{ period [" temperatureUnit" ]}
87+ Wind: { period [" windSpeed" ]} { period [" windDirection" ]}
88+ Forecast: { period [" detailedForecast" ]}
8789"""
8890 forecasts .append (forecast )
8991
9092 return "\n ---\n " .join (forecasts )
9193
94+
9295def main ():
9396 # Initialize and run the server
94- mcp .run (transport = 'stdio' )
97+ mcp .run (transport = "stdio" )
98+
9599
96100if __name__ == "__main__" :
97101 main ()
0 commit comments