get_weather
Retrieve current weather conditions for any city worldwide. Provide the city name in English to get temperature, humidity, wind, and other real-time weather data.
Instructions
获取指定城市的当前天气信息。
参数:
city: 城市名称(英文),如 Beijing、Tokyo、LondonInput Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes |
Implementation Reference
- server.py:26-56 (handler)The get_weather handler function registered as an MCP tool via @mcp.tool(). It fetches current weather from wttr.in API for a given city and returns temperature, humidity, feels-like, description, and wind speed.
@mcp.tool() async def get_weather(city: str) -> dict: """获取指定城市的当前天气信息。 参数: city: 城市名称(英文),如 Beijing、Tokyo、London """ 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() current = data["current_condition"][0] return { "city": city, "temperature_celsius": current["temp_C"], "humidity_percent": current["humidity"], "feels_like_celsius": current["FeelsLikeC"], "description": current["weatherDesc"][0]["value"], "wind_speed_kmh": current["windspeedKmph"], } except httpx.TimeoutException: return {"error": f"查询 {city} 超时,请稍后重试"} except Exception as e: return {"error": f"查询失败: {str(e)}"} - server.py:26-27 (registration)Registration of get_weather as an MCP tool using the @mcp.tool() decorator on the FastMCP server instance.
@mcp.tool() async def get_weather(city: str) -> dict: - server.py:26-32 (schema)Input schema for get_weather: parameter 'city' (str) with docstring describing the expected format. Output type is dict as per return type annotation.
@mcp.tool() async def get_weather(city: str) -> dict: """获取指定城市的当前天气信息。 参数: city: 城市名称(英文),如 Beijing、Tokyo、London """ - server_remote.py:29-57 (handler)Duplicate handler for get_weather in the Streamable HTTP remote server version (server_remote.py). Same logic as server.py version.
@mcp.tool() async def get_weather(city: str) -> dict: """获取指定城市的当前天气信息。 参数: city: 城市名称(英文),如 Beijing、Tokyo、London """ 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() current = data["current_condition"][0] return { "city": city, "temperature_celsius": current["temp_C"], "humidity_percent": current["humidity"], "feels_like_celsius": current["FeelsLikeC"], "description": current["weatherDesc"][0]["value"], "wind_speed_kmh": current["windspeedKmph"], } except httpx.TimeoutException: return {"error": f"查询 {city} 超时,请稍后重试"} except Exception as e: return {"error": f"查询失败: {str(e)}"}