MCP Gateway
One connector. Total visibility. Secure tool access.
A self-hosted MCP-native aggregation, routing, and security layer for desktop AI clients and agentic workflows. Connect all your MCP servers behind a single, centrally managed endpoint with full audit trails, RBAC, and policy enforcement.
演示
https://github.com/user-attachments/assets/d34467d1-8485-45d3-847f-7f9274142f7f
建筑
MCP网关是一个三组件系统:
┌─────────────────────┐ ┌─────────────────────────────────────────┐
│ AI Client │ │ MCP Gateway Server (Rust/Axum) │
│ (Claude, Cursor, │────▶│ │
│ etc.) │ MCP │ ┌─────────┐ ┌────────┐ ┌───────────┐ │
└─────────────────────┘ │ │ Router │ │ Policy │ │ Audit │ │
│ │ & Tools │ │ Engine │ │ Recorder │ │
│ └────┬─────┘ └────────┘ └───────────┘ │
│ │ │
│ ┌────┴──────────────────────────────┐ │
│ │ Backend Manager │ │
│ │ stdio | http | sse | agent(ws) │ │
│ └──┬─────────┬──────────┬───────────┘ │
└─────┼─────────┼──────────┼──────────────┘
│ │ │
┌─────┴──┐ ┌────┴───┐ ┌───┴──────────┐
│ Local │ │ Remote │ │ MCP Gateway │
│ stdio │ │ HTTP │ │ Agent (WS) │
│ MCP │ │ MCP │ │ │
│ Server │ │ Server │ │ ┌──────────┐ │
└────────┘ └────────┘ │ │local MCP │ │
│ │servers │ │
│ └──────────┘ │
└──────────────┘组件
| 部件 | 技术 | 说明 |
|---|---|---|
| mcp网关服务器 | Rust、Axum、PostgreSQL | 核心网关——MCP协议路由、认证、策略执行、审计、指标 |
| mcp网关仪表板 | React、TypeScript、Vite | 管理UI——工具清单、审计时间线、指标图表、用户/策略管理 |
| mcp网关代理 | Rust,ratatui TUI | 远程代理--通过WebSocket将本地MCP服务器连接到网关 |
| PostgreSQL | PostgreSQL 16 | 用户、后端、工具、审计事件、策略的持久存储 |
快速开始
1.启动网关
git clone https://github.com/SidPad03/unified-mcp-gateway.git
cd unified-mcp-gateway
# Start all services (server + dashboard + postgres)
docker compose up --build这将启动三个容器:
- PostgreSQL 在端口5432上
- MCP网关服务器 在端口3200上
- 仪表盘 在端口8080上
2.登录仪表板
打开http://localhost:8080并使用默认凭据登录: admin / admin.
立即更改管理员密码 --默认设置仅适用于初始设置。
3.添加MCP后端
在仪表板上 后端配置 页面,添加后端。例如,要添加GitHub MCP服务器:
| 字段 | 值 |
|---|---|
| 姓名 | github |
| 运输 | stdio |
| 指挥部 | npx |
| Args | -y @modelcontextprotocol/server-github |
| Env | GITHUB_TOKEN=ghp_your_token |
网关将启动后端并自动注册其工具。
4.连接您的AI客户端
将您的MCP客户端(Claude Desktop、Cursor等)指向网关的MCP端点:
// Claude Desktop config (~/.claude/claude_desktop_config.json)
{
"mcpServers": {
"gateway": {
"url": "http://localhost:3200/mcp",
"headers": {
"Authorization": "Bearer "
}
}
}
}从仪表板的生成API密钥 设置 页面。所有后端工具现在都可以通过这个端点使用。
生产部署
对于生产,设置一个强JWT密钥和数据库密码:
JWT_SECRET=$(openssl rand -hex 32)
POSTGRES_PASSWORD=$(openssl rand -hex 16)
docker compose up -d \
-e JWT_SECRET="$JWT_SECRET" \
-e POSTGRES_PASSWORD="$POSTGRES_PASSWORD" \
-e DATABASE_URL="postgresql://mcpgateway:${POSTGRES_PASSWORD}@postgres:5432/mcpgateway"或者创建一个 .env 文件(未提交到git):
JWT_SECRET=your-strong-random-secret
POSTGRES_PASSWORD=your-strong-db-password
DATABASE_URL=postgresql://mcpgateway:your-strong-db-password@postgres:5432/mcpgateway在生产环境中,始终部署在TLS终止的反向代理(nginx、Caddy等)后面。
远程代理
这 MCP网关代理 允许您通过单个经过身份验证的WebSocket将远程机器(笔记本电脑、开发设备、家庭服务器)上运行的MCP服务器连接到网关。网关看到代理的本地MCP服务器,就像它们在服务器本身上运行一样。
安装代理
macOS/Linux:
curl -fsSL https://raw.githubusercontent.com/SidPad03/unified-mcp-gateway/main/install.sh | bashWindows(PowerShell):
irm https://raw.githubusercontent.com/SidPad03/unified-mcp-gateway/main/install.ps1 | iex安装程序会下载适用于您平台的正确二进制文件,并将其放入 ~/.mcp-gateway-agent/bin/,并将其添加到您的 PATH.
配置代理
运行交互式安装向导:
mcp-gateway-agent setup这将引导您输入网关URL、API密钥,并添加本地MCP后端。配置已保存到 ~/.mcp-gateway-agent/config.toml.
您还可以直接编辑配置文件。下面是一个有三个后端的示例:
[agent]
agent_id = "my-macbook"
gateway_url = "wss://mcp-gateway.example.com/agent/ws"
api_key = "mcpgw_YOUR_API_KEY_HERE"
dashboard_url = "https://mcp-gateway.example.com"
tls_skip_verify = false # only set true for self-signed certs in dev
# A stdio backend — the agent spawns this process and talks JSON-RPC over stdin/stdout
[[backends]]
name = "playwright"
transport = "stdio"
command = "npx"
args = ["@playwright/mcp@latest"]
# Another stdio backend with environment variables
[[backends]]
name = "github"
transport = "stdio"
command = "npx"
args = ["-y", "@modelcontextprotocol/server-github"]
[backends.env]
GITHUB_TOKEN = "ghp_your_token_here"
# An HTTP backend — the agent connects to an already-running MCP server
[[backends]]
name = "obsidian"
transport = "stdio"
command = "npx"
args = ["obsidian-mcp-server"]
[backends.env]
OBSIDIAN_API_KEY = "your_obsidian_api_key"
OBSIDIAN_BASE_URL = "http://localhost:27123/"运行代理
# Start with the live TUI dashboard
mcp-gateway-agent run
# Or run in the background as a system service
mcp-gateway-agent service install
mcp-gateway-agent service startTUI仪表板实时显示连接状态、注册的工具、最近的工具调用和日志。按 q 退出, s 为了重新运行安装程序, u 以检查更新。
代理命令
| 命令 | 描述 |
|---|---|
mcp-gateway-agent setup | 交互式设置向导 |
mcp-gateway-agent run | 使用实时TUI连接到网关 |
mcp-gateway-agent dashboard | 仅打开TUI仪表板 |
mcp-gateway-agent update | 检查并安装更新 |
mcp-gateway-agent service install | 作为后台服务安装(launchd/systemd/Task Scheduler) |
mcp-gateway-agent service start | 启动后台服务 |
mcp-gateway-agent service stop | 停止后台服务 |
mcp-gateway-agent service status | 检查服务状态 |
mcp-gateway-agent logs | 跟踪代理日志文件 |
mcp-gateway-agent version | 打印版本 |
运作原理
- 代理通过WebSocket连接到网关(
/agent/ws) - 它从所有本地后端(stdio和HTTP)发现工具
- 它以代理的名称向网关注册这些工具
- 当AI客户端调用工具时,网关通过WebSocket将请求路由到代理
- 代理将调用转发到正确的本地后端并返回结果
所有工具调用都要经过网关的策略引擎、RBAC和审计日志记录,即使是远程代理工具也是如此。
特性
MCP聚合和路由
- 在单个端点后面连接多个MCP后端
- 支持 标准, 可流式传输http, 上海证券交易所,以及 代理 (WebSocket)传输
- 自动工具名称间距:
{backend}__{tool}具有碰撞分辨率 - 集中式工具注册表,每个工具启用/禁用
安全和访问控制
- JWT+neneneba API密钥 身份验证(API密钥使用
mcpgw_前缀,SHA-256散列) - 基于角色的访问控制 --具有工具级权限的所有者、操作员和查看者角色
- 策略引擎 --具有全局模式、风险类别和每个应用程序匹配的优先级排序允许/拒绝规则
- 风险分类 --工具自动分类为
read,write,admin,或external-api - 审计日志 --每个工具调用都记录了可配置的编校
可观测性
- 普罗米修斯指标 在
/metrics--呼叫计数、延迟直方图、错误率、后端运行状况 - 指标仪表板 带有数量、延迟和每个工具细分的图表
- 使用情况图 时间序列分析
远程代理系统
- mcp网关代理 二进制文件在远程计算机上运行
- 通过经过身份验证的WebSocket将本地MCP服务器(stdio/http)连接到网关
- TUI仪表板,带有实时连接状态、工具调用跟踪和日志
- 通过指数回退自动重新连接
- 通过网关的发布代理实现自我更新机制
- macOS启动后台操作服务管理
仪表板页面
| 第页 | 描述 |
|---|---|
| 工具清单 | 所有带有搜索、风险徽章、启用/禁用的聚合工具 |
| 审计时间线 | 按时间顺序排列的事件提要,包含深入查看的详细信息 |
| 指标概述 | 呼叫量、延迟、错误率、后端运行状况图表 |
| 使用图 | 时间序列使用分析 |
| 后端配置 | 带健康指示器的MCP服务器管理 |
| 策略编辑器 | 使用条件生成器进行安全规则管理 |
| 用户管理 | 带角色分配的用户CRUD |
| 设置 | API密钥,系统配置 |
API 参考
以下所有端点 /api/v1.通过验证 Authorization: Bearer .
| 方法 | 端点 | 描述 |
|---|---|---|
| 职位 | /auth/login | 身份验证,返回JWT |
| 职位 | /auth/refresh | 刷新JWT令牌 |
| 得到 | /tools | 列出所有工具 |
| 补丁 | /tools/{id} | 启用/禁用工具 |
| 得到 | /backends | 列出健康状况的后端 |
| 职位 | /backends | 添加后端 |
| 放置/删除 | /backends/{id} | 更新/删除后端 |
| 得到 | /audit | 查询审核事件 |
| 得到 | /audit/stats | 汇总审计统计数据 |
| 得到 | /metrics/summary | 指标仪表板数据 |
| 得到 | /usage/* | 使用情况分析 |
| 获取/发布 | /users | 用户管理 |
| 获取/发布 | /roles | 角色管理 |
| 获取/发布/放置/删除 | /policies | 策略CRUD |
| 获取/发布/删除 | /api-keys | API密钥管理 |
| 得到 | /agent/releases/* | 代理发布代理 |
MCP端点
| 端点 | 描述 |
|---|---|
职位 /mcp | 流式HTTP MCP端点 |
得到 /sse | SSE MCP传输 |
WS /agent/ws | 代理WebSocket连接 |
环境变量
服务器
| 变量 | 默认值 | 描述 |
|---|---|---|
DATABASE_URL | postgresql://mcpgateway:mcpgateway@localhost:5432/mcpgateway | PostgreSQL连接字符串 |
JWT_SECRET | mcpgw-dev-secret-change-in-production | JWT签名秘密 |
LISTEN_ADDR | 0.0.0.0:3200 | 服务器侦听地址 |
RUST_LOG | mcp_gateway_server=info,tower_http=debug | 日志级别筛选器 |
RELEASE_PROXY_URL | -- | Git伪造代理发布代理的URL(例如Gitea、GitHub) |
RELEASE_PROXY_REPO | -- | 代理发布库(例如。, owner/unified-mcp-gateway) |
RELEASE_PROXY_TOKEN | - | 用于发布代理身份验证的API令牌(同时读取 GITEA_TOKEN) |
发展
# Backend (requires Rust + PostgreSQL)
cd mcp-gateway-server
cargo run
# Dashboard (requires Node.js)
cd mcp-gateway-dashboard
npm install
npm run dev
# Agent
cd mcp-gateway-agent
cargo run -- setup # interactive setup wizard
cargo run -- run # connect to gateway部署
该项目包括通过GitHub Actions发布的CI/CD:
- 服务器+仪表板:构建Docker镜像并推送到GHCR
main推 - 代理:通过以下方式为macOS、Linux和Windows进行交叉编译
cargo-zigbuild,发布为GitHub版本
看 贡献.md 有关本地开发设置和部署的详细信息。
安全
请看 安全.md 有关报告漏洞和安全考虑的信息。
贡献
看 贡献.md 了解为该项目做出贡献的指导方针。
许可证
Apache 2.0
