← Blog

Don't Let the MCP Eat Your Agent's Context

· 15 min read · 372 views
Five-Layer Architecture MCP CLI Context Management AI Toolchain
Architecture Decision Log

Don't let MCP eat it

Your Agent's Context

199 tools, 40K tokens in schema overhead per round—until we slashed that by 90% using a five-layer architecture.
Service → SDK → CLI → MCP → Skill, allowing AI Agent to do the most with the fewest tokens.

Pain Points

What problems have we encountered?

Three real pain points led to the development of this five-layer architecture: an overwhelming number of tokens, an explosion in MCP processes, and too many tools to choose from.

1

Token Surge

Each call to Playwright MCP consumes a large number of tokens. Even when it is not in use, the "tool" schema still occupies the context window. Every round pays for it.

After switching to the Playwright CLI:

-98% Token Usage
2

MCP Process Explosion

Every time a Claude Code session is started, all MCP server processes duplicate proportionally.

3 sessions × 23 MCP servers
= 69 A background process
// RAM and CPU are consistently maxed out
3

There are too many tools to choose from.

When there are too many individual MCP server tools, the LLM's tool selection accuracy decreases.

Industry Best Practices: Each MCP Server ≤ 10 tools.

8 tools × 200 tokens/schema = 1,600 tokens per turn (Whether it works or not)

Data

Token Cost Comparison

MCP processes 1,600 tokens per round, while the CLI processes 0 (excluding actual I/O).

1,600
MCP (8 tools)
Schema overhead (tokens) per round
0
CLI via Bash
Schema overhead (tokens) per round
80K
50 Rounds of Dialogue
Total waste in unused schemas
2
Cloudflare Code Mode
The entire IDE uses only two tools
Architecture

What is each of the five layers used for?

The five layers from top to bottom are Skill (intent routing), MCP (write operations), CLI (read operations), SDK (the only encapsulation layer that touches HTTP), and Service (the core business logic itself), with upper layers strictly importing lower layers.

Skill
Intent Routing Layer—Determines whether to use the CLI or MCP based on the operation type; serves as the entry point for AI workflows
🤖 AI Scheduler / Automated Workflows
MCP
Write operation + typed schema return—LLMs require JSON schema validation of input
🤖 Claude Code (tool call)
CLI
A Universal Interface for Reading Operations—Zero schema overhead; anyone who can run a shell can use it
👤🤖📋 Human / AI / Cron / Script
SDK
The only HTTP boundary wrapper—both CLI and MCP import the SDK and do not interact with HTTP
🐍 Python Code / Notebook
Service
Business Logic Core—FastAPI routes + business logic + DB
⚙️ Internal Calls Between Modules

Core Principles: Bug Fix Auto-Propagation—Fix one SDK bug, and both the CLI and MCP are fixed at the same time. Without the SDK layer, each consumer implementing HTTP + auth + error handling on their own = maintenance hell.

// Dependency Direction (Upper-level imports lower-level)

Skill ──invoke──► CLI (Read) / MCP (Written by)
MCP  ──import──► SDK ──HTTP──► Service
CLI  ──import──► SDK ──HTTP──► Service

// The SDK is the only place that interacts with HTTP
// Change SDK = All upstream consumers are updated simultaneously
Consumer Matrix

Why can't we do it with one less layer?

Consumers Best Access Layer Reason
Other Core Modules Service Call `services.py` directly within the same process, with zero overhead
Python Script / Notebook SDK Programmatic Access, Comprehensive Error Handling
Person (End User) CLI --help Discoverable, pipe-composable
AI Coding Agent
Claude Code / Codex CLI / Gemini CLI
CLI (Read) / MCP (Written by) Reading Bash commands requires no schema; writing them requires typed input.
Scheduling System CLI Shell commands are the universal language of scheduling.
AI Workflows Skill Advanced Intent Routing: Automatically Select CLI or MCP

"The CLI is a zero-overhead, universal interface—anything that can run a shell can use it,
while MCP can only be used by clients that support the MCP protocol."

Evolutionary History

How did we end up here?

