通过管道分隔符替代JSON Schema格式,将MCP工具定义从39964令牌压缩至3511,节省91%上下文开销。
当你给 AI 编程助手添加 MCP 服务器时,每个服务器都会把完整的 JSON schema 全部塞进 context。255 个工具 = 39,964 个 tokens。在 128K 的 context window 下,光是这些还没开始打字就已经占了 31%。
我简直是在为 JSON 语法开销买单。每次 API 调用都包含 {"content":[{"type":"text","text":"..."}]} — 传输 6 个 token 的数据要花 80 个 tokens。
我写了一个 CLI,放在你的 agent 和 MCP 服务器之间。它做了三件事:
mcptoon 没有使用完整的 JSON schema,而是用一种紧凑的管道分隔格式来呈现工具:
# Full JSON (287 tokens per tool):
{"name":"search","description":"Search the web","inputSchema":{"type":"object","properties":{"q":{"type":"string","description":"Query"},"n":{"type":"number"}},"required":["q"]}}
# SLIM format (26 tokens):
search|q:s*|n:n
255 个工具:39,964 → 3,511 tokens。节省了 91%。已用 tiktoken.get_encoding("cl100k_base") 验证。
Schema 保存在磁盘上的 ~/.mcptoon/config.json 中。你的 agent 运行 mcptoon manifest --slim 来查看可用的工具,然后用 mcptoon call <server> <tool> '{"param":"value"}' --toon 来执行。只有压缩后的输出才会进入 context。
工具返回的结果是人性化的键值对,而不是嵌套的 JSON:
# JSON result (80 tokens):
{"content":[{"type":"text","text":"{\"name\":\"react\",\"stars\":219000}"}]}
# TOON result (12 tokens):
name: react
stars: 219000
我的第一个版本用 ∅(空集符号)来替换 null。我觉得自己很聪明。然后有人用 tiktoken 运行了一下:null = 1 个 token,∅ = 2 个 tokens。我实际上是在增加 token 数量还把它叫做优化。
HN 社区的人因此批评了我。公平地说——我没有在发布前测量过。现在一切都用 tiktoken 验证过了。true 保持 true。null 保持 null。没有 unicode 戏法。
按 GPT-4o 的定价($5/M tokens):
pip install mcptoon
mcptoon add fetch --stdio npx -y @anthropic/mcp-fetch
mcptoon manifest --slim # see what's available, compact
mcptoon call fetch fetch '{"url":"https://example.com"}' --toon
适用于任何可以运行 shell 命令的 agent。一个配置文件适用于所有 agent——不再需要为 Claude Code vs Cursor vs OpenCode 重新配置。
3000 行 Python 代码,309 个测试,零依赖。
你最大的 MCP token 浪费是什么?其他人是如何处理的?