Skip to main content
Glama
Danii2020

Weather MCP Server

by Danii2020

get_forecast

Retrieve weather forecasts for specific coordinates using latitude and longitude inputs to access current and upcoming conditions.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
latitudeYes
longitudeYes

Implementation Reference

  • The asynchronous handler function implementing the core logic of the 'get_forecast' tool. It retrieves weather forecast data from the NWS API using latitude and longitude, formats the first 5 periods, and returns a string summary.
    async def get_forecast(latitude: float, longitude: float) -> str:
        url = f"{NWS_API_BASE}/points/{latitude},{longitude}"
        data = await make_nws_request(url)
    
        if not data:
            return "Unable to fetch forecast data for this location."
        
        forecast_url = data["properties"]["forecast"]
        forecast_data = await make_nws_request(forecast_url)
    
        if not forecast_data:
            return "Unable to fetch detailed forecast."
    
        periods = forecast_data["properties"]["periods"]
        forecasts = []
        for period in periods[:5]:
            forecast = f"""
                {period['name']}:
                Temperature: {period['temperature']}°{period['temperatureUnit']}
                Wind: {period['windSpeed']} {period['windDirection']}
                Forecast: {period['detailedForecast']}
            """
            forecasts.append(forecast)
        return "\n---\n".join(forecasts)
  • The @mcp.tool() decorator registers the get_forecast function as an MCP tool, using its name as the tool identifier.
    @mcp.tool()
  • Utility function to make asynchronous HTTP requests to the NWS API, handling errors and headers. Called twice in the get_forecast handler.
    async def make_nws_request(url: str) -> dict[str, Any] | None:
        headers = {"User-Agent": USER_AGENT, "Accept": "application/geo+json"}
        async with httpx.AsyncClient() as client:
            try:
                response = await client.get(url, headers=headers, timeout=30.0)
                response.raise_for_status()
                return response.json()
            except Exception:
                return None
  • Constants used by the get_forecast tool: base URL for NWS API and User-Agent header.
    NWS_API_BASE = "https://api.weather.gov"
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/Danii2020/weather-mcp'

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