get_forecast
Retrieve weather forecasts for specific locations by providing latitude and longitude coordinates.
Instructions
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)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| latitude | Yes | ||
| longitude | Yes |
Implementation Reference
- weather.py:158-190 (handler)The handler function for the 'get_forecast' tool. It validates coordinates, fetches forecast data using a helper, formats the next 10 forecast periods including temperature, wind, humidity, precipitation, and detailed forecast, and returns a formatted string.@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)Helper function that fetches raw forecast data from the National Weather Service (NWS) API for given coordinates, adds location information, and returns the data or None on error.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 registers the get_forecast function as an MCP tool.@mcp.tool()