Skip to main content
Glama
lmanchu

Weather MCP Server

by lmanchu

get_weather_forecast

Retrieve a 5-day weather forecast for any city with temperature data in Celsius or Fahrenheit units.

Instructions

Get 5-day weather forecast for a specific city

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
cityYesCity name (e.g., "Taipei", "Tokyo", "New York")
unitsNoTemperature units: "metric" (Celsius) or "imperial" (Fahrenheit)metric

Implementation Reference

  • The main handler function that fetches the 5-day weather forecast from the OpenWeatherMap API, groups data by day, computes high/low temps and dominant condition, and formats a textual response.
    async getWeatherForecast(city, units = 'metric') {
      const url = `${API_BASE_URL}/forecast?q=${encodeURIComponent(city)}&units=${units}&appid=${API_KEY}`;
    
      const response = await fetch(url);
    
      if (!response.ok) {
        if (response.status === 404) {
          throw new Error(`City "${city}" not found`);
        }
        throw new Error(`Weather API error: ${response.statusText}`);
      }
    
      const data = await response.json();
    
      // Format the response
      const tempUnit = units === 'metric' ? '°C' : '°F';
    
      // Group forecasts by day
      const dailyForecasts = {};
    
      data.list.forEach(item => {
        const date = new Date(item.dt * 1000).toLocaleDateString();
        if (!dailyForecasts[date]) {
          dailyForecasts[date] = [];
        }
        dailyForecasts[date].push(item);
      });
    
      // Build forecast text
      let forecastText = `📅 5-Day Weather Forecast for ${data.city.name}, ${data.city.country}\n\n`;
    
      Object.entries(dailyForecasts).slice(0, 5).forEach(([date, forecasts]) => {
        const temps = forecasts.map(f => f.main.temp);
        const maxTemp = Math.max(...temps);
        const minTemp = Math.min(...temps);
        const conditions = forecasts.map(f => f.weather[0].main);
        const mostCommon = conditions.sort((a,b) =>
          conditions.filter(v => v===a).length - conditions.filter(v => v===b).length
        ).pop();
    
        forecastText += `${date}:\n`;
        forecastText += `  High: ${maxTemp.toFixed(1)}${tempUnit} | Low: ${minTemp.toFixed(1)}${tempUnit}\n`;
        forecastText += `  Condition: ${mostCommon}\n\n`;
      });
    
      return {
        content: [
          {
            type: 'text',
            text: forecastText.trim(),
          },
        ],
      };
    }
  • Input schema defining the required 'city' parameter and optional 'units' parameter (metric or imperial) for the tool.
    inputSchema: {
      type: 'object',
      properties: {
        city: {
          type: 'string',
          description: 'City name (e.g., "Taipei", "Tokyo", "New York")',
        },
        units: {
          type: 'string',
          description: 'Temperature units: "metric" (Celsius) or "imperial" (Fahrenheit)',
          enum: ['metric', 'imperial'],
          default: 'metric',
        },
      },
      required: ['city'],
    },
  • index.js:97-116 (registration)
    Tool registration in the ListTools response, including name, description, and input schema.
    {
      name: 'get_weather_forecast',
      description: 'Get 5-day weather forecast for a specific city',
      inputSchema: {
        type: 'object',
        properties: {
          city: {
            type: 'string',
            description: 'City name (e.g., "Taipei", "Tokyo", "New York")',
          },
          units: {
            type: 'string',
            description: 'Temperature units: "metric" (Celsius) or "imperial" (Fahrenheit)',
            enum: ['metric', 'imperial'],
            default: 'metric',
          },
        },
        required: ['city'],
      },
    },
  • index.js:129-130 (registration)
    Dispatch case in the CallToolRequestSchema handler that routes calls to the getWeatherForecast method.
    case 'get_weather_forecast':
      return await this.getWeatherForecast(args.city, args.units || 'metric');
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/lmanchu/weather-mcp-server'

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