get-station-code-of-citys
Retrieve station codes for Chinese cities by inputting city names. Use this tool to prepare the station_code parameter required for API queries on the 12306 train ticket search platform.
Instructions
通过中文城市名查询代表该城市的 station_code。此接口主要用于在用户提供城市名作为出发地或到达地时,为接口准备 station_code 参数。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| citys | Yes | 要查询的城市,比如"北京"。若要查询多个城市,请用|分割,比如"北京|上海"。 |
Implementation Reference
- src/index.ts:871-883 (handler)The handler function processes input city names (separated by '|'), checks CITY_CODES map for each city to find the representative station_code and station_name, returns errors for unknown cities, and outputs JSON string in MCP content format.async ({ citys }) => { let result: Record<string, object> = {}; for (const city of citys.split('|')) { if (!(city in CITY_CODES)) { result[city] = { error: '未检索到城市。' }; } else { result[city] = CITY_CODES[city]; } } return { content: [{ type: 'text', text: JSON.stringify(result) }], }; }
- src/index.ts:864-870 (schema)Zod schema defining the input parameter 'citys': a string of city names separated by '|', with descriptive help text.{ citys: z .string() .describe( '要查询的城市,比如"北京"。若要查询多个城市,请用|分割,比如"北京|上海"。' ), },
- src/index.ts:861-884 (registration)The server.tool registration call that defines the tool name, description, input schema, and handler function.server.tool( 'get-station-code-of-citys', '通过中文城市名查询代表该城市的 `station_code`。此接口主要用于在用户提供**城市名**作为出发地或到达地时,为接口准备 `station_code` 参数。', { citys: z .string() .describe( '要查询的城市,比如"北京"。若要查询多个城市,请用|分割,比如"北京|上海"。' ), }, async ({ citys }) => { let result: Record<string, object> = {}; for (const city of citys.split('|')) { if (!(city in CITY_CODES)) { result[city] = { error: '未检索到城市。' }; } else { result[city] = CITY_CODES[city]; } } return { content: [{ type: 'text', text: JSON.stringify(result) }], }; } );
- src/index.ts:69-86 (helper)Helper constant CITY_CODES: maps city names to the representative station (where station_name matches city) with station_code and station_name, derived from CITY_STATIONS.const CITY_CODES: Record< string, { station_code: string; station_name: string } > = (() => { const result: Record< string, { station_code: string; station_name: string } > = {}; for (const [city, stations] of Object.entries(CITY_STATIONS)) { for (const station of stations) { if (station.station_name == city) { result[city] = station; break; } } } return result; })(); //以城市名名为键的Station记录