MCP网关服务

Colloid是一个为模型上下文协议(MCP)设计的API网关,提供集中管理、安全和编排功能,支持多服务器请求分发和工具聚合。

  • by dd-colloid-inc
  • Rust
  • 本地部署
  • 更新于
在 GitHub 查看 dd-colloid-inc/colloid

使用说明

胶体

均匀分布的MCP网关

API网关 模型上下文协议(MCP),用Rust写的。

什么是胶体?

Colloid位于LLM应用程序和MCP服务器之间,提供集中管理、安全和编排。就像化学中的胶体一样,粒子均匀地分布在介质中,胶体将您的请求无缝地分布在多个MCP服务器上。

┌─────────────┐     ┌─────────────────┐     ┌─────────────────┐
│   Clients   │     │     Colloid     │     │  MCP Servers    │
│             │     │                 │     │                 │
│ Claude      │────>│ • Authenticate  │────>│ GitHub MCP      │
│ Desktop     │     │ • Authorize     │     │ Slack MCP       │
│             │<────│ • Route         │<────│ Database MCP    │
│ Custom Apps │     │ • Aggregate     │     │ Custom MCP      │
│             │     │ • Monitor       │     │                 │
└─────────────┘     └─────────────────┘     └─────────────────┘

特性

  • 工具聚合 -使用命名空间组合来自多个服务器的工具(github:create_issue)
  • 安全 -API密钥、RBAC、速率限制、审计日志记录、SSRF保护
  • 韧性 -断路器、重试、超时、幂等性
  • 可观测性 -OpenTetry跟踪、结构化日志记录、健康检查
  • 工作流 -将多个工具调用链接到自动化工作流程中
  • 多个运行时 -进程(stdio)、容器(Docker)、远程(HTTP代理)

快速开始

先决条件

  • 锈蚀1.75+
  • (可选)Docker用于容器运行时

构建

git clone https://github.com/dd-colloid-inc/colloid.git
cd colloid
cargo build --release

配置

创建一个 config.yaml 文件:

server:
  host: "0.0.0.0"
  port: 3001

auth:
  api_keys:
    # API keys must be at least 32 characters for security
    - key: "your-secure-api-key-at-least-32-characters-long"
      subject: "user@example.com"
      roles:
        - admin

rate_limit:
  requests_per_minute: 60
  burst: 10

servers:
  # Example: GitHub MCP Server
  - id: github
    command: npx
    args:
      - "-y"
      - "@modelcontextprotocol/server-github"
    env:
      GITHUB_TOKEN: "your-github-token"

  # Example: Filesystem MCP Server
  - id: filesystem
    command: npx
    args:
      - "-y"
      - "@modelcontextprotocol/server-filesystem"
      - "/workspace"

跑

# With configuration file
./target/release/colloid --config config.yaml

# Or use default configuration (development only)
./target/release/colloid

码头工人

# Build image
docker build -t colloid .

# Run with config
docker run -p 3001:3001 -v $(pwd)/config.yaml:/app/config.yaml colloid

用法

认证

Colloid支持多种身份验证方法。使用适当的标题:

身份验证方法 标头 示例
API密钥 X-API-Key X-API-Key: your-api-key-at-least-32-chars
JWT/承载 Authorization: Bearer Authorization: Bearer eyJ...
OAuth2 Authorization: Bearer Authorization: Bearer <access_token>

下面的示例使用API密钥身份验证。

初始化会话

curl -X POST http://localhost:3001/mcp \
  -H "Content-Type: application/json" \
  -H "X-API-Key: your-secure-api-key-at-least-32-characters-long" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "initialize",
    "params": {
      "protocolVersion": "2025-06-18",
      "capabilities": {},
      "clientInfo": { "name": "my-app", "version": "1.0.0" }
    }
  }'

列出工具

curl -X POST http://localhost:3001/mcp \
  -H "Content-Type: application/json" \
  -H "X-API-Key: your-secure-api-key-at-least-32-characters-long" \
  -d '{"jsonrpc": "2.0", "id": 2, "method": "tools/list"}'

调用工具

工具按服务器ID命名(server:tool):

curl -X POST http://localhost:3001/mcp \
  -H "Content-Type: application/json" \
  -H "X-API-Key: your-secure-api-key-at-least-32-characters-long" \
  -d '{
    "jsonrpc": "2.0",
    "id": 3,
    "method": "tools/call",
    "params": {
      "name": "github:create_issue",
      "arguments": { "repo": "owner/repo", "title": "Bug report" }
    }
  }'

临时请求

使用 X-Idempotency-Key 用于安全重试的标头:

curl -X POST http://localhost:3001/mcp \
  -H "X-API-Key: your-api-key-at-least-32-characters-long" \
  -H "X-Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000" \
  -d '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", ...}'

服务器发送事件(SSE)

对于流媒体响应:

curl -N http://localhost:3001/mcp/sse \
  -H "X-API-Key: your-secure-api-key-at-least-32-characters-long"

API终点

端点 方法 身份验证 描述
/health GET 否 健康检查
/servers GET 否 列出已注册的MCP服务器
/config GET 否 列出可用的客户端配置
/config/{client} GET 否 获取特定于客户端的配置
/ui GET 否 Web用户界面
/mcp POST 是 MCP JSON-RPC端点
/mcp/sse GET 是 服务器发送的事件流
/v1/mcp POST 是 版本化MCP端点
/v1/mcp/sse GET 是 版本化SSE端点

客户端配置

Colloid为流行的MCP客户端提供自动生成的配置:

克劳德桌面

curl http://localhost:3001/config/claude-desktop

退货:

{
  "mcpServers": {
    "colloid": {
      "url": "http://localhost:3001/mcp/sse",
      "transport": "sse"
    }
  }
}

光标

curl http://localhost:3001/config/cursor

工具名称间距

服务器 原始工具 网关工具
github create_issue github:create_issue
松弛 send_message slack:send_message
数据库 query database:query

安全

API关键要求

  • 最少32个字符(256位熵)
  • 持续的时间比较,防止计时攻击
  • 在生产过程中,需要API密钥(无默认密钥)

安全标头

所有响应都包含安全标头:

  • X-Content-Type-Options: nosniff
  • X-Frame-Options: DENY
  • X-XSS-Protection: 1; mode=block
  • Content-Security-Policy: default-src 'none'
  • Cache-Control: no-store

SSRF保护

远程运行时验证URL以防止SSRF攻击:

  • 阻止私有IP地址(10.x、192.168.x、127.x等)
  • 阻止云元数据端点(169.254.169.254)
  • 只允许http/https方案

请求限制

  • 最大机身尺寸:1MB
  • 速率限制:可配置每个用户的限制

工作流

将多个工具调用链接到自动化工作流程中:

curl -X POST http://localhost:3001/mcp \
  -H "Content-Type: application/json" \
  -H "X-API-Key: your-api-key-at-least-32-characters-long" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "workflows/list"
  }'

环境变量

变量 描述 默认值
RUST_LOG 日志级别筛选器 colloid=info
CONFIG_PATH 配置文件路径 config.yaml

文档

许可证

Apache许可证2.0

参考文献