How to add Claude Code-style PreToolUse/PostToolUse hooks to Qwen Code?
I want to control what happens before and after each tool call in Qwen Code, like Claude Code's PreToolUse/PostToolUse hooks — for example to block destructive bash commands, force an MCP tool for files with secrets, or auto-format after edits. A GitHub issue requested this feature and proposed a concrete protocol. Has it been implemented, and what config does it use?
Originally asked on GitHub: https://github.com/QwenLM/qwen-code/issues/268
2 answers
Status: closed as completed. A maintainer confirmed in March 2026 that a basic hooks implementation was submitted in PR #1988, with more hooks to follow in #2033. The thread does not state which Qwen Code version includes it or whether the final protocol matches the proposal below.
The thread's proposal (modeled on Claude Code/Cline) is the concrete reference. Add a
hooksblock tosettings.json:json{ "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [{ "type": "command", "command": "~/.qwen/hooks/bash-guard.sh" }] } ], "PostToolUse": [ { "matcher": "Write|Edit", "hooks": [{ "type": "command", "command": "~/.qwen/hooks/auto-format.sh" }] } ] } }Proposed protocol: the hook receives JSON on stdin with
{"tool_name": ..., "tool_input": ...};matchersupports pipe-separated tool names; PreToolUse exit 0 allows and exit 2 blocks, with stdout as the message to the agent; PostToolUse exit code is ignored.Use cases from the thread: enforce a local MCP redaction server so native Read/Write/Edit never expose secrets, block risky Bash commands, auto-format after edits, and a Stop hook that checks whether tests ran. The commenter noted they recommended Cline over Qwen Code for sensitive-data work because of this gap.
Security note: the core motivation is structural enforcement so agents can't send raw secrets to cloud APIs or run destructive commands; treat hook scripts as sensitive code and review them before use.
Did this work for you?What @ppiankov tried on GitHub (Outcome unclear)
Proposed adopting the Claude Code/Cline hook protocol: settings.json hooks config with matcher/command, JSON on stdin, exit 0=allow / exit 2=block for PreToolUse, exit ignored for PostToolUse. Use cases: enforce MCP redaction tools for secret files, block dangerous bash, auto-format after edits, and a Stop hook for test verification.
Source: https://github.com/QwenLM/qwen-code/issues/268#issuecomment-3966262415 (opens external site)
Did this work for you?
Related questions
- Why did qwen-code burn millions of tokens and push my Alibaba Cloud account into debt?
- Qwen Code shows far fewer tokens than Bailian bills — how do I stop charges and get a refund?
- qwen-code with OpenRouter: force a provider and fix OpenAI connection error?
- How do I enable Agentic Coding Protocol (ACP) support for qwen-code in Zed?
- Ollama qwen3-coder fails with "does not support tools" in qwen-code agent mode