Skip to main content
Glama

mcp-compressor

Release Build status Commit activity License

一个用于减少 MCP 工具所消耗令牌的 MCP 服务器封装器。


📋 更新日志

2026-04-06 — TypeScript 实现

添加了具有匹配压缩概念、OAuth 支持、进程内运行时 API 和 TypeScript CLI 模式的同级 TypeScript 实现。

2026-04-06 — 单服务器 MCP 配置 JSON 字符串

COMMAND_OR_URL 现在可以是一个单一的 MCP 配置 JSON 字符串。目前,mcpServers 必须恰好包含一个服务器,除非显式传递,否则其键将成为默认的 --server-name

2026-03-24 — CLI 模式

--cli-mode — 将任何封装的 MCP 服务器转换为本地 CLI。生成一个可执行的 shell 脚本(Unix)或 .cmd 文件(Windows),以便代理和用户可以通过熟悉的命令行约定而不是结构化的工具调用与后端进行交互。

2026-03-24 — TOON 输出

--toonify — 自动将来自封装后端工具的 JSON 响应转换为 TOON 格式,这是一种比 JSON 更紧凑、人类和 LLM 可读的替代方案。


概述

MCP Compressor 是一个代理服务器,它封装现有的 模型上下文协议 (MCP) 服务器并压缩其工具描述,以显著减少令牌消耗。它不是直接向语言模型公开所有带有完整模式的工具,而是提供了一个两步接口:

  1. get_tool_schema(tool_name) - 在需要时检索特定工具的完整模式

  2. invoke_tool(tool_name, tool_input) - 使用提供的参数执行工具

这种方法极大地减少了初始上下文中发送的令牌数量,同时保持了全部功能。

为什么?

MCP 服务器正变得越来越流行,但它们的工具描述在每次 LLM 请求中都会消耗大量的令牌。例如:

  • 官方 GitHub MCP 服务器 公开了 94 个工具,消耗 17,600 个令牌

  • 官方 Atlassian MCP 服务器 消耗约 10,000 个令牌

仅工具描述就占用 3 万多个令牌,根据提示词缓存的不同,成本可能达到 每请求 1-10 美分。MCP Compressor 通过用仅 2 个封装工具替换数十个工具来解决这个问题,在保持全部功能的同时实现了 70-97% 的令牌减少。这使得:

  • 添加多个 MCP 服务器而不会撑爆上下文窗口

  • 在基于令牌的 API 定价上显著节省成本

  • 支持为您的代理提供跨多个服务器的数百或数千个工具

特性

  • Python + TypeScript 实现:成熟的 Python 实现以及面向 Node.js 消费者的同级 TypeScript 包

  • 令牌减少:根据压缩级别和工具数量,将工具描述压缩高达 99%

  • 多种压缩级别:在 lowmediumhighmax 之间选择

  • 通用兼容性:适用于任何 MCP 服务器 (stdio, HTTP, SSE)

  • TOON 输出转换:使用 --toonify 可选地将 JSON 后端工具结果转换为 TOON

  • CLI 模式:使用 --cli-mode 将任何 MCP 服务器转换为本地 CLI — 生成一个 shell 脚本,让您(或 AI 代理)通过熟悉的命令行语法与后端交互

  • 零功能损失:所有工具仍然可以通过封装接口完全访问

  • 轻松集成:现有 MCP 服务器的直接替代品

Python 与 TypeScript 对比

功能

Python

TypeScript

核心压缩代理服务器

stdio / 可流式传输 HTTP / SSE 后端

单服务器 MCP 配置 JSON 字符串输入

持久化 OAuth 支持

CLI 模式

✅ 成熟

✅ 可用

用于应用/代理嵌入的进程内运行时 API

⚠️ 非一流

✅ 一流

提示词/资源透传对等性

✅ 更广泛

⚠️ 更窄

生产成熟度

✅ 主要实现

⚠️ 较新实现

如果您现在想要最成熟的功能集,请使用 Python 实现。如果您想要 Node.js 原生使用、进程内嵌入或更紧密的 TypeScript 生态系统集成,请使用 TypeScript 实现。

安装

使用 pip 或 uv 安装:

pip install mcp-compressor
# or
uv pip install mcp-compressor

快速入门

基本用法

通过提供其命令或 URL 来封装任何 MCP 服务器:

# Wrap a stdio MCP server
uvx mcp-compressor uvx mcp-server-fetch

# Wrap a remote HTTP MCP server
uvx mcp-compressor https://example.com/server/mcp

# Wrap a remote SSE MCP server
uvx mcp-compressor https://example.com/server/sse

有关可用参数的详细文档,请参阅 uvx mcp-compressor --help

压缩级别

