Skip to content

Commit 8ef0cbe

Browse files
authored
Merge pull request #73 from evaline-ju/weather-info
🌱🥅 Logs and error handling for weather tool
2 parents 2d139f7 + a97a5b7 commit 8ef0cbe

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

mcp/weather_tool/weather_tool.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
"Weather MCP tool example"
22

3-
import os
43
import json
4+
import logging
5+
import os
56
import requests
7+
import sys
68
from fastmcp import FastMCP
79

810
mcp = FastMCP("Weather")
11+
logger = logging.getLogger(__name__)
12+
logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"), stream=sys.stdout, format='%(levelname)s: %(message)s')
913

1014
@mcp.tool(annotations={"readOnlyHint": True, "destructiveHint": False, "idempotentHint": True})
1115
def get_weather(city: str) -> str:
1216
"""Get weather info for a city"""
17+
logger.debug(f"Getting weather info for city '{city}'.")
1318
base_url = "https://geocoding-api.open-meteo.com/v1/search"
1419
params = {"name": city, "count": 1}
1520
response = requests.get(base_url, params=params, timeout=10)
1621
data = response.json()
17-
if not data["results"]:
18-
return "City not found"
22+
if not data or not "results" in data:
23+
return f"City {city} not found"
1924
latitude = data["results"][0]["latitude"]
2025
longitude = data["results"][0]["longitude"]
2126

0 commit comments

Comments
 (0)