多MCP服务器网关

MCP Gateway是一个用于LibreChat的多MCP服务器网关,可在单个Docker容器中运行多个模型上下文协议(MCP)服务器,并使用Supergateway作为桥接。

  • by therebelrobot
  • Shell
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 therebelrobot/mcp-gateway

使用说明

MCP网关

LibreChat的多MCP服务器网关,使用Supergateway作为网桥在单个Docker容器中运行多个模型上下文协议(MCP)服务器。

特性

  • 多服务器支持:在单个容器中运行多个MCP服务器
  • 超级网关网桥:将基于stdio的MCP服务器转换为SSE端点
  • YAML配置:通过以下方式轻松配置 mcp-servers.yaml
  • Docker集成:与Docker和Docker Compose无缝协作
  • 健康检查:用于监测的内置健康端点
  • 日志记录:每个服务器的单独日志文件
  • 进程管理:自动重启和过程监控

建筑

┌─────────────────────────────────────────────────┐
│               LibreChat Container               │
│                                                 │
│  ┌─────────────────────────────────────────┐   │
│  │            MCP Gateway Container        │   │
│  │                                         │   │
│  │  ┌─────────┐  ┌─────────┐  ┌─────────┐ │   │
│  │  │ Brave   │  │  EMQX   │  │ Filesys │ │   │
│  │  │ Search  │  │  MCP    │  │   MCP   │ │   │
│  │  │ Port    │  │ Server  │  │ Server  │ │   │
│  │  │  8003   │  │  8004   │  │  8005   │ │   │
│  │  └─────────┘  └─────────┘  └─────────┘ │   │
│  │       ↑           ↑           ↑         │   │
│  │  ┌───────────────────────────────────┐  │   │
│  │  │        Supergateway Bridges       │  │   │
│  │  │   (stdio → SSE for each server)   │  │   │
│  │  └───────────────────────────────────┘  │   │
│  └─────────────────────────────────────────┘   │
│           ↑           ↑           ↑            │
│      http://.../sse  http://.../sse  http://.../sse
└─────────────────────────────────────────────────┘

快速开始

1.克隆存储库

git clone https://github.com/therebelrobot/mcp-gateway.git
cd mcp-gateway

2.配置您的MCP服务器

编辑 mcp-servers.yaml 定义要运行哪些MCP服务器:

servers:
  brave-search:
    command: "npx -y @modelcontextprotocol/server-brave-search"
    port: 8003
    env:
      BRAVE_API_KEY: ${BRAVE_API_KEY}

  emqx:
    command: "docker run -i --rm -e EMQX_API_URL=${EMQX_API_URL} -e EMQX_API_KEY=${EMQX_API_KEY} -e EMQX_API_SECRET=${EMQX_API_SECRET} benniuji/emqx-mcp-server"
    port: 8004
    requires_docker: true

3.构建和运行

# Build the Docker image
docker build -t mcp-gateway .

# Run the container
docker run -d \
  -p 8003:8003 \
  -p 8004:8004 \
  -p 8005:8005 \
  -e BRAVE_API_KEY="your-brave-api-key" \
  -e EMQX_API_URL="https://your-emqx-cloud-instance.com:8443/api/v5" \
  -e EMQX_API_KEY="your-emqx-api-key" \
  -e EMQX_API_SECRET="your-emqx-api-secret" \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v ./data:/data \
  --name mcp-gateway \
  mcp-gateway

4.与LibreChat集成

创建一个 librechat.yaml 文件:

mcpServers:
  brave-search:
    type: sse
    url: "http://mcp-gateway:8003/sse"
  
  emqx:
    type: sse
    url: "http://mcp-gateway:8004/sse"
  
  filesystem:
    type: sse
    url: "http://mcp-gateway:8005/sse"

Docker编写集成

看 docker-compose.example.yml 有关与LibreChat集成的完整示例:

# Copy example files
cp docker-compose.example.yml docker-compose.yml
cp librechat.yaml.example librechat.yaml

# Create .env file with your API keys
echo "BRAVE_API_KEY=your-brave-api-key" >> .env
echo "EMQX_API_KEY=your-emqx-api-key" >> .env
echo "EMQX_API_SECRET=your-emqx-api-secret" >> .env
echo "OPENAI_API_KEY=your-openai-api-key" >> .env

