Skip to main content
Glama
hafizrahman

Weather & WordPress MCP Server

by hafizrahman

get-alerts

Retrieve weather alerts for any U.S. 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

  • The async handler function that fetches weather alerts from the NWS API for the specified state, formats them using formatAlert, and returns formatted text content.
    async ({ state }) => {
        const stateCode = state.toUpperCase();
        const alertsUrl = `${NWS_API_BASE}/alerts?area=${stateCode}`;
        const alertsData = await fetchJson<AlertsResponse>(alertsUrl, {
            Accept: "application/geo+json",
        });
    
        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);
        return {
            content: [{ type: "text", text: `Active alerts for ${stateCode}:\n\n${formattedAlerts.join("\n")}` }],
        };
    }
  • Input schema for the get-alerts tool, defining the 'state' parameter as a 2-letter string.
    {
        state: z.string().length(2).describe("Two-letter state code (e.g. CA, NY)"),
    },
  • src/index.ts:79-110 (registration)
    Registration of the 'get-alerts' tool using server.tool, including name, description, input schema, and 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 fetchJson<AlertsResponse>(alertsUrl, {
                Accept: "application/geo+json",
            });
    
            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);
            return {
                content: [{ type: "text", text: `Active alerts for ${stateCode}:\n\n${formattedAlerts.join("\n")}` }],
            };
        }
    );
  • Helper function to format a single weather alert feature into a multi-line 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");
    }
  • Shared helper function used to fetch and parse JSON from APIs, with error handling.
    async function fetchJson<T>(url: string, headers: Record<string, string> = {}): Promise<T | null> {
        try {
            const response = await fetch(url, { headers: { "User-Agent": USER_AGENT, ...headers } });
            if (!response.ok) throw new Error(`HTTP error ${response.status}`);
            return (await response.json()) as T;
        } catch (err) {
            console.error("Fetch error:", err);
            return null;
        }
    }

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/hafizrahman/wp-mcp'

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