Skip to main content
Glama
akaramanapp

Weather MCP Server

by akaramanapp

get-forecast

Retrieve weather forecasts for US locations by providing latitude and longitude coordinates.

Instructions

Get weather forecast for a location

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
latitudeYesLatitude of the location
longitudeYesLongitude of the location

Implementation Reference

  • The inline async handler function for the 'get-forecast' tool. It takes latitude and longitude, fetches grid points and forecast data from the National Weather Service API, formats the forecast periods, and returns them as text content.
    async ({ latitude, longitude }) => {
      // Get grid point data
      const pointsData = await makeNWSRequest<PointsResponse>(
        `/points/${latitude.toFixed(4)},${longitude.toFixed(4)}`
      );
    
      if (!pointsData) {
        return {
          content: [
            {
              type: "text",
              text: `Failed to retrieve grid point data for coordinates: ${latitude}, ${longitude}. This location may not be supported by the NWS API (only US locations are supported).`,
            },
          ],
        };
      }
    
      const forecastUrl = pointsData.properties?.forecast;
      if (!forecastUrl) {
        return {
          content: [
            {
              type: "text",
              text: "Failed to get forecast URL from grid point data",
            },
          ],
        };
      }
    
      // Get forecast data - using full URL since the forecast URL is absolute
      const forecastData = await makeNWSRequest<ForecastResponse>(forecastUrl);
      if (!forecastData) {
        return {
          content: [
            {
              type: "text",
              text: "Failed to retrieve forecast data",
            },
          ],
        };
      }
    
      const periods = forecastData.properties?.periods || [];
      if (periods.length === 0) {
        return {
          content: [
            {
              type: "text",
              text: "No forecast periods available",
            },
          ],
        };
      }
    
      // Format forecast periods
      const formattedForecast = periods.map((period: ForecastPeriod) =>
        [
          `${period.name || "Unknown"}:`,
          `Temperature: ${period.temperature || "Unknown"}°${period.temperatureUnit || "F"}`,
          `Wind: ${period.windSpeed || "Unknown"} ${period.windDirection || ""}`,
          `${period.shortForecast || "No forecast available"}`,
          "---",
        ].join("\n"),
      );
    
      return {
        content: [
          {
            type: "text",
            text: formattedForecast.join("\n"),
          },
        ],
      };
    },
  • Zod input schema for the 'get-forecast' tool, validating latitude and longitude parameters.
    {
      latitude: z.number().min(-90).max(90).describe("Latitude of the location"),
      longitude: z.number().min(-180).max(180).describe("Longitude of the location"),
    },
  • src/index.ts:143-224 (registration)
    Registration of the 'get-forecast' tool on the MCP server, specifying name, description, input schema, and handler.
    server.tool(
      "get-forecast",
      "Get weather forecast for a location",
      {
        latitude: z.number().min(-90).max(90).describe("Latitude of the location"),
        longitude: z.number().min(-180).max(180).describe("Longitude of the location"),
      },
      async ({ latitude, longitude }) => {
        // Get grid point data
        const pointsData = await makeNWSRequest<PointsResponse>(
          `/points/${latitude.toFixed(4)},${longitude.toFixed(4)}`
        );
    
        if (!pointsData) {
          return {
            content: [
              {
                type: "text",
                text: `Failed to retrieve grid point data for coordinates: ${latitude}, ${longitude}. This location may not be supported by the NWS API (only US locations are supported).`,
              },
            ],
          };
        }
    
        const forecastUrl = pointsData.properties?.forecast;
        if (!forecastUrl) {
          return {
            content: [
              {
                type: "text",
                text: "Failed to get forecast URL from grid point data",
              },
            ],
          };
        }
    
        // Get forecast data - using full URL since the forecast URL is absolute
        const forecastData = await makeNWSRequest<ForecastResponse>(forecastUrl);
        if (!forecastData) {
          return {
            content: [
              {
                type: "text",
                text: "Failed to retrieve forecast data",
              },
            ],
          };
        }
    
        const periods = forecastData.properties?.periods || [];
        if (periods.length === 0) {
          return {
            content: [
              {
                type: "text",
                text: "No forecast periods available",
              },
            ],
          };
        }
    
        // Format forecast periods
        const formattedForecast = periods.map((period: ForecastPeriod) =>
          [
            `${period.name || "Unknown"}:`,
            `Temperature: ${period.temperature || "Unknown"}°${period.temperatureUnit || "F"}`,
            `Wind: ${period.windSpeed || "Unknown"} ${period.windDirection || ""}`,
            `${period.shortForecast || "No forecast available"}`,
            "---",
          ].join("\n"),
        );
    
        return {
          content: [
            {
              type: "text",
              text: formattedForecast.join("\n"),
            },
          ],
        };
      },
    );
  • Helper function used by the get-forecast handler to make API requests to the NWS weather service with axios and error handling.
    async function makeNWSRequest<T>(url: string): Promise<T | null> {
      try {
        const response = await api.get<T>(url);
        return response.data;
      } catch (error) {
        if (axios.isAxiosError(error)) {
          console.error("Error making NWS request:", error.message);
          if (error.response) {
            console.error("Response status:", error.response.status);
            console.error("Response data:", error.response.data);
          }
        } else {
          console.error("Unexpected error:", error);
        }
        return null;
      }
    }
  • TypeScript interfaces defining the structure of NWS API responses used in the get-forecast handler (PointsResponse and ForecastResponse). Also nearby ForecastPeriod (70-77).
    interface PointsResponse {
      properties: {
        forecast?: string;
      };
    }
    
    interface ForecastResponse {
      properties: {
        periods: ForecastPeriod[];
      };
    }
  • TypeScript interface for ForecastPeriod used in parsing forecast data.
    interface ForecastPeriod {
      name?: string;
      temperature?: number;
      temperatureUnit?: string;
      windSpeed?: string;
      windDirection?: string;
      shortForecast?: string;
    }
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/akaramanapp/weather-mcp-server'

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