get-current-weather
Retrieve real-time weather data by specifying a city and country code. Integrates with the MCP Servers for accurate and instant weather updates using a straightforward API.
Instructions
Get current weather for a location
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | City name (e.g. Beijing, London) | |
| country | No | Country code (e.g. CN, GB) |
Implementation Reference
- src/weather/index.ts:23-40 (registration)Registration of the 'get-current-weather' MCP tool using server.tool(), including description, input schema, and handler function."get-current-weather", "Get current weather for a location", { city: z.string().describe("City name (e.g. Beijing, London)"), country: z.string().optional().describe("Country code (e.g. CN, GB)") }, async ({ city, country }) => { const weatherText = await weatherController.getCurrentWeather(city, country); return { content: [ { type: "text", text: weatherText, }, ], }; } );
- src/weather/index.ts:25-28 (schema)Zod schema for tool inputs: city (required string), country (optional string).{ city: z.string().describe("City name (e.g. Beijing, London)"), country: z.string().optional().describe("Country code (e.g. CN, GB)") },
- src/weather/index.ts:29-39 (handler)Inline handler function for the tool: extracts parameters, calls WeatherController.getCurrentWeather(), and returns formatted MCP content response.async ({ city, country }) => { const weatherText = await weatherController.getCurrentWeather(city, country); return { content: [ { type: "text", text: weatherText, }, ], }; }
- WeatherController.getCurrentWeather(): fetches raw weather data from service and formats it into a human-readable string with temperature, conditions, humidity, etc.async getCurrentWeather(city: string, country?: string): Promise<string> { const weatherData = await this.weatherService.getCurrentWeather(city, country); if (!weatherData) { return `Failed to retrieve weather data for ${country ? `${city}, ${country}` : city}`; } return [ `Current weather in ${weatherData.name}, ${weatherData.sys.country}:`, `Temperature: ${weatherData.main.temp}°C (Feels like: ${weatherData.main.feels_like}°C)`, `Conditions: ${weatherData.weather[0].main} - ${weatherData.weather[0].description}`, `Humidity: ${weatherData.main.humidity}%`, `Wind Speed: ${weatherData.wind.speed} m/s`, `Pressure: ${weatherData.main.pressure} hPa`, ].join("\n"); }
- src/weather/service/weather.ts:35-38 (helper)WeatherService.getCurrentWeather(): builds location query and calls makeRequest to fetch current weather data from OpenWeatherMap API.async getCurrentWeather(city: string, country?: string): Promise<WeatherData | null> { const query = country ? `${city},${country}` : city; return this.makeRequest<WeatherData>("weather", { q: query }); }