get-forecast
Fetch precise weather forecasts for any location by providing latitude and longitude coordinates. Simplify weather data integration for informed planning.
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:119-163 (handler)The handler function fetches the weather forecast from the National Weather Service API using latitude and longitude, formats the forecast periods including temperature, wind, and short forecast, and returns formatted text content.async ({ latitude, longitude }) => { const pointsUrl = `${NWS_API_BASE}/points/${latitude.toFixed(4)},${longitude.toFixed(4)}`; const pointsData = await fetchJson<PointsResponse>(pointsUrl, { Accept: "application/geo+json", }); if (!pointsData || !pointsData.properties?.forecast) { return { content: [ { type: "text", text: `Failed to get forecast data for coordinates: ${latitude}, ${longitude}`, }, ], }; } const forecastData = await fetchJson<ForecastResponse>(pointsData.properties.forecast); 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" }], }; } const formattedForecast = periods.map((period) => [ `${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: `Forecast for ${latitude}, ${longitude}:\n\n${formattedForecast.join("\n")}` }], }; }
- src/index.ts:115-118 (schema)Input schema using Zod for validating 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:112-164 (registration)The server.tool call that registers the 'get-forecast' tool, including its name, description, input schema, and handler function.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 }) => { const pointsUrl = `${NWS_API_BASE}/points/${latitude.toFixed(4)},${longitude.toFixed(4)}`; const pointsData = await fetchJson<PointsResponse>(pointsUrl, { Accept: "application/geo+json", }); if (!pointsData || !pointsData.properties?.forecast) { return { content: [ { type: "text", text: `Failed to get forecast data for coordinates: ${latitude}, ${longitude}`, }, ], }; } const forecastData = await fetchJson<ForecastResponse>(pointsData.properties.forecast); 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" }], }; } const formattedForecast = periods.map((period) => [ `${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: `Forecast for ${latitude}, ${longitude}:\n\n${formattedForecast.join("\n")}` }], }; } );