使用说明
MCP网关
用于模型上下文协议(MCP)服务器的集中式API网关。MCP网关使LLM客户端能够通过具有统一身份验证、会话管理和速率限制的单个端点连接到多个MCP服务器。
特性
- 基于命名空间的路由:根据命名空间将请求路由到不同的MCP服务器
- API密钥验证:使用可配置的API密钥和命名空间限制进行安全访问
- 工具过滤:允许或拒绝每个命名空间的特定工具
- 请求日志记录:使用DuckDB跟踪所有MCP请求以进行分析
- 分析API:查询使用统计数据、工具流行度和时间序列数据
- SSE流媒体支持:完全支持用于流式响应的服务器发送事件
快速开始
先决条件
- 转到1.21或更高版本
- DuckDB(嵌入式,无需单独安装)
安装
# Clone the repository
git clone https://github.com/sekoudoumbouya/mcp-gateway.git
cd mcp-gateway
# Install dependencies
go mod download
# Build the gateway
go build -o mcp-gateway ./cmd/gateway
运行网关
# Start with default config
./mcp-gateway
# Start with custom config file
./mcp-gateway -config /path/to/config.yaml
网关将启动 http://localhost:8080 默认情况下。
配置
创建 config.yaml 文件:
server:
host: "0.0.0.0"
port: 8080
database:
path: "gateway.duckdb"
# Namespace to MCP server mappings
namespaces:
filesystem:
url: "http://localhost:3001/mcp"
description: "Filesystem operations"
# Optional: restrict which tools are allowed
allowed_tools:
- "read_file"
- "list_directory"
search:
url: "http://localhost:3002/mcp"
description: "Web search and browsing"
database:
url: "http://localhost:3003/mcp"
description: "Database operations"
# Authentication
auth:
enabled: true
api_keys:
"dev-key-12345":
user_id: "developer"
description: "Development key"
# Empty namespaces = access to all
"prod-key-67890":
user_id: "production"
namespaces:
- "filesystem"
- "database"
"search-only":
user_id: "search-user"
namespaces:
- "search"
# Rate limiting
rate_limit:
enabled: true
requests_per_minute: 100
burst_size: 10
环境变量
用环境变量覆盖配置:
| 变量 | 描述 |
|---|---|
GATEWAY_HOST |
服务器监听主机 |
GATEWAY_PORT |
服务器侦听端口 |
GATEWAY_DB_PATH |
DuckDB数据库路径 |
配置值中也支持环境变量扩展:
database:
path: "${DATABASE_PATH}"
api参考
健康检查
GET /health
返回网关运行状况。
答复:
{
"status": "healthy",
"time": "2024-01-15T10:30:00Z"
}
列出命名空间
GET /namespaces
返回所有已配置的命名空间。
答复:
{
"namespaces": [
{
"name": "filesystem",
"description": "Filesystem operations"
},
{
"name": "search",
"description": "Web search and browsing"
}
]
}
MCP端点
POST /mcp/:namespace
Authorization: Bearer <api-key>
Content-Type: application/json
将JSON-RPC 2.0请求转发到指定命名空间的MCP服务器。
请求:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list"
}
答复:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [...]
}
}
对于流式响应,请添加 Accept: text/event-stream 头球
分析API
获取使用统计信息
GET /analytics/usage?period=7d&group_by=user
Authorization: Bearer <api-key>
参数:
period:时间段(例如。,7d,24h,1h,30m)group_by:分组方式user(默认)或tool
答复:
{
"period": "168h0m0s",
"results": [
{
"user_id": "developer",
"namespace": "filesystem",
"request_count": 150,
"error_count": 2,
"avg_latency_ms": 45.5,
"total_request_bytes": 15000,
"total_response_bytes": 75000
}
]
}
获取用户统计信息
GET /analytics/usage/:user_id?period=24h
Authorization: Bearer <api-key>
返回特定用户的详细统计信息。
获得工具流行度
GET /analytics/tools?period=7d&limit=20
Authorization: Bearer <api-key>
答复:
{
"period": "168h0m0s",
"tools": [
{
"tool_name": "read_file",
"namespace": "filesystem",
"invocations": 500,
"unique_users": 10,
"avg_latency_ms": 25.3,
"error_rate": 0.02
}
]
}
获取时间序列数据
GET /analytics/timeseries?period=24h&bucket=1h
Authorization: Bearer <api-key>
答复:
{
"period": "24h0m0s",
"bucket": "1h0m0s",
"data": [
{
"timestamp": "2024-01-15T09:00:00Z",
"value": 45
},
{
"timestamp": "2024-01-15T10:00:00Z",
"value": 62
}
]
}
建筑
┌─────────────────────────────────────────────────────────────┐
│ LLM Clients │
│ (Claude, GPT, etc. with MCP config) │
└─────────────────────────┬───────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ MCP Gateway │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │
│ │ Auth │ │ Router │ │ Event Logger │ │
│ │ Middleware │ │ (Namespace) │ │ (DuckDB) │ │
│ └─────────────┘ └─────────────┘ └─────────────────────┘ │
│ ┌─────────────────────────────────────────────────────────┐│
│ │ MCP Handler ││
│ │ - HTTP/JSON-RPC proxy ││
│ │ - SSE streaming support ││
│ │ - Tool filtering ││
│ └─────────────────────────────────────────────────────────┘│
└─────────────────────────┬───────────────────────────────────┘
│
┌───────────────┼───────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ MCP │ │ MCP │ │ MCP │
│ Server 1 │ │ Server 2 │ │ Server N │
└──────────┘ └──────────┘ └──────────┘
发展
运行测试
# Run all unit tests
go test ./...
# Run with verbose output
go test -v ./...
# Run integration tests
go test -tags=integration ./...
# Run with coverage
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
项目结构
mcp-gateway/
├── cmd/
│ └── gateway/
│ └── main.go # Application entry point
├── internal/
│ ├── config/
│ │ ├── config.go # Configuration loading and validation
│ │ └── config_test.go
│ ├── auth/
│ │ ├── apikey.go # API key authentication middleware
│ │ └── apikey_test.go
│ ├── gateway/
│ │ ├── router.go # Namespace routing
│ │ ├── router_test.go
│ │ ├── handler.go # MCP request handler
│ │ └── handler_test.go
│ ├── db/
│ │ ├── duckdb.go # Database layer
│ │ ├── events.go # Event logging
│ │ └── db_test.go
│ ├── analytics/
│ │ ├── queries.go # Analytics query service
│ │ └── queries_test.go
│ └── api/
│ ├── analytics.go # Analytics REST API
│ └── analytics_test.go
├── testdata/ # Test fixtures
│ ├── config_valid.yaml
│ ├── config_minimal.yaml
│ └── config_invalid_*.yaml
├── integration_test.go # Integration tests
├── config.yaml # Default configuration
├── go.mod
└── README.md
错误代码
网关使用JSON-RPC 2.0错误代码:
| 代码 | 消息 |
|---|---|
| -32700 | 分析错误-JSON无效 |
| -32600 | 无效请求-缺少授权,访问被拒绝 |
| -32601 | 找不到方法 |
| -32603 | 内部错误-无法连接到MCP服务器 |
许可证
MIT许可证
