-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
36 lines (28 loc) · 1.2 KB
/
Copy pathserver.py
File metadata and controls
36 lines (28 loc) · 1.2 KB
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
from typing import Any
import httpx
from mcp.server.fastmcp import FastMCP
# initialize mcp server
mcp = FastMCP("weather")
OPEN_METEO_API_BASE_URL = "https://api.open-meteo.com/v1"
USER_AGENT = "weather-app/1.0"
async def make_open_meteo_request(url: str) -> dict[str, Any] |None:
headers = {
"User-Agent": USER_AGENT,
"Accept": "application/json",
}
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, headers=headers, timeout=30.0)
response.raise_for_status()
return response.json()
except Exception:
return None
@mcp.tool()
async def get_current_weather(latitude: float, longitude: float) -> str | dict[str, Any]:
url = f"{OPEN_METEO_API_BASE_URL}/forecast?latitude={latitude}&longitude={longitude}¤t=temperature_2m,is_day,showers,cloud_cover,wind_speed_10m,wind_direction_10m,pressure_msl,snowfall,precipitation,relative_humidity_2m,apparent_temperature,rain,weather_code,surface_pressure,wind_gusts_10m"
data = await make_open_meteo_request(url)
if not data:
return "No weather data available"
return data
if __name__ == "__main__":
mcp.run(transport='stdio')