Skip to main content
Glama
dhinojosac

TypeScript MCP Server Template

by dhinojosac

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
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • 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}`, }, ], }; }
  • Zod schema defining input parameters for getWeatherForecast tool: latitude and longitude with imported validation schemas.
    export const WeatherForecastSchema = z.object({ latitude: LatitudeSchema, longitude: LongitudeSchema, });
  • 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}`, }, ], }; } );
  • 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(); }
  • 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; } }

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/dhinojosac/calendar-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server