server.pyā¢1.46 kB
# Minimal Weather MCP server using the Python MCP SDK.
# Exposes one tool: weather(city: str) -> JSON with temp and wind.
# Transport: streamable HTTP at /mcp so clients and the gateway can reach it.
from mcp.server.fastmcp import FastMCP
import httpx
mcp = FastMCP("WeatherMCP", host="0.0.0.0", port=8000, stateless_http=True)
@mcp.tool()
async def weather(city: str = "Philadelphia") -> dict:
"""Return current temperature (C) and windspeed (km/h) for a city."""
async with httpx.AsyncClient(timeout=15) as client:
# 1) Geocode city to lat/lon
g = await client.get(
"https://geocoding-api.open-meteo.com/v1/search",
params={"name": city, "count": 1},
)
gj = g.json()
if not gj.get("results"):
return {"error": f"City '{city}' not found"}
lat = gj["results"][0]["latitude"]
lon = gj["results"][0]["longitude"]
# 2) Query current weather
w = await client.get(
"https://api.open-meteo.com/v1/forecast",
params={"latitude": lat, "longitude": lon, "current_weather": True},
)
cur = w.json().get("current_weather", {})
return {
"city": city,
"latitude": lat,
"longitude": lon,
"temperature_c": cur.get("temperature"),
"windspeed_kmh": cur.get("windspeed"),
}
if __name__ == "__main__":
mcp.run(transport="streamable-http")