MCP连接网关

MCP Connect是一个HTTP网关,允许通过流式HTTP或经典请求/响应桥接调用本地MCP服务器。

  • by EvalsOne
  • Python
  • 本地部署
  • 240 星标
  • 更新于
在 GitHub 查看 EvalsOne/MCP-connect

使用说明

MCP连接

███╗   ███╗ ██████╗██████╗      ██████╗ ██████╗ ███╗   ██╗███╗   ██╗███████╗ ██████╗████████╗
████╗ ████║██╔════╝██╔══██╗    ██╔════╝██╔═══██╗████╗  ██║████╗  ██║██╔════╝██╔════╝╚══██╔══╝
██╔████╔██║██║     ██████╔╝    ██║     ██║   ██║██╔██╗ ██║██╔██╗ ██║█████╗  ██║        ██║   
██║╚██╔╝██║██║     ██╔═══╝     ██║     ██║   ██║██║╚██╗██║██║╚██╗██║██╔══╝  ██║        ██║   
██║ ╚═╝ ██║╚██████╗██║         ╚██████╗╚██████╔╝██║ ╚████║██║ ╚████║███████╗╚██████╗   ██║   
╚═╝     ╚═╝ ╚═════╝╚═╝          ╚═════╝ ╚═════╝ ╚═╝  ╚═══╝╚═╝  ╚═══╝╚══════╝ ╚═════╝   ╚═╝   

轻量级网桥,将本地MCP服务器作为HTTP API公开


什么是MCP Connect?

MCP Connect是一个HTTP网关,允许您通过Streamable HTTP或经典的请求/响应桥调用本地MCP服务器(使用stdio)。

最新动态

  • 在经典的请求/响应桥上添加了可流式HTTP模式
  • 新的快速部署脚本和配置 deploy/e2b 用于在E2B沙盒中启动

运作原理

+-----------------+   HTTP (JSON)               +------------------+      stdio      +------------------+
|                 |   /bridge                   |                  |                 |                  |
|  Cloud AI tools | <------------------------>  |  Node.js Bridge  | <------------>  |    MCP Server    |
|   (Remote)      |                             |    (Local)       |                 |     (Local)      |
|                 |   HTTP (SSE stream)         |                  |                 |                  |
|                 |   /mcp/:serverId            |                  |                 |                  |
+-----------------+         Tunnels (optional)  +------------------+                 +------------------+

主要特点

|特性|描述| |--------|-------------| | 🚀 双模|通过Streamable HTTP或经典HTTP网桥调用本地stdio MCP服务器| | 🔄 会话管理|保持会话的连续性| | 🔐 安全|承载令牌认证+CORS分配列表| | 🌐 公共访问|内置Ngrok隧道,将端点暴露在外部| | ☁️ 云部署|一键部署到E2B云沙箱|

快速开始

先决条件

  • Node.js>=22.0.0(推荐)
  • npm或纱线

1) 安装

git clone https://github.com/EvalsOne/MCP-connect.git
cd mcp-connect
npm install

2) 准备工作

A.设置初始环境变量

cp .env.example .env

集 ACCESS_TOKEN 在 .env 在启动服务器之前。如果为空,则启动失败。

B.配置MCP服务器

对于Streamable HTTP方法,每个MCP服务器都需要单独配置,编辑文件以添加除现有示例服务器之外的更多配置。

vim mcp-servers.json  

3) 快跑

# Build and start
npm run build
npm start

# Or use dev mode (hot reload)
npm run dev

# Enable Ngrok tunnel
npm run start:tunnel

看到创业横幅后,请访问http://localhost:3000/health检查服务器状态。


用法

模式1:流式HTTP网桥

通用,与支持Streamable HTTP的任何MCP客户端兼容。

在流式HTTP模式下,每个MCP服务器都被分配了一个唯一的路由。示例:添加 fetch MCP服务器中 mcp-servers.json.

{
  "mcpServers": {
    "fetch": {
      "command": "uvx",
      "args": ["mcp-server-fetch"],
      "description": "HTTP/HTTPS content fetcher"
    }
  }
}

启动后,访问 fetch 带有您最喜欢的MCP客户端的MCP服务器(例如Claude Code、Cursor、Codex、GitHub Copilot……)

http://localhost:3000/mcp/fetch

注意:您必须进行配置 mcp-servers.json 在启动服务之前,否则服务器将不可用。


模式2:经典请求/响应桥

实现以下方法的非标准调用 tools/list, tools/call等等。

