get-weather
Retrieve real-time, hourly, or extended weather forecasts for specific locations in China by providing latitude and longitude coordinates with HeFeng Weather MCP Server.
Instructions
获取中国国内的天气预报
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| days | No | 预报天数,now为实时天气,24h为24小时预报,72h为72小时预报,168h为168小时预报,3d为3天预报,以此类推 | now |
| location | Yes | 逗号分隔的经纬度信息 (e.g., 116.40,39.90) |
Input Schema (JSON Schema)
{
"properties": {
"days": {
"default": "now",
"description": "预报天数,now为实时天气,24h为24小时预报,72h为72小时预报,168h为168小时预报,3d为3天预报,以此类推",
"enum": [
"now",
"24h",
"72h",
"168h",
"3d",
"7d",
"10d",
"15d",
"30d"
],
"type": "string"
},
"location": {
"description": "逗号分隔的经纬度信息 (e.g., 116.40,39.90)",
"type": "string"
}
},
"required": [
"location"
],
"type": "object"
}
Implementation Reference
- src/index.ts:44-63 (registration)Registration of the 'get-weather' tool in the ListToolsRequestHandler, including name, description, and input schema definition.{ name: "get-weather", description: "获取中国国内的天气预报", inputSchema: { type: "object", properties: { location: { type: "string", description: "逗号分隔的经纬度信息 (e.g., 116.40,39.90)", }, days: { type: "string", enum: ["now", "24h", "72h", "168h", "3d", "7d", "10d", "15d", "30d"], description: "预报天数,now为实时天气,24h为24小时预报,72h为72小时预报,168h为168小时预报,3d为3天预报,以此类推", default: "now" } }, required: ["location"], }, },
- src/index.ts:22-25 (schema)Zod schema for validating the input arguments to the get-weather tool.const WeatherArgumentsSchema = z.object({ location: z.string(), // Location name or coordinates days: z.enum(['now', '24h', '72h', '168h', '3d', '7d', '10d', '15d', '30d']).default('now'), // 预报天数 });
- src/index.ts:127-207 (handler)Core handler logic for executing the 'get-weather' tool: validates arguments, fetches weather data from HeFeng API based on 'days' parameter (now, hourly, or daily forecast), formats and returns the response.if (name === "get-weather") { const { location, days } = WeatherArgumentsSchema.parse(args); if (days === 'now') { // Get current weather data const weatherUrl = `${HEFENG_API_BASE}/weather/now?location=${location}&key=${HEFENG_API_KEY}`; const weatherData = await makeHeFengRequest<HeFengWeatherNowResponse>(weatherUrl); if (!weatherData || !weatherData.now) { return { content: [{ type: "text", text: `无法获取 ${location} 的天气数据` }], }; } const { now } = weatherData; const weatherText = `地点: ${location}\n` + `观测时间: ${now.obsTime}\n` + `天气: ${now.text}\n` + `温度: ${now.temp}°C\n` + `体感温度: ${now.feelsLike}°C\n` + `风向: ${now.windDir}\n` + `风力: ${now.windScale}级`; return { content: [{ type: "text", text: weatherText }] }; } else if (['24h', '72h', '168h'].includes(days)) { // Get hourly forecast data const weatherUrl = `${HEFENG_API_BASE}/weather/${days}?location=${location}&key=${HEFENG_API_KEY}`; const weatherData = await makeHeFengRequest<HeFengWeatherHourlyResponse>(weatherUrl); if (!weatherData || !weatherData.hourly) { return { content: [{ type: "text", text: `无法获取 ${location} 的逐小时天气预报数据` }], }; } const hoursText = weatherData.hourly.map(hour => { return `时间: ${hour.fxTime}\n` + `天气: ${hour.text}\n` + `温度: ${hour.temp}°C\n` + `湿度: ${hour.humidity}%\n` + `风向: ${hour.windDir} ${hour.windScale}级\n` + `------------------------`; }).join('\n'); return { content: [{ type: "text", text: `地点: ${location}\n${days}小时预报:\n${hoursText}` }], }; } else { // Get daily forecast weather data const daysNum = parseInt(days); const weatherUrl = `${HEFENG_API_BASE}/weather/${days}?location=${location}&key=${HEFENG_API_KEY}`; const weatherData = await makeHeFengRequest<HeFengWeatherDailyResponse>(weatherUrl); if (!weatherData || !weatherData.daily) { return { content: [{ type: "text", text: `无法获取 ${location} 的天气预报数据` }], }; } const forecastText = weatherData.daily.map(day => { return `日期: ${day.fxDate}\n` + `白天天气: ${day.textDay}\n` + `夜间天气: ${day.textNight}\n` + `最高温度: ${day.tempMax}°C\n` + `最低温度: ${day.tempMin}°C\n` + `白天风向: ${day.windDirDay} ${day.windScaleDay}级\n` + `夜间风向: ${day.windDirNight} ${day.windScaleNight}级\n` + `------------------------`; }).join('\n'); return { content: [{ type: "text", text: `地点: ${location}\n${daysNum}天预报:\n${forecastText}` }], }; } } else {
- src/index.ts:69-84 (helper)Helper function used by the get-weather handler to make HTTP requests to the HeFeng weather API.async function makeHeFengRequest<T>(url: string): Promise<T | null> { const headers = { Accept: "application/json", }; try { const response = await fetch(url, { headers }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return (await response.json()) as T; } catch (error) { console.error("Error making HeFeng request:", error); return null; } }