Skip to main content
Glama

MCP 网关

一个位于 Claude 和你的 MCP 服务器之间的懒加载代理。它不会在启动时加载每个服务器(这会将数百个工具模式转储到上下文中并消耗大量 Token),而是仅暴露 4 个轻量级工具。后端服务器仅在你真正需要时才会启动。

之前: 10 个 MCP 服务器 = 200 多个工具模式加载到每次对话中 = 浪费数千个 Token。

之后: 网关后的 10 个 MCP 服务器 = 加载 4 个工具模式。每个服务器按需启动。

问题所在

你添加到 Claude Code 的每个 MCP 服务器都会预先注册其所有工具。典型的服务器有 10-30 个工具,每个工具都有完整的 JSON 模式。如果有 10 个服务器,在你提问之前,就有 100-300 个工具定义占用了你的上下文窗口。

大多数对话只使用 1-2 个服务器。其余的都是累赘。

工作原理

网关向 Claude 暴露了 4 个工具:

工具

功能

gateway_list_servers

显示可用服务器及其状态

gateway_load_server

连接到服务器并发现其工具

gateway_call_tool

调用已连接服务器上的工具

gateway_reload_server

重新连接服务器(获取代码更改)

当 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"
    }
  }
}

配置字段

字段

必需

默认

描述

type

stdio

传输方式:stdio, http, sse, 或 streamable-http

command

是 (stdio)

-

运行服务器的命令

args

[]

命令参数

env

{}

环境变量

url

是 (http)

-

服务器 URL

headers

{}

HTTP 请求头(认证 Token 等)

timeout

30/60

请求超时时间(秒)(stdio 为 30,http 为 60)

description

-

gateway_list_servers 中显示的人类可读描述

迁移现有的 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 会自动使用网关。典型的交互流程:

  1. Claude 调用 gateway_list_servers 查看可用内容

  2. 当需要 GitHub 时,Claude 调用 gateway_load_server("github")

  3. Claude 调用 gateway_call_tool("github", "search_repositories", '{"query": "mcp"}') 来使用工具

  4. 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

-
security - not tested
A
license - permissive license
-
quality - not tested

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/RaiAnsar/mcp-gateway'

If you have feedback or need assistance with the MCP directory API, please join our Discord server