get-alerts
Retrieve weather alerts for any U.S. state using two-letter state codes to monitor hazardous conditions and stay informed about local weather 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/tools/get-alerts.ts:17-57 (handler)Handler function that implements the 'get-alerts' tool logic: fetches alerts from NWS API using makeNWSRequest for the given state code, handles no data or no alerts cases, formats each alert with formatAlert, and returns a structured text content 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/tools/get-alerts.ts:11-16 (schema)Tool metadata: title, description, and Zod inputSchema validating the 'state' parameter as a two-letter US state code.title: 'Weather alerts', description: 'Get weather alerts for a state', inputSchema: z.object({ state: z.string().length(2).describe('Two-letter state code (e.g. CA, NY)'), }), },
- src/tools/get-alerts.ts:7-58 (registration)registerGetAlerts exported function that performs server.registerTool('get-alerts', schema, handler) to define and register the tool.export function registerGetAlerts() { server.registerTool( 'get-alerts', { title: 'Weather alerts', description: 'Get weather alerts for a state', inputSchema: z.object({ 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:9-9 (registration)Top-level call to registerGetAlerts() during server initialization to enable the tool.registerGetAlerts()
- src/utils/index.ts:26-36 (helper)formatAlert helper: converts an AlertFeature object to a formatted string with key properties (event, areaDesc, severity, status, headline).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') }