MCP Server 每次会话先烧几千 token?用这个工具审计一下谁在偷你的 Context 预算

你写了一个 MCP Server,加了一堆工具,兴冲冲跑起来——然后发现每个 agent session 一上来就烧掉几千 token 光在 tools/list 上。这些 token 看不见摸不着,但每次新建会话都在烧钱。

mcp-context-audit 就是干这个的:它把你的 MCP Server 的 tools/list 和 prompts/list 响应拿来,用 tiktoken(gpt-4o encoding)精确统计每个字段吃了多少 token,然后给你排个榜——谁最贵,一目了然。

实际数字有多夸张?

作者 lewing 跑了一下自己的 helix.mcp(25 个工具):

  • outputSchema:吃掉 37% 的预算
  • inputSchema:吃掉 33.5%
  • annotations:9.7%
  • JSON-RPC 包络(每个工具对象):8.8%
  • description:7.2%

JSON Schema 合计 70.5%。你拼命精简 description,能省 2%;但动一动 schema 结构,省的可能是 30%。这就是为什么需要先测量再动手。

怎么用

在你的 MCP Server 仓库根目录跑:

plugins/mcp-tooling/skills/mcp-context-audit/scripts/audit-mcp.sh -- dotnet run --project src/MyServer

或者任何支持 stdio MCP 协议的可执行文件:

plugins/mcp-tooling/skills/mcp-context-audit/scripts/audit-mcp.sh -- ./bin/myserver mcp

脚本会:发送 initialize + notifications/initialized + tools/list + prompts/list,捕获响应,用 tiktoken 精确分词,输出每个工具的 token 排名和每个字段的占比。

什么时候跑?

  • 发版前:建立基准线,知道自己烧了多少
  • 重构后:加 --baseline diff 上一版本,看省了多少
  • Server 感觉”重”的时候:拿排名列表,决定先动哪块
  • 做 schema 决策前:reference/reduction-levers.md 里有各优化杠杆的 ROI

适合谁?

所有正在构建或维护 MCP Server 的开发者。Context 窗口寸土寸金,tools/list 是每个 session 最先触发的固定成本——优化它比优化 description 回报率高一个数量级。

来自 dotnet/runtime 团队维护的 lewing/agent-plugins 仓库,质量有保证。

https://github.com/lewing/agent-plugins


GitHub: https://github.com/lewing/agent-plugins

评论区

0 条评论

登录后可评论。

江望 38 阅读