get_forecast
Retrieve weather forecast data for any US location by providing latitude and longitude coordinates to access National Weather Service information.
Instructions
Get weather forecast for a location.
Args:
latitude: Latitude of the location
longitude: Longitude of the location
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| latitude | Yes | ||
| longitude | Yes |
Input Schema (JSON Schema)
{
"properties": {
"latitude": {
"title": "Latitude",
"type": "number"
},
"longitude": {
"title": "Longitude",
"type": "number"
}
},
"required": [
"latitude",
"longitude"
],
"type": "object"
}
Implementation Reference
- weather.py:158-190 (handler)The handler function for the 'get_forecast' MCP tool. It validates coordinates, fetches forecast data via helper, formats the next 10 forecast periods into a readable string response, and returns it. Registered via @mcp.tool() decorator.@mcp.tool() async def get_forecast(latitude: float, longitude: float) -> str: """Get weather forecast for a location using coordinates. Args: latitude: Latitude of the location (-90 to 90) longitude: Longitude of the location (-180 to 180) """ if not validate_coordinates(latitude, longitude): return "Error: Invalid coordinates. Latitude must be between -90 and 90, longitude between -180 and 180." forecast_data = await get_forecast_data(latitude, longitude) if not forecast_data: return f"Unable to fetch forecast data for coordinates ({latitude}, {longitude}). The location may be outside NWS coverage area (US only)." periods = forecast_data["properties"]["periods"] location = forecast_data.get("_location", {}) location_str = f"{location.get('city', 'Unknown')}, {location.get('state', 'Unknown')}" forecasts = [] for period in periods[:10]: # Show next 10 periods (5 days) forecast = f""" {period['name']}: Temperature: {period['temperature']}°{period['temperatureUnit']} Wind: {period['windSpeed']} {period['windDirection']} {f"Humidity: {period.get('relativeHumidity', {}).get('value', 'N/A')}%" if period.get('relativeHumidity') else ""} {f"Precipitation: {period.get('probabilityOfPrecipitation', {}).get('value', 0)}%" if period.get('probabilityOfPrecipitation') else ""} Forecast: {period['detailedForecast']} """ forecasts.append(forecast) return f"Weather Forecast for {location_str}:\n" + "\n---\n".join(forecasts)
- weather.py:129-155 (helper)Core helper function that fetches raw forecast data from NWS API (/points and /forecast endpoints), adds location info, and returns the data dict or None on failure. Called by get_forecast and other tools.async def get_forecast_data(latitude: float, longitude: float) -> dict[str, Any] | None: """Get forecast data for coordinates. Returns None on error.""" if not validate_coordinates(latitude, longitude): return None points_url = f"{NWS_API_BASE}/points/{latitude},{longitude}" points_data = await make_nws_request(points_url) if not points_data or "properties" not in points_data: return None forecast_url = points_data["properties"].get("forecast") if not forecast_url: return None forecast_data = await make_nws_request(forecast_url) if not forecast_data or "properties" not in forecast_data: return None # Add location info to the forecast data location_info = points_data["properties"].get("relativeLocation", {}) forecast_data["_location"] = { "city": location_info.get("properties", {}).get("city", "Unknown"), "state": location_info.get("properties", {}).get("state", "Unknown") } return forecast_data
- weather.py:158-158 (registration)The @mcp.tool() decorator above get_forecast registers it as an MCP tool with FastMCP, using function signature and docstring for input schema.@mcp.tool()