Skip to main content
Glama

12306-MCP 车票查询工具

魔搭部署指南 | 实现原理 | 架构说明

12306-MCP 是基于 Model Context Protocol(MCP)的 12306 车票信息查询服务。它把 12306 的余票、车次、车站与中转查询封装成标准 MCP 工具,供支持 MCP 的 AI 客户端(Cursor、Cherry Studio、通义灵码、Claude 等)通过 Stdio 或 SSE / Streamable HTTP 远程调用。

功能

  • 查询指定日期两地之间的 12306 车票余票与票价

  • 查询车次的具体经停站、到发时刻与历时

  • 城市 / 站名 / 站码(telecode)互查

  • 中转方案查询

  • 输出格式支持 text / csv / json

内置工具:

工具

说明

get-current-date

获取上海时区(Asia/Shanghai)当前日期

get-stations-code-in-city

查询某城市下的全部车站

get-station-code-of-citys

批量查询城市对应站码

get-station-code-by-names

站名查询站码与车站信息

get-station-by-telecode

站码反查车站详情

get-tickets

查询车票余票、票价并可过滤/排序

get-interline-tickets

查询中转车票方案

get-train-route-stations

查询车次经停站信息

Related MCP server: 12306-MCP

本地使用(Stdio)

环境要求:Node.js 18 及以上。

npm ci
npm start

在 MCP 客户端配置:

{
  "mcpServers": {
    "12306-mcp": {
      "command": "node",
      "args": ["/绝对路径/build/index.js"]
    }
  }
}

也可以直接运行已发布的 npm 包:

{
  "mcpServers": {
    "12306-mcp": {
      "command": "npx",
      "args": ["-y", "12306-mcp"]
    }
  }
}

远程使用(SSE / Streamable HTTP)

本地启动 HTTP 服务:

npm run start:http

默认监听 0.0.0.0:8080(可通过 --host、--port 或环境变量 HOST、PORT 修改):

  • Streamable HTTP:POST /mcp

  • SSE:GET /sse,消息回传 POST /message

部署到魔搭(ModelScope)MCP 广场时,推荐选择 Stdio/npx 托管方式,平台会自动生成独立的 SSE 地址,无需自己维护 HTTP 服务。创建页面各项填法、可直接复制的“服务介绍”、以及三种部署方式的配置 JSON,见 魔搭部署指南。

Docker

docker build -t 12306-mcp .

# HTTP/SSE 模式
docker run -d -p 8080:8080 12306-mcp

# Stdio 模式
docker run --rm -it 12306-mcp node build/index.js

注意事项

  • 本服务无需注册 12306 账号,也无需 API Key。

  • 服务进程启动时需要访问 kyfw.12306.cn 等 12306 站点拉取车站表与查询路径;请确保运行环境(尤其云实例)能访问 12306。

  • 12306 接口存在限流与反爬策略,仅供学习与研究使用,请勿高频滥用。

参考

License

MIT

Available Tools

8 tools
get-current-dateA

获取当前日期,以上海时区(Asia/Shanghai, UTC+8)为准,返回格式为 "yyyy-MM-dd"。主要用于解析用户提到的相对日期(如“明天”、“下周三”),提供准确的日期输入。

Inputs: no named parameters.

get-interline-ticketsB

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

Inputs: date (required), toStation (required), fromStation (required), format, +8 more.

get-station-by-telecodeA

通过车站的 station_telecode 查询车站的详细信息,包括名称、拼音、所属城市等。此接口主要用于在已知 telecode 的情况下获取更完整的车站数据,或用于特殊查询及调试目的。一般用户对话流程中较少直接触发。

Inputs: stationTelecode (required).

get-station-code-by-namesA

通过具体的中文车站名查询其 station_code 和车站名。

Inputs: stationNames (required).

get-station-code-of-citysB

通过中文城市名查询代表该城市的 station_code。

Inputs: citys (required).

get-stations-code-in-cityB

通过中文城市名查询该城市 所有 火车站的名称及其对应的 station_code,结果是一个包含多个车站信息的列表。

Inputs: city (required).

get-ticketsB

查询12306余票信息。

Inputs: date (required), toStation (required), fromStation (required), format, +6 more.

get-train-route-stationsA

查询特定列车车次在指定区间内的途径车站、到站时间、出发时间及停留时间等详细经停信息。当用户询问某趟具体列车的经停站时使用此接口。

Inputs: trainCode (required), departDate (required), format.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    B3.4/5.0

    Scored across 8 tools

    Disambiguation2/5

    The station lookup tools heavily overlap: get-stations-code-in-city and get-station-code-of-citys both map a city to station codes, differing only by 'all stations' vs 'representative station'. get-station-code-by-names and get-station-by-telecode add further lookup variants, making it easy for an agent to select the wrong one.

    Naming Consistency3/5

    All tools use a get- prefix, which provides some consistency, but the station-related names are not parallel: get-stations-code-in-city, get-station-code-of-citys, get-station-code-by-names, and get-station-by-telecode follow inconsistent patterns. The 'citys' typo further reduces naming quality.

    Tool Count4/5

    Eight tools is a reasonable size for a train travel assistant. However, four of those tools are dedicated to station-code lookup, which inflates the count and makes the surface feel slightly heavier than necessary.

    Completeness4/5

    Core workflows are covered: getting the current date, resolving station codes, querying direct tickets, querying transfer tickets, and retrieving train route stops. Minor gaps exist such as no explicit ticket price or train status tool, but agents can likely complete common travel queries.

    Maintenance

    ActivityStale
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      B
      quality
      D
      maintenance
      12306 ticket purchase search server based on Model Context Protocol (MCP). It provides a simple API interface, allowing large models to search for 12306 ticket purchasing information through the interface.
      8
      1
      Apache 2.0
    • F
      license
      Not graded
      quality
      D
      maintenance
      Enables querying train and high-speed rail schedules, durations, and seat availability between Chinese stations using 12306 data, with automatic fallback to simulated data when the API is rate-limited.
      -
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables AI agents to query public China Railway 12306 information via MCP, including station lookup, ticket availability, transfer planning, and train route details. It does not support login, booking, or payment.
      MIT