get-alerts
Retrieve weather alerts for any U.S. state by entering its two-letter code to stay informed about severe weather conditions via the Weather 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:102-140 (handler)The async handler function that implements the core logic of the 'get-alerts' tool: fetches alerts from NWS API using makeNWSRequest, handles errors and empty results, formats alerts using formatAlert, 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:96-141 (registration)Registration of the 'get-alerts' tool on the McpServer instance, specifying name, description, input parameters schema, and the 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 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)Zod schema for input validation of the 'state' parameter (two-letter US 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 a single weather alert feature into a multi-line text 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:29-44 (helper)Helper function for making requests to the National Weather Service API using axios, with error handling, used by the get-alerts handler.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; }