Skip to main content

A routing/proxy system for MCP servers

Project description

MCP Router

PyPI Publish Code style: ruff Python 3.10+ License: MIT

MCP Router 是一个模型上下文协议(MCP)路由/代理系统,作为MCP服务端和客户端,支持动态管理MCP工具配置,解决LLM无法区分同名工具的问题。

特性

  • 动态路由: 类似Next.js的文件路由系统,使用 mcp_settings.json 作为配置文件
  • 热加载: 自动检测配置文件变化并重新加载
  • 多传输支持: 支持 Stdio、SSE、HTTP 三种传输方式
  • 安全认证: 可选的 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": true,
    "port": 8000,
    "host": "127.0.0.1",
    "cors_origin": "*"
  },
  "server": {
    "enabled": true,
    "transport_type": "stdio"
  },
  "mcp_client": {
    "enabled": true,
    "timeout": 30
  },
  "security": {
    "bearer_token": "",
    "enable_validation": true
  },
  "logging": {
    "level": "INFO",
    "file": "logs/mcp_router.log"
  },
  "watcher": {
    "enabled": true,
    "watch_path": "data",
    "debounce_delay": 1.0
  }
}

添加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.use(instance_name) - 使用指定的MCP实例
  • mcp.router.list() - 列出所有已注册的MCP客户端实例
  • mcp.router.help() - 返回所有实例的工具列表和使用说明
  • mcp.router.add(provider_name, config) - 动态添加新的MCP配置
  • mcp.router.call(instance_name, tool_name, **kwargs) - 调用指定实例的指定工具
  • mcp.router.remove(instance_name) - 移除MCP配置
  • mcp.router.disable(instance_name) - 禁用MCP实例
  • mcp.router.enable(instance_name) - 启用MCP实例

REST API

当 API 模式启用时,可通过以下端点管理 MCP Router:

  • GET /api/instances - 列出所有实例
  • GET /api/instances/{name} - 获取实例详情
  • GET /api/tools - 列出所有工具
  • GET /api/tools/{instance_name} - 获取实例的工具列表
  • POST /api/instances - 添加新实例
  • PATCH /api/instances/{name} - 更新实例配置
  • DELETE /api/instances/{name} - 删除实例
  • POST /api/instances/{name}/enable - 启用实例
  • POST /api/instances/{name}/disable - 禁用实例
  • POST /api/call - 调用工具
  • GET /api/config - 获取配置 (调试用)

与 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

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request!

贡献指南:

  1. Fork 本仓库
  2. 创建特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 开启 Pull Request

请确保:

  • 代码通过 ruff 检查
  • 添加或更新相关测试
  • 更新文档(如果需要)

联系方式

如有问题,请提交 Issue。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_router-1.0.2.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_router-1.0.2-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file mcp_router-1.0.2.tar.gz.

File metadata

  • Download URL: mcp_router-1.0.2.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mcp_router-1.0.2.tar.gz
Algorithm Hash digest
SHA256 428c4b6d76ebfb54c282d8fb8e2cc7672791f63b7222db400866df44f1b90c8e
MD5 95361c3d3f9c4280fc6f4e0ed985758b
BLAKE2b-256 7b40e726bb4ce2d8b1472a516f452b5df9cb4e8a0b735fc8557b5f40c977163e

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcp_router-1.0.2.tar.gz:

Publisher: python-publish.yml on ChuranNeko/mcp_router

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mcp_router-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: mcp_router-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mcp_router-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 53397fb8043dda69f2d833153e1de959882e8d99d79c97db32b8a747a0a503cd
MD5 fc80c3569d503b9c32a0cebc9858f732
BLAKE2b-256 f4ea369209b80bb1ff9ba9d432ca828f8ac1c35f8298db8324d05c1046c39bb1

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcp_router-1.0.2-py3-none-any.whl:

Publisher: python-publish.yml on ChuranNeko/mcp_router

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page