Skip to main content
Glama

get-interline-tickets

Query interline ticket availability on China's 12306 railway system for connecting journeys between stations, with filtering options for train types and schedules.

Instructions

查询12306中转余票信息。尚且只支持查询前十条。

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dateYes查询日期,格式为 "yyyy-MM-dd"。如果用户提供的是相对日期(如“明天”),请务必先调用 `get-current-date` 接口获取当前日期,并计算出目标日期。
fromStationYes出发地的 `station_code` 。必须是通过 `get-station-code-by-names` 或 `get-station-code-of-citys` 接口查询得到的编码,严禁直接使用中文地名。
toStationYes出发地的 `station_code` 。必须是通过 `get-station-code-by-names` 或 `get-station-code-of-citys` 接口查询得到的编码,严禁直接使用中文地名。
middleStationNo中转地的 `station_code` ,可选。必须是通过 `get-station-code-by-names` 或 `get-station-code-of-citys` 接口查询得到的编码,严禁直接使用中文地名。
showWZNo是否显示无座车,默认不显示无座车。
trainFilterFlagsNo车次筛选条件,默认为空。从以下标志中选取多个条件组合[G(高铁/城际),D(动车),Z(直达特快),T(特快),K(快速),O(其他),F(复兴号),S(智能动车组)]
earliestStartTimeNo最早出发时间(0-24),默认为0。
latestStartTimeNo最迟出发时间(0-24),默认为24。
sortFlagNo排序方式,默认为空,即不排序。仅支持单一标识。可选标志:[startTime(出发时间从早到晚), arriveTime(抵达时间从早到晚), duration(历时从短到长)]
sortReverseNo是否逆向排序结果,默认为false。仅在设置了sortFlag时生效。
limitedNumNo返回的中转余票数量限制,默认为10。
formatNo返回结果格式,默认为text,建议使用text。可选标志:[text, json]text

