Skip to main content
Glama
dhinojosac

TypeScript MCP Server Template

by dhinojosac

getWeatherForecast

Retrieve weather forecast data for any location using geographic coordinates to support planning and decision-making.

Instructions

Get weather forecast for a specific location using coordinates

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The core handler function for the getWeatherForecast tool. Validates input arguments using WeatherForecastSchema and simulates a weather API call to return formatted forecast data.
    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 the input parameters for getWeatherForecast: latitude and longitude numbers (referencing common schemas).
    export const WeatherForecastSchema = z.object({
      latitude: LatitudeSchema,
      longitude: LongitudeSchema,
    });
  • Tool registration call within registerWeatherTools function, including name, metadata, and inline handler. Called from src/server.ts.
    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 a weather API call, generating mock forecast data including temperature, conditions, and location info.
    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();
    }

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