伦敦巴士MCP服务器

一个通过Transport for London (TfL) API提供实时伦敦公交信息的MCP服务器。

  • by netologist
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 netologist/mcp-transport-for-london

使用说明

🚍 伦敦交通MCP服务器(TFL)

伦敦巴士MCP服务器通过伦敦交通局(TfL)API提供实时伦敦巴士信息。

概述

该项目实现了一个模型上下文协议(MCP)服务器,该服务器通过FastMCP公开伦敦公交车站信息。它包括:

  • MCP服务器 (server.py)-带有TfL API集成的FastMCP服务器
  • AI客户端 (client.py)-使用MCP工具的Pydantic AI代理
  • 评估 (evaluations.py)-代理的测试用例和评估

先决条件

  • Python 3.13+
  • 紫外线 -快速Python包安装程序
  • Node.js(用于MCP检查器)

快速开始

1.安装依赖项

make install

这运行 uv sync 从安装所有依赖项 pyproject.toml.

2.启动MCP服务器

make server

服务器将在以下时间可用 http://127.0.0.1:8000/mcp

3.使用客户端进行测试(在另一个终端中)

make client

它运行Pydantic AI代理,查询皮卡迪利广场附近的公交车站。

Opik集成(本地部署)

该项目支持 Opik 用于实验跟踪和评估。您可以使用Docker Compose在本地运行Opik。

Opik的先决条件

  • 已安装Docker和Docker Compose
  • 克隆Opik存储库(此项目之外):
# Clone the Opik repository
git clone https://github.com/comet-ml/opik.git

# Navigate to the opik folder

cd opik

# Start the Opik platform

./opik.sh

# Stop the Opik platform
./opik.sh --stop

Opik将在 http://localhost:5173

使用Opik进行评估

Opik运行后,您的评估跟踪将自动记录到本地Opik实例中 http://localhost:5173.

可用命令

核心命令

命令 描述
make help 显示所有可用命令
make install 使用安装依赖项 uv sync
make server 启动MCP服务器
make client 运行AI客户端
make eval 运行评估
make test 运行所有测试

开发命令

命令 描述
make dev-install 安装开发依赖项
make format 黑色和isort格式代码
make lint 带褶边的棉绒代码
make check 运行所有检查(棉绒+类型检查)
make shell 在uv环境下打开Python shell

包管理

命令 描述
make add PKG=package-name 添加包
make add-dev PKG=package-name 添加开发包
make lock 更新uv.lock文件
make tree 显示依赖关系树

调试和实用程序

命令 描述
make server-debug 使用调试输出启动服务器
make inspector 运行MCP检查器
make verify 验证服务器终结点是否可访问
make status 检查服务器是否正在运行
make kill-server 在端口8000上终止进程

清理

命令 描述
make clean 清理缓存文件并构建工件
make clean-all 删除所有内容,包括.vev

项目结构

tfl-mcp/
├── server.py           # FastMCP server with TfL API integration
├── client.py           # Pydantic AI agent client
├── evaluations.py      # Test cases and evaluations
├── otel.py            # OpenTelemetry configuration
├── pyproject.toml     # Project dependencies and configuration
├── Makefile           # Task automation
└── README.md          # This file

使用示例

添加新包

# Add a runtime package
make add PKG=requests

# Add a development package
make add-dev PKG=pytest

开发工作流程

# Install all dependencies including dev tools
make dev-install

# Format your code
make format

# Check code quality
make lint

# Run all checks
make check

使用MCP检查器运行

MCP Inspector提供了一个web UI来测试您的MCP服务器:

# Terminal 1: Start the server
make server

# Terminal 2: Start MCP Inspector
make inspector

然后打开浏览器,查看检查器显示的URL。

API工具

search_bus_stops

按名称或邮政编码搜索公交车站。

参数:

  • query (str):站点名称或邮政编码(例如,“牛津马戏团”、“SW1A 1AA”)

退货:

  • 带有ID、名称和坐标的匹配公交车站列表

例子:

# Using the client
res = agent.run_sync("Find bus stops near Piccadilly Circus")
print(res.output)

get_route_info

获取特定公交路线的详细信息。

参数:

  • route_number (str):公交线路号(例如“73”、“159”、“N9”)

退货:

  • 路线信息,包括每个路段的起点、终点和方向

例子:

# Using the client
res = agent.run_sync("Get information about bus route 73")
print(res.output)

配置

服务器配置

服务器运行在 streamable-http 默认传输:

mcp.run(transport="streamable-http")  # Accessible at http://127.0.0.1:8000/sse

客户端配置

客户端通过HTTP连接到服务器:

tfl_mcp_toolset = FastMCPToolset("http://127.0.0.1:8000/sse")

故障排除

服务器无法启动

# Check if port 8000 is already in use
make status

# Kill existing process
make kill-server

# Try starting again
make server

连接错误

# Verify server is accessible
make verify

# Should return SSE stream output

清洁安装

# Remove everything and start fresh
make clean-all
make install

依赖项

主要依赖关系:

  • fastmcp -FastMCP服务器框架
  • pydantic-ai -AI代理框架
  • httpx -用于TfL API的HTTP客户端
  • anthropic -克劳德人工智能模型

开发依赖关系:

  • black -代码格式化程序
  • isort -进口分拣机
  • ruff -快速Python linter
  • mypy -类型检查器

伦敦交通局API

本项目使用伦敦交通局(TfL)API。基本使用不需要API密钥。

API文件:https://api.tfl.gov.uk

许可证

GPLv3

贡献

欢迎投稿!请打开问题或提交拉取请求。