get-station-code-of-citys
Retrieve railway station codes for Chinese cities by name. Use this to convert city names to station codes required for 12306 ticket search queries.
Instructions
通过中文城市名查询代表该城市的 station_code。此接口主要用于在用户提供城市名作为出发地或到达地时,为接口准备 station_code 参数。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| citys | Yes | 要查询的城市,比如"北京"。若要查询多个城市,请用|分割,比如"北京|上海"。 |
Implementation Reference
- src/index.ts:886-898 (handler)The handler function for the 'get-station-code-of-citys' tool. It accepts a 'citys' string (pipe-separated city names), looks up each city in the CITY_CODES map, and returns the station_code for each city. If a city is not found, it returns an error.
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:879-885 (schema)The input schema for the 'get-station-code-of-citys' tool, defining the 'citys' parameter as a Zod string with a description about pipe-separated city names.
{ citys: z .string() .describe( '要查询的城市,比如"北京"。若要查询多个城市,请用|分割,比如"北京|上海"。' ), }, - src/index.ts:876-899 (registration)The registration of the 'get-station-code-of-citys' tool using server.tool(), with its name, description, schema, and handler.
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)The CITY_CODES constant - an IIFE that builds a map from city name to the station that shares the city's name (e.g., '北京' -> { station_code: 'BJP', station_name: '北京' }). This is the data source used by the handler.
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记录