get_forecast
Retrieve weather forecasts for any city up to three days ahead. Enter a city name (English) and optional number of days to get future weather predictions. Supports up to 3 days.
Instructions
获取指定城市未来几天的天气预报。
参数:
city: 城市名称(英文)
days: 预报天数,默认 3 天,最多 3 天Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | ||
| days | No |
Implementation Reference
- server.py:59-87 (handler)The main handler function for the 'get_forecast' tool. Fetches weather forecast from wttr.in API and returns list of daily forecasts.
@mcp.tool() async def get_forecast(city: str, days: int = 3) -> list: """获取指定城市未来几天的天气预报。 参数: city: 城市名称(英文) days: 预报天数,默认 3 天,最多 3 天 """ import httpx try: async with httpx.AsyncClient() as client: resp = await client.get( f"https://wttr.in/{city}?format=j1", timeout=10, ) resp.raise_for_status() data = resp.json() forecasts = [] for day in data.get("weather", [])[:days]: forecasts.append({ "date": day["date"], "max_temp_c": day["maxtempC"], "min_temp_c": day["mintempC"], "avg_temp_c": day["avgtempC"], "description": day["hourly"][4]["weatherDesc"][0]["value"], }) return forecasts - server.py:59-59 (registration)Registration of the get_forecast tool via @mcp.tool() decorator on FastMCP instance.
@mcp.tool() - server_remote.py:60-85 (handler)Duplicate handler for get_forecast in the Streamable HTTP version (server_remote.py), slightly different output (no avg_temp_c).
@mcp.tool() async def get_forecast(city: str, days: int = 3) -> list: """获取指定城市未来几天的天气预报。 参数: city: 城市名称(英文) days: 预报天数,默认 3 天,最多 3 天 """ try: async with httpx.AsyncClient() as client: resp = await client.get( f"https://wttr.in/{city}?format=j1", timeout=10, ) resp.raise_for_status() data = resp.json() forecasts = [] for day in data.get("weather", [])[:days]: forecasts.append({ "date": day["date"], "max_temp_c": day["maxtempC"], "min_temp_c": day["mintempC"], "description": day["hourly"][4]["weatherDesc"][0]["value"], }) return forecasts - server_remote.py:60-60 (registration)Registration of get_forecast in server_remote.py via @mcp.tool() decorator.
@mcp.tool()