The MCP Server 12306 provides real-time Chinese railway ticket query capabilities by integrating with the official 12306 system, offering tools for ticket availability, pricing, station information, transfer routes, and train schedules. It delivers these services through FastAPI with support for both stdio and HTTP transport protocols, enabling seamless integration with AI assistants and automation workflows. The server solves the problem of accessing comprehensive train travel information programmatically while maintaining high performance and MCP standard compliance.
MCP Server 12306是一款基于 Model Context Protocol (MCP) 的高性能火车票查询后端,支持官方 12306 余票、票价、车站、经停、换乘查询以及智能时间工具,适配 AI/自动化/智能助手等场景。界面友好,易于集成,开箱即用。
本项目支持两种运行模式:
在此模式下,MCP Server 通过标准输入/输出与客户端通信,无需占用网络端口。
uvx 是 uv 包管理器提供的工具,环境隔离且启动极快。
{
"mcpServers": {
"12306": {
"command": "uvx",
"args": ["mcp-server-12306"]
}
}
}
如果您更习惯使用 pipx:
{
"mcpServers": {
"12306": {
"command": "pipx",
"args": ["run", "--no-cache", "mcp-server-12306"]
}
}
}
适用于开发者调试:
{
"mcpServers": {
"12306": {
"command": "uv",
"args": ["run", "python", "-m", "mcp_12306.cli"],
"cwd": "/path/to/mcp-server-12306"
}
}
}
在此模式下,Server 启动一个 Web 服务(默认 8000 端口),支持 MCP 的 SSE(Server-Sent Events)和 POST 交互。
# 1. 克隆并安装依赖
git clone https://github.com/drfccv/mcp-server-12306.git
cd mcp-server-12306
uv sync
# 2. 启动服务器
uv run python scripts/start_server.py
MCP 客户端配置:
{
"mcpServers": {
"12306": {
"url": "http://localhost:8000/mcp"
}
}
}
# 拉取镜像并运行
docker run -d -p 8000:8000 --name mcp-server-12306 drfccv/mcp-server-12306:latest
| 工具名 | 典型场景/功能描述 |
|---|---|
| query_tickets | 余票/车次/座席/时刻一站式查询 |
| query_ticket_price | 实时查询各车次票价信息 |
| search_stations | 车站模糊搜索,支持中文/拼音/简拼 |
| get_station_info | 获取车站详情(名称、代码、地理等) |
| query_transfer | 一次中转换乘方案,自动拼接最优中转 |
| get_train_route_stations | 查询指定列车经停站及时刻表 |
| get_current_time | 获取当前时间与相对日期,帮助用户准确选择出行日期 |
本项目所有主流程工具的详细功能、实现与使用方法,均已收录于 /docs 目录下:
每个文档包含:
如需二次开发或集成,建议先阅读对应工具的文档。
src/mcp_12306/ # 主源代码
├─ server.py # FastAPI主入口
├─ services/ # 业务逻辑(车票/车站/HTTP)
├─ utils/ # 工具与配置
scripts/ # 启动与数据脚本
MIT License