Skip to main content
Glama
gaurigshankar

Weather MCP Server

get_alerts

Retrieve active weather alerts for any US state using two-letter state codes to monitor severe conditions and stay informed about local warnings.

Instructions

Get weather alerts for a state

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
stateYesTwo-letter state code (e.g. CA, NY)

Implementation Reference

  • src/index.ts:19-65 (registration)
    Registers the get_alerts tool with the MCP server using server.tool(), including name, description, Zod input schema, and inline async handler function.
    server.tool(
      "get_alerts",
      "Get weather alerts for a state",
      {
        state: z.string().length(2).describe("Two-letter state code (e.g. CA, NY)"),
      },
      async ({ state }) => {
        const stateCode = state.toUpperCase();
        const alertsUrl = `${NWS_API_BASE}/alerts?area=${stateCode}`;
        const alertsData = await makeNWSRequest<AlertsResponse>(alertsUrl);
    
        if (!alertsData) {
          return {
            content: [
              {
                type: "text",
                text: "Failed to retrieve alerts data",
              },
            ],
          };
        }
    
        const features = alertsData.features || [];
        if (features.length === 0) {
          return {
            content: [
              {
                type: "text",
                text: `No active alerts for ${stateCode}`,
              },
            ],
          };
        }
    
        const formattedAlerts = features.map(formatAlert);
        const alertsText = `Active alerts for ${stateCode}:\n\n${formattedAlerts.join("\n")}`;
    
        return {
          content: [
            {
              type: "text",
              text: alertsText,
            },
          ],
        };
      },
    );
  • The async handler function implementing get_alerts tool logic: uppercases state code, constructs NWS alerts URL, fetches data with makeNWSRequest, handles no-data cases, maps features to formatAlert, joins formatted alerts into text response.
    async ({ state }) => {
      const stateCode = state.toUpperCase();
      const alertsUrl = `${NWS_API_BASE}/alerts?area=${stateCode}`;
      const alertsData = await makeNWSRequest<AlertsResponse>(alertsUrl);
    
      if (!alertsData) {
        return {
          content: [
            {
              type: "text",
              text: "Failed to retrieve alerts data",
            },
          ],
        };
      }
    
      const features = alertsData.features || [];
      if (features.length === 0) {
        return {
          content: [
            {
              type: "text",
              text: `No active alerts for ${stateCode}`,
            },
          ],
        };
      }
    
      const formattedAlerts = features.map(formatAlert);
      const alertsText = `Active alerts for ${stateCode}:\n\n${formattedAlerts.join("\n")}`;
    
      return {
        content: [
          {
            type: "text",
            text: alertsText,
          },
        ],
      };
    },
  • Zod input schema for get_alerts tool, validating 'state' as exactly 2-character string with description.
    {
      state: z.string().length(2).describe("Two-letter state code (e.g. CA, NY)"),
    },
  • Helper function to format a single AlertFeature into a readable multi-line string, used in get_alerts handler.
    export function formatAlert(feature: AlertFeature): string {
      const props = feature.properties;
      return [
        `Event: ${props.event || "Unknown"}`,
        `Area: ${props.areaDesc || "Unknown"}`,
        `Severity: ${props.severity || "Unknown"}`,
        `Status: ${props.status || "Unknown"}`,
        `Headline: ${props.headline || "No headline"}`,
        "---",
      ].join("\n");
    }
  • Generic helper function for making authenticated fetch requests to NWS API, returning typed JSON or null on error, used in get_alerts to fetch alerts data.
    export async function makeNWSRequest<T>(url: string): Promise<T | null> {
      const headers = {
        "User-Agent": USER_AGENT,
        Accept: "application/geo+json",
      };
    
      try {
        const response = await fetch(url, { headers });
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        return (await response.json()) as T;
      } catch (error) {
        console.error("Error making NWS request:", error);
        return null;
      }
    }
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/gaurigshankar/weather-mcp-server'

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