get-weather-forecast
Retrieve weather forecasts by providing city names to access current conditions and predictions through structured API calls.
Instructions
根据经纬度获取天气信息
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | No |
Implementation Reference
- src/index.ts:57-73 (handler)The handler function that implements the core logic of the 'get-weather-forecast' tool. It constructs the API URL using the gaode endpoint and key, fetches the weather data for the given city, types it as Root, and returns a standardized content response with the JSON stringified data.async ({ city }) => { // 获取网格点数据的 URL const pointsUrl = `${gaode}?key=${key}&city=${city}`; const data: Root = await fetch(pointsUrl).then((response) => response.json(), ); return { content: [ { type: 'text', text: JSON.stringify(data), }, ], }; },
- src/index.ts:16-36 (schema)TypeScript interfaces defining the expected structure of the weather API response data, used for typing the 'data' variable in the handler.export interface Root { status: string; count: string; info: string; infocode: string; lives: Lfe[]; } export interface Lfe { province: string; city: string; adcode: string; weather: string; temperature: string; winddirection: string; windpower: string; humidity: string; reporttime: string; temperature_float: string; humidity_float: string; }
- src/index.ts:53-56 (schema)Zod validation schema for the tool's input parameters. Defines an optional 'city' string parameter.{ city: z.string().optional(), }, // 工具的异步处理函数
- src/index.ts:46-74 (registration)The MCP server.tool registration for the 'get-weather-forecast' tool, including name, description, input schema, and inline handler implementation.// 注册获取天气预报的工具 server.tool( // 工具名称 'get-weather-forecast', // 工具描述 '根据经纬度获取天气信息', // 工具参数 { city: z.string().optional(), }, // 工具的异步处理函数 async ({ city }) => { // 获取网格点数据的 URL const pointsUrl = `${gaode}?key=${key}&city=${city}`; const data: Root = await fetch(pointsUrl).then((response) => response.json(), ); return { content: [ { type: 'text', text: JSON.stringify(data), }, ], }; }, );
- src/index.ts:13-14 (helper)Constants defining the Amap (Gaode) weather API endpoint URL and the API key loaded from environment variables, used in the tool handler.const gaode = 'https://restapi.amap.com/v3/weather/weatherInfo'; const key = process.env.GAODE_KEY;