包含 Authorization: Bearer <token> 在每个请求标头中。 ACCESS_TOKEN 启动时需要。

示例1:列出可用工具

curl -X POST http://localhost:3000/bridge \
  -H "Authorization: Bearer your-secret-token-here" \
  -H "Content-Type: application/json" \
  -d '{
    "serverPath": "uvx",
    "args": ["mcp-server-fetch"],
    "method": "tools/list",
    "params": {}
  }'

示例2:调用工具

curl -X POST http://localhost:3000/bridge \
  -H "Authorization: Bearer your-secret-token-here" \
  -H "Content-Type: application/json" \
  -d '{
    "serverPath": "uvx",
    "args": ["mcp-server-fetch"],
    "method": "tools/call",
    "params": {
      "name": "fetch",
      "arguments": {
        "url": "https://example.com"
      }
    }
  }'

安全

认证

MCP Connect需要基于令牌的身份验证系统。集 ACCESS_TOKEN 在 .env 在启动之前。如果为空,服务器将退出并显示配置错误。

Authorization: Bearer <your_auth_token>

允许的来源

生产中,set ALLOWED_ORIGINS 限制跨源请求:

ALLOWED_ORIGINS=https://yourdomain.com,https://app.yourdomain.com

如果 ALLOWED_ORIGINS 设置后,不匹配的来源将被拒绝。


API 参考

GET /health

健康检查端点(无需身份验证)

答复:

{"status": "ok"}

POST /mcp/:serverId

流式HTTP模式

路径参数:

  • serverId:中定义的服务器ID MCP_SERVERS

标题:

  • Authorization: Bearer <token> (必填)
  • Accept: application/json, text/event-stream (必填)
  • mcp-session-id: <session-id> (可选,重用现有会话)

主体:

[
  {"jsonrpc":"2.0","id":"1","method":"tools/list","params":{}},
  {"jsonrpc":"2.0","method":"notifications/initialized"}
]

POST /bridge

原始请求/响应桥接模式

标题:

  • Authorization: Bearer <token> (必填)
  • Content-Type: application/json

主体:

{
  "serverPath": "Executable command or URL (http/https/ws/wss)",
  "method": "JSON-RPC method name",
  "params": {},
  "args": ["optional command-line args"],
  "env": {"OPTIONAL_ENV_VAR": "value"}
}

支持的方法:

  • tools/list, tools/call
  • prompts/list, prompts/get
  • resources/list, resources/read
  • resources/subscribe, resources/unsubscribe
  • completion/complete
  • logging/setLevel

通过Ngrok公开曝光

  1. 获取令牌:https://dashboard.ngrok.com/get-started/your-authtoken

  2. 增添 .env:

    NGROK_AUTH_TOKEN=your-token-here
    
  3. 启动服务:

    npm run start:tunnel
    
  4. 控制台将显示公共URL:

    Tunnel URL: https://abc123.ngrok.io
    MCP Bridge URL: https://abc123.ngrok.io/bridge
    

快速部署到E2B沙盒

E2B提供隔离的云沙盒,非常适合安全运行不受信任的MCP服务器。

步骤1:准备E2B环境

# Sign up at https://e2b.dev and get an API key
pip install e2b
export E2B_API_KEY=your-e2b-api-key

可选择为网桥设置承载令牌(E2B部署的首选环境变量):

export E2B_MCP_AUTH_TOKEN=your-secure-token

步骤2:构建沙盒模板

cd deploy/e2b
python build.py

步骤3:从模板启动

python sandbox_deploy.py --template-id mcp-dev-gui

请参阅:E2B部署指南 了解详情。


记录和监控

日志文件

  • combined.log:所有级别
  • error.log:仅错误级别

等级

通过以下方式控制冗长 LOG_LEVEL:

LOG_LEVEL=DEBUG  # development
LOG_LEVEL=INFO   # production (default)
LOG_LEVEL=WARN   # warnings + errors

发展

项目布局

src/
├── server/
│   └── http-server.ts      # HTTP server and routes
├── client/
│   └── mcp-client-manager.ts  # MCP client manager
├── stream/
│   ├── session-manager.ts   # session lifecycle
│   └── stream-session.ts    # SSE session implementation
├── config/
│   └── config.ts            # config loading & validation
├── utils/
│   ├── logger.ts            # Winston logger
│   └── tunnel.ts            # Ngrok tunnel management
└── index.ts                 # entry point

贡献

欢迎发布问题和PR!


如果这个项目对你有帮助,请⭐️ 明星!