Skip to main content
Glama
CodeByWaqas

Weather MCP Server

by CodeByWaqas

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
NameRequiredDescriptionDefault
cityYes

Implementation Reference

  • 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
  • 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
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/CodeByWaqas/weather-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server