使用说明
MCP网关
一个轻量级的MCP(模型上下文协议)网关,按需将请求路由到下游MCP服务器,通过仅公开3个通用工具而不是预先从所有服务器加载所有工具来解决上下文窗口饱和问题。
问题已解决
无网关:每个MCP服务器向客户端注册其工具,为每个工具模式使用上下文令牌。
使用Gateway:仅注册了3个网关工具(discover, dispatch, close).下游服务器工具按需加载。
建筑
Client (Claude Code/Cursor/etc)
↓
MCP Gateway (3 tools only)
↓
Downstream Servers (loaded on-demand)
├── Local (stdio)
│ - llm-memory
│ - code-trm
│ - codex
│ - code-analysis
└── Remote (http/sse)
- nuxt-ui-remote
- example-sse-server
自动安装(推荐)
安装网关和可选下游MCP服务器的最简单方法:
./install.sh
此脚本将:
- 构建网关
- 配置Claude Code以使用网关
- 检查父目录中的可用MCP服务器
- 提供克隆、安装和配置任何缺失的服务器:
- llm_memory_mcp
- codex_mcp
- 代码分析上下文mcp
- code_trm_mcp
- code_trm_python_mcp
- 代码分析上下文python-mcp
- 词人_mcp
安装后,重新启动Claude Code以加载网关。
卸载
要从Claude Code配置中删除网关:
./uninstall.sh
备注:这只会从Claude Code配置中删除网关。网关目录和下游服务器保持不变。
手动安装
# Install dependencies
npm install
# Build
npm run build
# Run in production
npm start
# Or run in development mode
npm run dev
配置
在中配置下游服务器 registry.config.json (不支持git):
本地服务器(stdio)
{
"id": "server-id",
"kind": "stdio",
"command": "node",
"args": ["/path/to/server"],
"connectTimeoutMs": 8000,
"idleTtlMs": 300000
}
远程服务器(HTTP/SSE)
网关通过HTTP(带SSE回退的流式HTTP)和SSE传输支持远程MCP服务器:
{
"id": "nuxt-ui-remote",
"kind": "http",
"url": "https://ui.nuxt.com/mcp",
"remote": true,
"connectTimeoutMs": 10000,
"idleTtlMs": 600000
}
{
"id": "example-sse-server",
"kind": "sse",
"url": "https://example.com/mcp/sse",
"remote": true,
"connectTimeoutMs": 10000,
"idleTtlMs": 300000
}
运输类型:
stdio:本地进程通信(默认)http:具有自动SSE回退功能的流式HTTP传输(建议用于远程)sse:服务器发送事件传输(适用于仅支持SSE的服务器)
首次设置:复制 registry.config.example.json 到 registry.config.json 并更新路径/URL以匹配您的环境。
网关工具
1. discover(serverId)
从目标服务器返回元数据和工具模式,而不进行注册。
2. dispatch(serverId, tool, args)
调用目标服务器上的工具。使用 discover 首先查看可用的工具。
3. close(serverId)
手动关闭服务器连接并将其从缓存中删除。
用法示例
本地服务器(stdio)
// 1. Discover what tools are available
discover({ serverId: "llm-memory" })
// 2. Call a tool with the correct arguments
dispatch({
serverId: "llm-memory",
tool: "mem.upsert",
args: {
type: "note",
scope: "local",
text: "My note"
}
})
// 3. Optionally close the connection when done
close({ serverId: "llm-memory" })
远程服务器(HTTP/SSE)
// 1. Discover tools from a remote MCP server
discover({ serverId: "nuxt-ui-remote" })
// 2. Call a tool on the remote server
dispatch({
serverId: "nuxt-ui-remote",
tool: "get_component",
args: {
name: "Button"
}
})
// 3. Close the remote connection when done
close({ serverId: "nuxt-ui-remote" })
从客户端的角度来看,远程服务器与本地服务器的工作方式相同。网关在内部处理传输差异。
连接管理
- 按需加载:服务器仅在首次访问时生成
- 连接缓存:可重用活动连接以提高性能
- 自动清理:空闲连接在5分钟后关闭
- 超时保护:120秒后工具调用超时
环境变量
GATEWAY_LOG_LEVEL:控制日志的详细程度debug:详细的连接和操作日志info:仅启动和错误(默认)silent:无日志
添加自定义服务器
-
创建
registry.config.json如果它不存在:cp registry.config.example.json registry.config.json -
将您的服务器配置添加到JSON数组中:
对于本地服务器(stdio):
{ "id": "my-server", "kind": "stdio", "command": "node", "args": ["/absolute/path/to/server.js"], "connectTimeoutMs": 8000, "idleTtlMs": 300000 }用于远程服务器(http/sse):
{ "id": "my-remote-server", "kind": "http", "url": "https://api.example.com/mcp", "remote": true, "connectTimeoutMs": 10000, "idleTtlMs": 600000 } -
对于本地服务器,确保构建了下游服务器:
cd /path/to/downstream/server && npm run build -
重建并重新启动网关:
npm run build npm start
客户端配置
添加到您的MCP客户端配置中(例如,Claude Code、Cursor):
{
"mcpServers": {
"gateway": {
"command": "node",
"args": ["/path/to/gateway_mcp/dist/gateway.js"]
}
}
}
看 mcp-config-example.json 举一个完整的例子。
需求
- Node.js>=18.0.0
- TypeScript 5.x
- @模型上下文协议/sdk^1.20.0
局限性
- WebSocket未实现:WebSocket传输已中断,但无法正常工作
- 需要本地配置:每个安装都需要自己的
registry.config.json带有路径/URL - 无速率限制:如果公开披露,请添加
- 无需认证:依赖于本地服务器的进程隔离;远程服务器可能需要自己的身份验证
文档
故障排除
本地服务器无法连接:验证中的路径 registry.config.json 是绝对的,并确保构建下游服务器。
远程服务器无法连接:检查URL是否正确且可访问。网关首先尝试Streamable HTTP,然后回退到SSE http 种类服务器。
“找不到注册表配置文件”:复制 registry.config.example.json 到 registry.config.json 并配置您的服务器。
未找到工具:使用 discover 列出可用工具并验证工具名称是否完全匹配。
内存使用率高:减少 idleTtlMs 或手动 close 服务器完成后。
远程服务器超时:增加 connectTimeoutMs 对于远程服务器(建议网络延迟为10000ms+)。
许可证
麻省理工学院
