使用说明
MCP代理
一种用于模型上下文协议的高性能安全网关
为什么选择MCP代理?
随着人工智能代理获得更强大的工具, 安全变得至关重要.MCP Proxy位于您的AI代理和MCP服务器之间,提供:
🛡️ 策略强制使用OPA策略进行细粒度访问控制。准确定义每个代理可以访问哪些工具以及在什么条件下访问。 |
📋 审计日志完成每次工具调用的审计跟踪。确切地知道你的代理人在做什么,什么时候做,为什么做。 |
⚡ 高性能亚微秒路由延迟。你的特工甚至不会注意到它的存在。 |
📊 可观测性Prometheus指标和健康终点。全面了解您的AI基础设施。 |
✨ 特性
| 特性 | 描述 |
|---|---|
| OPA策略引擎 | 在Rego中编写策略以实现细粒度的访问控制 |
| SQLite审计存储 | 具有可配置保留的持久、可查询的审计日志 |
| 苏格兰和南方能源公司运输 | 用于实时双向通信的服务器发送事件 |
| 会话管理 | 具有TTL和连接限制的安全会话处理 |
| 普罗米修斯指标 | 请求延迟、策略决策、错误率 |
| 健康检查 | Kubernetes就绪活性和就绪性探针 |
| TLS支持 | 用于生产部署的加密通信 |
🚀 快速开始
选项1:去安装
go install github.com/agentfacts/mcp-proxy/cmd/proxy@latest
选项2:从源代码构建
git clone https://github.com/agentfacts/mcp-proxy.git
cd mcp-proxy
make build
./bin/mcp-proxy --config config/proxy.yaml
选项3:Docker
docker run -p 3000:3000 ghcr.io/agentfacts/mcp-proxy:latest
Docker Compose (Development Stack)
# Start PostgreSQL, Redis, and supporting services
make docker-up
# Or with the full stack including the proxy
docker-compose --profile full up -d
⚙️ 配置
创建一个 config.yaml 文件:
version: "1.0"
server:
listen:
address: "0.0.0.0"
port: 3000
transport: "sse"
upstream:
url: "http://your-mcp-server:8080"
transport: "sse"
policy:
enabled: true
mode: "enforce" # "audit" to log only, "enforce" to block
policy_dir: "policies"
audit:
enabled: true
db_path: "audit.db"
Environment Variables
所有配置都可以通过环境变量覆盖:
| 变量 | 描述 | 默认值 |
|---|---|---|
MCP_SERVER_PORT |
代理监听端口 | 3000 |
MCP_UPSTREAM_URL |
上游MCP服务器URL | - |
MCP_POLICY_MODE |
策略模式(审核/执行) | enforce |
MCP_POLICY_ENABLED |
启用策略引擎 | true |
MCP_AUDIT_ENABLED |
启用审核日志记录 | true |
MCP_LOGGING_LEVEL |
日志级别(调试/信息/警告/错误) | info |
MCP_METRICS_ENABLED |
启用Prometheus指标 | false |
MCP_HEALTH_ENABLED |
启用健康端点 | false |
Full Configuration Reference
看 config/proxy.yaml 查看包含所有可用选项的完整配置示例。
📜 制定政策
政策是写在 雷戈,OPA的声明性策略语言。
示例:限制文件系统访问
package mcp.policy
default allow = false
# Allow read operations for agents with read capability
allow {
input.method == "tools/call"
startswith(input.tool, "read_")
has_capability("read:*")
}
# Block write operations to sensitive directories
deny {
input.method == "tools/call"
input.tool == "write_file"
startswith(input.arguments.path, "/etc/")
}
# Helper: check if agent has capability
has_capability(cap) {
input.agent.capabilities[_] == cap
}
示例:速率限制
package mcp.policy
# Limit to 100 requests per session
deny {
input.session.request_count > 100
}
看 政策/ 更多示例。
📈 演出
MCP Proxy旨在将开销降至最低:
| 操作 | 延迟 | 分配 |
|---|---|---|
| 路由消息 | 约1µs 26个位置 | |
| 解析请求 | 约337纳秒 5个位置。 | |
| 生成请求ID | 约87纳秒 1 alloc。 |
Run Benchmarks
go test -bench=. -benchmem ./internal/router/...
📊 可观测性
度量(普罗米修斯)
启用 metrics.enabled: true:
curl http://localhost:9090/metrics
可用指标:
mcp_proxy_requests_total-按方法和状态列出的请求总数mcp_proxy_request_duration_seconds-请求延迟直方图mcp_proxy_policy_decisions_total-按结果分列的政策决定mcp_proxy_active_sessions-当前活动会话
健康检查
启用 health.enabled: true:
# Liveness probe
curl http://localhost:8080/health
# Readiness probe
curl http://localhost:8080/ready
🗂️ 项目结构
mcp-proxy/
├── cmd/proxy/ # Application entry point
├── config/ # Configuration files
├── docs/ # Documentation
├── internal/
│ ├── audit/ # SQLite audit store
│ ├── config/ # Configuration loading
│ ├── observability/ # Metrics & health checks
│ ├── policy/ # OPA policy engine
│ ├── router/ # Message routing
│ ├── session/ # Session management
│ ├── transport/sse/ # SSE transport
│ └── upstream/ # Upstream client
├── policies/ # Example Rego policies
└── scripts/ # Utility scripts
📚 文档
| 文档 | 描述 |
|---|---|
| 安装指南 | 详细的安装和配置 |
| 贡献 | 如何做出贡献 |
| 更新日志 | 版本历史 |
| 安全 | 安全策略 |
🗺️ 路线图
- [x] 苏格兰和南方能源公司运输支持
- [x] OPA策略引擎
- [x] SQLite审计日志记录
- [x] 普罗米修斯指标
- []STDIO传输
- []HTTP传输
- []WebSocket传输
- []代理行为验证
- []策略热重新加载
- []管理员API
- []Web仪表板
🤝 贡献
我们欢迎捐款!请查看我们的 贡献指南.
# Clone the repo
git clone https://github.com/agentfacts/mcp-proxy.git
cd mcp-proxy
# Install dependencies
go mod download
# Run tests
make test
# Run linter
make lint
📄 许可证
MIT许可证-请参阅 许可证 了解详情。
由...制作❤️ 通过 代理行为