使用 --compression-level-c 标志控制应用的压缩程度:

# Low
mcp-compressor uvx mcp-server-fetch -c low

# Medium (default)
mcp-compressor uvx mcp-server-fetch -c medium

# High
mcp-compressor uvx mcp-server-fetch -c high

# Max
mcp-compressor uvx mcp-server-fetch -c max

CLI 模式

如果您希望封装的后端表现得像一个本地命令行工具,请从这里开始:

mcp-compressor --cli-mode --server-name atlassian -- https://mcp.atlassian.com/v1/mcp

然后使用生成的 CLI 脚本:

atlassian --help

--cli-mode 不会将封装的后端公开为许多 MCP 工具,而是将后端转换为一个带有单个帮助工具以供发现的本地 CLI。 这在您希望代理通过 shell 风格的接口工作时,或者当后端服务器作为命令和标志比作为直接 MCP 工具调用更有意义时,特别有用。

flowchart LR
    Client["MCP Client / Agent"] -->|discovers| HelpTool["<server_name>_help"]
    HelpTool -->|explains commands| GeneratedCLI["Generated local CLI script\n(e.g. atlassian)"]
    User["User or Agent"] -->|runs CLI subcommands| GeneratedCLI
    GeneratedCLI --> Bridge["Local HTTP bridge\n127.0.0.1:<port>"]
    Bridge --> Compressor["mcp-compressor\n--cli-mode"]
    Compressor --> Backend["Wrapped MCP server"]
    Backend --> Compressor
    Compressor --> Bridge
    Bridge --> GeneratedCLI

为什么 CLI 模式很重要

  • 一个工具而不是多个:MCP 客户端看到的是单个 <server_name>_help 工具,而不是封装工具集

  • 自然的 shell 用户体验:后端工具成为带有从 JSON 模式派生的标志的 CLI 子命令

  • 非常适合代理:代理可以检查帮助,然后重复调用本地命令,而无需在上下文中携带完整的 MCP 工具表面

  • OAuth 仍然有效:如果封装的后端需要 OAuth,CLI 模式会在生成本地 CLI 之前执行该连接流程

  • 默认 TOON:在 CLI 模式下自动启用 --toonify 以获得紧凑、可读的输出

CLI 模式快速入门

# Wrap a remote MCP server as a local CLI
uvx mcp-compressor --cli-mode --server-name atlassian -- https://mcp.atlassian.com/v1/mcp

# Or pass a single MCP config JSON string
uvx mcp-compressor --cli-mode '{"mcpServers": {"atlassian": {"url": "https://mcp.atlassian.com/v1/mcp"}}}'

当 CLI 模式启动时,它:

  1. 连接到封装的后端服务器,包括在需要时进行 OAuth

  2. 127.0.0.1:<port> 上启动本地 HTTP 网桥

  3. 生成一个可执行脚本 — 在 Unix 上,如果 PATH 中可用,通常会写入 ~/.local/bin/<name>,否则写入当前目录;在 Windows 上,它会将 .cmd 启动器写入 PATH 中的合适目录

  4. 公开一个名为 <server_name>_help 的单个 MCP 工具,以便客户端可以发现生成的 CLI 及其子命令

启动后的示例用法:

# Top-level help — lists all subcommands
atlassian --help

# Per-tool help — shows flags derived from the backend tool schema
atlassian get-confluence-page --help

# Invoke a tool using ordinary CLI flags
atlassian get-confluence-page --cloud-id abc123 --page-id 456

# Escape hatch for complex inputs
atlassian create-jira-issue --json '{"cloudId":"abc","projectKey":"PROJ","summary":"Bug"}'

CLI 子命令名称是后端工具名称的 snake_case → kebab-case 转换(例如 getConfluencePageget-confluence-page)。 生成的脚本仅在 mcp-compressor --cli-mode 运行时有效。 如果您想将本地网桥固定到特定端口,请使用 --cli-port

高级选项

Stdio 服务器

# Set working directory
mcp-compressor uvx mcp-server-fetch --cwd /path/to/dir

# Pass environment variables (supports environment variable expansion)
mcp-compressor uvx mcp-server-fetch \
  -e API_KEY=${MY_API_KEY} \
  -e DEBUG=true

远程服务器 (HTTP/SSE)

# Add custom headers
mcp-compressor https://api.example.com/mcp \
  -H "Authorization=Bearer ${TOKEN}" \
  -H "X-Custom-Header=value"

# Set timeout (default: 10 seconds)
mcp-compressor https://api.example.com/mcp \
  --timeout 30

自定义服务器名称

当通过 mcp-compressor 运行多个 MCP 服务器时,您可以为封装工具名称添加自定义前缀以避免冲突:

