get-alerts
Retrieve weather alerts for any US state using its two-letter code to monitor hazardous 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:79-120 (handler)The handler function that implements the core logic of the get-alerts tool: fetches weather alerts for the specified state from the NWS API, handles errors and empty results, formats the alerts using the formatAlert helper, and returns a formatted text response.async ({ state }) => { const stateCode = state.toUpperCase(); const alertsUrl = `${NWS_API_BASE}/alerts?area=${stateCode}`; const alertsData = await makeNWSRequest<AlertsResponse>(alertsUrl); 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, }, ], }; }
- src/index.ts:77-78 (schema)Zod input schema defining the 'state' parameter as a 2-character string for the US state code.state: z.string().length(2).describe("Two-letter state code (e.g. CA, NY)"), },
- src/index.ts:73-121 (registration)Registration of the get-alerts tool using server.tool(), including name, description, input schema, and reference to the inline handler.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 makeNWSRequest<AlertsResponse>(alertsUrl); 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, }, ], }; } );
- src/index.ts:50-60 (helper)Helper function to format an individual 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/tools/weather.ts:4-20 (helper)Supporting utility function to perform fetch requests to the National Weather Service API with proper headers and error handling, used by the get-alerts handler.export async function makeNWSRequest<T>(url: string): Promise<T | null> { const headers = { "User-Agent": USER_AGENT, Accept: "application/geo+json", }; try { const response = await fetch(url, { headers }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return (await response.json()) as T; } catch (error) { console.error("Error making NWS request:", error); return null; } }