get-station-code-of-citys
Find station codes for Chinese cities to prepare departure or destination parameters for 12306 ticket booking interfaces.
Instructions
通过中文城市名查询代表该城市的 station_code。此接口主要用于在用户提供城市名作为出发地或到达地时,为接口准备 station_code 参数。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| citys | Yes | 要查询的城市,比如"北京"。若要查询多个城市,请用|分割,比如"北京|上海"。 |
Implementation Reference
- src/index.ts:871-883 (handler)The core handler function that processes input city names (separated by '|'), queries the CITY_CODES map for representative station codes, returns error for unknown cities, and formats the result as JSON text content.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-based input schema defining the 'citys' parameter as a string supporting multiple cities separated by '|'.{ citys: z .string() .describe( '要查询的城市,比如"北京"。若要查询多个城市,请用|分割,比如"北京|上海"。' ), },
- src/index.ts:861-884 (registration)Full MCP server.tool registration including name, description, input schema using Zod, and the async 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)Supporting constant CITY_CODES mapping city names to representative stations (where station_name equals city name), built from CITY_STATIONS data.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记录