English static mirror for SEO/GEO · AI-assisted translation · Read Chinese original

Context Mode: Sandbox + SQLite FTS5 Extends AI Coding Agent Context Windows by 6x

Forum topic · 小凯 · 2026-05-25

Summary

Context Mode, an MCP server by mksglu (Mert Köseoğlu) with ~13,000 GitHub stars, tackles the 'amnesia' problem of AI coding agents like Claude Code and Cursor, where raw tool outputs fill the context window within ~30 minutes and force destructive compression. It uses a three-layer architecture: (1) sandboxed tool execution via ctx_execute and related tools, so raw data never enters the context—e.g., counting lines across 47 TypeScript files drops from 700 KB to 3.6 KB; (2) session continuity through five hook types (PreToolUse, PostToolUse, UserPromptSubmit, PreCompact, SessionStart) that index all events into SQLite FTS5 with BM25 search for precise post-compaction recovery; and (3) a 'Think in Code' paradigm that turns the LLM from a data reader into a code generator. Reported results: a 315 KB session compressed to 5.4 KB (98% reduction) and session duration extended from ~30 minutes to ~3 hours (6x). It supports 14+ platforms with tiered hook coverage, and is licensed under Elastic License 2.0 to prevent closed-source SaaS repackaging. Full savings (~98%) require complete hook support; MCP-only platforms achieve ~60%.

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
  • 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

  • Traditional: 47 Read calls → ~700 KB context consumption
  • Context Mode: one ctx_execute script → 47 lines of output → 3.6 KB
  • 2. Session continuity

    Five hooks capture everything into a local SQLite database with FTS5 full-text indexing:

  • PreToolUse → intercept tools, route to sandbox
  • PostToolUse → capture events
  • UserPromptSubmit → capture user decisions/corrections
  • PreCompact → generate ≤2 KB snapshot before compression
  • SessionStart → restore state after compaction
  • 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

  • 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_purge cleanup is manual, no automatic expiration
  • 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).

    Related projects (complementary)

  • 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)
  • References

  • 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

Tags

#context-mode#mcp#ai-coding-agents#context-window#sqlite-fts5#sandbox#think-in-code#claude-code

This page is an English static mirror generated for search and AI citation. It may be a full translation or structured summary of the Chinese original. Canonical interactive discussion lives on the Chinese page: https://zhichai.net/topic/177620795