This architecture wasn't designed all at once—each of the four phases addressed the pain points we were facing at the time, and it wasn't until they were pieced together that it took its current form.

Discovery Phase — Playwright MCP

The First Lesson from the Token Surge

The Playwright MCP has a massive tool schema that is injected into the system prompt on every turn of the conversation. Even when the browser isn't used in that turn, it still consumes tokens. After replacing it with the Playwright CLI, token usage dropped by 98%.

Insight: If you can use the CLI, you shouldn't use MCP.

Expansion Phase — MCP Process Explosion

The Multiplicative Disaster of Multiple Sessions

Once the number of MCP servers grew to 23, opening each additional Claude Code session proportionally duplicates all processes. 3 sessions = 69 resident processes. The solution is mcpproxy aggregation + offloading read operations to the CLI.

Insight: It is necessary to reduce the number of MCP servers and tools.

Validation Period — Cloudflare Code Mode

The industry's most extreme tool compression

Cloudflare's Code Mode MCP exposes only 2 tools: search() and execute(). All other operations are written in real time within the sandbox. This proves that the number of tools is inversely proportional to their capabilities.

Insight: We don't take it to such extremes, but the same principle applies—the fewer tools, the better.

Maturity Phase — Proven in Practice

The new module has successfully completed all five levels.

Backend → SDK → CLI → MCP → Skill, missing a single layer causes downstream integration to break. With the BaseClient DRY pattern, the marginal cost of adding new modules decreases, but the composability gained with each added layer is a permanent benefit.

Insight: Five layers aren't a burden—they're compound interest.

Sandbox Pattern

Disposable Tools — Filling in for Sandbox

Not all operations require a permanent tool. Inspired by Cloudflare Code Mode, batch operations are written in real time within the sandbox and do not take up an MCP tool slot.

Permanent Tool (Five-Layer Coating)

High-frequency, stable operations that require discoverability

  • search — Frequent daily use
  • create — Requires typed schema validation
  • render — Complex Combinations of Parameters

✓ Worth the maintenance effort ✓ Has a --help option ✓ Can be tested

Disposable Tool (Sandbox)

Low-frequency, one-time, or exploratory batch operations

  • Batch Scanning + Aggregation (3+ API calls)
  • One-Time Data Conversion
  • Exploratory Scripts (Disposable)

✓ Zero schema overhead ✓ Doesn't take up a tool slot ✓ Write and use immediately

// Cloudflare's Extreme Approach vs. Pragmatic Approach

Cloudflare: 2 permanent tools + sandbox: Log all operations in real time
Pragmatists:  ≤10 permanent tools/server + sandbox to cover low-frequency operations

// General rule: Permanent tools are a scarce resource; if something isn't worth taking up space, use the sandbox instead.

Criteria: Is a single operation worth becoming a permanent tool?
→ Used ≥ 3 times per week + complex parameter combinations + requires discoverability → Five-layer coverage
→ Otherwise → sandbox: write in real time, use and discard

Comparison

What if we don't use five layers?

Alternatives Question Consequences
Only Service HTTP calls only AI agents, cron jobs, and CLI users all have to write their own HTTP clients.
Service + MCP Token overhead + process explosion 23 servers × 3 sessions = 69 processes; the schema consumes a token per round
Service + CLI Write operation missing typed schema LLM passed incorrect parameters; no input validation
Go with MCP across the board CLI users are excluded Codex CLI, Gemini CLI, and cron are all unable to connect
Each layer implements HTTP independently No unified SDK Fix one thing, miss another: The “Ghost Parameter” anti-pattern runs rampant
Implementation Examples

Use the Agent SDK to Control Tool Injection Yourself

If you're building your own client using the Agent SDK, you can fully control, at the application layer, which tool schemas are included in the LLM's context—no need to wait for platform support. Speakeasy's Dynamic Toolset Solution uses the same approach to filter tools, cutting token usage by 96%.

# Sample Pseudocode

