Context-Mode MCP Plugin Cuts AI Context 96% via Sandbox & SQLite FTS5
The article analyzes context-mode, an MCP plugin that reduces AI coding agent context usage by 96% across 21 real-world scenarios by intercepting tool outputs, executing analysis in sandboxes, and indexing content in SQLite FTS5 with BM25 retrieval for session continuity.
Problem: Context Explosion in AI Coding Agents
When using tools like Claude Code, Cursor, or Copilot on real projects, the main bottleneck isn't model intelligence but context consumption. Tool outputs such as gh issue list returning 59KB JSON, test logs of tens of KB, and Playwright snapshots of 56KB are repeatedly re-sent across dozens of turns, burning tokens. Worse, when context fills up, agents summarize and discard original content, forcing developers to re-explain project structure and past decisions.
Solution: context-mode as MCP Server + Hooks Layer
context-mode sits between the AI agent and its tools. Tool outputs are first routed to a sandbox or local index; only summaries, pointers, or retrieval results enter the context. This differs from prompt engineering (asking the model to read less) by changing the data flow so raw data never enters context.
Three Core Features
1. Sandbox: Keeping Raw Bytes Out
Official BENCHMARK.md tests 21 real scenarios: total raw data 376KB, context input 16.5KB, overall 96% savings. Structured-data subset: 315KB → 5.5KB (98% savings). Examples:
Playwright page snapshot: 56.2KB → 299B
GitHub Issues: 58.9KB → 1.1KB
nginx access log: 45.1KB → 155B
Large JSON API: 7.5MB → 0.9KB
Raw content is stored in local SQLite; context retains only summaries or index pointers.
2. Think in Code: Offload Analysis to CPU
Old flow: model reads 47 files (~700KB), counts lines, finds patterns, fills context. New flow: model writes a script, sandbox executes it, only stdout returns to context. Official example: 47 Read calls (~700KB) become 1 ctx_execute() call (3.6KB). Optimization comes from workflow redesign, not smarter models.
3. Session Continuity: Compression Without Amnesia
Records file edits, git ops, tasks, errors, user decisions. Before compaction, hooks capture state; after compaction, SQLite FTS5 + BM25 retrieves relevant content. FTS5 is SQLite's full-text search module; BM25 is the ranking algorithm. Context can be discarded, but the index persists; sessions can break, yet key decisions remain recoverable.
Architecture: Context Router, Not Just a Plugin
Standard AI tool data flow: Agent → Tool → Raw output → Context → Next turn repeats.
Agent calls tool
↓
hooks intercept at PreToolUse / PostToolUse / PreCompact
↓
Large outputs go to sandbox execution or SQLite FTS5 indexing
↓
Context receives only summaries, pointers, retrieval snippets
↓
After compaction, state restored via session events and BM25 resultsKey architectural benefits:
Reduced duplicate transmission — raw output no longer occupies context each turn.
Precise content retrieval — docs, code snippets, API fragments fetched via index, not model hallucination.
Unified routing — commands like curl, wget, rm -rf can be blocked or redirected to sandbox on hook-enabled platforms.
Deployment
Universal install path:
npm install -g context-mode
context-mode doctorRequires Node.js ≥ 22.5 or Bun. Claude Code plugin install:
/plugin marketplace add mksglu/context-mode
/plugin install context-mode@context-mode
/context-mode:ctx-doctorTest commands:
context-mode index . --source project:demo
context-mode search "authentication middleware" --source project:demo
context-mode insightTools split into two categories: ctx_execute / ctx_execute_file / ctx_batch_execute: for logs, test output, CSV, build results (summarization). ctx_index / ctx_fetch_and_index / ctx_search: for docs, API references, MCP tool signatures (exact retrieval).
Selection rule: use execution for aggregation statistics; use indexing for verbatim fidelity.
Enterprise Adoption
1. Transform Data Flow First, Style Later
Don't mandate company-wide immediately. Pick 1-2 real teams, list high-output tools: logs, tests, builds, issue lists, page snapshots, MCP returns. Define rules:
Large outputs must go to sandbox.
Document content must go to index.
Dangerous commands must be blocked or redirected.
Session compaction must preserve key decisions.
2. Bulk Deployment: No Fleet Console in Open Source
Supports 17 clients (Claude Code, Cursor, Copilot, Codex, Gemini CLI, OpenCode, Zed, etc.) but open-source repo isn't a one-click enterprise control plane. Teams must wrap:
Lock versions via internal npm mirror.
Claude Code via internal plugin registry.
Push unified hooks config to Copilot/Cursor/OpenCode.
New hire machines run ctx doctor for install verification.
Roll out upgrades via canary then full push.
3. Pipeline Integration: Don't Cross Source-Code Boundary
Plugin runs locally by default — no cloud sync, telemetry, or accounts. For audit, keep events in local SQLite; if org view needed, Context Mode Platform (paid, opt-in) forwards only structured events. Hard boundary: no source code, no prompts, no file contents collected.
4. Team Norms: Routing Beats Slogans
On hook-enabled platforms, auto-route and block. Without hooks, rely on instruction files like AGENTS.md, GEMINI.md. Concrete rules:
Log analysis must use ctx_execute_file.
API docs must use ctx_index + ctx_search.
Large files forbidden from direct Read.
Network commands default to sandbox.
Sensitive variables added to denylist.
Pros and Cons
Pros
Clear data pipeline: intercept → sandbox → index → retrieve → restore.
Official benchmark covers 21 real scenarios.
Broad 17-platform support; Claude Code hooks mature.
Local-first, clear privacy boundaries.
Limitations
License is Elastic License 2.0 (ELv2), not OSI-approved open source. You may use, modify, deploy internally, but cannot offer as a hosted service to third parties or remove license notices.
Platform capability varies:
Claude Code: full hooks, high maturity.
Cursor, Codex CLI, Kiro: partial capability.
Antigravity, Zed, OMP: no hooks, rely on instruction files; ~60% compliance per docs.
Session restore depends on platform exposing PreCompact / SessionStart.
Author's Recommendations
Don't treat 98% as a guarantee — applies mainly to summarizable data (logs, tests, builds, snapshots); code retrieval emphasizes fidelity, so savings lower.
Don't treat benchmark as production load test — run your own logs, repos, toolchains.
Don't ignore 276 open issues — active project means interfaces and platform adapters still evolving rapidly.
Author's Closing Take
If you already use AI coding agents on real projects, context-mode deserves a pilot. Its real value isn't "315KB to 5KB" but turning context from chat logs into queryable, routable, recoverable engineering data. However, don't overhype: ELv2, platform hook gaps, and benchmark scope determine fit for your team.
GitHub: https://github.com/mksglu/context-mode
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
