一个灵活的网关服务器,连接模型上下文协议(MCP)STDIO服务器到MCP HTTP+SSE和REST API,使多实例MCP服务器能够通过HTTP暴露。
MCP网关现在提供了一个REST API接口给MCP服务器,使其可以被任何支持OpenAPI/Swagger规范的HTTP客户端访问。此功能特别适用于与OpenAI的自定义GPT和其他REST API客户端集成。
在调用工具之前,需要获取一个会话ID:
curl "http://localhost:3000/api/sessionid"
# 返回:{"sessionId": "<生成的ID>"}
每个由MCP服务器公开的工具都可以通过以下方式访问:
POST /api/{serverName}/{toolName}?sessionId={session-id}
注意:所有工具调用都需要sessionId查询参数。
例如,要调用filesystem MCP服务器上的directory_tree工具:
# 首先获取一个会话ID
SESSION_ID=$(curl -s "http://localhost:3000/api/sessionid" | jq -r .sessionId)
# 然后进行工具调用
curl -X POST "http://localhost:3000/api/filesystem/directory_tree?sessionId=$SESSION_ID" \
-H "Content-Type: application/json" \
-d '{"path": "/some/path"}'
网关可以为所有配置的工具生成OpenAPI模式,便于与兼容OpenAPI的客户端集成:
# 生成YAML格式(默认)
npm start -- --schemaDump
# 生成JSON格式
npm start -- --schemaDump --schemaFormat json
生成的模式包括:
目前,大多数MCP服务器设计用于本地执行。MCP网关使得HTTP+SSE能力的客户端能够与远程机器上运行的MCP服务器交互。这解决了常见的部署场景,如在容器化环境中运行LibreChat,其中某些MCP服务器,如Puppeteer服务器,可能具有有限的功能。MCP网关提供了一个强大的解决方案,可以在多台机器之间分布MCP服务器,同时保持无缝连接。
MCP网关支持两种可以独立启用的身份验证方法:
这两种方法可以同时启用,并且任何有效的身份验证都会授予访问权限。
在你的config.yaml中添加身份验证设置:
auth:
basic:
enabled: true
credentials:
- username: "admin"
password: "your-secure-password"
# 根据需要添加更多用户名/密码对
bearer:
enabled: true
tokens:
- "your-secure-token"
# 根据需要添加更多令牌
curl -u username:password http://localhost:3000/serverName
curl -H "Authorization: Bearer your-secure-token" http://localhost:3000/serverName
npm install
网关使用YAML文件进行配置。默认情况下,它会在当前目录查找config.yaml,但你可以使用CONFIG_PATH环境变量指定不同的路径。
网关使用Winston进行日志记录,提供丰富的格式和多个日志级别:
debug:
level: "info" # 可能值:"error", "warn", "info", "debug", "verbose"
从最少到最多详细程度的日志级别:
error:仅显示错误warn:显示警告和错误info:显示一般信息、警告和错误(默认)debug:显示调试信息和以上所有内容verbose:显示所有可能的日志信息日志包含时间戳,并在终端查看时按级别进行颜色编码。当相关时,还会包含作为JSON的附加元数据。
示例日志输出:
2024-01-20T10:15:30.123Z [INFO]: 新的SSE连接到filesystem
2024-01-20T10:15:30.124Z [DEBUG]: 使用sessionId创建服务器实例:/filesystem?sessionId=abc123
2024-01-20T10:15:30.125Z [VERBOSE]: 接收到STDIO消息:{"type":"ready"}
hostname: "0.0.0.0" # 监听所有接口
port: 3000
servers:
filesystem:
command: npx
args:
- -y
- "@modelcontextprotocol/server-filesystem"
- "/path/to/root"
git:
command: npx
args:
- -y
- "@modelcontextprotocol/server-git"
hostname: "127.0.0.1"
port: 3000
hostname: "192.168.1.100"
port: 3000
hostname: "0.0.0.0"
port: 3000
servers部分中的每个服务器需要:
command:运行服务器的命令args:命令的参数列表path(可选):服务器的工作目录带有所有选项的示例:
servers:
myserver:
command: npx
args:
- -y
- "@modelcontextprotocol/server-mytype"
- "--some-option"
hostname: "0.0.0.0"
port: 3000
# 身份验证配置(可选)
auth:
basic:
enabled: true
credentials:
- username: "admin"
password: "your-secure-password"
bearer:
enabled: true
tokens:
- "your-secure-token"
servers:
filesystem:
command: npx
args:
- -y
- "@modelcontextprotocol/server-filesystem"
- "/path/to/root"
标准启动:
npm start
使用自定义配置:
CONFIG_PATH=/path/to/my/config.yaml npm start
servers部分添加一个新的条目:servers:
mynewserver:
command: npx
args:
- -y
- "@modelcontextprotocol/server-newtype"
# 在这里添加任何服务器特定的参数
网关为每个服务器实例创建一个唯一的会话,允许多个客户端独立地使用相同的服务器类型。每个会话维护自己的:
当客户端断开连接时,所有相关的资源都会自动清理。
CONFIG_PATH:指向YAML配置文件的路径(默认:./config.yaml)欢迎提交问题和PR,但坦率地说,它们可能会被搁置一段时间。
MIT许可证
curl -X POST "http://localhost:3000/api/filesystem/directory_tree?sessionId=randomSession12345" -H "Content-Type: application/json" -d '{ "path": "/home/aaron/Clara" }'