context-mode
Installation

Kimi Code

Install context-mode in the Kimi Code CLI through its full TOML hook surface and MCP server.

The Kimi Code CLI exposes a full hook surface, so context-mode integrates through TOML-declared hooks plus an MCP server. Together they keep 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.

context-mode registers seven hooks on Kimi Code: PreToolUse, PostToolUse, SessionStart, SessionEnd, PreCompact, UserPromptSubmit, and Stop. The wire protocol is JSON stdin/stdout, the same one Codex CLI uses.

SessionEnd is distinctive on Kimi Code. It is a genuine session-close event, separate from Stop, so context-mode can finalize and index a session when it actually ends rather than at every turn boundary.

On Kimi Code, PreToolUse is deny-only. permissionDecision: "deny" (or exit code 2) blocks a tool, but permissionDecision: "ask", argument rewrites, and additionalContext are silently dropped by the host runner. context-mode injects restored memory through SessionStart and PostToolUse instead.

Install

Kimi Code has no plugin marketplace, so install the package globally first, then declare the hooks and register the server. Hooks live in ~/.kimi-code/config.toml and the MCP server is registered in ~/.kimi-code/mcp.json. If you set KIMI_CODE_HOME, both files live under that directory instead.

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 hook dispatcher, the MCP server, and the context-mode command line.

Global install
npm install -g context-mode

Register the MCP server

Register the context-mode server in ~/.kimi-code/mcp.json, which is JSON. This backs the ctx_* tools the agent calls to run code and search the knowledge base. The bare binary with no arguments starts the MCP server.

~/.kimi-code/mcp.json
{
  "mcpServers": {
    "context-mode": {
      "command": "context-mode",
      "args": []
    }
  }
}

Add the hooks

The hook config lives in ~/.kimi-code/config.toml and is TOML. Hooks are declared as a TOML array of tables, so add one [[hooks]] entry per event. Each one invokes the context-mode dispatcher, which reads the event on stdin and returns its decision on stdout. The dispatcher takes the platform first, then a lowercase event name: context-mode hook kimi <event>.

~/.kimi-code/config.toml
[[hooks]]
event = "PreToolUse"
matcher = "Bash|Shell|Read|Edit|Write|WebFetch|Agent|ctx_execute|ctx_execute_file|ctx_batch_execute|ctx_fetch_and_index|ctx_search|ctx_index|mcp__"
command = "context-mode hook kimi pretooluse"
timeout = 30

[[hooks]]
event = "PostToolUse"
command = "context-mode hook kimi posttooluse"
timeout = 30

[[hooks]]
event = "SessionStart"
command = "context-mode hook kimi sessionstart"
timeout = 30

[[hooks]]
event = "SessionEnd"
command = "context-mode hook kimi sessionend"
timeout = 30

[[hooks]]
event = "PreCompact"
command = "context-mode hook kimi precompact"
timeout = 30

[[hooks]]
event = "UserPromptSubmit"
command = "context-mode hook kimi userpromptsubmit"
timeout = 30

[[hooks]]
event = "Stop"
command = "context-mode hook kimi stop"
timeout = 30

Restart Kimi Code after editing both files so the hooks and the server load.

Copy the routing instructions (optional)

context-mode reads project routing rules from AGENTS.md. Copy the shipped file so the model knows to send data-heavy work into the sandbox.

Per-project routing rules
cp "$(npm root -g)/context-mode/configs/codex/AGENTS.md" ./AGENTS.md

For global use, copy it into the Kimi Code config directory instead:

Global routing rules
CM_ROOT="$(npm root -g)/context-mode"; cp "$CM_ROOT/configs/codex/AGENTS.md" ~/.kimi-code/AGENTS.md

How detection works

context-mode identifies Kimi Code from the ~/.kimi-code/ config directory (or KIMI_CODE_HOME when set). Captured sessions are stored under ~/.kimi-code/context-mode/sessions/.

Verify

Confirm the install with the doctor. It checks for the Kimi Code CLI on your PATH, the language runtimes, the FTS5 knowledge base, and each hook entry in config.toml, then reports anything that needs attention.

Verify the setup
context-mode doctor

A clean run means every hook is registered and context-mode is ready to route your next data-heavy command.

On this page