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
| Name | Required | Description | Default |
|---|---|---|---|
| state | Yes | Two-letter state code (e.g. CA, NY) |
Implementation Reference
- src/index.ts:85-109 (handler)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")}` }], }; }
- src/index.ts:82-84 (schema)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")}` }], }; } );
- src/index.ts:42-52 (helper)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"); }
- src/index.ts:20-29 (helper)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; } }