TechSetupGuides
Intermediatemcpai-agentscontext-optimizationclaude-codegemini-clicursorvscode-copilot

context-mode: Context window optimization for AI coding agents

MCP plugin that saves 98% of context window through sandboxed code execution, FTS5 search, and session continuity. Works with Claude Code, Gemini CLI, VS Code Copilot, Cursor, and 12 other platforms.

  1. Step 1

    What is context-mode?

    context-mode is an MCP (Model Context Protocol) server that solves the context window problem for AI coding agents. Every tool call dumps raw data into your context window — a Playwright snapshot costs 56 KB, twenty GitHub issues cost 59 KB, one access log costs 45 KB. After 30 minutes, 40% of your context is gone.

    context-mode solves this by:

    1. Context Saving — Sandbox tools keep raw data out of the context window. 315 KB becomes 5.4 KB (98% reduction).
    2. Session Continuity — When conversation compacts, context-mode restores your working state (files edited, tasks, errors, decisions) from SQLite via FTS5 search.
    3. Think in Code — Instead of reading 50 files into context, the agent writes a script that does the analysis and logs only the result.
  2. Step 2

    Verify prerequisites

    context-mode requires Node.js 22.5 or higher. Check your version:

    node --version
  3. Step 3

    Install for Claude Code (recommended, easiest)

    Claude Code has the simplest installation via the plugin marketplace with automatic hook registration and routing enforcement.

    # First, ensure you have Claude Code v1.0.33+
    claude --version
    
    # Install via marketplace (automatic hooks + MCP tools)
    /plugin marketplace add mksglu/context-mode
    /plugin install context-mode@context-mode
    
    # Restart Claude Code or reload plugins
    /reload-plugins
  4. Step 4

    Verify Claude Code installation

    Run the doctor command to validate your setup. All checks should show [x]:

    /context-mode:ctx-doctor
  5. Step 5

    Optional: Add status line to Claude Code

    To see real-time context savings, add a status line to your Claude Code settings. This is a one-time manual edit since the plugin manifest cannot declare a status line.

    {
      "statusLine": {
        "type": "command",
        "command": "context-mode statusline"
      }
    }
    ⚠ Heads up: Add this to ~/.claude/settings.json, then restart Claude Code. The bar shows tokens saved this session, all time, and efficiency percentage.
  6. Step 6

    Alternative: MCP-only install for Claude Code

    If you want to try context-mode without automatic routing enforcement (no hooks), use the MCP-only install. The model can still use the tools but won't be nudged to prefer them over raw commands.

    claude mcp add context-mode -- npx -y context-mode
  7. Step 7

    Install for Gemini CLI

    Gemini CLI requires manual configuration of both MCP server and hooks in your settings.json file.

    # Install context-mode globally
    npm install -g context-mode
    
    # Add MCP server to ~/.gemini/settings.json (existing settings will be merged)
    # The config adds mcpServers and hooks sections
  8. Step 8

    Install for VS Code Copilot

    VS Code Copilot requires project-level configuration for both MCP and hooks.

    # Install globally
    npm install -g context-mode
    
    # Create .vscode/mcp.json in project root
    {
      "servers": {
        "context-mode": {
          "command": "context-mode"
        }
      }
    }
    
    # Create .github/hooks/context-mode.json for hook configuration
  9. Step 9

    Install for Cursor

    Cursor installation has two options. Option A (marketplace plugin) is recommended once published; until then, use the local-folder method.

    # Option A: Local-folder method (macOS/Linux)
    git clone https://github.com/mksglu/context-mode.git
    cd context-mode
    ln -s "$PWD/context-mode" ~/.cursor/plugins/local/context-mode
    
    # Restart Cursor to activate the plugin
  10. Step 10

    Install for OpenCode

    OpenCode uses a TypeScript plugin for full integration without MCP stdio overhead.

    # Add to opencode.json
    {
      "$schema": "https://opencode.ai/config.json",
      "plugin": ["context-mode"]
    }
    
    # Optional: Copy routing rules
    cp node_modules/context-mode/configs/opencode/AGENTS.md ./AGENTS.md
  11. Step 11

    Core sandbox tools

    context-mode provides six sandbox tools that keep raw data out of context. These are available as MCP tools on all platforms.

    // ctx_execute - Run code in sandbox, log only result
    ctx_execute("javascript", `
      const files = fs.readdirSync('src').filter(f => f.endsWith('.ts'));
      files.forEach(f => console.log(f + ': ' + fs.readFileSync('src/'+f,'utf8').split('\n').length + ' lines'));
    `);
    
    // ctx_fetch_and_index - Fetch URL, index content, return summary
    ctx_fetch_and_index("https://example.com/docs");
    
    // ctx_batch_execute - Run multiple commands efficiently
    ctx_batch_execute([
      { language: "bash", code: "git log --oneline -10" },
      { language: "javascript", code: "console.log(process.env)" }
    ]);
    
    // ctx_search - Search indexed content via BM25
    ctx_search("how to configure authentication", 5);
    
    // ctx_index - Persist content for later search
    ctx_index("React docs", reactDocumentationContent);
    
    // ctx_execute_file - Execute file content
    ctx_execute_file("analysis.js", "javascript");
    ⚠ Heads up: The sandbox blocks dangerous commands by default: sudo, rm -rf /, curl/wget (redirected to ctx_fetch_and_index), npm/yarn install (redirected to ctx_execute). See the Security section for customization.
  12. Step 12

    Utility commands

    context-mode provides five utility commands for diagnostics and maintenance. Type these in the AI chat (the model calls the MCP tools automatically), or use CLI equivalents in your terminal.

    # In AI chat - type these directly:
    ctx stats       -> context savings, call counts, session report
    ctx doctor      -> diagnose runtimes, hooks, FTS5, versions
    ctx upgrade     -> update from GitHub, rebuild, reconfigure hooks
    ctx purge       -> permanently delete all indexed content
    ctx insight     -> open analytics dashboard (99 metrics, 37 insights)
    
    # In terminal:
    context-mode doctor
    context-mode upgrade
    context-mode insight          # opens browser
    context-mode statusline       # live counter while running
  13. Step 13

    Understanding session continuity

    context-mode captures every meaningful event during your session and persists them in a per-project SQLite database. When conversation compacts (or you resume with --continue), your working state is restored automatically.

    The system tracks:

    • Critical (P1): Files read/edited/written, tasks created/updated, plans entered/approved, CLAUDE.md/GEMINI.md/AGENTS.md rules, user prompts with corrections
    • High (P2): Git operations, tool errors with resolutions, discovered constraints, blockers, rejected approaches, environment changes
    • Normal (P3): Latency spikes, MCP tool calls, subagent completions, slash command invocations
    • Low (P4): Session intent, role directives, large data references

    When compaction occurs, a priority-tiered snapshot (≤2 KB) is built and stored. After compaction, the model receives a Session Guide with 15+ categories of actionable context.

    Session Guide structure after compaction:
    - Last Request
    - Tasks (with completion status)
    - Plans (approvals/rejections)
    - Key Decisions
    - Files Modified
    - Unresolved Errors
    - Constraints & Blockers
    - Git Operations
    - Project Rules
    - MCP Tools Used
    - Subagent Tasks
    - Skills Used
    - Rejected Approaches
    - External References
    - Environment
    - Data References
  14. Step 14

    Security configuration

    context-mode enforces the same permission rules you already use in Claude Code settings (or platform equivalent), extended to the MCP sandbox. No setup required by default.

    {
      "permissions": {
        "deny": [
          "Bash(sudo *)",
          "Bash(rm -rf /*)",
          "Read(.env)",
          "Read(**/.env*)"
        ],
        "allow": [
          "Bash(git:*)",
          "Bash(npm:*)"
        ]
      }
    }
    ⚠ Heads up: Add to .claude/settings.json or ~/.claude/settings.json. The pattern is Tool(what to match) where * means anything. Commands chained with &&, ;, or | are split and each part is checked. deny always wins over allow. More specific (project-level) rules override global ones.
  15. Step 15

    Network fetch hardening

    ctx_fetch_and_index blocks dangerous URL targets by default:

    • Only http: and https: schemes allowed (no file://, gopher://, javascript:, data:)
    • Cloud metadata + link-local (169.254.0.0/16) hard-blocked, including AWS/GCP/Azure IMDS
    • Multicast/reserved (224.0.0.0/4, 0.0.0.0/8, IPv6 ff00::/8, fe80::/10) blocked
    • Loopback + RFC1918 allowed by default for local dev servers

    For CI/hosted environments, set CTX_FETCH_STRICT=1 to also block loopback + RFC1918 + ULA.

    # For hosted/CI environments (blocks private targets too)
    export CTX_FETCH_STRICT=1
  16. Step 16

    Environment variables

    context-mode supports several environment variables for customization:

    | Variable | Default | Purpose |
    |---|---|---|
    | CONTEXT_MODE_DIR | Adapter-specific (~/.claude/context-mode) | Root for context-mode storage. Must be absolute since v1.0.147. |
    | CONTEXT_MODE_EXTERNAL_MCP_NUDGE_EVERY | 10 | Cadence for re-injecting MCP-wrapping guidance (range 1-100). |
    | CTX_FETCH_STRICT | 0 | Block loopback + RFC1918 + ULA in addition to always-blocked ranges. |
  17. Step 17

    Try it out - quick tests

    These prompts work out of the box. Run ctx stats after each to see the savings.

    ## Test 1: Git history analysis (62 KB context, raw: 986 KB, 94% saved)
    
    > Research https://github.com/modelcontextprotocol/servers - architecture, tech stack, top contributors, open issues. Then run ctx stats
    
    ## Test 2: Access log analysis (155 B context, raw: 45.1 KB, 100% saved)
    
    > Analyze this access log for top endpoints and error rates: [paste 500-line log]
    
    ## Test 3: Web scraping (3.2 KB context)
    
    > Fetch the Hacker News front page, extract all posts with titles, scores, and domains. Group by domain. Then run ctx stats.
  18. Step 18

    Platform compatibility summary

    context-mode works with 15+ AI platforms with varying levels of hook support. Full session continuity (capture + snapshot + restore) requires all five hooks: PreToolUse, PostToolUse, SessionStart, PreCompact, and optionally UserPromptSubmit.

    | Platform | MCP | Hooks | Session | Routing | Install Type |
    |---|:--:|:--:|:--:|:--:|:--:|
    | Claude Code | ✓ | ✓ (auto) | Full | Auto | Plugin marketplace |
    | Gemini CLI | ✓ | ✓ | High | Auto | Manual config |
    | VS Code Copilot | ✓ | ✓ | High | Auto | Project config |
    | JetBrains Copilot | ✓ | ✓ | High | Auto | Settings UI |
    | Cursor | ✓ | ✓ (partial) | Partial | Rules file | Local plugin |
    | OpenCode | ✓ | ✓ (plugin) | Full | Auto | TypeScript plugin |
    | OpenClaw | ✓ | ✓ (plugin) | High | Auto | Gateway plugin |
    | Pi Agent | ✓ | ✓ | High | Auto | Extension |
    | Codex CLI | ✓ | ✓ | Partial | Auto | Hook config |
    | Kiro | ✓ | ✓ | Partial | Manual | Hook config |
    | Zed | ✓ | ✗ | ✗ | Manual | MCP only |
    | Antigravity | ✓ | ✗ | ✗ | Manual | MCP only |
  19. Step 19

    Common issues and troubleshooting

    If context-mode isn't working as expected, use the diagnostic tools:

    # Check all systems
    ctx doctor
    
    # Upgrade to latest
    ctx upgrade
    
    # View detailed stats
    cat ~/.claude/context-mode/sessions/stats.json
    
    # Clear corrupted indexed content
    ctx purge
  20. Step 20

    Privacy and architecture

    Nothing leaves your machine. No telemetry, no cloud sync, no usage tracking, no account required. Your code, prompts, and session data stay local in SQLite databases in your home directory.

    context-mode operates at the MCP protocol layer - raw data stays in a sandboxed subprocess and never enters your context window. This is a deliberate architecture choice, not a missing feature.

    Data stored locally:
    - ~/.{platform}/context-mode/sessions/ - session events, stats
    - ~/.{platform}/context-mode/content/ - indexed content (FTS5)
    
    License: Elastic License 2.0 (source-available, not MIT)
    - Use, fork, modify, distribute freely
    - Cannot offer as hosted/managed service
    - Cannot remove licensing notices

Feature requests

Sign in to suggest features or vote on existing ones.

No feature requests yet.

Discussion

0 people marked this as worked·Sign in to mark your own.

Sign in to join the discussion.

No comments yet.