all_tools = mcp_server.tools_list()    # Received 199 (protocol layer)
relevant = bm25_filter(all_tools, query) # You've narrowed it down to 5 yourself
response = client.messages.create(
    tools=relevant,                # Only 5 to be given to LLM
)

No filtering (default behavior)

All 199 tool schemas were crammed into tools[]

~40K tokens per round

After BM25 filtration

We'll send you only the 5–10 most relevant tools each time.

~2,000 tokens per round

Key Distinctions: At the MCP protocol layer, tools/list returns all tools—there's no changing that.
But it's up to your client to decide what to put into the API's tools[] — that's completely under your control.
Use an off-the-shelf client like Claude Code? It handles everything for you. Build your own client? You have to filter the data yourself.

Cost Analysis

The implementation costs are actually quite low.

SDK

Inheritance BaseClient, with each method encapsulated on a separate line. Error handling, authentication, and base paths all follow the DRY principle.

CLI

Import the SDK's command packages. Use a shared parser for unified signing and pagination.

MCP

SDK packaging + tool definitions. Only includes write operations and structured returns; read operations are not required.

"Diminishing marginal costs and scalability offer long-term benefits—but maintenance costs are also long-term."

⚠ The Cost of a Five-Tier Architecture

Maintain three additional packaging sets

The SDK, CLI, and MCP all contain the same logic. When the API changes, all three layers must be updated accordingly. This is manageable when the team is small, but as the team grows, automated testing becomes essential to ensure consistency.

The debug path has become longer

When a bug occurs, you must first determine at which layer the problem lies: is it a logic error in the service, a missing parameter in the SDK, an incorrect CLI parsing, or an outdated MCP schema? The more layers involved, the longer it takes to troubleshoot.

Not all modules are worth it

Internal tools don’t need five layers. Only core modules used by a wide range of consumers are worth investing in. Over-application can actually become a burden.

External Verification

A Shift in the OpenCLI Community's Sentiment

The open-source community is adopting the CLI as the default interface, not the direction we ourselves are calling for.

OpenCLI converts websites and desktop apps into a standardized CLI; CLI-Anything wants to ensure that all software can be controlled by the Agent via the CLI. More and more developers are asking, “Is there a CLI?” rather than “Is there an API?”

The reason is simple: The New Stack's 2025 report already said as much—the entire industry is entering the era of the Agentic CLI. When all AI coding tools can run shell commands, the CLI naturally becomes the greatest common denominator across tools.

In the past

Common practice: Agent + MCP

All tools use MCP, and the schema is entirely stored in the context; the more tools there are, the slower it gets.

Now

Evolutionary Approach: Agent + Skill (Calling CLI / MCP)

Skill On-Demand Routing—Read operations go through the CLI at zero cost; write operations go through the MCP.

It’s not a CLI revival—it’s the rediscovery of the CLI’s value in the age of AI.

Conclusion

199 Tools, One Set of Logic

Read via the CLI, write via MCP—these 199 tools all operate based on this single distinction, People, Scripts, AI, to each his own.
From the surge in token volume to the validation of community sentiment, every step was driven by pain points—change one place, and it updates everywhere.

// Decision Chain

Pain Point 1: Playwright MCP Token Surges in Volume
  → CLI Save 98% token

Pain Point 2: Running multiple instances of CC → Processes increase proportionally
  → mcpproxy Aggregation + CLI Unload Read Operations

Pain Point 3: There are too many LLM tools to choose from
  → ≤10 tools/server + sandbox Fill-in

Verification 1: Running the Entire IDE with Just 2 Cloudflare Tools
  → Following the same principle, we further Pragmatic

Verification 2: The OpenCLI community is shifting toward a CLI-first approach
  → In the Age of AI, CLI is Greatest Common Divisor Across Tools

// Induction
Service → SDK → CLI → MCP → Skill
199 Tools, One Logic, Three Types of Consumers
Tools

First, calculate your own schema bill

The 40K tokens mentioned above are based on my setup; if you substitute your own numbers, the results will be completely different. Fill in three fields and calculate your per-round overhead and resident process count using the two constants cited in this text (approximately 200 tokens per tool schema, industry recommendation of ≤ 10 tools per MCP server). Everything is calculated entirely in the browser with no data sent out.

