mksglu / context-mode

mksglu / context-mode

Context Mode: The other half of the context problem. Context Mode:上下文问题的另一半。

The Problem

问题所在

Every MCP tool call dumps raw data into your context window. A Playwright snapshot costs 56 KB. Twenty GitHub issues cost 59 KB. One access log — 45 KB. After 30 minutes, 40% of your context is gone. And when the agent compacts the conversation to free space, it forgets which files it was editing, what tasks are in progress, and what you last asked for. On top of that, the agent wastes output tokens on filler, pleasantries, and verbose explanations — burning context from both sides. 每一次 MCP 工具调用都会将原始数据倾倒进你的上下文窗口。一个 Playwright 快照占用 56 KB,二十个 GitHub Issue 占用 59 KB,一个访问日志占用 45 KB。30 分钟后,你 40% 的上下文就没了。当 Agent 为了腾出空间而压缩对话时,它会忘记正在编辑的文件、正在进行的任务以及你最后的要求。更糟糕的是,Agent 还会将输出 Token 浪费在填充词、客套话和冗长的解释上——从两端同时消耗你的上下文。

How Context Mode Solves It

Context Mode 如何解决

Context Mode is an MCP server that solves all four sides of this problem: Context Mode 是一个 MCP 服务器,它从四个维度解决了这个问题:

  • Context Saving — Sandbox tools keep raw data out of the context window. 315 KB becomes 5.4 KB. 98% reduction. 上下文节省 — 沙盒工具将原始数据挡在上下文窗口之外。315 KB 缩减至 5.4 KB,减少了 98%。
  • Session Continuity — Every file edit, git operation, task, error, and user decision is tracked in SQLite. When the conversation compacts, context-mode doesn’t dump this data back into context — it indexes events into FTS5 and retrieves only what’s relevant via BM25 search. The model picks up exactly where you left off. If you don’t --continue, previous session data is deleted immediately — a fresh session means a clean slate. 会话连续性 — 每一个文件编辑、Git 操作、任务、错误和用户决策都会被记录在 SQLite 中。当对话压缩时,context-mode 不会将这些数据塞回上下文,而是将事件索引到 FTS5 中,并通过 BM25 搜索仅检索相关内容。模型可以从你上次中断的地方继续。如果你不使用 --continue,之前的会话数据会立即删除——新的会话意味着全新的开始。
  • Think in Code — The LLM should program the analysis, not compute it. Instead of reading 50 files into context to count functions, the agent writes a script that does the counting and console.log()s only the result. One script replaces ten tool calls and saves 100x context. This is a mandatory paradigm across all 14 platforms: stop treating the LLM as a data processor, treat it as a code generator. 代码化思考 — LLM 应该编写分析程序,而不是亲自计算。与其将 50 个文件读入上下文来统计函数,不如让 Agent 写一个脚本来完成统计,并仅通过 console.log() 输出结果。一个脚本可以替代十次工具调用,并节省 100 倍的上下文。这是所有 14 个平台必须遵循的范式:停止将 LLM 视为数据处理器,将其视为代码生成器。

