1+ "Weather MCP tool example"
2+
13import os
24import json
35import requests
46from fastmcp import FastMCP
57
68mcp = FastMCP ("Weather" )
79
8- @mcp .tool ()
10+ @mcp .tool (annotations = { "readOnlyHint" : True , "destructiveHint" : False , "idempotentHint" : True } )
911def get_weather (city : str ) -> str :
1012 """Get weather info for a city"""
1113 base_url = "https://geocoding-api.open-meteo.com/v1/search"
1214 params = {"name" : city , "count" : 1 }
13- response = requests .get (base_url , params = params )
15+ response = requests .get (base_url , params = params , timeout = 10 )
1416 data = response .json ()
1517 if not data ["results" ]:
1618 return "City not found"
@@ -24,14 +26,15 @@ def get_weather(city: str) -> str:
2426 "temperature_unit" : "fahrenheit" ,
2527 "current_weather" : True
2628 }
27- weather_response = requests .get (weather_url , params = weather_params )
29+ weather_response = requests .get (weather_url , params = weather_params , timeout = 10 )
2830 weather_data = weather_response .json ()
2931
3032 return json .dumps (weather_data ["current_weather" ])
3133
3234# host can be specified with HOST env variable
3335# transport can be specified with MCP_TRANSPORT env variable (defaults to streamable-http)
3436def run_server ():
37+ "Run the MCP server"
3538 transport = os .getenv ("MCP_TRANSPORT" , "streamable-http" )
3639 host = os .getenv ("HOST" , "0.0.0.0" )
3740 port = int (os .getenv ("PORT" , "8000" ))
0 commit comments