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