How I Cut MCP Token Usage by 91% (and Learned a Humbling Lesson About Tokenizers)
The Problem When you add MCP servers to your AI coding agent, each one dumps its full JSON schema into context. 255 tools across all servers = 39,964 tokens. On a 128K context window, that's 31% gone before you type a single character. I was literally paying for JSON syntax overhead. Every API call included {"content":[{"type":"text","text":"..."}]} — 80 tokens to deliver 6 tokens of data. The…
MCP servers add their entire JSON schema to context when included in an AI coding agent. This can consume a significant portion of the context window, costing valuable token space. The author built a tool called mcptoon to address this issue. Mcptoon works in three ways:
1. It compresses the JSON schema into a more compact format, reducing token usage by 91%. The original JSON schema for 255 tools used 39,964 tokens, but mcptoon compresses this down to just 3,511 tokens.
2. Schemas are stored on disk in a config file, allowing the agent to discover available tools and execute them without context. The compressed output enters context only when needed.
3. Results are returned in a human-readable key-value format (TOON) instead of nested JSON, saving even more token space. For example, instead of receiving a large JSON response, the agent gets a concise string like "name: react stars: 219000".
The author learned a valuable lesson about the importance of token optimization. An early attempt to replace null values with the empty set symbol (∅) actually increased the token count, demonstrating the need for thorough testing and measurement before implementing optimizations.
The cost impact of using mcptoon is substantial. At GPT-4o pricing, without the tool, 25 requests would consume 1 million tokens, costing $5. With mcptoon, the same number of requests would only use 87,500 tokens, costing just $0.44. This translates to around $540 in monthly savings for 100 sessions.
Implementing mcptoon is straightforward. You can install it via pip, set it up with your existing MCP fetch tool, and generate a schema manifest to see what's available. After that, you can execute tools using a single command that works with any agent compatible with shell commands.
With over 3,000 lines of Python code, 309 tests, and no external dependencies, mcptoon is a lightweight solution to a common problem. The author is working on adding more MCP servers to the default configuration and plans to develop a more comprehensive quality benchmark beyond just token counts. Feedback on the SLIM format and suggestions for reducing other MCP token waste are welcome.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.