blob: 13a3bf6ee77e59068a3c7e55d16431a7a15b1902 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
"""Test MCP server simulating a weather service.
Usage:
python scripts/weather_server.py # stdio transport
python scripts/weather_server.py streamable-http # HTTP transport
"""
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("weather")
_FORECASTS: dict[str, str] = {
"tokyo": "sunny, 28C",
"osaka": "cloudy, 26C",
"sapporo": "rain, 18C",
"new york": "sunny, 22C",
"london": "rain, 15C",
}
@mcp.tool()
def get_weather(city: str) -> str:
"""Return a simple weather forecast for a city."""
key = city.strip().lower()
return _FORECASTS.get(key, f"no forecast available for {city}")
@mcp.tool()
def list_cities() -> list[str]:
"""Return the list of cities with available forecasts."""
return list(_FORECASTS.keys())
if __name__ == "__main__":
import sys
transport = sys.argv[1] if len(sys.argv) > 1 else "stdio"
port = int(sys.argv[2]) if len(sys.argv) > 2 else 9000
mcp.settings.port = port
mcp.run(transport=transport)
|