一个全面的模型上下文协议(MCP)服务器,提供统一访问德国铁路(DB)和德国移动性API。使用Python、FastAPI和FastMCP构建,以实现与Claude Desktop和其他MCP客户端的无缝集成。
该服务器已准备好进行本地和云部署。要连接运行中的MCP服务器,请在您的MCP客户端中添加以下URL:https://db-mcp.datamonkey.tech/mcp。
克隆并设置:
git clone <repository-url>
cd deutsche-bahn-mcp-server
python3 -m venv .venv
source .venv/bin/activate # 在Windows上:.venv\Scripts\activate
pip install -r requirements.txt
配置环境:
cp .env.example .env
接下来,编辑新的.env文件,填入您的DB API凭证。有关每个变量的详情,请参阅“配置”部分。
运行服务器:
python main.py
# 服务器可用地址:http://localhost:8080
德国铁路MCP服务器已在https://db-mcp.datamonkey.tech上线,随时可以使用。请按照以下逐步指南开始在Claude中访问德国铁路数据。
步骤1:找到您的配置文件
~/Library/Application Support/Claude/claude_desktop_config.json~/.config/Claude/claude_desktop_config.json步骤2:编辑配置 打开文件并添加德国铁路服务器:
{
"mcpServers": {
"deutschebahn": {
"command": "npx",
"args": [
"-y", "mcp-remote",
"https://db-mcp.datamonkey.tech/mcp",
"--transport", "http-only"
]
}
}
}
步骤3:重启Claude Desktop
步骤4:测试连接 询问Claude:“柏林有哪些火车站?”
步骤1:开始对话 前往claude.ai并开始新对话。
步骤2:添加MCP服务器
点击“搜索和工具 > 添加连接器 > 管理连接器”
选择“添加自定义连接器”
输入名称,例如:“德国铁路MCP服务器”
输入远程MCP服务器URL:https://db-mcp.datamonkey.tech/mcp
步骤3:立即测试 尝试询问:“查找法兰克福附近的火车站,并检查法兰克福中央车站是否有当前延误。”
一旦连接成功,尝试这些实用示例:
🔍 车站搜索
🚄 实时信息
♿ 无障碍规划
🅿️ 停车信息
🗺️ 旅行规划
Claude Desktop问题:
问题:MCP部分未显示服务器
问题:连接超时
curl https://db-mcp.datamonkey.tech/mcp/ping 终端问题:“命令未找到”错误
npm install -g mcp-remoteClaude.ai Web问题:
问题:“无法连接到MCP服务器”
一般问题:
服务器状态检查:
# 测试服务器是否响应
curl https://db-mcp.datamonkey.tech/health
# 测试MCP端点
curl https://db-mcp.datamonkey.tech/mcp/ping
速率限制:
get_station_by_name(name, limit=10) - 通过名称搜索车站,带有回退过滤get_stations_by_position(lat, lon, radius=2.0) - 使用智能区域过滤查找附近车站get_planned_timetable(eva_number, date, hour) - 计划的出发/到达时间get_recent_timetable_changes(eva_number) - 实时更新(最后2分钟)get_full_timetable_changes(eva_number) - 所有已知未来中断get_parking_by_station(stop_place_id) - 车站附近的停车设施search_parking_facilities(station_name) - 通过车站名称搜索停车设施,使用真实APIget_parking_prognoses(facility_id, datetime) - 停车位可用预测find_facilities(station_number, type, state) - 电梯、扶梯状态get_facilities_by_station(station_number) - 完整设施概览get_szentralen_by_location(lat, lon) - 使用区域过滤的移动服务中心search_szentralen(limit, offset) - 服务中心搜索,带分页accessibility_check - 车站间无障碍旅行验证parking_prognosis - 车站停车设施可用预测recent_timetable_changes - 实时延误和服务中断planned_timetable_window - 特定时间段的列车时刻表station_services - 完整车站信息及设施nearby_stations - 查找地理坐标附近的铁路车站current_disruptions - 综合服务中断概述file://reference/station-categories - 德国铁路车站分类指南file://reference/train-types - 完整的德国列车类型指南(ICE、IC、RE等)file://stations/major-hubs - 德国最重要的铁路车站列表file://services/accessibility-guide - 综合无障碍服务指南file://status/current-disruptions - 系统范围内的中断概述├── main.py # FastAPI应用程序入口点
├── server_instance.py # 共享MCP服务器实例
├── mcp_server.py # 服务器设置和模块注册
├── utils.py # 共享实用程序和验证
├── config.py # 配置和密钥管理
├── models.py # Pydantic数据模型
├── rate_limiter.py # 安全:速率限制
├── tools/ # MCP工具
│ ├── station_tools.py # 车站搜索和查找
│ ├── timetable_tools.py # 时刻表和中断
│ ├── parking_tools.py # 停车设施
│ └── facility_tools.py # 无障碍服务
├── resources/ # MCP资源
│ └── travel_resources.py # 旅行指南和参考资料
└── prompts/ # MCP提示
└── travel_prompts.py # 互动旅行协助
服务器实现了智能区域过滤以最大化API效率和结果相关性:
get_stations_by_position()从相关联邦州获取车站,而不是随机1000个get_station_by_name()使用API搜索字符串和客户端侧回退部署您自己的德国铁路MCP服务器实例,按以下步骤操作:
设置密钥:
# 创建您的DB API凭证密钥
echo "your_db_api_key" | gcloud secrets create DB_API_KEY --data-file=-
echo "your_db_api_secret" | gcloud secrets create DB_API_SECRET --data-file=-
部署服务:
# 克隆仓库
git clone https://github.com/your-repo/deutsche-bahn-mcp-server
cd deutsche-bahn-mcp-server
# 部署到Cloud Run
gcloud run deploy deutschebahn-mcp-server \\
--source . \\
--region europe-west3 \\
--allow-unauthenticated \\
--port 8080
授予密钥访问权限:
# 获取部署输出中的服务帐户电子邮件
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \\
--member="serviceAccount:your-service-account@your-project.iam.gserviceaccount.com" \\
--role="roles/secretmanager.secretAccessor"
使用您的MCP服务器:
您的服务器将在提供的Cloud Run URL上可用,例如:
https://deutscheb.ahn-mcp-server-123456789.region.run.app/mcp
对于带有自定义域名和增强安全性的生产使用:
遵循上述基本部署步骤1-3
设置自定义域名和负载均衡器(参见Google Cloud负载均衡器文档)
使用自定义域名重新部署:
gcloud run deploy deutschebahn-mcp-server \\
--source . \\
--region europe-west3 \\
--allow-unauthenticated \\
--port 8080 \\
--set-env-vars CUSTOM_DOMAIN=your-domain.com
# 构建镜像
docker build -t deutschebahn-mcp-server .
# 使用您的API凭证本地运行
docker run -p 8080:8080 \\
-e DB_API_KEY=your_db_api_key \\
-e DB_API_SECRET=your_db_api_secret \\
deutschebahn-mcp-server
# 服务器可用地址:http://localhost:8080/mcp
Secret Manager访问问题:
# 验证密钥存在
gcloud secrets list
# 检查服务帐户权限
gcloud projects get-iam-policy YOUR_PROJECT_ID \\
--flatten="bindings[].members" \\
--filter="bindings.members:*gserviceaccount.com"
连接问题:
# 测试已部署的服务
curl https://your-service-url/health
# 查看日志
gcloud run logs read --service=deutschebahn-mcp-server --region=your-region
# 询问Claude:“查找靠近慕尼黑市中心的火车站”
# 使用:get_stations_by_position(48.1351, 11.5820)
# → 智能区域过滤返回相关的巴伐利亚车站
# 询问Claude:“查找法兰克福的火车站”
# 使用:get_station_by_name("Frankfurt")
# → API搜索字符串参数,带有智能回退
# 询问Claude:“查找柏林附近的移动服务中心”
# 使用:get_szentralen_by_location(52.5200, 13.4050)
# → 返回柏林/勃兰登堡S-Zentralen,使用城市过滤
# 询问Claude:“柏林中央车站现在有延误吗?”
# 使用:recent_timetable_changes提示
# 询问Claude:“查找法兰克福中央车站的停车位”
# 使用:search_parking_facilities("Frankfurt Hauptbahnhof")
# → 使用真实API的stationName参数获得准确结果
# 安装开发依赖
pip install -r requirements-dev.txt
# 运行测试
pytest tests/
# 带覆盖率运行
pytest --cov=. tests/
# 格式化代码
black .
# 代码检查
flake8 .
# 类型检查
mypy .
# 安装MCP Inspector
npm install -g @modelcontextprotocol/inspector
# 检查本地服务器
mcp-inspector http://localhost:8080/mcp
我们欢迎社区贡献!请参阅我们的CONTRIBUTING.md指南以获取详细信息。
git checkout -b feature-name如果您在自己的项目中使用了这项工作、代码或概念,请提供适当的归属:
基于Paul von Berg的德国铁路MCP服务器
https://github.com/PaulvonBerg/db-mcp-server
感谢您!
服务器通过环境变量进行配置。对于本地开发,在根目录创建一个.env文件,复制.env.example模板。对于云部署,这些值通过Google Secret Manager或直接在服务上设置。
查看.env.example文件中的注释,了解哪些凭证应放置在哪里,以避免混淆。
| 变量 | 描述 | 来源 / 备注 |
|---|---|---|
DB_API_KEY | 您的德国铁路API 客户端ID | 必需。在.env(本地)或DB_API_KEY密钥(云)中设置。 |
DB_API_SECRET | 您的德国铁路API 密钥(秘密) | 必需。在.env(本地)或DB_API_SECRET密钥(云)中设置。 |
GCP_PROJECT_ID | 您的Google Cloud项目ID | 云部署所需。如果未设置,则自动检测。 |