getWeatherForecast
Retrieve weather forecasts for specific locations using coordinates with this TypeScript-based MCP server tool, designed for integration and accuracy.
Instructions
Get weather forecast for a specific location using coordinates
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/plugins/weatherPlugin.ts:24-41 (handler)The anonymous async handler function that executes the getWeatherForecast tool logic: validates input using WeatherForecastSchema, calls simulateWeatherAPI, and returns formatted forecast response.async (args: { [x: string]: any }) => { const { latitude, longitude }: WeatherForecastArgs = validateToolArgs( WeatherForecastSchema, args ); // Simulate weather API call (replace with actual API) const forecast = await simulateWeatherAPI(latitude, longitude); return { content: [ { type: 'text', text: `Weather forecast for coordinates (${latitude}, ${longitude}):\n${forecast}`, }, ], }; }
- src/schemas/toolSchemas.ts:46-49 (schema)Zod schema defining input parameters for getWeatherForecast tool: latitude and longitude with imported validation schemas.export const WeatherForecastSchema = z.object({ latitude: LatitudeSchema, longitude: LongitudeSchema, });
- src/plugins/weatherPlugin.ts:17-42 (registration)Registration of the getWeatherForecast tool on the MCP server, including name, metadata, and handler function.server.registerTool( 'getWeatherForecast', { title: 'Get Weather Forecast', description: 'Get weather forecast for a specific location using coordinates', }, async (args: { [x: string]: any }) => { const { latitude, longitude }: WeatherForecastArgs = validateToolArgs( WeatherForecastSchema, args ); // Simulate weather API call (replace with actual API) const forecast = await simulateWeatherAPI(latitude, longitude); return { content: [ { type: 'text', text: `Weather forecast for coordinates (${latitude}, ${longitude}):\n${forecast}`, }, ], }; } );
- src/plugins/weatherPlugin.ts:76-96 (helper)Helper function simulating weather API call, generating mock forecast data based on coordinates, used by the tool handler.async function simulateWeatherAPI( latitude: number, longitude: number ): Promise<string> { // Simulate API delay await new Promise(resolve => setTimeout(resolve, 100)); // Generate mock forecast based on coordinates const temperature = Math.round( 20 + latitude * 0.5 + (Math.random() * 10 - 5) ); const conditions = ['Sunny', 'Cloudy', 'Rainy', 'Partly Cloudy']; const condition = conditions[Math.floor(Math.random() * conditions.length)]; return ` 🌤️ Current Conditions: ${condition} 🌡️ Temperature: ${temperature}°C Location: ${latitude.toFixed(4)}, ${longitude.toFixed(4)} ⏰ Updated: ${new Date().toLocaleString()} `.trim(); }
- src/schemas/toolSchemas.ts:65-77 (helper)Utility function to validate tool arguments using Zod schemas, throws descriptive errors, used in the getWeatherForecast handler.export function validateToolArgs<T>(schema: z.ZodSchema<T>, args: unknown): T { try { return schema.parse(args); } catch (error) { if (error instanceof z.ZodError) { const errorMessages = error.errors .map(err => `${err.path.join('.')}: ${err.message}`) .join(', '); throw new Error(`Validation failed: ${errorMessages}`); } throw error; } }