/ 目錄 / 演練場 / Context Mode
● 社群 mksglu ⚡ 即開即用

Context Mode

作者 mksglu · mksglu/context-mode

將工具輸出沙箱化並將 session 狀態保存於 SQLite——減少 98% 的 token 浪費,告別壓縮失憶症。

Context Mode 以兩種方式解決「LLM 忘記自己在做什麼」的問題。首先,沙箱工具將原始資料移出 context window(playwright 快照:56KB → 建立索引,agent 搜尋所需資訊)。其次,每個 session 的 SQLite 事件日誌在 context 壓縮後仍能存活——模型從中斷處精確恢復。以 plugin 加 MCP 的形式支援 12 個平台。

為什麼要用

核心特性

即時演示

實際使用效果

context-mode-mcp.replay ▶ 就緒
0/0

安裝

選擇你的客戶端

~/Library/Application Support/Claude/claude_desktop_config.json  · Windows: %APPDATA%\Claude\claude_desktop_config.json
{
  "mcpServers": {
    "context-mode-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "context-mode"
      ]
    }
  }
}

開啟 Claude Desktop → Settings → Developer → Edit Config。儲存後重啟應用。

~/.cursor/mcp.json · .cursor/mcp.json
{
  "mcpServers": {
    "context-mode-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "context-mode"
      ]
    }
  }
}

Cursor 使用與 Claude Desktop 相同的 mcpServers 格式。專案級設定優先於全域。

VS Code → Cline → MCP Servers → Edit
{
  "mcpServers": {
    "context-mode-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "context-mode"
      ]
    }
  }
}

點擊 Cline 側欄中的 MCP Servers 圖示,然後選 "Edit Configuration"。

~/.codeium/windsurf/mcp_config.json
{
  "mcpServers": {
    "context-mode-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "context-mode"
      ]
    }
  }
}

格式與 Claude Desktop 相同。重啟 Windsurf 生效。

~/.continue/config.json
{
  "mcpServers": [
    {
      "name": "context-mode-mcp",
      "command": "npx",
      "args": [
        "-y",
        "context-mode"
      ]
    }
  ]
}

Continue 使用伺服器物件陣列,而非映射。

~/.config/zed/settings.json
{
  "context_servers": {
    "context-mode-mcp": {
      "command": {
        "path": "npx",
        "args": [
          "-y",
          "context-mode"
        ]
      }
    }
  }
}

加入 context_servers。Zed 儲存後熱重載。

claude mcp add context-mode-mcp -- npx -y context-mode

一行命令搞定。用 claude mcp list 驗證,claude mcp remove 移除。

使用場景

實戰用法: Context Mode

停止在原始工具輸出上浪費 context

👤 頻繁使用 Claude Code、session 中途觸發壓縮的重度使用者 ⏱ ~15 min intermediate

何時使用: 你發現 agent 在 30 分鐘後「忘記」了,並花了大量費用反覆重讀。

前置條件
  • Claude Code v1.0.33+brew upgrade claude-codenpm update -g @anthropic-ai/claude-code
步驟
  1. 安裝
    /plugin marketplace add mksglu/context-mode
    /plugin install context-mode@context-mode✓ 已複製
    → Plugin 已註冊 hook 和 6 個沙箱工具
  2. 驗證
    /context-mode:ctx-doctor✓ 已複製
    → 所有檢查通過 [x]
  3. 正常使用
    Now do a big task that would normally blow context — e.g. analyze all .log files under /var/log✓ 已複製
    → 使用 ctx_execute 而非直接讀取;輸出被索引而非傾印
  4. 查看節省結果
    /context-mode:ctx-stats✓ 已複製
    → 各工具的 context 縮減比率

結果: 相同的任務,壓縮前的持續時間延長 10 倍,且 agent 仍記得在做什麼。

注意事項
  • 其他 MCP 繞過了路由 — 將其工具呼叫透過 ctx_batch_execute 執行,以保持一致的統計
搭配使用: serena

在模型壓縮後無縫恢復長任務

👤 進行多小時 agent session 的開發者 ⏱ ~5 min beginner

何時使用: 你的任務太大,無法在單一 context window 內完成。

步驟
  1. 使用 --continue 啟動
    [CLI flag] claude --continue✓ 已複製
    → 已載入前一個 session 的 SQLite
  2. 觀察
    What did I last ask you about? What tasks are in progress?✓ 已複製
    → 從 FTS5 索引得出的準確回顧,而非憑空捏造

結果: 真正可恢復的工作流程。

注意事項
  • 忘記加 --continue 等於從頭開始 — 持久化需要 --continue;沒有加就代表刻意重新開始

組合

與其他 MCP 搭配,撬動十倍槓桿

context-mode-mcp + serena

Serena 負責語意代碼導航,context-mode 負責將其輸出移出 context

Use serena for the lookup, ctx_execute to post-process the results.✓ 已複製
context-mode-mcp + playwright

頁面快照輸出保持在沙箱中

Snapshot the page via playwright, let context-mode hold it — search it instead of dumping.✓ 已複製

工具

此 MCP 暴露的能力

工具輸入參數何時呼叫成本
ctx_execute code: str (js/python) 執行腳本處理資料;只輸出所需結果 free
ctx_batch_execute ops: [{tool, args}] 將多個工具呼叫透過沙箱包裝 free
ctx_index content: str, tags?: [] 暫存資料以供後續取用 free
ctx_search query: str 從 session 索引中取回資料 free
ctx_fetch_and_index url: str 抓取 URL 內容,不放入 context 但可被搜尋 free
ctx_stats none 查看節省了多少 context free

成本與限制

運行它的成本

API 配額
本機
每次呼叫 Token 數
大幅縮減——實際 session 中引用的縮減率達 98%
費用
免費(ELv2 授權)
提示
session 越長,收益越大——用在長任務上

安全

權限、密鑰、影響範圍

憑證儲存: Session SQLite 存放在 ~/.context-mode/
資料出站:

故障排查

常見錯誤與修復

ctx-doctor shows missing hooks

安裝後完全重新啟動 Claude Code;hook 在啟動時才會註冊

驗證: Re-run /context-mode:ctx-doctor
Sandbox won't execute code

缺少 Node 或 Python runtime。Doctor 會告訴你缺哪個。用 brew 或 asdf 安裝。

Search returns nothing

驗證 FTS5 是否已建置:sqlite3 ~/.context-mode/session.db "SELECT name FROM sqlite_master"。若損壞則重新安裝。

替代方案

Context Mode 對比其他方案

替代方案何時用它替代權衡
Raw Claude Code (no plugin)短 session,從未觸發壓縮沒有 context 節省功能
claude-mem你只需要記憶層,不需要沙箱工具典範不同的取捨——只有記憶,沒有工具輸出沙箱

更多

資源

📖 閱讀 GitHub 上的官方 README

🐙 查看未解決的 issue

🔍 瀏覽全部 400+ MCP 伺服器和 Skills