# Without server name - tools will be: get_tool_schema, invoke_tool
mcp-compressor uvx mcp-server-fetch

# With server name - tools will be: github_get_tool_schema, github_invoke_tool
mcp-compressor https://api.githubcopilot.com/mcp/ --server-name github

# Special characters are automatically sanitized
mcp-compressor uvx mcp-server-fetch --server-name "My Server!"
  # Results in: my_server__get_tool_schema, my_server__invoke_tool

TOON 输出转换

使用 --toonify 自动将 JSON 后端工具结果转换为 TOON 格式。

# Convert JSON backend tool results to TOON
mcp-compressor https://api.example.com/mcp --toonify

启用 --toonify 后:

  • 如果后端工具结果是 JSON 对象或数组,则通过直接工具调用返回的成功结果将被转换为 TOON

  • 通过 invoke_tool(...) 返回的成功后端工具结果也会被转换为 TOON

  • 来自 get_tool_schema(...)list_tools(...) 的封装响应永远不会被转换

  • 来自 invoke_tool(...) 的封装生成的指导或错误文本永远不会被转换

  • 非 JSON 文本将原样返回

CLI 模式

CLI 模式在上面的专用 CLI 模式 部分中有记录。 简而言之:使用 --cli-mode,给服务器起个名字,并在 mcp-compressor 运行时与生成的本地脚本交互。

mcp-compressor https://mcp.atlassian.com/v1/mcp --server-name atlassian --cli-mode --cli-port 8765

日志记录

# Set log level
mcp-compressor uvx mcp-server-fetch --log-level debug
mcp-compressor uvx mcp-server-fetch -l warning

工作原理

MCP Compressor 在您的 LLM 客户端和底层 MCP 服务器之间充当透明代理:

flowchart TB
    subgraph github["GitHub MCP"]
        g1["create_pr"]
        g2["get_me"]
        g3["list_repos"]
        g4["get_issue"]
        g5["..."]
        g6["(+87 more tools)"]
    end

    subgraph proxy["MCP Compressor"]
        t1["get_tool_schema"]
        t2["invoke_tool"]
    end

    subgraph client["MCP Client"]
    end

    g1 <--> proxy
    g2 <--> proxy
    g3 <--> proxy
    g4 <--> proxy
    g6 <--> proxy
    t1 <--> client
    t2 <--> client

LLM 不会看到所有带有完整模式的工具(通常是数千个令牌),而是只看到:

Available tools:
<tool>search_web(query, max_results): Search the web for information</tool>
<tool>get_weather(location, units): Get current weather for a location</tool>
<tool>send_email(to, subject, body): Send an email message</tool>

当 LLM 需要使用工具时,它首先调用 get_tool_schema(tool_name) 来检索完整模式,然后调用 invoke_tool(tool_name, tool_input) 来执行它。

如果启用了 --toonify,成功的后端工具结果在返回给客户端之前会从 JSON 转换为 TOON。封装辅助响应本身不会被重新格式化。

在 CLI 模式 (--cli-mode) 下,压缩器公开一个单个 <server_name>_help 工具,而不是通常的封装器。所有实际的工具交互都通过本地 HTTP 网桥通过生成的 shell 脚本进行。

sequenceDiagram
    participant Client as MCP Client
    participant Compressor as MCP Compressor
    participant Server as GitHub MCP<br/>(91 tools)

    Client->>Compressor: list_tools()
    Compressor->>Server: list_tools()
    Server-->>Compressor: create_pr, get_me, list_repos, ...
    Compressor-->>Client: get_tool_schema, invoke_tool

    Client->>Compressor: get_tool_schema("create_pr")
    Compressor-->>Client: create_pr description & schema

    Client->>Compressor: invoke_tool("create_pr", {...})
    Compressor->>Server: create_pr({...})
    Server-->>Compressor: result
    Compressor-->>Client: result

压缩级别详情

级别

描述

用例

max

最大压缩 - 公开 list_tools() 函数

最大令牌节省。适用于 (1) 您想提供给代理但预计很少使用的 MCP 服务器,以及 (2) 具有大量工具的服务器

high

仅工具名称和参数名称

最大令牌节省,最适合大型工具集

medium (默认)

每个描述的第一句话

平衡方法,适用于大多数情况。

low

完整工具描述

适用于不寻常且代理难以理解和使用的工具。在这些情况下使用较低级别的压缩可以为 LLM 提供更多关于工具用途及其相互关系的上下文。

