get_current_weather
Fetch real-time weather data for a specified city or default to Sydney. Use the tool to retrieve current conditions, including temperature and forecasts, via the Weather MCP Server.
Instructions
Récupère la météo actuelle pour une ville (Sydney par défaut)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | No | Nom de la ville (optionnel, Sydney par défaut) | Sydney |
| country | No | Code pays optionnel (ex: AU, FR, US) |
Implementation Reference
- src/index.js:192-238 (handler)The main handler function that fetches geographical coordinates if needed, retrieves current weather data from Open-Meteo API, formats it with descriptions and wind direction, and returns a formatted text response.async getCurrentWeather(city, country) { let location; if (city.toLowerCase() === 'sydney' && !country) { location = DEFAULT_LOCATION; } else { location = await this.getCoordinates(city, country); } const url = `${WEATHER_API}?latitude=${location.latitude}&longitude=${location.longitude}¤t=temperature_2m,relative_humidity_2m,apparent_temperature,is_day,precipitation,rain,showers,snowfall,weather_code,cloud_cover,pressure_msl,surface_pressure,wind_speed_10m,wind_direction_10m,wind_gusts_10m&timezone=auto`; const response = await fetch(url); const data = await response.json(); if (!response.ok) { throw new Error('Erreur lors de la récupération de la météo'); } const current = data.current; const weatherDesc = this.getWeatherDescription(current.weather_code); const windDir = this.getWindDirection(current.wind_direction_10m); return { content: [ { type: 'text', text: `🌤️ Météo actuelle à ${location.name || location.admin1 || 'Localisation'}${location.country ? `, ${location.country}` : ''} 📍 **Localisation:** ${location.latitude}°, ${location.longitude}° 🌡️ **Température:** ${Math.round(current.temperature_2m)}°C 🤔 **Ressenti:** ${Math.round(current.apparent_temperature)}°C ☁️ **Conditions:** ${weatherDesc} ${current.is_day ? '☀️' : '🌙'} 💧 **Humidité:** ${current.relative_humidity_2m}% 🌬️ **Vent:** ${Math.round(current.wind_speed_10m)} km/h ${windDir} 💨 **Rafales:** ${Math.round(current.wind_gusts_10m)} km/h 🔽 **Pression:** ${Math.round(current.pressure_msl)} hPa ☁️ **Couverture nuageuse:** ${current.cloud_cover}% ${current.precipitation > 0 ? `🌧️ **Précipitations:** ${current.precipitation} mm` : ''} ${current.rain > 0 ? `🌧️ **Pluie:** ${current.rain} mm` : ''} ${current.snowfall > 0 ? `❄️ **Neige:** ${current.snowfall} cm` : ''} ⏰ **Dernière mise à jour:** ${new Date(current.time).toLocaleString('fr-FR')}`, }, ], }; }
- src/index.js:51-70 (registration)Registers the 'get_current_weather' tool in the ListToolsRequestSchema handler, including name, description, and input schema definition.{ name: 'get_current_weather', description: 'Récupère la météo actuelle pour une ville (Sydney par défaut)', inputSchema: { type: 'object', properties: { city: { type: 'string', description: 'Nom de la ville (optionnel, Sydney par défaut)', default: 'Sydney' }, country: { type: 'string', description: 'Code pays optionnel (ex: AU, FR, US)', } }, required: [], }, }, {
- src/index.js:146-147 (registration)Dispatches tool calls named 'get_current_weather' to the getCurrentWeather handler method within the CallToolRequestSchema handler.case 'get_current_weather': return await this.getCurrentWeather(args.city || 'Sydney', args.country);
- src/index.js:178-190 (helper)Helper function to resolve city/country to latitude/longitude coordinates using Open-Meteo geocoding API, called by the handler.async getCoordinates(city, country) { const query = country ? `${city}, ${country}` : city; const url = `${GEOCODING_API}?name=${encodeURIComponent(query)}&count=1&language=fr&format=json`; const response = await fetch(url); const data = await response.json(); if (!response.ok || !data.results || data.results.length === 0) { throw new Error(`Ville "${query}" non trouvée`); } return data.results[0]; }
- src/index.js:395-428 (helper)Helper function to translate weather codes from the API into human-readable French descriptions, used in the handler.getWeatherDescription(code) { const descriptions = { 0: 'Ciel dégagé', 1: 'Principalement dégagé', 2: 'Partiellement nuageux', 3: 'Couvert', 45: 'Brouillard', 48: 'Brouillard givrant', 51: 'Bruine légère', 53: 'Bruine modérée', 55: 'Bruine dense', 56: 'Bruine verglaçante légère', 57: 'Bruine verglaçante dense', 61: 'Pluie légère', 63: 'Pluie modérée', 65: 'Pluie forte', 66: 'Pluie verglaçante légère', 67: 'Pluie verglaçante forte', 71: 'Neige légère', 73: 'Neige modérée', 75: 'Neige forte', 77: 'Grains de neige', 80: 'Averses légères', 81: 'Averses modérées', 82: 'Averses violentes', 85: 'Averses de neige légères', 86: 'Averses de neige fortes', 95: 'Orage', 96: 'Orage avec grêle légère', 99: 'Orage avec grêle forte' }; return descriptions[code] || 'Conditions inconnues'; }