get_weather_forecast
Retrieve accurate weather forecasts for any location with customizable timeframes. Specify the location and the number of days (1-7) to predict weather conditions effectively.
Instructions
Get weather forecast for a specified location
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| days | No | Forecast days (1-7 days, default is 3 days) | |
| location | Yes | Location name |
Input Schema (JSON Schema)
{
"properties": {
"days": {
"description": "Forecast days (1-7 days, default is 3 days)",
"maximum": 7,
"minimum": 1,
"type": "number"
},
"location": {
"description": "Location name",
"type": "string"
}
},
"required": [
"location"
],
"type": "object"
}
Implementation Reference
- src/weather-service.ts:31-44 (handler)Core handler function that implements the get_weather_forecast tool logic. Normalizes location, fetches from mock data if available, or generates synthetic forecast.async getWeatherForecast(location: string, days: number = 3): Promise<WeatherForecast> { const normalizedLocation = this.normalizeLocation(location); if (mockForecastData[normalizedLocation]) { const forecast = mockForecastData[normalizedLocation]; return { ...forecast, forecast: forecast.forecast.slice(0, days) }; } // If location not found, generate mock forecast data return this.generateMockForecast(location, days); }
- src/types.ts:15-28 (schema)TypeScript interface definitions for the WeatherForecast output type used by the tool.export interface WeatherForecast { location: string; forecast: DailyForecast[]; } export interface DailyForecast { date: string; highTemp: number; lowTemp: number; condition: string; description: string; chanceOfRain: number; humidity: number; }
- src/index.ts:53-71 (registration)Tool registration in the main stdio MCP server (index.ts), defining name, description, and input schema for ListTools response.name: 'get_weather_forecast', description: 'Get weather forecast for a specified location', inputSchema: { type: 'object', properties: { location: { type: 'string', description: 'Location name', }, days: { type: 'number', description: 'Forecast days (1-7 days, default is 3 days)', minimum: 1, maximum: 7, }, }, required: ['location'], }, },
- src/sse-server.ts:42-59 (registration)Tool registration in the SSE MCP server.name: "get_weather_forecast", description: "Get weather forecast for a specified location", inputSchema: { type: "object", properties: { location: { type: "string", description: "Location name", }, days: { type: "number", description: "Forecast days (1-7 days, default 3 days)", minimum: 1, maximum: 7, }, }, required: ["location"], },
- src/weather-service.ts:124-158 (helper)Private helper method called by the handler to generate synthetic forecast data when no mock data is available for the location.private generateMockForecast(location: string, days: number): WeatherForecast { const forecast = []; const baseTemp = 20 + Math.random() * 15; // Temperature between 20-35°C const conditions = ['Sunny', 'Partly Cloudy', 'Cloudy', 'Rainy', 'Thunderstorms', 'Overcast', 'Light Rain']; const descriptions = { 'Sunny': 'Clear skies with bright sunshine', 'Partly Cloudy': 'Mix of sun and clouds', 'Cloudy': 'Overcast skies with mild temperatures', 'Rainy': 'Rain showers expected', 'Thunderstorms': 'Thunderstorms with heavy rain', 'Overcast': 'Cloudy skies throughout the day', 'Light Rain': 'Light rain showers' }; for (let i = 1; i <= days; i++) { const date = new Date(Date.now() + i * 24 * 60 * 60 * 1000); const tempVariation = (Math.random() - 0.5) * 10; const condition = conditions[Math.floor(Math.random() * conditions.length)]; forecast.push({ date: date.toISOString().split('T')[0], highTemp: Math.round(baseTemp + tempVariation + 5), lowTemp: Math.round(baseTemp + tempVariation - 5), condition, description: descriptions[condition as keyof typeof descriptions] || `${condition} conditions expected`, chanceOfRain: condition.includes('Rain') || condition.includes('Thunder') ? 70 + Math.random() * 30 : Math.random() * 40, humidity: 50 + Math.random() * 40 }); } return { location, forecast }; }