MCP Gateway 是一个精简版 MCP 聚合代理。它把多个后端 MCP Server 合并成一个统一入口,对外暴露单一的 MCP 服务,同时保留工具名空间隔离,避免同名工具冲突。
当前项目只保留核心能力:
- 聚合多个后端 MCP Server
- 支持后端
stdio/sse/streamable-http - 支持网关自身以
stdio/sse/streamable-http运行 - 提供基础 HTTP 管理接口:
/health、/servers、/tools - 使用
uv管理虚拟环境和依赖
MCP Gateway:作为多个 MCP 服务的统一网关。
uv sync --extra dev这会:
- 创建
.venv - 安装开发依赖
- 解析并锁定当前依赖版本
当前锁定的 MCP Python SDK 版本为 mcp==1.27.1。
uv run python main.py --config ./examples/config/servers.jsonuv run python main.py --transport sse --config ./examples/config/sse-backend.json默认监听:http://127.0.0.1:8080/sse
uv run python main.py --transport streamable-http --config ./examples/config/streamable-http-backend.json默认监听:http://127.0.0.1:8080/mcp
配置文件统一使用:
{
"mcpServers": {
"server-name": {
"transport": "stdio | sse | streamable-http",
"command": "python",
"args": ["./server.py"],
"url": "http://127.0.0.1:9000/sse",
"headers": {
"Authorization": "Bearer ..."
}
}
}
}{
"mcpServers": {
"calculator": {
"transport": "stdio",
"command": "python",
"args": ["./tests/tools/calculator.py"]
},
"echo": {
"transport": "stdio",
"command": "python",
"args": ["./tests/tools/echo_server.py", "--name", "echo"]
}
}
}见:examples/config/servers.json
{
"mcpServers": {
"remote-echo": {
"transport": "sse",
"url": "http://127.0.0.1:9090/sse"
}
}
}见:examples/config/sse-backend.json
{
"mcpServers": {
"remote-echo": {
"transport": "streamable-http",
"url": "http://127.0.0.1:9091/mcp"
}
}
}见:examples/config/streamable-http-backend.json
网关会把工具名改写为:
<server_name>.<tool_name>
例如:
calculator.addcalculator.multiplyremote-echo.ping
这样可以安全聚合多个存在同名工具的后端。
当网关以 sse 或 streamable-http 方式运行时,会额外提供:
GET /healthGET /serversPOST /serversDELETE /servers/{name}GET /tools
POST /servers 的请求体与主配置文件格式一致。
main.py
src/mcp_gateway/
examples/config/
tests/
其中:
- main.py 是统一入口
- src/mcp_gateway/app.py 负责网关生命周期和 HTTP 服务
- src/mcp_gateway/clients.py 负责后端连接管理
- src/mcp_gateway/proxy.py 负责 MCP 请求路由与聚合
uv run pytest -q当前测试覆盖:
- 代理工具聚合与路由
- SSE 网关传输
- Streamable HTTP 网关传输
- Streamable HTTP 后端接入