返回市场
MCP网关

MCP网关

作者:lucky-aeon24 星标更新:2025-09-13

项目介绍

MCP网关

描述

MCP网关是一个反向代理服务器,它将客户端的请求转发到MCP服务器,或者通过统一的门户使用网关下的所有MCP服务器。

特性

  • 部署多个MCP服务器
  • 连接到MCP服务器
  • 使用网关调用MCP服务器
  • 获取所有MCP服务器的SSE流
  • 获取所有MCP服务器的工具

安装

  1. 拉取GitHub包
docker pull ghcr.io/lucky-aeon/mcp-gateway:latest
  1. 自建Docker镜像
docker build -t mcp-gateway .

使用

运行GitHub Docker容器

docker run -d --name mcp-gateway -p 8080:8080 ghcr.io/lucky-aeon/mcp-gateway

运行自建Docker容器

docker run -d --name mcp-gateway -p 8080:8080 mcp-gateway

API

部署

支持: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"
            }
        }
    }
}

使用MCP

GET SSE

GET /{mcp-server-name}/sse HTTP/1.1
Host: localhost:8080

POST 消息

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服务器。在调用时,需要在method前面添加 mcpServerName 内容,标识该请求来自哪个 MCP 服务器。

GET SSE

GET /sse HTTP/1.1
Host: localhost:8080

这里的 sse 是整个网关下所有 MCP 服务器的 SSE 流。

当客户端订阅 sse 时,网关会为每个 MCP 服务器创建一个 SSE 连接,并将所有 MCP 服务器的 SSE 流合并到一起。

在响应的所有tools/call 的结果中,会在method前面添加 mcpServerName 内容,标识该结果来自哪个 MCP 服务器。

POST 消息

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 event

{
  "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"
          ]
        }
      }
    ]
  }
}