Authenticated MCP SSE Server

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; import { AlertsResponse, PointsResponse, ForecastResponse, } from "../types/weather"; import { formatAlertsResponse, formatForecastResponse, } from "../utils/formatters"; import { makeNWSRequest, NWS_API_BASE } from "../utils/makeNWSRequest"; export const weatherTools = (server: McpServer) => { // Register weather tools 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 formattedText = formatAlertsResponse(alertsData, stateCode); return { content: [ { type: "text", text: formattedText, }, ], }; } ); server.tool( "get_forecast", "Get weather forecast for a location", { latitude: z .number() .min(-90) .max(90) .describe("Latitude of the location"), longitude: z .number() .min(-180) .max(180) .describe("Longitude of the location"), }, async ({ latitude, longitude }) => { // Get grid point data const pointsUrl = `${NWS_API_BASE}/points/${latitude.toFixed( 4 )},${longitude.toFixed(4)}`; const pointsData = await makeNWSRequest<PointsResponse>(pointsUrl); if (!pointsData) { return { content: [ { type: "text", text: `Failed to retrieve grid point data for coordinates: ${latitude}, ${longitude}. This location may not be supported by the NWS API (only US locations are supported).`, }, ], }; } const forecastUrl = pointsData.properties?.forecast; if (!forecastUrl) { return { content: [ { type: "text", text: "Failed to get forecast URL from grid point data", }, ], }; } // Get forecast data const forecastData = await makeNWSRequest<ForecastResponse>(forecastUrl); if (!forecastData) { return { content: [ { type: "text", text: "Failed to retrieve forecast data", }, ], }; } const formattedText = formatForecastResponse( forecastData, latitude, longitude ); return { content: [ { type: "text", text: formattedText, }, ], }; } ); };