get-forecast
Retrieve weather forecasts for specific US locations by providing latitude and longitude coordinates. Access detailed predictions via the Weather MCP Server using the National Weather Service API.
Instructions
Get weather forecast for a location
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| latitude | Yes | Latitude of the location | |
| longitude | Yes | Longitude of the location |
Implementation Reference
- src/index.ts:150-223 (handler)Handler function that implements the core logic of the 'get-forecast' tool: fetches NWS grid points and forecast data for given lat/long, handles errors, formats periods, and returns markdown text.async ({ latitude, longitude }) => { // Get grid point data const pointsData = await makeNWSRequest<PointsResponse>( `/points/${latitude.toFixed(4)},${longitude.toFixed(4)}` ); 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 - using full URL since the forecast URL is absolute const forecastData = await makeNWSRequest<ForecastResponse>(forecastUrl); if (!forecastData) { return { content: [ { type: "text", text: "Failed to retrieve forecast data", }, ], }; } const periods = forecastData.properties?.periods || []; if (periods.length === 0) { return { content: [ { type: "text", text: "No forecast periods available", }, ], }; } // Format forecast periods const formattedForecast = periods.map((period: ForecastPeriod) => [ `${period.name || "Unknown"}:`, `Temperature: ${period.temperature || "Unknown"}°${period.temperatureUnit || "F"}`, `Wind: ${period.windSpeed || "Unknown"} ${period.windDirection || ""}`, `${period.shortForecast || "No forecast available"}`, "---", ].join("\n"), ); return { content: [ { type: "text", text: formattedForecast.join("\n"), }, ], }; },
- src/index.ts:146-149 (schema)Input schema for the 'get-forecast' tool using Zod to validate latitude and longitude parameters.{ latitude: z.number().min(-90).max(90).describe("Latitude of the location"), longitude: z.number().min(-180).max(180).describe("Longitude of the location"), },
- src/index.ts:143-149 (registration)Registration of the 'get-forecast' tool on the MCP server, specifying name, description, and input schema.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"), },
- src/index.ts:29-45 (helper)Utility helper function to make requests to the NWS API with axios, configured headers, and comprehensive error handling. Used by get-forecast handler.async function makeNWSRequest<T>(url: string): Promise<T | null> { try { const response = await api.get<T>(url); return response.data; } catch (error) { if (axios.isAxiosError(error)) { console.error("Error making NWS request:", error.message); if (error.response) { console.error("Response status:", error.response.status); console.error("Response data:", error.response.data); } } else { console.error("Unexpected error:", error); } return null; } }
- src/index.ts:70-93 (schema)TypeScript interfaces defining the structure of NWS API responses (ForecastPeriod, PointsResponse, ForecastResponse) used for typing in the get-forecast handler.interface ForecastPeriod { name?: string; temperature?: number; temperatureUnit?: string; windSpeed?: string; windDirection?: string; shortForecast?: string; } interface AlertsResponse { features: AlertFeature[]; } interface PointsResponse { properties: { forecast?: string; }; } interface ForecastResponse { properties: { periods: ForecastPeriod[]; }; }