// Before: 47 × Read() = 700 KB. After: 1 × ctx_execute() = 3.6 KB. // 之前:47 次 Read() = 700 KB。之后:1 次 ctx_execute() = 3.6 KB。

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'));
`);
  • Output Compression — Terse like caveman. Technical substance exact. Only fluff die. Drop articles, filler (just/really/basically), pleasantries, hedging. Fragments OK. Short synonyms. Code unchanged. Pattern: [thing] [action] [reason]. [next step]. Auto-expand for security warnings, irreversible actions, and user confusion. ~65-75% output token reduction with full technical accuracy. 输出压缩 — 像原始人一样简洁。技术实质精准。只剔除废话。去掉冠词、填充词(just/really/basically)、客套话和模棱两可的措辞。允许使用片段。使用短同义词。代码保持不变。模式:[事物] [动作] [原因]。[下一步]。 针对安全警告、不可逆操作和用户困惑时自动展开。在保持完全技术准确性的前提下,减少约 65-75% 的输出 Token。

Install

安装

Platforms are grouped by install complexity. Hook-capable platforms get automatic routing enforcement. Non-hook platforms need a one-time routing file copy. 平台按安装复杂度分组。支持 Hook 的平台可实现自动路由强制执行。不支持 Hook 的平台需要一次性复制路由文件。

Claude Code — plugin marketplace, fully automatic

Claude Code — 插件市场,全自动

Prerequisites: Claude Code v1.0.33+ (claude --version). If /plugin is not recognized, update first: brew upgrade claude-code or npm update -g @anthropic-ai/claude-code. 前置条件: Claude Code v1.0.33+ (claude --version)。如果无法识别 /plugin,请先更新:brew upgrade claude-codenpm update -g @anthropic-ai/claude-code

Install: 安装:

/plugin marketplace add mksglu/context-mode
/plugin install context-mode@context-mode

Restart Claude Code (or run /reload-plugins). 重启 Claude Code(或运行 /reload-plugins)。

Verify: /context-mode:ctx-doctor All checks should show [x]. The doctor validates runtimes, hooks, FTS5, and plugin registration. 验证: /context-mode:ctx-doctor 所有检查项应显示 [x]。Doctor 会验证运行时、Hook、FTS5 和插件注册情况。

Routing: Automatic. The SessionStart hook injects routing instructions at runtime — no file is written to your project. The plugin registers all hooks (PreToolUse, PostToolUse, PreCompact, SessionStart) and 11 MCP tools — six sandbox tools (ctx_batch_execute, ctx_execute, ctx_execute_file, ctx_index, ctx_search, ctx_fetch_and_index) plus five meta-tools (ctx_stats, ctx_doctor, ctx_upgrade, ctx_purge, ctx_insight). 路由: 自动。SessionStart Hook 会在运行时注入路由指令——不会向你的项目写入任何文件。该插件注册了所有 Hook(PreToolUse, PostToolUse, PreCompact, SessionStart)和 11 个 MCP 工具——六个沙盒工具(ctx_batch_execute, ctx_execute, ctx_execute_file, ctx_index, ctx_search, ctx_fetch_and_index)以及五个元工具(ctx_stats, ctx_doctor, ctx_upgrade, ctx_purge, ctx_insight)。

Slash Command

斜杠命令

What it does功能描述
/context-mode:ctx-statsContext savings — per-tool breakdown, tokens consumed, savings ratio.
/context-mode:ctx-doctorDiagnostics — runtimes, hooks, FTS5, plugin registration, versions.
/context-mode:ctx-upgradePull latest, rebuild, migrate cache, fix hooks.
/context-mode:ctx-purgePermanently delete all indexed content from the knowledge base.
/context-mode:ctx-insightPersonal analytics dashboard — 90 metrics, 37 insight patterns, 4 composite scores (productivity, quality, delegation, context health) across 23 event categories. Opens a local web UI.

Note: Slash commands are a Claude Code plugin feature. On other platforms, type ctx stats, ctx doctor, ctx upgrade, or ctx insight in the chat — the model calls the MCP tool automatically. 注意:斜杠命令是 Claude Code 插件的功能。在其他平台上,在聊天中输入 ctx stats, ctx doctor, ctx upgradectx insight——模型会自动调用 MCP 工具。

Status line (optional): Claude Code’s plugin manifest cannot declare a status line, so this is a one-time manual edit to ~/.claude/settings.json: 状态栏(可选): Claude Code 的插件清单无法声明状态栏,因此需要手动编辑一次 ~/.claude/settings.json

{
  "statusLine": {
    "type": "command",
    "command": "context-mode statusline"
  }
}

After saving, restart Claude Code. The bar shows $ saved this session · $ saved across sessions · % efficient so you can see savings accumulate in real time. 保存后重启 Claude Code。状态栏会显示 本次会话节省 $ · 累计节省 $ · % 效率,让你实时看到节省的成果。

Alternative — MCP-only install (no hooks or slash commands)

替代方案 — 仅 MCP 安装(无 Hook 或斜杠命令)

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

This gives you all 11 MCP tools without automatic routing. The model can still use them — it just won’t be nudged to prefer them over raw Bash/Read/WebFetch. Good for trying it out before committing to the full plugin. 这为你提供了所有 11 个 MCP 工具,但没有自动路由。模型仍然可以使用它们——只是不会被引导优先使用它们而非原始的 Bash/Read/WebFetch。适合在决定使用完整插件前进行试用。

Gemini CLI — one config file, hooks included

Gemini CLI — 单个配置文件,包含 Hook

Prerequisites: Node.js 18+, Gemini CLI installed. 前置条件: Node.js 18+,已安装 Gemini CLI。

Install: 安装:

  1. Install context-mode globally: npm install -g context-mode 全局安装 context-mode:npm install -g context-mode
  2. Add the following to ~/.gemini/settings.json. This single file registers the MCP server and all four hooks: 将以下内容添加到 ~/.gemini/settings.json。这个单一文件即可注册 MCP 服务器和所有四个 Hook:
{
  "mcpServers": {
    "context-mode": {
      "command": "context-mode"
    }
  },
  "hooks": {
    "BeforeTool": [ { "matcher": "run_shell_command|read_file|read_many_files|grep_search|search_file_content|web_fetch|activate_skill|mcp__plugin_context-mode", "hooks": [{ "type": "command", "command": "context-mode hook gemini-cli beforetool" }] } ],
    "AfterTool": [ { "matcher": "", "hooks": [{ "type": "command", "command": "context-mode hook gemini-cli aftertool" }] } ],
    "PreCompress": [ { "matcher": "", "hooks": [{ "type": "command", "command": "context-mode hook gemini-cli precompress" }] } ],
    "SessionStart": [ { "matcher": "", "hooks": [{ "type": "command", "command": "context-mode hook gemini-cli sessionstart" }] } ]
  }
}

Restart Gemini CLI. Verify: /m 重启 Gemini CLI。验证:/m