get_forecast
Retrieve real-time weather forecasts for specific locations by providing latitude and longitude coordinates. Fetches data from the National Weather Service API via the Weather MCP Server.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| latitude | Yes | ||
| longitude | Yes |
Implementation Reference
- tools/weather_tools.py:20-44 (handler)The core handler function implementing the 'get_forecast' tool logic. It fetches the weather forecast for given coordinates from the National Weather Service API, formats the first 5 forecast periods, and returns them as a string.@mcp.tool() async def get_forecast(latitude: float, longitude: float) -> str: url = f"{NWS_API_BASE}/points/{latitude},{longitude}" data = await make_nws_request(url) if not data: return "Unable to fetch forecast data for this location." forecast_url = data["properties"]["forecast"] forecast_data = await make_nws_request(forecast_url) if not forecast_data: return "Unable to fetch detailed forecast." periods = forecast_data["properties"]["periods"] forecasts = [] for period in periods[:5]: forecast = f""" {period['name']}: Temperature: {period['temperature']}°{period['temperatureUnit']} Wind: {period['windSpeed']} {period['windDirection']} Forecast: {period['detailedForecast']} """ forecasts.append(forecast) return "\n---\n".join(forecasts)
- utils/weather_utils.py:7-16 (helper)Helper utility function used by the get_forecast handler to make asynchronous HTTP requests to the NWS API endpoints.async def make_nws_request(url: str) -> dict[str, Any] | None: headers = {"User-Agent": USER_AGENT, "Accept": "application/geo+json"} async with httpx.AsyncClient() as client: try: response = await client.get(url, headers=headers, timeout=30.0) response.raise_for_status() return response.json() except Exception: return None
- tools/weather_tools.py:4-4 (registration)The MCP server instance creation and tool registration via @mcp.tool() decorator for get_forecast.mcp = FastMCP("weather")