The sources for all constants are in this text: 200 tokens/schema comes from the linear relationship of "8 tools × 200 tokens/schema = 1,600 tokens/turn," the number of processes comes from "3 sessions × 23 MCP servers = 69 resident processes," and 10 tools/server is the industry recommendation cited in the text. This figure only accounts for the overhead associated with the schema; it does not include actual I/O.—The actual usage depends on how you make the calls.

Copy this section

Copy this to your AI Agent

The MCP tool schema consumes tokens every round, but most of your tools are actually “read” operations—they don’t need the schema at all. Once you have too many tools, the schemas alone consume tens of thousands of tokens, and with more than 10 tools, the probability of the LLM making the wrong choice increases. The five-tier architecture in this article relegates "reading" away from the CLI and leaves only "writing" for MCP.

Send the text below to your AI, have it conduct an on-site inventory of your current MCP configuration, and then use this five-tier framework to determine which elements can be migrated:

I'm reading an article about the "Five-Layer Architecture to Save MCP Tokens" (https://blog.joneshong.com/en/blog/five-layer-architecture). The author breaks down AI tool integration into five layers, moving read operations to the CLI and leaving only write operations for the MCP. Please first conduct an on-site inventory of my project, and then evaluate what I can migrate based on this framework. Do not answer with general rules: First, check my current status: 1. List the MCP servers I currently have connected and the number of tools in each server (highlight any with more than 10 tools—the article states that when exceeding 10, the LLM error rate increases). 2. Categorize these tools one by one into two piles: "read operations" and "write operations". 3. Estimate roughly how many tokens the schemas for these tools consume per turn. Then, evaluate how to migrate based on this five-layer architecture (from bottom to top): - Service: The core business logic (API + DB). - SDK: A unified HTTP wrapper; upper layers import the SDK and do not touch HTTP directly. - CLI: A general-purpose interface with zero schema overhead; read operations go through this layer. - MCP: Reserved only for write operations that require typed schema validation. - Skill: The intent routing layer that automatically determines whether reads go to the CLI and writes go to the MCP. Report back to me: - Which "read" tools can be directly migrated to the CLI, and estimate the token savings for each. - Whether my HTTP call logic is scattered everywhere and if it's worth extracting into an SDK. - Which tools can be merged or dynamically generated using a sandbox to cut down the number of resident tools. If I've only connected one or two tools and the schema overhead isn't painful at all, just say there's no need to split them, and don't force a split just to fit the architecture.
References

Further Reading

I’ve actually read all of the articles below, and they’ve genuinely influenced my decisions—this isn’t just a random list I put together.

Article Why It Matters
Code Mode: Give Agents an Entire API in 1,000 Tokens Cloudflare's real-world test showed that running the entire IDE with just two tools reduced token overhead by 81%. This validated the principle that "the fewer tools, the better."
Your MCP Server Is Consuming Your Context Window Apideck analyzes how MCP schema eats up context, and proposes CLI as an alternative—80 tokens prompt vs tens of thousands of tokens schema
Advanced Tool Use — Anthropic Engineering Anthropic has officially acknowledged the issue of the tool schema consuming context and has introduced the Tool Search mechanism to achieve an 85% reduction in context usage.
How We Reduced Token Usage by a Factor of 100: Dynamic Toolsets Speakeasy's dynamic toolkit solution achieves a 96% reduction in input tokens
MCPProxy — MCP Gateway Open-source MCP aggregation gateway that uses BM25 to index dynamic discovery tools, supporting hundreds of servers and thousands of tools
Welcome to the Agentic CLI Era The New Stack Reports on the Industry Trend of AI Coding Tools Shifting Entirely to the CLI
OpenCLI — Universal CLI Hub A universal CLI hub designed specifically for AI agents, transforming websites and desktop applications into standardized CLIs
CLI-Anything — Making ALL Software Agent-Native University of Hong Kong Study: Enabling AI Agents to Operate All Software via the CLI
✦ Copy Prompt