fetch-weather
Retrieve current weather data for a specific city using the MCP Weather Server's integration with Claude AI and Postgres database, supporting custom Model Context Protocol.
Instructions
Tool to fetch the weather of a city
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | Nombre de la ciudad |
Implementation Reference
- main.ts:20-49 (handler)Handler function that fetches weather data: first geocodes the city using Open-Meteo geocoding API, then retrieves current weather forecast.async ({ city }) => { const response = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${city}&count=10&language=en&format=json`) const data = await response.json(); if (data.length === 0) { return { content: [ { type: "text", text: `No se encontró información del clima para la ciudad: ${city}.` }, ], }; } const { latitude, longitude } = data.results[0] const weatherResponse = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&hourly=temperature_2m¤t=temperature_2m,is_day,precipitation,rain&forecast_days=1`) const weatherData = await weatherResponse.json(); return { content: [ { type: "text", text: JSON.stringify(weatherData, null, 2), }, ], }; }
- main.ts:17-19 (schema)Input schema defined with Zod: requires a 'city' string parameter.{ city: z.string().describe('Nombre de la ciudad'), },
- main.ts:14-50 (registration)Registration of the 'fetch-weather' tool on the McpServer instance.server.tool( 'fetch-weather', 'Tool to fetch the weather of a city', { city: z.string().describe('Nombre de la ciudad'), }, async ({ city }) => { const response = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${city}&count=10&language=en&format=json`) const data = await response.json(); if (data.length === 0) { return { content: [ { type: "text", text: `No se encontró información del clima para la ciudad: ${city}.` }, ], }; } const { latitude, longitude } = data.results[0] const weatherResponse = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&hourly=temperature_2m¤t=temperature_2m,is_day,precipitation,rain&forecast_days=1`) const weatherData = await weatherResponse.json(); return { content: [ { type: "text", text: JSON.stringify(weatherData, null, 2), }, ], }; } );