Skip to main content
Glama

get-alerts

Receive real-time weather alerts for any U.S. state by providing its two-letter code. Stay informed about severe conditions with this Weather MCP tool.

Instructions

Get weather alerts for a state

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
stateYesTwo-letter state code (e.g. CA, NY)

Implementation Reference

  • The handler function for the 'get-alerts' tool. It takes a state code, fetches weather alerts from the National Weather Service API using makeNWSRequest, formats them with formatAlert, and returns a 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, }, ], }; },
  • Input schema for the 'get-alerts' tool defined using Zod, requiring a two-letter state code.
    { state: z.string().length(2).describe("Two-letter state code (e.g. CA, NY)"), },
  • src/index.ts:27-73 (registration)
    Registration of the 'get-alerts' tool on the MCP server, including name, description, schema, and 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, }, ], }; }, );
  • Helper function used by the 'get-alerts' handler to make API requests to the NWS weather service.
    export async function makeNWSRequest<T>(url: string): Promise<T | null> { const USER_AGENT = "weather-app/1.0"; 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; } }
  • Helper function used by the 'get-alerts' handler to format individual alert data into readable text.
    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"); }

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/gifflet/weather-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server