压缩级别的最佳选择取决于许多因素,包括:

  1. MCP 服务器中的工具数量 - 工具越多,使用的压缩就越多。

  2. 预计工具的使用频率 - 如果来自压缩服务器的工具很少使用,请将其压缩得更多,以防止无谓地消耗令牌。

  3. 工具的不寻常或复杂程度 - 更简单的工具可以被更重地压缩,而几乎没有缺点。考虑一个带有单个输入参数 command 的简单 bash 工具。任何现代 LLM 在看到工具名称和参数名称后都会准确理解如何使用它,因此除非工具内部存在意外的逻辑,否则可以使用激进的压缩而几乎没有缺点。

使用 MCP JSON 文件进行配置

您还可以直接在 CLI 上将单服务器 MCP 配置 JSON 字符串作为 COMMAND_OR_URL 传递。这对于远程服务器特别有用,当您希望配置本身携带 URL、标头、传输或 stdio 命令详情时。

目前,直接 JSON 字符串输入在 mcpServers 中仅支持一个服务器条目。

要在 MCP JSON 配置文件中配置 mcp-compressor,请使用以下模式:

{
  "mcpServers": {
    "compressed-github": {
      "command": "mcp-compressor",
      "args": [
        "https://api.githubcopilot.com/mcp/",
        "--header",
        "Authorization=Bearer ${GH_PAT}",
        "--server-name",
        "github"
      ],
    },
    "compressed-fetch": {
      "command": "mcp-compressor",
      "args": [
        "uvx",
        "mcp-server-fetch",
        "--server-name",
        "fetch"
      ],
    }
  }
}

此配置将创建名为 github_get_tool_schemagithub_invoke_toolfetch_get_tool_schemafetch_invoke_tool 的工具,从而在同时使用多个压缩服务器时避免命名冲突。

使用压缩级别:

{
  "mcpServers": {
    "compressed-fetch": {
      "command": "mcp-compressor",
      "args": [
        "uvx",
        "mcp-server-fetch",
        "--compression-level", "high"
      ],
    }
  }
}

用例

  • 大型工具集:当您的 MCP 服务器公开数十或数百个工具时

  • 令牌受限模型:为实际对话最大化可用上下文窗口

  • 成本优化:减少按令牌付费的 API 使用的令牌成本

  • 性能:更小的上下文带来更快的初始响应

  • 多服务器设置:与多个 MCP 服务器一起使用,而不会压垮上下文

命令行参考

Usage: mcp-compressor [OPTIONS] COMMAND_OR_URL

  Run the MCP Compressor proxy server.

  This is the main entry point for the CLI application. It connects to an MCP
  server (via stdio, HTTP, or SSE) and wraps it with a compressed tool
  interface.

Arguments:
  COMMAND_OR_URL  The URL of the MCP server to connect to for streamable HTTP
                  or SSE servers, or the command and arguments to run for
                  stdio servers. Example: uvx mcp-server-fetch  \[required]

Options:
  --cwd TEXT                      The working directory to use when running
                                  stdio MCP servers.
  -e, --env TEXT                  Environment variables to set when running
                                  stdio MCP servers, in the form
                                  VAR_NAME=VALUE. Can be used multiple times.
                                  Supports environment variable expansion with
                                  ${VAR_NAME} syntax.
  -H, --header TEXT               Headers to use for remote (HTTP/SSE) MCP
                                  server connections, in the form Header-
                                  Name=Header-Value. Can be use multiple
                                  times. Supports environment variable
                                  expansion with ${VAR_NAME} syntax.
  -t, --timeout FLOAT             The timeout in seconds for connecting to the
                                  MCP server and making requests.  \[default:
                                  10.0]
  -c, --compression-level [max|high|medium|low]
                                  The level of compression to apply to tool
                                  the tools descriptions of the wrapped MCP
                                  server.  \[default: medium]
  -n, --server-name TEXT          Optional custom name to prefix the wrapper
                                  tool names (get_tool_schema, invoke_tool,
                                  list_tools). The name will be sanitized to
                                  conform to MCP tool name specifications
                                  (only A-Z, a-z, 0-9, _, -, .).
  -l, --log-level [debug|info|warning|error|critical]
                                  The logging level. Used for both the MCP
                                  Compressor server and the underlying MCP
                                  server if it is a stdio server.  \[default:
                                  WARNING]
  --toonify                       Convert JSON backend tool responses to TOON
                                  format automatically.
  --cli-mode                      Start in CLI mode: expose a single help MCP
                                  tool, start a local HTTP bridge, and generate
                                  a shell script for interacting with the
                                  wrapped server via CLI. --toonify is
                                  automatically enabled in this mode.
  --cli-port INTEGER              Port for the local CLI bridge HTTP server
                                  (default: random free port).
  --install-completion            Install completion for the current shell.
  --show-completion               Show completion for the current shell, to
                                  copy it or customize the installation.
  --help                          Show this message and exit.
-
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/atlassian-labs/mcp-compressor'

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