context-mode
Installation

Qwen Code

Install context-mode in Qwen Code through its JSON hook surface and MCP server, using the same wire protocol as Claude Code.

Qwen Code, from the Alibaba Qwen team, uses the exact same JSON stdin/stdout hook wire protocol as Claude Code. context-mode integrates through those hooks plus an MCP server, keeping 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.

Because the wire protocol matches Claude Code, hook responses behave the same way: permissionDecision: "deny" blocks a tool, updatedInput rewrites arguments, updatedMCPToolOutput rewrites results, and additionalContext injects restored memory.

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

Install

Qwen Code has no plugin marketplace, so install the package globally first, then declare the hooks and register the server inside ~/.qwen/settings.json.

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

Add the hooks and register the server

Both the hooks and the MCP server live in a single file, ~/.qwen/settings.json. context-mode uses five of the twelve events Qwen Code emits. Each hook invokes the context-mode dispatcher, which reads the event on stdin and returns its decision on stdout. The mcpServers entry backs the ctx_* tools. The PreToolUse matcher targets Qwen Code's native tool names (run_shell_command, read_file, and so on) plus context-mode's own MCP tools and a catch-all for external MCP servers.

~/.qwen/settings.json
{
  "hooks": {
    "PreToolUse": [{ "matcher": "run_shell_command|read_file|read_many_files|grep_search|web_fetch|agent|mcp__plugin_context-mode_context-mode__ctx_execute|mcp__plugin_context-mode_context-mode__ctx_execute_file|mcp__plugin_context-mode_context-mode__ctx_batch_execute|mcp__(?!.*context-mode)", "hooks": [{ "type": "command", "command": "context-mode hook qwen-code pretooluse" }] }],
    "PostToolUse": [{ "matcher": "", "hooks": [{ "type": "command", "command": "context-mode hook qwen-code posttooluse" }] }],
    "SessionStart": [{ "matcher": "", "hooks": [{ "type": "command", "command": "context-mode hook qwen-code sessionstart" }] }],
    "PreCompact": [{ "matcher": "", "hooks": [{ "type": "command", "command": "context-mode hook qwen-code precompact" }] }],
    "UserPromptSubmit": [{ "matcher": "", "hooks": [{ "type": "command", "command": "context-mode hook qwen-code userpromptsubmit" }] }]
  },
  "mcpServers": {
    "context-mode": {
      "command": "context-mode",
      "args": []
    }
  }
}

The five hook commands are:

Hook dispatch commands
context-mode hook qwen-code pretooluse
context-mode hook qwen-code posttooluse
context-mode hook qwen-code sessionstart
context-mode hook qwen-code precompact
context-mode hook qwen-code userpromptsubmit

Restart Qwen Code after editing the file so the hooks and the server load.

Copy the routing instructions (optional)

context-mode reads project routing rules from QWEN.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/qwen-code/QWEN.md" ./QWEN.md

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

Global routing rules
cp "$(npm root -g)/context-mode/configs/qwen-code/QWEN.md" ~/.qwen/QWEN.md

How detection works

context-mode identifies Qwen Code from the MCP client info (the qwen-cli-mcp-client-* pattern), the QWEN_PROJECT_DIR environment variable, or the presence of the ~/.qwen/ config directory. Captured sessions are stored under ~/.qwen/context-mode/sessions/.

Verify

Confirm the install with the doctor. It checks language runtimes, the FTS5 knowledge base, and hook registration, 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