weather_forecast
Get weather forecasts for 1 to 14 days for any location using city names, coordinates, or postal codes to plan activities and prepare for conditions.
Instructions
Get weather forecast (1-14 days) for a location
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| q | Yes | Location query (city name, lat/lon, postal code, etc) | |
| days | No | Number of days (1-14) |
Implementation Reference
- server.py:185-193 (handler)Handler logic for the weather_forecast tool within the tools/call method. Validates location 'q' and days (1-14), then fetches forecast data using the shared fetch function.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:128-146 (registration)Registration of the weather_forecast tool in the tools/list response, including name, description, and input 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:131-145 (schema)Input schema for weather_forecast tool, specifying parameters 'q' (required) and 'days' (optional, 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:42-80 (helper)Shared helper function that performs the actual API call to WeatherAPI.com for all weather tools, including forecast.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}")