12306-MCP-Python
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@12306-MCP-Python查一下明天北京南到上海虹桥的高铁余票"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
12306-MCP-Python
一个可部署到 ModelScope 魔搭社区自定义 MCP 服务、并供 Dify 智能体调用的 Python MCP Server。项目参考 Joooook/12306-mcp 的公开架构和工具语义,以 Python + 官方 MCP Python SDK/FastMCP 独立实现。
仅用于查询公开铁路信息;不登录、不下单、不支付、不绕过验证码、不抢票。12306 网页公开接口可能调整,因此线上使用时应做好接口变化和限流失败的容错。
功能
MCP Tool | 作用 |
| 获取 Asia/Shanghai 当前日期 |
| 按站名/拼音搜索车站和电报码 |
| 精确站名转电报码,支持 ` |
| 电报码反查车站 |
| 查询直达余票、时间、席位状态并支持车型过滤 |
| 指定中转站组合一次换乘方案 |
| 查询某个 |
Related MCP server: 12306-MCP
项目结构
.
├── src/mcp_12306/
│ ├── server.py # FastMCP 工具注册与服务入口
│ ├── client.py # 12306 HTTP 查询与余票解析
│ ├── stations.py # 车站数据下载、缓存、索引、搜索
│ ├── transfer.py # 中转换乘组合
│ ├── models.py # Pydantic 数据模型
│ └── config.py # 环境变量
├── tests/ # 解析单元测试
├── docs/
│ ├── MODELSCOPE.md # 魔搭部署
│ └── DIFY.md # Dify 接入
├── Dockerfile
├── docker-compose.yml
├── pyproject.toml
└── .env.example本地运行
Python 3.10+:
python -m venv .venv
# Linux/macOS
source .venv/bin/activate
# Windows PowerShell: .venv\Scripts\Activate.ps1
pip install -e '.[dev]'
12306-mcp-python --transport streamable-http --host 0.0.0.0 --port 8000MCP URL:http://127.0.0.1:8000/mcp
stdio:
12306-mcp-python --transport stdioDocker
docker build -t 12306-mcp-python .
docker run --rm -p 8000:8000 12306-mcp-python或:
docker compose up --buildMCP Inspector 测试
npx -y @modelcontextprotocol/inspector在 Inspector 中连接:http://127.0.0.1:8000/mcp。
推荐测试顺序:
get-current-date
search-stations(keyword="北京南")
get-station-code-by-names(station_names="北京南|上海虹桥")
get-tickets(date="未来有效日期", from_station="VNP", to_station="AOH", train_filter_flags="G")工具参数说明
get-tickets
date:YYYY-MM-DD,不能早于上海时区当天。from_station/to_station: 12306 电报码,不是中文站名。train_filter_flags:G、D、ZTK、GD等首字母组合。only_available: 过滤明显无可售席位的车次。limit: 1-100。
返回 JSON 中 train_no 是 12306 内部车次编号,查询经停站时应把它传给 get-train-route-stations。
get-interline-tickets
为提高长期可维护性,本实现没有强依赖 12306 可能变化的 /lcquery/queryU,而是分别查询“出发→中转”和“中转→目的地”的直达车,再按换乘时间组合。当前仅组合同一天第二程,跨零点方案应对次日第二程再查一次。
ModelScope 部署
详见 docs/MODELSCOPE.md。核心配置:
Transport: Streamable HTTP
Host: 0.0.0.0
Port: 8000
Path: /mcp启动命令:
12306-mcp-python --transport streamable-http --host 0.0.0.0 --port 8000Dify 接入
详见 docs/DIFY.md。ModelScope 部署成功后,使用平台提供的 Hosted MCP Remote URL 给 Dify 的 MCP Client;不要把 localhost 地址填写到云端 Dify。
环境变量
复制 .env.example 为 .env 可修改:
变量 | 默认 | 说明 |
|
| 传输方式 |
|
| HTTP 监听地址 |
|
| HTTP 端口 |
|
| MCP 路径 |
|
| 请求超时(秒) |
|
| 车站索引缓存秒数 |
|
| 余票默认最大结果数 |
已知限制
12306 Web API 属于网页公开接口,可能改变路径、返回字段或访问策略。
本项目没有购票能力,也不应向 Agent 描述为“12306 官方购票接口”。
车站 JS 下载失败时会退化到少量常用站点的 bootstrap 数据;联网恢复后会重新加载完整车站数据。
不建议高频轮询。若用于生产,应在上层增加缓存、并发限制和观测告警,并遵守目标站点服务规则。
余票字段位置源于 12306 当前常见返回格式;如官方格式改变,应更新
client.py中的解析映射。
测试
pytest -q
ruff check src testsLicense / Attribution
MIT。架构与工具语义参考开源项目 Joooook/12306-mcp(MIT),详见 NOTICE。本项目与中国铁路12306、ModelScope、Dify 以及原项目作者均无隶属或背书关系。
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceEnables searching for train tickets on China's 12306 railway system, including ticket availability queries, train filtering, station stopover information, and transfer route planning.8,3132MIT
- AlicenseBqualityDmaintenance12306 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.81Apache 2.0
- FlicenseNot gradedqualityDmaintenanceEnables real-time flight and train ticket queries, dynamic trip tracking, and knowledge-based Q&A about rail and aviation policies.
- FlicenseNot gradedqualityCmaintenanceEnables travel-related queries and planning including weather, train and flight information, itinerary generation, and travel knowledge retrieval via RAG, using an AI agent with MCP protocol.
Related MCP Connectors
TravelMind: 8 MCP tools for travel (12306 trains, flights, hotels, geocode, planning, policy).
AI marketplace — flights, tours, activities, transport & more via MCP. No auth required.
Aggregated travel MCP — flights, tours, activities, price checks, visas, and more.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
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/DomainTang/Local-12036-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server