get-stations-code-in-city
Retrieve a list of all railway stations and their corresponding codes within a Chinese city using the city name as input. Ideal for identifying available train stations for departure or arrival planning.
Instructions
通过中文城市名查询该城市 所有 火车站的名称及其对应的 station_code,结果是一个包含多个车站信息的列表。当用户想了解一个城市有哪些火车站,或者不确定具体从哪个车站出发/到达时可以使用此接口。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | 中文城市名称,例如:"北京", "上海" |
Implementation Reference
- src/index.ts:597-606 (handler)The handler function that implements the core logic of the 'get-stations-code-in-city' tool. It takes a city name, checks if it exists in the CITY_STATIONS map, and returns a JSON string of the stations in that city or an error message.async ({ city }) => { if (!(city in CITY_STATIONS)) { return { content: [{ type: 'text', text: 'Error: City not found. ' }], }; } return { content: [{ type: 'text', text: JSON.stringify(CITY_STATIONS[city]) }], }; }
- src/index.ts:594-596 (schema)The input schema for the tool, defining the 'city' parameter as a string using Zod validation with a description.{ city: z.string().describe('中文城市名称,例如:"北京", "上海"'), },
- src/index.ts:591-607 (registration)The registration of the 'get-stations-code-in-city' tool using McpServer.tool method, including name, description, schema, and handler.server.tool( 'get-stations-code-in-city', '通过中文城市名查询该城市 **所有** 火车站的名称及其对应的 `station_code`,结果是一个包含多个车站信息的列表。当用户想了解一个城市有哪些火车站,或者不确定具体从哪个车站出发/到达时可以使用此接口。', { city: z.string().describe('中文城市名称,例如:"北京", "上海"'), }, async ({ city }) => { if (!(city in CITY_STATIONS)) { return { content: [{ type: 'text', text: 'Error: City not found. ' }], }; } return { content: [{ type: 'text', text: JSON.stringify(CITY_STATIONS[city]) }], }; } );
- src/index.ts:41-60 (helper)Precomputed data structure mapping Chinese city names to arrays of stations (with station_code and station_name) in that city, directly used by the tool handler. Built from the global STATIONS map.const CITY_STATIONS: Record< string, { station_code: string; station_name: string }[] > = (() => { const result: Record< string, { station_code: string; station_name: string }[] > = {}; for (const station of Object.values(STATIONS)) { const city = station.city; if (!result[city]) { result[city] = []; } result[city].push({ station_code: station.station_code, station_name: station.station_name, }); } return result; })(); //以城市名名为键,位于该城市的的所有Station列表的记录