使用说明
MCP HTTP网桥
🌉 一个轻量级的HTTP网桥 模型上下文协议(MCP) 服务器。
将您的MCP服务器转换为REST API!此桥允许任何HTTP客户端发现和调用在Docker容器中运行的MCP服务器上的工具。
特性
- REST API -用于工具发现和执行的简单HTTP端点
- 多服务器 -从一个网桥管理多个MCP服务器
- Docker集成 -通过以下方式与MCP服务器通信
docker exec - 自动重启 -崩溃的进程会以指数级回退自动重启
- 缓冲保护 -防止大响应导致内存耗尽
- 优雅关闭 -退出时终止清洁流程
- 速率限制 -高流量服务器的可选请求队列
快速开始
先决条件
- Node.js 18+
- 码头工人
- 在Docker容器中运行的MCP服务器
安装
git clone https://github.com/Stavion-Colquitt/mcp-http-bridge.git
cd mcp-http-bridge
npm install
配置
编辑 bridge.js 要定义您的MCP服务器:
const MCP_SERVERS = {
'filesystem': {
container: 'filesystem-mcp',
command: ['docker', 'exec', '-i', 'filesystem-mcp', 'npx', '-y',
'@modelcontextprotocol/server-filesystem', '/data']
},
'fetch': {
container: 'fetch-mcp',
command: ['docker', 'exec', '-i', 'fetch-mcp', 'mcp-fetch-server']
}
};
或者从环境变量加载服务器:
export MCP_SERVERS_CONFIG='{"myserver":{"container":"my-mcp","command":["docker","exec","-i","my-mcp","node","/app/index.js"]}}'
跑
npm start
或者使用环境变量:
PORT=3100 npm start
API终点
健康检查
GET /health
返回所有已配置服务器的状态。
列出所有工具
GET /tools
返回所有服务器上的所有可用工具。
列出服务器工具
GET /tools/:serverName
从特定服务器返回工具。
调用工具
POST /call
Content-Type: application/json
{
"server": "filesystem",
"tool": "read_file",
"arguments": {
"path": "/data/example.txt"
}
}
列出服务器
GET /servers
返回已配置服务器的列表。
环境变量
| 变量 | 默认值 | 描述 |
|---|---|---|
PORT |
3000 | HTTP端口 |
MAX_BUFFER_SIZE |
1048576 | 最大响应缓冲区(字节) |
DEFAULT_TIMEOUT |
30000 | 请求超时(毫秒) |
STARTUP_TIMEOUT |
30000 | 进程启动超时(毫秒) |
MAX_RESTARTS |
5 | 每个窗口的最大自动重启次数 |
RESTART_WINDOW_MS |
300000 | 重新启动计数窗口(ms) |
FETCH_QUEUE_ENABLED |
false | 启用速率限制 |
FETCH_MAX_CONCURRENT |
5 | 最大并发获取请求数 |
FETCH_MAX_QUEUE |
20 | 排队获取请求的最大数量 |
MCP_SERVERS_CONFIG |
- | JSON服务器配置(可选) |
Docker部署
Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY bridge.js ./
# Mount Docker socket to communicate with other containers
# docker run -v /var/run/docker.sock:/var/run/docker.sock ...
EXPOSE 3000
CMD ["node", "bridge.js"]
Docker Compose
version: '3.8'
services:
mcp-bridge:
build: .
ports:
- "3100:3000"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- PORT=3000
restart: unless-stopped
# Example MCP server
filesystem-mcp:
image: node:20-alpine
command: npx -y @modelcontextprotocol/server-filesystem /data
volumes:
- ./data:/data
使用示例
python
import requests
# List tools
response = requests.get('http://localhost:3100/tools')
tools = response.json()['tools']
# Call a tool
response = requests.post('http://localhost:3100/call', json={
'server': 'filesystem',
'tool': 'read_file',
'arguments': {'path': '/data/test.txt'}
})
result = response.json()
JavaScript
// List tools
const tools = await fetch('http://localhost:3100/tools').then(r => r.json());
// Call a tool
const result = await fetch('http://localhost:3100/call', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
server: 'filesystem',
tool: 'read_file',
arguments: { path: '/data/test.txt' }
})
}).then(r => r.json());
卷曲
# Health check
curl http://localhost:3100/health
# List tools
curl http://localhost:3100/tools
# Call a tool
curl -X POST http://localhost:3100/call \
-H "Content-Type: application/json" \
-d '{"server":"filesystem","tool":"read_file","arguments":{"path":"/data/test.txt"}}'
建筑
┌─────────────────────────────────────────────────────────────┐
│ MCP HTTP Bridge │
├─────────────────────────────────────────────────────────────┤
│ │
│ HTTP Client (AI App, Script, etc.) │
│ │ │
│ ▼ │
│ ┌───────────────────┐ │
│ │ Express Server │ :3000 │
│ │ - /health │ │
│ │ - /tools │ │
│ │ - /call │ │
│ └─────────┬─────────┘ │
│ │ │
│ ▼ │
│ ┌───────────────────┐ │
│ │ Process Manager │ │
│ │ - spawn processes │ │
│ │ - JSON-RPC comms │ │
│ │ - auto-restart │ │
│ └─────────┬─────────┘ │
│ │ │
│ docker exec -i container_name ... │
│ │ │
│ ┌─────────┴─────────────────────────────────┐ │
│ │ Docker Containers │ │
│ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │
│ │ │ MCP │ │ MCP │ │ MCP │ │ │
│ │ │ Server 1│ │ Server 2│ │ Server 3│ │ │
│ │ └─────────┘ └─────────┘ └─────────┘ │ │
│ └───────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
运作原理
- 初创公司:Bridge启动Express服务器并等待请求
- 工具发现:在第一次向服务器发出请求时,生成
docker exec过程 - 初始化:发送MCP
initialize建立协议的消息 - 工具调用:将HTTP请求转换为JSON-RPC,通过stdin发送
- 回复:从stdout解析JSON-RPC响应,返回HTTP JSON
- 错误处理:自动重新启动崩溃的进程,拒绝挂起的请求
贡献
欢迎投稿!请打开问题或PR。
许可证
麻省理工学院-见 许可证
作者
Stavion Colquitt
为人工智能驱动的未来而打造 🚀
