context-mode
Installation

OpenCode

Install context-mode in OpenCode as a native TypeScript plugin, with the surrogate session hooks the host requires.

OpenCode loads context-mode as a native TypeScript plugin rather than JSON stdin/stdout hooks:

  • The plugin array registers all 11 ctx_* tools in-process and wires the hooks, so there is no separate MCP child per session.
  • The plugin keeps the bulk of raw payload bytes out of your window by routing data-heavy output into the sandbox and answering from the FTS5 knowledge base.

Hooks run as in-process functions. OpenCode exposes no real SessionStart hook (#14808), so context-mode uses two surrogates:

In-process hookStands in forRole
tool.execute.beforePreToolUseEnforces routing and can block
tool.execute.afterPostToolUseRecords post-execution telemetry
experimental.session.compactingPreCompactBuilds resume snapshots
experimental.chat.system.transformSessionStart (surrogate)Injects the routing block and restores prior-session snapshots (skipped when the system prompt already carries context-mode instructions, for example from AGENTS.md, to avoid duplication)
chat.messageUserPromptSubmit (surrogate)Records each request and decision for later search
event (generic bus)—Captures per-turn token and cost telemetry as messages update

context-mode runs 100% locally with zero telemetry. The sandbox, indexing, and search all stay on your machine.

Install

OpenCode has no plugin marketplace, so install the package globally first, then point the host config at it.

Check the prerequisites

Install Node 22.5 or newer, or Bun. The sandbox and FTS5 knowledge base depend on a current runtime.

Check your runtime
node --version

Install the package

This installs the plugin entry point and the context-mode command line.

Global install
npm install -g context-mode

Register the plugin

Add the context-mode plugin to opencode.json in your project root (or ~/.config/opencode/opencode.json for a global install). The native TypeScript plugin registers all ctx_* tools in-process, so you do not add a separate mcp entry.

opencode.json
{
  "$schema": "https://opencode.ai/config.json",
  "plugin": ["context-mode"]
}

Restart OpenCode after editing the config so the plugin loads.

Adding a separate mcp entry makes OpenCode register zero ctx_* tools — the plugin suppresses MCP duplicates, but a legacy mcp.context-mode entry confuses the loader.

Copy the AGENTS.md routing file into your project so the model knows which tools to use and which commands are blocked.

Copy the routing rules
cp node_modules/context-mode/configs/opencode/AGENTS.md AGENTS.md

On OpenCode the tool.execute.before hook can enforce routing but cannot inject context back to the model. Without AGENTS.md the hooks still block data-heavy commands — the model just won't know why a command was denied.

Configure

The plugin-only config above needs no extra environment variable:

  • The native plugin already exposes the ctx_* tools in-process.
  • It sets CONTEXT_MODE_EMBEDDED_PLUGIN_TOOLS=1 for itself.

If you ever run the standalone MCP server alongside the plugin (for example, a leftover mcp.context-mode entry), set this variable where the spawned server can read it — an env block in the opencode.json MCP entry, not a transient shell export — so the duplicate copy of each tool is suppressed:

opencode.json (only if you also run the standalone server)
{
  "$schema": "https://opencode.ai/config.json",
  "plugin": ["context-mode"],
  "mcp": {
    "context-mode": {
      "type": "local",
      "command": ["context-mode"],
      "env": { "CONTEXT_MODE_EMBEDDED_PLUGIN_TOOLS": "1" }
    }
  }
}

For the normal plugin-only install, prefer removing the mcp entry entirely (run context-mode upgrade) over running both. See /docs/configuration for the full list of environment variables, storage paths, and permission rules.

Verify

Confirm the install with the doctor. It checks language runtimes (including the Linux Node 22.5+ SIGSEGV gate), the FTS5 knowledge base, the hooks configuration, and plugin registration, then reports anything that needs attention.

Verify the setup
context-mode doctor

A clean run means the plugin is loaded and context-mode is ready to route your next data-heavy command.

If the ctx_* tools appear empty after install, a legacy mcp.context-mode entry from an earlier version is usually conflicting with the native plugin — with both present, OpenCode registers zero ctx_* tools. Run context-mode upgrade to reconcile the config and remove the legacy server entry; other MCP servers are preserved (v1.0.140+ also emits a stderr diagnostic when this happens). Restart OpenCode once it finishes. See /docs/troubleshooting for related empty-tool symptoms.

Verify your savings

These two checks confirm context-mode is actually saving you tokens, not just that the tools loaded.

Check the session savings

In the OpenCode session, type ctx stats to your agent. It reports tokens saved and the context-window savings ratio for the session, plus a per-tool token breakdown — so you can see routing is keeping raw data out of your window.

In the OpenCode chat
ctx stats

Confirm hooks and MCP are registered

Run the doctor to confirm context-mode is in the plugin array of opencode.json and that the hooks and ctx_* tools are wired. It warns on a legacy mcp.context-mode block and prints context-mode upgrade as the fix.

Confirm registration
context-mode doctor

OpenCode has no persistent status line, so ctx stats in chat is the quickest read on your live savings.

On this page