weather_forecast
Fetch accurate weather forecasts for 1-14 days using location details like city name, postal code, or coordinates. Ideal for planning activities and updates.
Instructions
Get weather forecast (1-14 days) for a location
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| days | No | Number of days (1-14) | |
| q | Yes | Location query (city name, lat/lon, postal code, etc) |
Implementation Reference
- server.py:185-193 (handler)Handler logic for the weather_forecast tool: extracts and validates location (q) and days parameters, fetches forecast data from WeatherAPI, and prepares JSON response.elif tool_name == "weather_forecast": q = arguments.get("q") days = arguments.get("days", 1) if not q: raise ValueError("Location (q) is required") if days < 1 or days > 14: raise ValueError("Days must be between 1 and 14") result = await fetch("forecast.json", {"q": q, "days": days}) content = json.dumps(result, indent=2)
- server.py:131-145 (schema)Input schema defining the parameters for weather_forecast: required 'q' (location string) and optional 'days' (integer, default 1)."inputSchema": { "type": "object", "properties": { "q": { "type": "string", "description": "Location query (city name, lat/lon, postal code, etc)" }, "days": { "type": "integer", "description": "Number of days (1-14)", "default": 1 } }, "required": ["q"] }
- server.py:128-146 (registration)Registration of the weather_forecast tool in the tools/list MCP method response, including name, description, and schema.{ "name": "weather_forecast", "description": "Get weather forecast (1-14 days) for a location", "inputSchema": { "type": "object", "properties": { "q": { "type": "string", "description": "Location query (city name, lat/lon, postal code, etc)" }, "days": { "type": "integer", "description": "Number of days (1-14)", "default": 1 } }, "required": ["q"] } },
- server.py:42-80 (helper)Shared helper function fetch() that performs the HTTP request to WeatherAPI, handles errors, and returns JSON data. Used by the weather_forecast handler.async def fetch(endpoint: str, params: dict) -> dict: """Perform async GET to WeatherAPI and return JSON.""" logger.debug(f"fetch() called with endpoint: {endpoint}, params: {params}") if not WEATHER_API_KEY: logger.error("Weather API key not set.") raise Exception("Weather API key not set.") params["key"] = WEATHER_API_KEY url = f"https://api.weatherapi.com/v1/{endpoint}" logger.info(f"Requesting {url}") async with httpx.AsyncClient() as client: logger.debug("HTTPx client created") try: resp = await client.get(url, params=params) logger.debug(f"HTTP response received: status={resp.status_code}") if resp.status_code != 200: try: error_data = resp.json() detail = error_data.get("error", {}).get("message", resp.text) except: detail = resp.text logger.error(f"WeatherAPI error {resp.status_code}: {detail}") raise Exception(f"WeatherAPI error {resp.status_code}: {detail}") data = resp.json() logger.debug(f"JSON parsing successful") logger.info(f"WeatherAPI success: {url}") return data except httpx.RequestError as e: logger.error(f"HTTPX request error: {e}") raise Exception(f"Request error: {e}") except Exception as e: logger.error(f"Unexpected error: {e}") raise Exception(f"Unexpected error: {e}")