weather
Get current weather data for any city including temperature, humidity, wind speed, and sunrise/sunset times.
Instructions
It fetches the latest weather reports for the given city. Args: city (str): The city name for which weather reports are required. Returns: dict: The weather reports for the given city.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes |
Implementation Reference
- src/resources/server.py:50-62 (handler)The main handler function for the 'weather' tool. It calls Fetch_weather_info to get the data and returns it as JSON string. Registered via @mcp.tool() decorator.@mcp.tool() async def weather(city: str): """ It fetches the latest weather reports for the given city. Args: city (str): The city name for which weather reports are required. Returns: dict: The weather reports for the given city. """ weather_data = await Fetch_weather_info(city) if weather_data: return json.dumps(weather_data) return None
- src/resources/server.py:18-43 (helper)Helper function that performs the actual API call to OpenWeatherMap to fetch weather data for the given city and formats the response.async def Fetch_weather_info(city: str): """ It pulls the latest weather reports from the OpenWeatherMap API. """ url = f"{SITE}?q={city}&appid={API_KEY}&units=metric" print("Hello from weather-mcp-server!") async with httpx.AsyncClient() as client: try: response = await client.get(url, headers={"User-Agent": USER_AGENT}) response.raise_for_status() weather_data = response.json() if weather_data: return { "city": weather_data["name"], "country": weather_data["sys"]["country"], "temperature": weather_data["main"]["temp"], "description": weather_data["weather"][0]["description"], "humidity": weather_data["main"]["humidity"], "wind_speed": weather_data["wind"]["speed"], "sunrise": weather_data["sys"]["sunrise"], "sunset": weather_data["sys"]["sunset"], } return None except httpx.HTTPError as e: print(f"Error fetching weather data: {e}") return None