# Start all services
docker-compose up -d

配置

MCP服务器YAML格式

这 mcp-servers.yaml 文件支持以下结构:

servers:
  server-name:
    command: "command to run the MCP server"
    port: 8000  # Port for Supergateway to listen on
    env:  # Optional environment variables
      KEY: "value"
      ANOTHER_KEY: "${ENV_VAR_NAME}"
    requires_docker: false  # Set to true if command uses Docker
    volumes:  # Optional volume mounts (for filesystem access)
      - /host/path:/container/path

可用的MCP服务器

以下是您可以集成的一些流行的MCP服务器:

服务器 命令 描述
勇敢的搜索 npx -y @modelcontextprotocol/server-brave-search 使用Brave search API进行网络搜索
文件系统 npx -y @modelcontextprotocol/server-filesystem /path 文件系统访问
计算器 npx -y @modelcontextprotocol/server-calculator 数学计算
Git uvx mcp-server-git Git存储库操作
Docker uvx mcp-server-docker Docker容器管理
SQLite uvx mcp-server-sqlite SQLite数据库操作
EMQX docker run ... benniuji/emqx-mcp-server EMQX MQTT代理管理
Postgres uvx mcp-server-postgres PostgreSQL数据库操作

环境变量

变量 描述 必填
BRAVE_API_KEY 勇敢搜索API密钥 用于勇敢搜索
EMQX_API_URL EMQX API URL 用于EMQX
EMQX_API_KEY EMQX API密钥 用于EMQX
EMQX_API_SECRET EMQX API机密 用于EMQX
其他变量 根据MCP服务器的需要 取决于服务器

为不同建筑而建

适用于Raspberry Pi(ARM64)

# Build for ARM64
docker buildx build --platform linux/arm64 -t therebelrobot/mcp-gateway:arm64 .

# Push to GitHub Container Registry
docker push therebelrobot/mcp-gateway:arm64

多架构构建

# Create and use buildx builder
docker buildx create --name multiarch --use

# Build for multiple architectures
docker buildx build \
  --platform linux/amd64,linux/arm64 \
  -t therebelrobot/mcp-gateway:latest \
  --push .

GitHub容器注册表

推送到GHCR

# Login to GHCR
echo $GITHUB_TOKEN | docker login ghcr.io -u therebelrobot --password-stdin

# Build and push
docker build -t ghcr.io/therebelrobot/mcp-gateway:latest .
docker push ghcr.io/therebelrobot/mcp-gateway:latest

拉上Raspberry Pi

docker pull ghcr.io/therebelrobot/mcp-gateway:latest
docker run -d \
  -p 8003:8003 \
  -p 8004:8004 \
  -e BRAVE_API_KEY="your-key" \
  ghcr.io/therebelrobot/mcp-gateway:latest

故障排除

检查日志

# View container logs
docker logs mcp-gateway

# View individual server logs
docker exec mcp-gateway cat /app/logs/brave-search.log

健康检查

每个Supergateway实例都提供一个健康端点:

# Check if a server is healthy
curl http://localhost:8003/healthz

常见问题

  1. Docker套接字权限被拒绝:确保容器用户可以读取Docker套接字。

    chmod 666 /var/run/docker.sock
    
  2. 端口已在使用中:更改中的端口号 mcp-servers.yaml.

  3. 缺少环境变量:确保设置了所有必需的环境变量。

  4. YAML解析错误:使用 yq 要验证您的YAML:

    yq eval mcp-servers.yaml
    

发展

添加新的MCP服务器

  1. 将服务器添加到 mcp-servers.yaml
  2. 如果需要,更新启动脚本
  3. 重建Docker镜像

自定义启动脚本

这 start-servers.sh 脚本可以定制为:

  • 添加预启动挂钩
  • 修改环境变量
  • 实施自定义健康检查
  • 添加监控集成

使用监督员(替代方案)

该图像包括过程管理主管。要使用它:

  1. 编辑 supervisord.conf 定义您的服务器
  2. 将Dockerfile CMD更新为:
    CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]
    

许可证

MIT许可证-有关详细信息,请参阅许可证文件。

贡献

欢迎投稿!请随时提交拉取请求。

支持