get_alerts
Retrieve current weather alerts for any US state by providing the two-letter state code to monitor severe conditions and stay informed.
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
- example-01/src/tools/getAlerts.ts:18-59 (handler)The handler function for the 'get_alerts' tool. Fetches active weather alerts from the National Weather Service API for a specified US state, handles errors and empty results, formats the alerts using formatAlert helper, and returns a formatted text response.async ({ state }) => { const stateCode = state.toUpperCase(); const alertsUrl = `${CONFIG.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, }, ], }; },
- Input schema for the 'get_alerts' tool using Zod: requires a 2-character string for the US state code.{ state: z .string() .length(2) .describe("Two-letter state code (e.g. CA, NY)"), },
- example-01/src/tools/getAlerts.ts:8-60 (registration)Tool registration function that defines and registers the 'get_alerts' tool on an MCP server instance, including name, description, input schema, and handler.export const getAlertsTool = (server: McpServer) => 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 = `${CONFIG.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, }, ], }; }, );
- example-01/src/index.ts:17-17 (registration)Invokes the getAlertsTool function to register the 'get_alerts' tool on the main MCP server.getAlertsTool(server);
- example-01/src/utils/index.ts:4-15 (helper)Helper function used by the get_alerts handler to format individual alert features into a readable string.// Format alert data export 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"); }