Skip to main content
Glama

get-station-code-by-names

Find station codes for Chinese railway stations by providing 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
NameRequiredDescriptionDefault
stationNamesYes具体的中文车站名称,例如:"北京南", "上海虹桥"。若要查询多个站点,请用|分割,比如"北京南|上海虹桥"。

Implementation Reference

  • Inline async handler function for the 'get-station-code-by-names' tool. It processes multiple station names separated by '|', optionally strips trailing '站', looks up in NAME_STATIONS map, and returns JSON results or error.
    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) }],
        };
    }
  • Zod input schema defining 'stationNames' parameter as a string supporting multiple names separated by '|'.
    {
        stationNames: z
            .string()
            .describe(
                '具体的中文车站名称,例如:"北京南", "上海虹桥"。若要查询多个站点,请用|分割,比如"北京南|上海虹桥"。'
            ),
    },
  • src/index.ts:886-912 (registration)
    Registration of the tool using server.tool(), including name, description, schema, and inline 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) }],
            };
        }
    );
  • Constant NAME_STATIONS map generated from STATIONS data, mapping station names to {station_code, station_name}, used by the tool handler.
    const NAME_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 station_name = station.station_name;
            result[station_name] = {
                station_code: station.station_code,
                station_name: station.station_name,
            };
        }
        return result;
    })(); //以车站名为键的Station记录

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Joooook/12306-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server