Key points
Context Mode is an MCP server that optimizes the context window of AI coding agents (Claude Code, Cursor, etc.). It doesn't enlarge the window—it prevents raw tool output from ever entering it.
The problem: agent "amnesia"
A typical agent session accumulates raw tool output rapidly:
- One Playwright browser snapshot: 56.2 KB
- 20 GitHub Issues: 58.9 KB
- 500 access log lines: 45.1 KB
- 153 git commits: 11.6 KB
- Traditional: 47 Read calls → ~700 KB context consumption
- Context Mode: one
ctx_executescript → 47 lines of output → 3.6 KB PreToolUse→ intercept tools, route to sandboxPostToolUse→ capture eventsUserPromptSubmit→ capture user decisions/correctionsPreCompact→ generate ≤2 KB snapshot before compressionSessionStart→ restore state after compaction- Savings depend heavily on hook completeness per platform (Cursor's SessionStart is currently rejected by its validator)
- Think in Code requires prompt engineering; results depend on model adherence
- FTS5/BM25 handles text search well but not structured queries (e.g., graph-style queries)—pairs well with knowledge-graph tools like codebase-memory-mcp
- The SQLite index accumulates over long sessions;
ctx_purgecleanup is manual, no automatic expiration - Context Mode — agent memory/context management (sandbox + FTS5)
- Understand-Anything — human code comprehension (multi-agent pipeline + visualization, MIT)
- codebase-memory-mcp — agent codebase querying (Tree-sitter knowledge graph, MIT)
- GitHub: https://github.com/mksglu/context-mode
- Architecture deep-dive: https://pyshine.com/Context-Mode-Context-Window-Optimization-AI-Coding-Agents/
- Usage guide: https://www.itnotetk.com/2026/05/05/context-mode-mcp-context-window-saver/
- Guide: https://www.easytool.me/blog/context-mode-ai-coding-context-window-guide.html
- MCP Market: https://mcpmarket.com/server/context-mode
- Tool comparison: https://milvus.io/blog/claude-code-context-management-tools.md
Within ~30 minutes the window fills and the agent compacts (deletes) context—often losing recent file edits, task progress, and user constraints. The agent then forgets what it did minutes ago and repeats finished work.
Three-layer architecture
1. Sandboxed tool execution (Context Saving)
Tools: ctx_execute (run code in isolated subprocesses in 11 languages, return only stdout), ctx_batch_execute, ctx_execute_file, ctx_index, ctx_search (BM25), ctx_fetch_and_index (URL → markdown → indexed, 24h TTL cache).
Example: counting lines across 47 TypeScript files
2. Session continuity
Five hooks capture everything into a local SQLite database with FTS5 full-text indexing:
After compaction, the agent uses BM25 search to retrieve only relevant context instead of re-injecting everything. BM25 is configured with Porter stemming, trigram substring matching, and 5x weighting for headers.
3. Think in Code paradigm
Shifts the LLM from a "data reader" (must see all data) to a "code generator" (writes code to process data, inspects only results)—mirroring how human engineers use find src -name "*.ts" | xargs wc -l instead of reading files one by one.
Measured savings
| Scenario | Raw | After sandbox | Saved | |---|---|---|---| | Playwright snapshot | 56.2 KB | 299 B | 99% | | GitHub Issues (20) | 58.9 KB | 1.1 KB | 98% | | Access logs (500) | 45.1 KB | 155 B | ~100% | | React docs (Context7) | 5.9 KB | 261 B | 96% | | CSV analysis (500 rows) | 85.5 KB | 222 B | ~100% | | Git log (153 commits) | 11.6 KB | 107 B | 99% | | Test output (30 suites) | 6.0 KB | 337 B | 95% | | Repo research (subagent) | 986 KB | 62 KB | 94% |
Full session: 315 KB → 5.4 KB (98%); session time ~30 min → ~3 hours (6x).
Platform support (tiered)
| Tier | Platforms | Expected savings | |---|---|---| | Full hooks | Claude Code, Qwen Code, Gemini CLI, VS Code/JetBrains Copilot, OpenCode, KiloCode | ~98% | | Partial hooks | Cursor, OpenClaw, Codex CLI, Pi | ~98% (SessionStart may be limited) | | MCP-only | Antigravity, Zed, Kiro | ~60% (manual routing config needed) |
Without PreToolUse, a single unrouted fetch can erase accumulated savings.
License
Elastic License 2.0 (not MIT): forking, modification, and internal use allowed; closed-source SaaS repackaging and license-notice removal prohibited. The author chose ELv2 explicitly to block vendor repackaging.
Limitations
Who should use it
Strongly recommended: daily agent coding >1 hour, frequent tool calls, long multi-turn sessions, large log/data processing.
Not useful for: light/occasional agent use, hook-less platforms without willingness to configure manually, or anyone intending to sell it as closed-source (ELv2 forbids it).