MCP网关是一个反向代理服务器,它将客户端请求转发到MCP服务器,或者通过统一入口点利用网关下的所有MCP服务器。
构建Docker镜像
docker build -t mcp-gateway .
运行自建的Docker容器
docker run -d --name mcp-gateway -p 8080:8080 mcp-gateway
支持:uvx、npx或SSE URL
POST /deploy HTTP/1.1
Host: localhost:8080
Content-Type: application/json
{
"mcpServers": {
"time": {
"url": "http://mcp-server:8080", // 选择url或command之一
"command": "uvx", // 选择url或command之一
"args": ["mcp-server-time", "--local-timezone=America/New_York"], // 可选,command的参数
"env": { // 可选,环境变量
"KEY1": "VALUE1",
"KEY2": "VALUE2"
}
}
}
}
GET /{mcp-server-name}/sse HTTP/1.1
Host: localhost:8080
POST /{mcp-server-name}/message HTTP/1.1
Host: localhost:8080
Content-Type: application/json
{
"method": "tools/call",
"params": {
"name": "get_current_time",
"arguments": {
"timezone": "Asia/Seoul"
}
},
"jsonrpc": "2.0",
"id": 2
}
网关与直接使用MCP的区别在于,只需与网关进行交互,网关会自动将请求转发到相应的MCP服务器。在调用时,需要在方法内容前添加mcpServerName,指示请求来自哪个MCP服务器。
GET /sse HTTP/1.1
Host: localhost:8080
这里,sse是整个网关下所有MCP服务器的SSE流。
当客户端订阅SSE时,网关为每个MCP服务器创建一个SSE连接,并合并来自所有MCP服务器的SSE流。
在所有工具/call响应的结果中,会在方法内容前添加mcpServerName前缀,指示结果来自哪个MCP服务器。
POST /message HTTP/1.1
Host: localhost:8080
Content-Type: application/json
{
"method": "tools/call",
"params": {
"name": "{mcp-server-name}-get_current_time",
"arguments": {
"timezone": "Asia/Seoul"
}
},
"jsonrpc": "2.0",
"id": 2
}
获取网关下的所有工具
POST /message HTTP/1.1
Host: localhost:8080
Content-Type: application/json
{
"method": "tools/list",
"jsonrpc": "2.0",
"id": 1
}
# SSE响应message事件
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [
{
"name": "{mcpServerName}-get_current_time",
"description": "获取特定时区的当前时间",
"inputSchema": {
"type": "object",
"properties": {
"timezone": {
"type": "string",
"description": "IANA时区名称(例如,'America/New_York','Europe/London')。如果用户未提供时区,则使用'America/New_York'作为本地时区。"
}
},
"required": [
"timezone"
]
}
},
{
"name": "{mcpServerName}-convert_time",
"description": "在时区之间转换时间",
"inputSchema": {
"type": "object",
"properties": {
"source_timezone": {
"type": "string",
"description": "源IANA时区名称(例如,'America/New_York','Europe/London')。如果用户未提供源时区,则使用'America/New_York'作为本地时区。"
},
"time": {
"type": "string",
"description": "要转换的时间,24小时格式(HH:MM)"
},
"target_timezone": {
"type": "string",
"description": "目标IANA时区名称(例如,'Asia/Tokyo','America/San_Francisco')。如果用户未提供目标时区,则使用'America/New_York'作为本地时区。"
}
},
"required": [
"source_timezone",
"time",
"target_timezone"
]
}
}
]
}
}