Skip to main content
Glama
hafizrahman

Weather & WordPress MCP Server

by hafizrahman

get-alerts

Receive real-time weather alerts for specific states by entering the two-letter state code. Access critical weather updates directly from the National Weather Service API via the Weather & WordPress MCP Server.

Instructions

Get weather alerts for a state

Input Schema

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

Implementation Reference

  • The main handler function for the 'get-alerts' tool. It fetches current weather alerts for a US state from the National Weather Service API, handles errors and empty results, formats the alerts using the formatAlert helper, and returns a markdown-compatible text response.
    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 tool, validating the 'state' parameter as a two-letter US state code using Zod.
    { 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 on the MCP server using server.tool(), specifying name, description, input schema, and inline 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 human-readable multi-line string, used by the get-alerts handler.
    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 utility function for making JSON API requests with error handling and custom User-Agent, used by the get-alerts handler to fetch alert data.
    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; } }

Other Tools

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

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