A routing/proxy system for MCP servers
Project description
MCP Router
MCP Router 是一个模型上下文协议(MCP)路由/代理系统,作为MCP服务端和客户端,支持动态管理MCP工具配置,解决LLM无法区分同名工具的问题。
特性
- 动态路由: 文件系统路由,使用
mcp_settings.json配置 - 快速启动: 后台加载客户端,启动时间<0.1秒
- 热加载: 自动检测配置变化并重新加载
- 多传输支持: Stdio、SSE、HTTP 传输协议
- 实时日志: WebSocket实时日志流(可选)
- 权限控制: 可配置的LLM实例管理权限
- 智能端口: 端口占用时自动查找可用端口
- 安全认证: Bearer Token认证,输入验证
- REST API: 完整的HTTP API用于配置管理
项目结构
mcp_router/
├── main.py # 项目入口
├── config.json # 全局配置文件
├── requirements.txt # 依赖文件
├── .python-version # Python版本管理
├── pyproject.toml # uv项目配置
│
├── src/
│ ├── core/ # 核心模块
│ │ ├── logger.py # 日志系统
│ │ ├── config.py # 配置管理器
│ │ └── exceptions.py # 自定义异常
│ │
│ ├── mcp/ # MCP模块
│ │ ├── client.py # MCP客户端管理
│ │ ├── server.py # MCP服务端
│ │ ├── router.py # 路由核心逻辑
│ │ └── transport.py # 传输层
│ │
│ ├── api/ # API模块
│ │ ├── app.py # FastAPI应用
│ │ └── routes.py # API路由处理
│ │
│ └── utils/ # 工具模块
│ ├── validator.py # 输入验证
│ ├── watcher.py # 文件监视器
│ └── security.py # 安全工具
│
├── data/ # MCP配置目录
│ ├── example/
│ │ └── mcp_settings.json
│ ├── Openai/
│ │ └── mcp_settings.json
│ └── Wxcom/
│ └── mcp_settings.json
│
└── test/ # 测试文件
├── test_router.py
├── test_api.py
└── test_security.py
快速开始
环境要求
- Python 3.10+
- uv (推荐) 或 pip 或 conda
安装
从 PyPI 安装(推荐):
pip install mcp-router
从源码安装:
# 使用 uv (推荐)
uv venv .venv
uv pip install -e ".[dev]"
# 或使用 pip
python -m venv .venv
.venv\Scripts\activate # Windows
source .venv/bin/activate # Linux/Mac
pip install -e ".[dev]"
# 或使用 conda
conda env create -f environment.yml
conda activate mcp_router
配置
编辑 config.json 文件:
{
"api": {
"enabled": false,
"port": 8000,
"host": "127.0.0.1",
"cors_origin": "*",
"auto_find_port": true,
"enable_realtime_logs": false
},
"server": {
"enabled": true,
"transport_type": "stdio",
"allow_instance_management": false
},
"mcp_client": {
"enabled": true,
"timeout": 30
},
"security": {
"bearer_token": "",
"enable_validation": true
},
"logging": {
"level": "INFO",
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
"directory": "logs"
},
"watcher": {
"enabled": true,
"watch_path": "data",
"debounce_delay": 1.0
}
}
主要配置项说明:
auto_find_port: 端口占用时自动递增查找可用端口enable_realtime_logs: 启用WebSocket实时日志 (ws://host:port/ws)allow_instance_management: 允许LLM管理实例 (add/remove/enable/disable)logging.directory: 日志目录,使用Minecraft风格 (latest.txt + 时间戳备份)
添加MCP配置
在 data/{provider}/mcp_settings.json 中添加MCP服务器配置:
{
"provider": "example",
"isActive": true,
"name": "example_instance",
"type": "stdio",
"command": "python",
"args": ["-m", "example_mcp"],
"env": {},
"metadata": {
"description": "Example MCP server",
"version": "1.0.0"
}
}
运行
# 使用 uv
uv run python main.py
# 或直接运行
python main.py
使用模式
1. MCP Server 模式 (Stdio)
适用于与 LLM 集成,通过 stdio 协议通信。
配置:
{
"api": {"enabled": false},
"server": {"enabled": true, "transport_type": "stdio"}
}
2. API 模式
仅启动 REST API 服务器,用于配置管理。
配置:
{
"api": {"enabled": true},
"server": {"enabled": false}
}
3. 组合模式
同时运行 MCP Server 和 REST API。
配置:
{
"api": {"enabled": true},
"server": {"enabled": true}
}
MCP 工具
MCP Router 提供以下工具给 LLM 使用:
基础工具 (总是可用):
mcp.router.list()- 列出所有已注册的MCP客户端实例mcp.router.help()- 返回所有实例的工具列表和使用说明mcp.router.use(instance_name)- 使用指定的MCP实例mcp.router.call(instance_name, tool_name, **kwargs)- 调用指定实例的指定工具
管理工具 (需启用 allow_instance_management):
mcp.router.add(provider_name, config)- 动态添加新的MCP配置mcp.router.remove(instance_name)- 移除MCP配置mcp.router.enable(instance_name)- 启用MCP实例mcp.router.disable(instance_name)- 禁用MCP实例
REST API
当 API 模式启用时,可通过以下端点管理 MCP Router:
实例管理:
GET /api/instances- 列出所有实例GET /api/instances/{name}- 获取实例详情POST /api/instances- 添加新实例PATCH /api/instances/{name}- 更新实例配置DELETE /api/instances/{name}- 删除实例POST /api/instances/{name}/enable- 启用实例POST /api/instances/{name}/disable- 禁用实例
工具管理:
GET /api/tools- 列出所有工具GET /api/tools/{instance_name}- 获取实例的工具列表POST /api/call- 调用工具
其他:
GET /- 服务状态GET /health- 健康检查GET /api/config- 获取配置WS /ws- 实时日志流 (需启用enable_realtime_logs)
与 LLM 集成
在您的 LLM 客户端配置中添加:
{
"mcpServers": {
"mcp_router": {
"isActive": true,
"name": "mcp_router",
"type": "stdio",
"command": "uv",
"args": [
"--directory",
"path/to/mcp_router",
"run",
"python",
"main.py"
]
}
}
}
开发
代码风格
本项目使用 Ruff 进行代码格式化和 linting:
# 安装开发依赖
pip install -e ".[dev]"
# 格式化代码
ruff format .
# 检查代码
ruff check .
# 自动修复问题
ruff check --fix .
运行测试
# 运行所有测试
pytest
# 运行特定测试
pytest test/test_router.py
# 带覆盖率
pytest --cov=src --cov-report=html
本项目配置了 GitHub Actions CI,每次推送到 main 分支时会自动运行代码检查和测试。
安全性
- 输入验证: 防止SQL注入、XSS攻击、路径遍历
- Bearer Token: 可选的API认证
- CORS配置: 灵活的跨域请求控制
- 日志记录: 不记录敏感信息如完整token
许可证
贡献
欢迎提交 Issue 和 Pull Request!
贡献指南:
- Fork 本仓库
- 创建特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 开启 Pull Request
请确保:
- 代码通过
ruff检查 - 添加或更新相关测试
- 更新文档(如果需要)
联系方式
如有问题,请提交 Issue。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcp_router-1.0.5.tar.gz.
File metadata
- Download URL: mcp_router-1.0.5.tar.gz
- Upload date:
- Size: 21.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aca1ef09b75010cb77b422fcb46b29ce6cd14123cd0089544993a6af322747ce
|
|
| MD5 |
7b8d1e192382fb2528ad815b7b4b5b32
|
|
| BLAKE2b-256 |
8fb0beceaf1c4e88c002aec9a74b3a3c6d6492a7f90af827ec682c8e38c407c8
|
Provenance
The following attestation bundles were made for mcp_router-1.0.5.tar.gz:
Publisher:
python-publish.yml on ChuranNeko/mcp_router
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_router-1.0.5.tar.gz -
Subject digest:
aca1ef09b75010cb77b422fcb46b29ce6cd14123cd0089544993a6af322747ce - Sigstore transparency entry: 650998094
- Sigstore integration time:
-
Permalink:
ChuranNeko/mcp_router@c166cfedd7424df0812c667ee5222aa893e2e066 -
Branch / Tag:
refs/tags/v1.0.5 - Owner: https://github.com/ChuranNeko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c166cfedd7424df0812c667ee5222aa893e2e066 -
Trigger Event:
release
-
Statement type:
File details
Details for the file mcp_router-1.0.5-py3-none-any.whl.
File metadata
- Download URL: mcp_router-1.0.5-py3-none-any.whl
- Upload date:
- Size: 27.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c60cb8e5cbfdd2913246e45cb9d82c4cbea85606c43dd8e123e6d56f47f12d50
|
|
| MD5 |
69d5f5cc16c35d312420bbd7322d5b9f
|
|
| BLAKE2b-256 |
f9b45ff31ad301e2c6777f59b1bb98c0e38a7252ced09482514670f77e74dd86
|
Provenance
The following attestation bundles were made for mcp_router-1.0.5-py3-none-any.whl:
Publisher:
python-publish.yml on ChuranNeko/mcp_router
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mcp_router-1.0.5-py3-none-any.whl -
Subject digest:
c60cb8e5cbfdd2913246e45cb9d82c4cbea85606c43dd8e123e6d56f47f12d50 - Sigstore transparency entry: 650998163
- Sigstore integration time:
-
Permalink:
ChuranNeko/mcp_router@c166cfedd7424df0812c667ee5222aa893e2e066 -
Branch / Tag:
refs/tags/v1.0.5 - Owner: https://github.com/ChuranNeko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c166cfedd7424df0812c667ee5222aa893e2e066 -
Trigger Event:
release
-
Statement type: