Skip to main content
Glama
dimonets

MCP Weather Server

by dimonets

get-forecast

Retrieve 7-day weather forecasts including temperatures, precipitation, and sunrise/sunset times for any city worldwide.

Instructions

Get 7-day weather forecast for any city including daily temperatures, precipitation, and sunrise/sunset times

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
cityYesThe name of the city to get forecast information for (e.g., 'New York', 'London', 'Tokyo')

Implementation Reference

  • main.ts:535-586 (registration)
    Registration of the 'get-forecast' tool using McpServer.tool(), including description, input schema with Zod validation for city parameter, and async handler function that delegates to getForecastForCity and formats response as text or JSON.
    server.tool(
      'get-forecast',
      'Get 7-day weather forecast for any city including daily temperatures, precipitation, and sunrise/sunset times',
      {
        city: z.string()
          .min(1, "City name cannot be empty")
          .max(100, "City name is too long")
          .describe("The name of the city to get forecast information for (e.g., 'New York', 'London', 'Tokyo')")
      },
      async({ city }) => {
        try {
          const result = await getForecastForCity(city);
          
          // If it's an error string, return it as text
          if (typeof result === 'string') {
            return {
              content: [
                {
                  type: "text",
                  text: result
                }
              ]
            };
          }
          
          // If it's processed data, return it as JSON string for structured access
          return {
            content: [
              {
                type: "text",
                text: JSON.stringify(result, null, 2)
              }
            ]
          };
        } catch (error) {
          console.error('Forecast fetch error:', error);
          
          const errorMessage = error instanceof Error && error.message.includes('fetch') 
            ? `❌ Unable to fetch forecast data. Please check your internet connection and try again.`
            : `❌ Error: ${error instanceof Error ? error.message : 'Unknown error'}`;
          
          return {
            content: [
              {
                type: "text",
                text: errorMessage
              }
            ]
          };
        }
      }
    );
  • main.ts:356-406 (handler)
    Core implementation of the forecast tool logic: performs geocoding to get lat/long for the city, fetches 7-day daily forecast data from Open-Meteo API, processes it into a structured ProcessedForecastData object with formatted strings and descriptions, returns error string if city not found.
    async function getForecastForCity(city: string): Promise<ProcessedForecastData | string> {
      // Step 1: Get coordinates for the city
      const geoUrl = `${CONFIG.GEOCODING_API}?name=${encodeURIComponent(city)}&count=1&language=en&format=json`;
      const geoResponse = await fetchWithRetry(geoUrl);
      const geoData: GeocodingResponse = await geoResponse.json();
    
      // Handle city not found
      if (!geoData.results || geoData.results.length === 0) {
        return `❌ Sorry, I couldn't find a city named "${city}". Please check the spelling and try again.`;
      }
    
      const location = geoData.results[0];
      
      // Step 2: Get 7-day forecast data
      const forecastUrl = `${CONFIG.WEATHER_API}?latitude=${location.latitude}&longitude=${location.longitude}&daily=temperature_2m_max,temperature_2m_min,precipitation_sum,weather_code,sunrise,sunset&timezone=auto`;
      const forecastResponse = await fetchWithRetry(forecastUrl);
      const forecastData: ExtendedWeatherResponse = await forecastResponse.json();
    
      // Process and structure the forecast data
      const locationName = location.admin1 ? `${location.name}, ${location.admin1}, ${location.country}` : `${location.name}, ${location.country}`;
      
      const processedData: ProcessedForecastData = {
        location: {
          name: location.name,
          fullName: locationName,
          latitude: location.latitude,
          longitude: location.longitude,
          country: location.country,
          admin1: location.admin1
        },
        daily: forecastData.daily.time.map((date, index) => ({
          date: date,
          formattedDate: formatDate(date),
          maxTemperature: forecastData.daily.temperature_2m_max[index],
          formattedMaxTemperature: formatTemperature(forecastData.daily.temperature_2m_max[index]),
          minTemperature: forecastData.daily.temperature_2m_min[index],
          formattedMinTemperature: formatTemperature(forecastData.daily.temperature_2m_min[index]),
          precipitation: forecastData.daily.precipitation_sum[index],
          formattedPrecipitation: formatPrecipitation(forecastData.daily.precipitation_sum[index]),
          weatherCode: forecastData.daily.weather_code[index],
          weatherDescription: getWeatherDescription(forecastData.daily.weather_code[index]),
          sunrise: forecastData.daily.sunrise[index],
          formattedSunrise: formatTime(forecastData.daily.sunrise[index]),
          sunset: forecastData.daily.sunset[index],
          formattedSunset: formatTime(forecastData.daily.sunset[index])
        })),
        raw: forecastData
      };
    
      return processedData;
    }
  • Zod input schema for the 'get-forecast' tool, validating the 'city' parameter as a non-empty string up to 100 chars with description.
      city: z.string()
        .min(1, "City name cannot be empty")
        .max(100, "City name is too long")
        .describe("The name of the city to get forecast information for (e.g., 'New York', 'London', 'Tokyo')")
    },
  • TypeScript interface defining the structured output format of the processed forecast data returned by getForecastForCity.
    interface ProcessedForecastData {
      location: {
        name: string;
        fullName: string;
        latitude: number;
        longitude: number;
        country: string;
        admin1?: string;
      };
      daily: Array<{
        date: string;
        formattedDate: string;
        maxTemperature: number;
        formattedMaxTemperature: string;
        minTemperature: number;
        formattedMinTemperature: string;
        precipitation: number;
        formattedPrecipitation: string;
        weatherCode: number;
        weatherDescription: string;
        sunrise: string;
        formattedSunrise: string;
        sunset: string;
        formattedSunset: string;
      }>;
      raw: ExtendedWeatherResponse;
    }
Install Server

Other 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/dimonets/mcp-weather-server'

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