from mcp.server.fastmcp import FastMCP, Context logging.getLogger("mcp").setLevel(logging.WARNING)
mcp = FastMCP("demo-weather-server")
@mcp.tool() asyncdefget_weather(city: str, ctx: Context) -> dict: """Get the current weather for a city. should get support cities from resource weather://citys first""" weather_data = { "guangzhou": {"temp": 22, "condition": "sunny"}, "shanghai": {"temp": 15, "condition": "cloudy"}, "beijing": {"temp": 12, "condition": "rainy"}, }
city_lower = city.lower() if city_lower in weather_data: await ctx.session.send_tool_list_changed() return {"city": city, **weather_data[city_lower]} else: return {"city": city, "temp": "unknown", "condition": "unknown", "error": "city not supported"} @mcp.resource("weather://citys") asyncdefcitys() -> str: """Get the list of cities.""" return ["guangzhou", "shanghai", "beijing"]
if __name__ == "__main__": mcp.run(transport="stdio")
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"ExampleClient","version":"1.0.0","description":"An example MCP client application"}}}
{"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"get_weather","description":"Get the current weather for a city. should get support cities from resource weather://citys first","inputSchema":{"properties":{"city":{"title":"City","type":"string"}},"required":["city"],"title":"get_weatherArguments","type":"object"}}]}}
Connected to server: demo-weather-server (version 1.26.0)
Available tools: - get_weather: Get the current weather for a city. should get support cities from resource weather://citys first {'properties': {'city': {'title': 'City', 'type': 'string'}}, 'required': ['city'], 'title': 'get_weatherArguments', 'type': 'object'} Available resources (static): - weather://citys: Get the list of cities.