返回市场
MCP网关

MCP网关

作者:Feynix200480 星标更新:2025-10-09

项目介绍

MCP网关

描述

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

功能特性

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

安装

构建Docker镜像

docker build -t mcp-gateway .

使用

运行自建的Docker容器

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

API

部署MCP服务器

支持: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服务器。在调用时,需要在方法内容前添加mcpServerName,指示请求来自哪个MCP服务器。

GET SSE

GET /sse HTTP/1.1
Host: localhost:8080

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

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

在所有工具/call响应的结果中,会在方法内容前添加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事件

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