weather
Fetch real-time weather reports for any city, including temperature, humidity, wind speed, and sunrise/sunset times, using OpenWeatherMap API integration.
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 handler function for the 'weather' tool, decorated with @mcp.tool() to register it with the MCP server. It takes a city name as input and returns JSON-serialized weather data by calling the helper function.@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-44 (helper)Supporting helper function that performs the HTTP request to the OpenWeatherMap API, parses the response, and extracts relevant weather information for the given city.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
- src/resources/server.py:50-50 (registration)The @mcp.tool() decorator registers the 'weather' function as an MCP tool.@mcp.tool()
- src/resources/server.py:52-57 (schema)The docstring provides the input schema (city: str) and output description (dict of weather data) for the tool.""" 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. """