Skip to main content
Glama
akaramanapp

Weather MCP Server

by akaramanapp

get-alerts

Retrieve 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:96-141 (registration)
    Registration of the 'get-alerts' MCP tool, including input schema and inline handler function that fetches and formats weather alerts for a US state.
    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 alertsData = await makeNWSRequest<AlertsResponse>(`/alerts?area=${stateCode}`);
    
        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 handler function for 'get-alerts' tool: fetches alerts from National Weather Service API using the provided state code, handles errors/no data, formats alerts using helper, and returns formatted text content.
    async ({ state }) => {
      const stateCode = state.toUpperCase();
      const alertsData = await makeNWSRequest<AlertsResponse>(`/alerts?area=${stateCode}`);
    
      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,
          },
        ],
      };
    },
  • Input schema for 'get-alerts' tool using Zod: requires a 2-letter state code.
    {
      state: z.string().length(2).describe("Two-letter state code (e.g. CA, NY)"),
    },
  • Helper function to format individual alert feature into a readable string.
    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");
    }
  • Helper function to make API requests to the National Weather Service with error handling using axios.
    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;
      }
    }
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