import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import z from "zod";
import { CONFIG } from "../config.js";
import { formatAlert, makeNWSRequest } from "../utils/index.js";
import { AlertsResponse } from "../interfaces/index.js";
// Register weather tools
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,
},
],
};
},
);