get-station-code-by-names
Find station codes for Chinese railway stations by entering their Chinese names. Use this tool to get the required station_code parameter when booking tickets through 12306.
Instructions
通过具体的中文车站名查询其 station_code 和车站名。此接口主要用于在用户提供具体车站名作为出发地或到达地时,为接口准备 station_code 参数。
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| stationNames | Yes | 具体的中文车站名称,例如:"北京南", "上海虹桥"。若要查询多个站点,请用|分割,比如"北京南|上海虹桥"。 |
Implementation Reference
- src/index.ts:644-659 (handler)The handler function processes one or more Chinese station names (split by '|'), optionally strips trailing '站', looks them up in the NAME_STATIONS dictionary, and returns a JSON object with station codes or error messages.async ({ stationNames }) => { let result: Record<string, object> = {}; for (let stationName of stationNames.split('|')) { stationName = stationName.endsWith('站') ? stationName.substring(0, -1) : stationName; if (!(stationName in NAME_STATIONS)) { result[stationName] = { error: '未检索到城市。' }; } else { result[stationName] = NAME_STATIONS[stationName]; } } return { content: [{ type: 'text', text: JSON.stringify(result) }], }; }
- src/index.ts:637-643 (schema)Zod input schema defining 'stationNames' as a string parameter with description for querying station codes by Chinese names.{ stationNames: z .string() .describe( '具体的中文车站名称,例如:"北京南", "上海虹桥"。若要查询多个站点,请用|分割,比如"北京南|上海虹桥"。' ), },
- src/index.ts:634-660 (registration)Registers the 'get-station-code-by-names' tool on the MCP server, including name, description, input schema, and handler function.server.tool( 'get-station-code-by-names', '通过具体的中文车站名查询其 `station_code` 和车站名。此接口主要用于在用户提供**具体车站名**作为出发地或到达地时,为接口准备 `station_code` 参数。', { stationNames: z .string() .describe( '具体的中文车站名称,例如:"北京南", "上海虹桥"。若要查询多个站点,请用|分割,比如"北京南|上海虹桥"。' ), }, async ({ stationNames }) => { let result: Record<string, object> = {}; for (let stationName of stationNames.split('|')) { stationName = stationName.endsWith('站') ? stationName.substring(0, -1) : stationName; if (!(stationName in NAME_STATIONS)) { result[stationName] = { error: '未检索到城市。' }; } else { result[stationName] = NAME_STATIONS[stationName]; } } return { content: [{ type: 'text', text: JSON.stringify(result) }], }; } );