MCP Gateway
MCP 网关
一个位于 Claude 和你的 MCP 服务器之间的懒加载代理。它不会在启动时加载每个服务器(这会将数百个工具模式转储到上下文中并消耗大量 Token),而是仅暴露 4 个轻量级工具。后端服务器仅在你真正需要时才会启动。
之前: 10 个 MCP 服务器 = 200 多个工具模式加载到每次对话中 = 浪费数千个 Token。
之后: 网关后的 10 个 MCP 服务器 = 加载 4 个工具模式。每个服务器按需启动。
问题所在
你添加到 Claude Code 的每个 MCP 服务器都会预先注册其所有工具。典型的服务器有 10-30 个工具,每个工具都有完整的 JSON 模式。如果有 10 个服务器,在你提问之前,就有 100-300 个工具定义占用了你的上下文窗口。
大多数对话只使用 1-2 个服务器。其余的都是累赘。
Related MCP server: MCP Gateway
工作原理
网关向 Claude 暴露了 4 个工具:
工具 | 功能 |
| 显示可用服务器及其状态 |
| 连接到服务器并发现其工具 |
| 调用已连接服务器上的工具 |
| 重新连接服务器(获取代码更改) |
当 Claude 需要服务器时,它会调用 gateway_load_server。网关启动子进程,执行 MCP 握手,并缓存连接。后续调用将重用正在运行的进程。
未使用的服务器永远不会启动。不会浪费 Token。
快速开始
git clone https://github.com/raiansar/mcp-gateway.git
cd mcp-gateway
./install.sh编辑 config.json 以添加你的服务器,然后将网关添加到 Claude Code:
claude mcp add gateway -- /path/to/mcp-gateway/run.sh就是这样。你所有的服务器现在都位于单个网关之后。
配置
config.json 是服务器名称与其连接详细信息的简单映射。网关支持 stdio(本地进程)和 HTTP(远程服务器)传输。
Stdio 服务器(本地)
{
"servers": {
"my-server": {
"type": "stdio",
"command": "npx",
"args": ["-y", "some-mcp-server@latest"],
"env": {
"API_KEY": "your-key"
},
"timeout": 30,
"description": "What this server does"
}
}
}HTTP 服务器(远程)
{
"servers": {
"remote-server": {
"type": "http",
"url": "https://mcp.example.com/mcp",
"headers": {
"Authorization": "Bearer your-token"
},
"timeout": 60,
"description": "Remote MCP server"
}
}
}Python 服务器 (uv)
{
"servers": {
"my-python-server": {
"type": "stdio",
"command": "uv",
"args": ["run", "--directory", "/path/to/server", "server-name"],
"env": {},
"timeout": 120,
"description": "Python server managed by uv"
}
}
}配置字段
字段 | 必需 | 默认 | 描述 |
| 否 |
| 传输方式: |
| 是 (stdio) | - | 运行服务器的命令 |
| 否 |
| 命令参数 |
| 否 |
| 环境变量 |
| 是 (http) | - | 服务器 URL |
| 否 |
| HTTP 请求头(认证 Token 等) |
| 否 | 30/60 | 请求超时时间(秒)(stdio 为 30,http 为 60) |
| 否 | - | 在 |
迁移现有的 MCP 服务器
如果你已经在 Claude Code 中配置了 MCP 服务器,请将它们迁移到网关:
之前(在 ~/.claude.json 或 Claude Desktop 配置中):
{
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_xxx" }
},
"tavily": {
"command": "npx",
"args": ["-y", "tavily-mcp@latest"],
"env": { "TAVILY_API_KEY": "tvly-xxx" }
},
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/user"],
"env": {}
}
}
}之后(在 config.json 中):
{
"servers": {
"github": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_xxx" },
"description": "GitHub - repos, issues, PRs, code search"
},
"tavily": {
"type": "stdio",
"command": "npx",
"args": ["-y", "tavily-mcp@latest"],
"env": { "TAVILY_API_KEY": "tvly-xxx" },
"description": "Tavily AI search"
},
"filesystem": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/user"],
"env": {},
"description": "File system access"
}
}
}然后从 Claude 中删除单个服务器,仅添加网关:
claude mcp remove github -s user
claude mcp remove tavily -s user
claude mcp remove filesystem -s user
claude mcp add gateway -- /path/to/mcp-gateway/run.sh使用方法
配置完成后,Claude 会自动使用网关。典型的交互流程:
Claude 调用
gateway_list_servers查看可用内容当需要 GitHub 时,Claude 调用
gateway_load_server("github")Claude 调用
gateway_call_tool("github", "search_repositories", '{"query": "mcp"}')来使用工具GitHub 服务器在同一会话的后续调用中保持运行状态
配置中的 description 字段有助于 Claude 决定针对特定任务加载哪个服务器,因此请编写良好的描述。
与 RTK 的区别
RTK 是一个 CLI 代理,它压缩 shell 命令输出(git, ls, 测试运行器等),以减少 60-90% 的 Token 消耗。
MCP 网关解决了不同的问题:它通过按需懒加载服务器而不是预先注册所有工具,从而防止 MCP 工具模式膨胀。
MCP 网关 | RTK | |
问题 | 空闲 MCP 服务器的工具模式浪费上下文 | 冗长的 CLI 输出浪费上下文 |
方式 | 懒加载服务器,暴露 4 个代理工具 | 在进入上下文之前压缩命令输出 |
时机 | 启动 / 工具注册阶段 | 运行时 / 命令执行阶段 |
范围 | MCP 服务器管理 | Shell 命令 (git, npm, cargo 等) |
它们是互补的。同时使用两者可实现最大的 Token 节省。
要求
Python 3.10+
mcp包(由install.sh安装)
许可证
MIT
This server cannot be deployed
Maintenance
Related MCP Connectors
The OpenRouter for tools. One MCP connection gives any AI agent 254 hosted tools, pay per call.
Nifty's MCP server — exposes tasks, projects, messages, and files as tools for AI agents.
Zero-setup MCP gateway securely connecting AI to your tools with authentication and workflows
Gateway between LLM agents and world data through eight tools and a bundled endpoint catalog.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceActs as a proxy for multiple MCP servers, reducing context window usage from 15,000+ tokens to ~500 tokens by dynamically loading servers on-demand and exposing only 3 tools instead of all tool definitions.5GPL 3.0
- FlicenseAqualityCmaintenanceAggregates multiple MCP servers into a single gateway with unified top-level tools, reducing LLM context usage and enabling IDE compatibility by consolidating many tools into fewer interface functions.417-
- AlicenseNot gradedqualityCmaintenanceAggregates multiple Model Context Protocol servers into a single gateway to provide unified search, description, and execution of tools. It reduces context limit issues by dynamically fetching specific tool schemas only when needed rather than loading all available tools at once.6 npm23MIT
- AlicenseNot gradedqualityAmaintenanceAggregates tools from multiple upstream MCP servers and exposes them through 4 meta-tools, enabling LLMs to discover and use hundreds of tools without loading all schemas upfront.2Apache 2.0