Implementation Reference

  • The core handler function for the 'get-interline-tickets' tool. It validates inputs, fetches data from 12306 interline API using cookies, paginates results, parses raw data into structured InterlineInfo using helpers, applies filters and sorting, and returns formatted text or JSON.
    async ({
        date,
        fromStation,
        toStation,
        middleStation,
        showWZ,
        trainFilterFlags,
        earliestStartTime,
        latestStartTime,
        sortFlag,
        sortReverse,
        limitedNum,
        format,
    }) => {
        // 检查日期是否早于当前日期
        if (!checkDate(date)) {
            return {
                content: [
                    {
                        type: 'text',
                        text: 'Error: The date cannot be earlier than today.',
                    },
                ],
            };
        }
        if (
            !Object.keys(STATIONS).includes(fromStation) ||
            !Object.keys(STATIONS).includes(toStation)
        ) {
            return {
                content: [{ type: 'text', text: 'Error: Station not found. ' }],
            };
        }
        const queryUrl = `${API_BASE}${LCQUERY_PATH}`;
        const cookies = await getCookie();
        if (cookies == null || Object.entries(cookies).length === 0) {
            return {
                content: [
                    {
                        type: 'text',
                        text: 'Error: get cookie failed. Check your network.',
                    },
                ],
            };
        }
    
        var interlineData: InterlineData[] = [];
        const queryParams = new URLSearchParams({
            train_date: date,
            from_station_telecode: fromStation,
            to_station_telecode: toStation,
            middle_station: middleStation,
            result_index: '0',
            can_query: 'Y',
            isShowWZ: showWZ ? 'Y' : 'N',
            purpose_codes: '00', // 00: 成人票 0X: 学生票
            channel: 'E', // 没搞清楚什么用
        });
        while (interlineData.length < limitedNum) {
            const queryResponse =
                await make12306Request<InterlineQueryResponse>(
                    queryUrl,
                    queryParams,
                    { Cookie: formatCookies(cookies) }
                );
            // 处理请求错误
            if (queryResponse === null || queryResponse === undefined) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: 'Error: request interline tickets data failed. ',
                        },
                    ],
                };
            }
            // 请求成功,但查询有误
            if (typeof queryResponse.data == 'string') {
                return {
                    content: [
                        {
                            type: 'text',
                            text: `很抱歉,未查到相关的列车余票。(${queryResponse.errorMsg})`,
                        },
                    ],
                };
            }
            interlineData = interlineData.concat(queryResponse.data.middleList);
            if (queryResponse.data.can_query == 'N') {
                break;
            }
            queryParams.set(
                'result_index',
                queryResponse.data.result_index.toString()
            );
        }
        // 请求和查询都没问题
        let interlineTicketsInfo: InterlineInfo[];
        try {
            interlineTicketsInfo = parseInterlinesInfo(interlineData);
        } catch (error) {
            return {
                content: [
                    {
                        type: 'text',
                        text: `Error: parse tickets info failed. ${error}`,
                    },
                ],
            };
        }
        const filteredInterlineTicketsInfo = filterTicketsInfo<InterlineInfo>(
            interlineTicketsInfo,
            trainFilterFlags,
            earliestStartTime,
            latestStartTime,
            sortFlag,
            sortReverse,
            limitedNum
        );
        var formatedResult;
        switch (format) {
            case 'json':
                formatedResult = JSON.stringify(filteredInterlineTicketsInfo);
                break;
            default:
                formatedResult = formatInterlinesInfo(
                    filteredInterlineTicketsInfo
                );
                break;
        }
        return {
            content: [
                {
                    type: 'text',
                    text: formatedResult,
                },
            ],
        };
  • Zod schema defining the input parameters for the 'get-interline-tickets' tool, including date, stations, filters, sorting options, and output format.
    {
        date: z
            .string()
            .length(10)
            .describe(
                '查询日期,格式为 "yyyy-MM-dd"。如果用户提供的是相对日期(如“明天”),请务必先调用 `get-current-date` 接口获取当前日期,并计算出目标日期。'
            ),
        fromStation: z
            .string()
            .describe(
                '出发地的 `station_code` 。必须是通过 `get-station-code-by-names` 或 `get-station-code-of-citys` 接口查询得到的编码,严禁直接使用中文地名。'
            ),
        toStation: z
            .string()
            .describe(
                '出发地的 `station_code` 。必须是通过 `get-station-code-by-names` 或 `get-station-code-of-citys` 接口查询得到的编码,严禁直接使用中文地名。'
            ),
        middleStation: z
            .string()
            .optional()
            .default('')
            .describe(
                '中转地的 `station_code` ,可选。必须是通过 `get-station-code-by-names` 或 `get-station-code-of-citys` 接口查询得到的编码,严禁直接使用中文地名。'
            ),
        showWZ: z
            .boolean()
            .optional()
            .default(false)
            .describe('是否显示无座车,默认不显示无座车。'),
        trainFilterFlags: z
            .string()
            .regex(/^[GDZTKOFS]*$/)
            .max(8)
            .optional()
            .default('')
            .describe(
                '车次筛选条件,默认为空。从以下标志中选取多个条件组合[G(高铁/城际),D(动车),Z(直达特快),T(特快),K(快速),O(其他),F(复兴号),S(智能动车组)]'
            ),
        earliestStartTime: z
            .number()
            .min(0)
            .max(24)
            .optional()
            .default(0)
            .describe('最早出发时间(0-24),默认为0。'),
        latestStartTime: z
            .number()
            .min(0)
            .max(24)
            .optional()
            .default(24)
            .describe('最迟出发时间(0-24),默认为24。'),
        sortFlag: z
            .string()
            .optional()
            .default('')
            .describe(
                '排序方式,默认为空,即不排序。仅支持单一标识。可选标志:[startTime(出发时间从早到晚), arriveTime(抵达时间从早到晚), duration(历时从短到长)]'
            ),
        sortReverse: z
            .boolean()
            .optional()
            .default(false)
            .describe(
                '是否逆向排序结果,默认为false。仅在设置了sortFlag时生效。'
            ),
        limitedNum: z
            .number()
            .min(1)
            .optional()
            .default(10)
            .describe('返回的中转余票数量限制,默认为10。'),
        format: z
            .string()
            .regex(/^(text|json)$/i)
            .default('text')
            .optional()
            .describe(
                '返回结果格式,默认为text,建议使用text。可选标志:[text, json]'
            ),
    },
  • src/index.ts:1146-1369 (registration)
    Registration of the 'get-interline-tickets' tool on the MCP server, including name, description, input schema, and handler function.
    server.tool(
        'get-interline-tickets',
        '查询12306中转余票信息。尚且只支持查询前十条。',
        {
            date: z
                .string()
                .length(10)
                .describe(
                    '查询日期,格式为 "yyyy-MM-dd"。如果用户提供的是相对日期(如“明天”),请务必先调用 `get-current-date` 接口获取当前日期,并计算出目标日期。'
                ),
            fromStation: z
                .string()
                .describe(
                    '出发地的 `station_code` 。必须是通过 `get-station-code-by-names` 或 `get-station-code-of-citys` 接口查询得到的编码,严禁直接使用中文地名。'
                ),
            toStation: z
                .string()
                .describe(
                    '出发地的 `station_code` 。必须是通过 `get-station-code-by-names` 或 `get-station-code-of-citys` 接口查询得到的编码,严禁直接使用中文地名。'
                ),
            middleStation: z
                .string()
                .optional()
                .default('')
                .describe(
                    '中转地的 `station_code` ,可选。必须是通过 `get-station-code-by-names` 或 `get-station-code-of-citys` 接口查询得到的编码,严禁直接使用中文地名。'
                ),
            showWZ: z
                .boolean()
                .optional()
                .default(false)
                .describe('是否显示无座车,默认不显示无座车。'),
            trainFilterFlags: z
                .string()
                .regex(/^[GDZTKOFS]*$/)
                .max(8)
                .optional()
                .default('')
                .describe(
                    '车次筛选条件,默认为空。从以下标志中选取多个条件组合[G(高铁/城际),D(动车),Z(直达特快),T(特快),K(快速),O(其他),F(复兴号),S(智能动车组)]'
                ),
            earliestStartTime: z
                .number()
                .min(0)
                .max(24)
                .optional()
                .default(0)
                .describe('最早出发时间(0-24),默认为0。'),
            latestStartTime: z
                .number()
                .min(0)
                .max(24)
                .optional()
                .default(24)
                .describe('最迟出发时间(0-24),默认为24。'),
            sortFlag: z
                .string()
                .optional()
                .default('')
                .describe(
                    '排序方式,默认为空,即不排序。仅支持单一标识。可选标志:[startTime(出发时间从早到晚), arriveTime(抵达时间从早到晚), duration(历时从短到长)]'
                ),
            sortReverse: z
                .boolean()
                .optional()
                .default(false)
                .describe(
                    '是否逆向排序结果,默认为false。仅在设置了sortFlag时生效。'
                ),
            limitedNum: z
                .number()
                .min(1)
                .optional()
                .default(10)
                .describe('返回的中转余票数量限制,默认为10。'),
            format: z
                .string()
                .regex(/^(text|json)$/i)
                .default('text')
                .optional()
                .describe(
                    '返回结果格式,默认为text,建议使用text。可选标志:[text, json]'
                ),
        },
        async ({
            date,
            fromStation,
            toStation,
            middleStation,
            showWZ,
            trainFilterFlags,
            earliestStartTime,
            latestStartTime,
            sortFlag,
            sortReverse,
            limitedNum,
            format,
        }) => {
            // 检查日期是否早于当前日期
            if (!checkDate(date)) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: 'Error: The date cannot be earlier than today.',
                        },
                    ],
                };
            }
            if (
                !Object.keys(STATIONS).includes(fromStation) ||
                !Object.keys(STATIONS).includes(toStation)
            ) {
                return {
                    content: [{ type: 'text', text: 'Error: Station not found. ' }],
                };
            }
            const queryUrl = `${API_BASE}${LCQUERY_PATH}`;
            const cookies = await getCookie();
            if (cookies == null || Object.entries(cookies).length === 0) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: 'Error: get cookie failed. Check your network.',
                        },
                    ],
                };
            }
    
            var interlineData: InterlineData[] = [];
            const queryParams = new URLSearchParams({
                train_date: date,
                from_station_telecode: fromStation,
                to_station_telecode: toStation,
                middle_station: middleStation,
                result_index: '0',
                can_query: 'Y',
                isShowWZ: showWZ ? 'Y' : 'N',
                purpose_codes: '00', // 00: 成人票 0X: 学生票
                channel: 'E', // 没搞清楚什么用
            });
            while (interlineData.length < limitedNum) {
                const queryResponse =
                    await make12306Request<InterlineQueryResponse>(
                        queryUrl,
                        queryParams,
                        { Cookie: formatCookies(cookies) }
                    );
                // 处理请求错误
                if (queryResponse === null || queryResponse === undefined) {
                    return {
                        content: [
                            {
                                type: 'text',
                                text: 'Error: request interline tickets data failed. ',
                            },
                        ],
                    };
                }
                // 请求成功,但查询有误
                if (typeof queryResponse.data == 'string') {
                    return {
                        content: [
                            {
                                type: 'text',
                                text: `很抱歉,未查到相关的列车余票。(${queryResponse.errorMsg})`,
                            },
                        ],
                    };
                }
                interlineData = interlineData.concat(queryResponse.data.middleList);
                if (queryResponse.data.can_query == 'N') {
                    break;
                }
                queryParams.set(
                    'result_index',
                    queryResponse.data.result_index.toString()
                );
            }
            // 请求和查询都没问题
            let interlineTicketsInfo: InterlineInfo[];
            try {
                interlineTicketsInfo = parseInterlinesInfo(interlineData);
            } catch (error) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: `Error: parse tickets info failed. ${error}`,
                        },
                    ],
                };
            }
            const filteredInterlineTicketsInfo = filterTicketsInfo<InterlineInfo>(
                interlineTicketsInfo,
                trainFilterFlags,
                earliestStartTime,
                latestStartTime,
                sortFlag,
                sortReverse,
                limitedNum
            );
            var formatedResult;
            switch (format) {
                case 'json':
                    formatedResult = JSON.stringify(filteredInterlineTicketsInfo);
                    break;
                default:
                    formatedResult = formatInterlinesInfo(
                        filteredInterlineTicketsInfo
                    );
                    break;
            }
            return {
                content: [
                    {
                        type: 'text',
                        text: formatedResult,
                    },
                ],
            };
        }
    );
  • Generic helper function to filter, sort, and limit ticket or interline ticket results based on train flags, start times, sort criteria.
    function filterTicketsInfo<T extends TicketInfo | InterlineInfo>(
        ticketsInfo: T[],
        trainFilterFlags: string,
        earliestStartTime: number = 0,
        latestStartTime: number = 24,
        sortFlag: string = '',
        sortReverse: boolean = false,
        limitedNum: number = 0
    ): T[] {
        let result: T[];
        // FilterFlags过滤
        if (trainFilterFlags.length === 0) {
            result = ticketsInfo;
        } else {
            result = [];
            for (const ticketInfo of ticketsInfo) {
                for (const filter of trainFilterFlags) {
                    if (
                        TRAIN_FILTERS[filter as keyof typeof TRAIN_FILTERS](
                            ticketInfo
                        )
                    ) {
                        result.push(ticketInfo);
                        break;
                    }
                }
            }
        }
        // startTime 过滤
        result = result.filter((ticketInfo) => {
            const startTimeHour = parseInt(ticketInfo.start_time.split(':')[0], 10);
            if (
                startTimeHour >= earliestStartTime &&
                startTimeHour < latestStartTime
            ) {
                return true;
            }
            return false;
        });
    
        // sort排序
        if (Object.keys(TIME_COMPARETOR).includes(sortFlag)) {
            result.sort(TIME_COMPARETOR[sortFlag as keyof typeof TIME_COMPARETOR]);
            if (sortReverse) {
                result.reverse();
            }
        }
        if (limitedNum == 0) {
            return result;
        }
        return result.slice(0, limitedNum);
    }
  • Helper to parse raw API interline data into structured InterlineInfo objects, calling sub-parsers for tickets.
    function parseInterlinesInfo(interlineData: InterlineData[]): InterlineInfo[] {
        const result: InterlineInfo[] = [];
        for (const ticket of interlineData) {
            const interlineTickets = parseInterlinesTicketInfo(ticket.fullList);
            const lishi = extractLishi(ticket.all_lishi);
            result.push({
                lishi: lishi,
                start_time: ticket.start_time,
                start_date: ticket.train_date,
                middle_date: ticket.middle_date,
                arrive_date: ticket.arrive_date,
                arrive_time: ticket.arrive_time,
                from_station_code: ticket.from_station_code,
                from_station_name: ticket.from_station_name,
                middle_station_code: ticket.middle_station_code,
                middle_station_name: ticket.middle_station_name,
                end_station_code: ticket.end_station_code,
                end_station_name: ticket.end_station_name,
                start_train_code: interlineTickets[0].start_train_code,
                first_train_no: ticket.first_train_no,
                second_train_no: ticket.second_train_no,
                train_count: ticket.train_count,
                ticketList: interlineTickets,
                same_station: ticket.same_station == '0' ? true : false,
                same_train: ticket.same_train == 'Y' ? true : false,
                wait_time: ticket.wait_time,
            });
        }
        return result;
  • Helper to format the parsed and filtered interline tickets into a human-readable text string.
    function formatInterlinesInfo(interlinesInfo: InterlineInfo[]): string {
        let result =
            '出发时间 -> 到达时间 | 出发车站 -> 中转车站 -> 到达车站 | 换乘标志 |换乘等待时间| 总历时\n\n';
        interlinesInfo.forEach((interlineInfo) => {
            result += `${interlineInfo.start_date} ${interlineInfo.start_time} -> ${interlineInfo.arrive_date} ${interlineInfo.arrive_time} | `;
            result += `${interlineInfo.from_station_name} -> ${interlineInfo.middle_station_name} -> ${interlineInfo.end_station_name} | `;
            result += `${
                interlineInfo.same_train
                    ? '同车换乘'
                    : interlineInfo.same_station
                    ? '同站换乘'
                    : '换站换乘'
            } | ${interlineInfo.wait_time} | ${interlineInfo.lishi}\n\n`;
            result +=
                '\t' +
                formatTicketsInfo(interlineInfo.ticketList).replace(/\n/g, '\n\t');
            result += '\n';
        });
        return result;
    }
  • TypeScript type definitions for data structures used in interline tickets processing: InterlineData, InterlineInfo, InterlineTicketData.
    export type InterlineData = {
        all_lishi: string;
        all_lishi_minutes: number;
        arrive_date: string;
        arrive_time: string;
        end_station_code: string;
        end_station_name: string;
        first_train_no: string;
        from_station_code: string;
        from_station_name: string;
        fullList: InterlineTicketData[];
        isHeatTrain: string;
        isOutStation: string;
        lCWaitTime: string;
        lishi_flag: string;
        middle_date: string;
        middle_station_code: string;
        middle_station_name: string;
        same_station: string;
        same_train: string;
        score: number;
        score_str: string;
        scretstr: string;
        second_train_no: string;
        start_time: string;
        train_count: number;
        train_date: string; // 出发时间
        use_time: string;
        wait_time: string;
        wait_time_minutes: number;
    };
    
    export type InterlineInfo = {
        lishi: string;
        //all_lishi_minutes: number;
        start_time: string;
        start_date: string;
        middle_date: string;
        arrive_date: string;
        arrive_time: string;
        from_station_code: string;
        from_station_name: string;
        middle_station_code: string;
        middle_station_name: string;
        end_station_code: string;
        end_station_name: string;
        start_train_code: string; // 用于过滤
        first_train_no: string;
        second_train_no: string;
        train_count: number;
        ticketList: TicketInfo[];
        //isHeatTrain: string;
        //isOutStation: string;
        //lCWaitTime: string;
        //lishi_flag: string;
        same_station: boolean;
        same_train: boolean;
        wait_time: string;
        //wait_time_minutes: number;
    };
    
    export type InterlineTicketData = {
        arrive_time: string;
        bed_level_info: string;
        controlled_train_flag: string;
        country_flag: string;
        day_difference: string;
        dw_flag: string;
        end_station_name: string;
        end_station_telecode: string;
        from_station_name: string;
        from_station_no: string;
        from_station_telecode: string;
        gg_num: string;
        gr_num: string;
        is_support_card: string;
        lishi: string;
        local_arrive_time: string;
        local_start_time: string;
        qt_num: string;
        rw_num: string;
        rz_num: string;
        seat_discount_info: string;
        seat_types: string;
        srrb_num: string;
        start_station_name: string;
        start_station_telecode: string;
        start_time: string;
        start_train_date: string;
        station_train_code: string;
        swz_num: string;
        to_station_name: string;
        to_station_no: string;
        to_station_telecode: string;
        train_no: string;
        train_seat_feature: string;
        trms_train_flag: string;
        tz_num: string;
        wz_num: string;
        yb_num: string;
        yp_info: string;
        yw_num: string;
        yz_num: string;
        ze_num: string;
        zy_num: string;
    };
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the tool queries interline ticket information and reveals a key behavioral constraint: '尚且只支持查询前十条' (currently only supports querying the first ten results). However, it doesn't disclose other important behavioral aspects like whether this is a read-only operation, error handling, rate limits, or what the return format looks like (though format is a parameter).

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is extremely concise at just two short sentences in Chinese. The first sentence states the core purpose, and the second adds an important behavioral constraint. There's no wasted text, though some might argue it's too brief given the tool's complexity. The structure is front-loaded with the main purpose.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a complex tool with 12 parameters, no annotations, and no output schema, the description is minimal. It states what the tool does and one behavioral constraint, but doesn't provide context about the return format, error conditions, or how results are structured. The schema handles parameter documentation well, but the description doesn't compensate for the lack of output schema and annotations.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 100%, so the schema already documents all 12 parameters thoroughly. The tool description adds no parameter-specific information beyond what's in the schema. The baseline score of 3 is appropriate when the schema does all the parameter documentation work.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: '查询12306中转余票信息' (query 12306 interline ticket availability). It specifies the verb (query) and resource (interline tickets from 12306), but doesn't explicitly differentiate from sibling tools like 'get-tickets' which might query direct tickets. The limitation '尚且只支持查询前十条' (currently only supports querying the first ten) adds specificity.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context through the limitation statement about only returning the first ten results, but doesn't provide explicit guidance on when to use this tool versus alternatives like 'get-tickets'. The input schema descriptions reference sibling tools for parameter preparation, but the tool description itself lacks explicit when/when-not guidance.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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