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:
| Response key | Effect |
|---|---|
permissionDecision: "deny" | Blocks a tool. |
updatedInput | Rewrites arguments. |
updatedMCPToolOutput | Rewrites results. |
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. You also need Qwen Code itself installed.
node --versionIf you do not already have Qwen Code, install it globally:
npm install -g @qwen-code/qwen-codeInstall the package
This installs the hook dispatcher, the MCP server, and the context-mode
command line.
npm install -g context-modeAdd 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
mcpServersentry backs thectx_*tools. - The
PreToolUsematcher targets Qwen Code's native tool names (run_shell_command,read_file, and so on) plus a catch-all for external MCP servers (mcp__(?!.*context-mode)). - On Qwen Code, context-mode's own tools are named
mcp__context-mode__ctx_executeand so on; the explicitmcp__plugin_context-mode_context-mode__ctx_*entries in the matcher are the Claude Code marketplace form, carried over verbatim.
Rather than hand-tune the matcher, run context-mode upgrade so it stays in
sync with the adapter.
{
"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": "run_shell_command|read_file|write_file|edit|glob|grep_search|todo_write|agent|ask_user_question|mcp__", "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 dispatched hook commands are:
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 userpromptsubmitcontext-mode also wires a 6th hook — Stop — to capture per-turn token cost.
Token usage is unreachable through hook stdin, so the Stop script tails the Qwen
chat log. The five events above cover routing and session continuity even without
it.
The dispatcher form context-mode hook qwen-code stop is not wired today,
so do not hand-add it to settings.json — it would silently no-op. Run
context-mode upgrade instead: it writes all six hooks, including Stop as a
direct node-script command (hooks/qwen-code/stop.mjs) that fires correctly.
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.
cp "$(npm root -g)/context-mode/configs/qwen-code/QWEN.md" ./QWEN.mdFor global use, copy it into the Qwen Code config directory instead:
cp "$(npm root -g)/context-mode/configs/qwen-code/QWEN.md" ~/.qwen/QWEN.mdHow detection works
context-mode identifies Qwen Code from any of these signals:
- The MCP client info (the
qwen-cli-mcp-client-*pattern). - The
QWEN_PROJECT_DIRenvironment variable. - The presence of the
~/.qwen/config directory.
Captured sessions are stored under ~/.qwen/context-mode/sessions/.
Verify your savings
Confirm context-mode is actually saving you tokens, not just that the tools respond.
Check the savings in chat
Type ctx stats in Qwen Code. It reports the context-window tokens saved this
session — the savings ratio plus a per-tool token breakdown — so you can watch
routing pay off after a few data-heavy operations.
Confirm hooks and MCP are registered
Run the doctor. It confirms all six hooks in ~/.qwen/settings.json and that
context-mode is in mcpServers, rendering each check as an [OK], [FAIL],
or [WARN] line.
context-mode doctorA missing hook reports a [FAIL] whose fix reads
Add <HookName> hook to ~/.qwen/settings.json. Re-running context-mode upgrade
rewrites every hook and is the quickest way to clear those failures.
A clean doctor run plus a climbing savings ratio means context-mode is ready to route your next data-heavy command.