Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions mcp/weather_tool/weather_tool.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
"Weather MCP tool example"

import os
import json
import requests
from fastmcp import FastMCP

mcp = FastMCP("Weather")

@mcp.tool()
@mcp.tool(annotations={"readOnlyHint": True, "destructiveHint": False, "idempotentHint": True})
def get_weather(city: str) -> str:
"""Get weather info for a city"""
base_url = "https://geocoding-api.open-meteo.com/v1/search"
params = {"name": city, "count": 1}
response = requests.get(base_url, params=params)
response = requests.get(base_url, params=params, timeout=10)
data = response.json()
if not data["results"]:
return "City not found"
Expand All @@ -24,14 +26,15 @@ def get_weather(city: str) -> str:
"temperature_unit": "fahrenheit",
"current_weather": True
}
weather_response = requests.get(weather_url, params=weather_params)
weather_response = requests.get(weather_url, params=weather_params, timeout=10)
weather_data = weather_response.json()

return json.dumps(weather_data["current_weather"])

# host can be specified with HOST env variable
# transport can be specified with MCP_TRANSPORT env variable (defaults to streamable-http)
def run_server():
"Run the MCP server"
transport = os.getenv("MCP_TRANSPORT", "streamable-http")
host = os.getenv("HOST", "0.0.0.0")
port = int(os.getenv("PORT", "8000"))
Expand Down