mcp2rest
⚠️ 已存档:此存储库已被移动 此项目不再在此存储库中维护。它已被移至: https://github.com/ulasbilgen/mcp2skill-tools 请访问新存储库以获取最新更新、问题和文档。 API和CLI命令保持相同,从而实现无缝迁移。
______________________________________________________________________
一个独立的Node.js守护进程,用于管理多个MCP(模型上下文协议)服务器,并通过REST API公开其工具。mcp2rest提供了一个通用的HTTP接口,允许使用任何编程语言的开发人员在没有Node.js集成复杂性的情况下利用MCP工具。
特性
- 通用HTTP接口:通过REST API从任何语言访问MCP工具
- 多服务器管理:同时连接到多个MCP服务器
- 动态服务器管理:添加/删除服务器而不重新启动
- 后台守护程序:使用PM2作为系统服务运行
- 自动重新连接:优雅地处理服务器断开连接
- 命令行界面:易于使用的命令行界面
安装
通过npm全局安装:
npm install -g mcp2rest快速开始
1.启动网关
在前台模式下启动网关:
mcp2rest start或作为系统服务安装(推荐):
mcp2rest service install2.添加MCP服务器
添加Chrome DevTools MCP服务器:
mcp2rest add chrome chrome-devtools-mcp@latest3.调用工具
使用任何HTTP客户端调用工具:
curl -X POST http://localhost:28888/call \
-H "Content-Type: application/json" \
-d '{
"server": "chrome",
"tool": "navigate",
"arguments": {
"url": "https://example.com"
}
}'API终点
执行工具
在特定的MCP服务器上执行工具。
端点: POST /call
请求正文:
{
"server": "chrome",
"tool": "navigate",
"arguments": {
"url": "https://example.com"
}
}答复:
{
"success": true,
"result": {
"content": [
{
"type": "text",
"text": "Navigated to https://example.com"
}
]
}
}列出服务器
获取所有连接的服务器及其状态。
端点: GET /servers
答复:
{
"servers": [
{
"name": "chrome",
"package": "chrome-devtools-mcp@latest",
"status": "connected",
"toolCount": 5
}
]
}获取服务器工具
列出特定服务器的所有可用工具。
端点: GET /servers/:name/tools
答复:
{
"server": "chrome",
"tools": [
{
"name": "navigate",
"description": "Navigate to a URL",
"inputSchema": { ... }
}
]
}添加服务器
动态添加新的MCP服务器。
端点: POST /servers
请求正文:
{
"name": "filesystem",
"package": "@modelcontextprotocol/server-filesystem",
"args": ["/home/user/workspace"]
}删除服务器
卸下MCP服务器。
端点: DELETE /servers/:name
健康检查
检查网关运行状况。
端点: GET /health
答复:
{
"status": "ok",
"serverCount": 2,
"connectedServers": 2
}OpenAPI规范
获取OpenAPI/Swagger格式的完整API规范。
端点: GET /openapi.yaml
答复: 具有完整API规范的YAML文件
例子:
curl http://localhost:28888/openapi.yaml > mcp2rest-api.yaml此规范可以与Swagger UI、Postman或其他OpenAPI工具一起使用,以探索和测试API。
CLI命令
网关管理
# Start gateway in foreground (default port 28888)
mcp2rest start
# Start with custom port and host
mcp2rest start --port 4000 --host 0.0.0.0
# Start with custom config file
mcp2rest start --config /path/to/config.yaml
# Stop gateway
mcp2rest stop服务管理
# Install as system service
mcp2rest service install
# Uninstall service
mcp2rest service uninstall
# Check service status
mcp2rest service status
# View service logs
mcp2rest service logs
# Follow logs in real-time
mcp2rest service logs --follow服务器管理
# Add a server
mcp2rest add
[--args ]
# Examples:
mcp2rest add chrome chrome-devtools-mcp@latest
mcp2rest add fs @modelcontextprotocol/server-filesystem --args /home/user/workspace配置
配置存储在 ~/.mcp2rest/config.yaml:
servers:
chrome:
package: chrome-devtools-mcp@latest
args: []
filesystem:
package: "@modelcontextprotocol/server-filesystem"
args: ["/home/user/workspace"]
gateway:
port: 28888
host: localhost
timeout: 30000
logLevel: info端口和主机配置
mcp2rest支持多种配置端口和主机的方式,优先级顺序如下(从高到低):
- CLI标志 (最高优先级)
- 环境变量
- 配置文件
- 默认值 (端口:28888,主机:localhost)
使用CLI标志
# Start with custom port
mcp2rest start --port 4000
# Start with custom host
mcp2rest start --host 0.0.0.0
# Start with both
mcp2rest start --port 4000 --host 0.0.0.0使用环境变量
# Set port via environment variable
MCP2REST_PORT=4000 mcp2rest start
# Set host via environment variable
MCP2REST_HOST=0.0.0.0 mcp2rest start
# Set both
MCP2REST_PORT=4000 MCP2REST_HOST=0.0.0.0 mcp2rest start使用配置文件
编辑 ~/.mcp2rest/config.yaml:
gateway:
port: 4000
host: 0.0.0.0更改服务端口
要更改已安装服务的端口,请执行以下操作:
# 1. Edit the configuration file
nano ~/.mcp2rest/config.yaml
# Change gateway.port to your desired port
# 2. Reinstall the service (updates PM2 config)
mcp2rest service install
# 3. Restart the service
mcp2rest service restartservice install命令会自动从配置文件中读取端口和主机,并相应地配置PM2服务。
示例:Python客户端
import requests
# Execute a tool
response = requests.post('http://localhost:28888/call', json={
'server': 'chrome',
'tool': 'navigate',
'arguments': {
'url': 'https://example.com'
}
})
result = response.json()
print(result)示例:Go客户端
package main
import (
"bytes"
"encoding/json"
"net/http"
)
func main() {
payload := map[string]interface{}{
"server": "chrome",
"tool": "navigate",
"arguments": map[string]string{
"url": "https://example.com",
},
}
jsonData, _ := json.Marshal(payload)
resp, _ := http.Post(
"http://localhost:28888/call",
"application/json",
bytes.NewBuffer(jsonData),
)
defer resp.Body.Close()
}错误处理
所有错误都遵循一致的格式:
{
"error": {
"code": "SERVER_NOT_FOUND",
"message": "Server 'chrome' not found",
"serverName": "chrome"
}
}常见错误代码:
SERVER_NOT_FOUND:指定的服务器不存在SERVER_DISCONNECTED:服务器未连接TOOL_NOT_FOUND:服务器上不存在工具TOOL_EXECUTION_ERROR:工具执行失败TOOL_TIMEOUT:工具执行时间超过30秒INVALID_ARGUMENTS:请求参数无效
需求
- Node.js>=18.0.0
- npm或npx
最近完成✅
- \[x\] 自定义端口和主机配置 (v0.2.8)
- CLI标志: --port 和 --host 用于启动命令 - 环境变量: MCP2REST_PORT 和 MCP2REST_HOST - 优先级顺序:CLI标志>环境变量>配置文件>默认值 - PM2服务集成:使用配置文件设置自动配置服务 - 动态端口检测 add 和 remove CLI命令
- \[x\] 自动重新连接:当MCP服务器断开连接时,通过指数回退自动重新连接
- 实施于 src/gateway/Gateway.ts - 最多10次指数回退的重新连接尝试 - 从服务器崩溃中自动恢复
- \[x\] 删除端点:
DELETE /servers/:nameREST端点
- 实施于 src/api/APIServer.ts - 通过API动态删除服务器
TODO
中优先级
- \[\]可选CLI便利命令(列表、工具、删除)
- 注意:REST API已经提供了此功能
未来的增强功能
- API密钥和身份验证支持 -请参阅 API_KEY_SUPPORT.md 详细的实施计划
- 基于HTTP的MCP服务器的HTTP标头 - 基于stdio的MCP服务器的环境变量 - 支持Context7、PostHog、Figma Cloud等服务。
- 速率限制
- WebSocket支持流媒体
- 基于Web的仪表板
- 工具结果缓存
许可证
麻省理工学院
贡献
欢迎投稿!请打开问题或提交拉取请求。
文档
- openapi.yaml -针对REST API的完整OpenAPI/Swagger规范
- MCP_Gateway_PRD.md -详细的实施规范
- API_KEY_SUPPORT.md -API密钥和认证实施计划
