get-forecast
Retrieve weather forecast data for specific coordinates using latitude and longitude parameters to plan activities or monitor conditions.
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/tools/get-forecast.ts:22-96 (handler)The handler function for the 'get-forecast' tool. It fetches grid point data and forecast from NWS API using the provided latitude and longitude, handles errors, and formats the forecast periods into a structured text response.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 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'), ) const forecastText = `Forecast for ${latitude}, ${longitude}:\n\n${formattedForecast.join('\n')}` return { content: [ { type: 'text', text: forecastText, }, ], } },
- src/tools/get-forecast.ts:13-20 (schema)Zod input schema defining the required latitude (number, -90 to 90) and longitude (number, -180 to 180) parameters.inputSchema: z.object({ 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/tools/get-forecast.ts:7-98 (registration)The registerGetForecast() function that calls server.registerTool('get-forecast', metadata including title, description, and inputSchema, handler).export function registerGetForecast() { server.registerTool( 'get-forecast', { title: 'Weather forecast', description: 'Get weather forecast for a location', inputSchema: z.object({ 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 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'), ) const forecastText = `Forecast for ${latitude}, ${longitude}:\n\n${formattedForecast.join('\n')}` return { content: [ { type: 'text', text: forecastText, }, ], } }, ) }
- src/index.ts:10-10 (registration)Call to registerGetForecast() in the main server startup code to enable the tool.registerGetForecast()