get-alerts
Retrieve weather alerts for any US 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:96-141 (registration)Registration of the 'get-alerts' MCP tool, including input schema and inline handler function that fetches and formats weather alerts for a US state.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 alertsData = await makeNWSRequest<AlertsResponse>(`/alerts?area=${stateCode}`); 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:102-140 (handler)The handler function for 'get-alerts' tool: fetches alerts from National Weather Service API using the provided state code, handles errors/no data, formats alerts using helper, and returns formatted text content.async ({ state }) => { const stateCode = state.toUpperCase(); const alertsData = await makeNWSRequest<AlertsResponse>(`/alerts?area=${stateCode}`); 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:99-101 (schema)Input schema for 'get-alerts' tool using Zod: requires a 2-letter state code.{ state: z.string().length(2).describe("Two-letter state code (e.g. CA, NY)"), },
- src/index.ts:58-68 (helper)Helper function to format individual alert feature into a readable 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:29-45 (helper)Helper function to make API requests to the National Weather Service with error handling using axios.async function makeNWSRequest<T>(url: string): Promise<T | null> { try { const response = await api.get<T>(url); return response.data; } catch (error) { if (axios.isAxiosError(error)) { console.error("Error making NWS request:", error.message); if (error.response) { console.error("Response status:", error.response.status); console.error("Response data:", error.response.data); } } else { console.error("Unexpected error:", error); } return null; } }