Qwen Code

Make Qwen Code wait for a person's yes before every shell command, file change, web fetch and MCP tool call, with --yolo too: a PreToolUse hook set up in one command that answers inside Qwen Code's 60 seconds, verified in Qwen Code 0.28.3.

Qwen Code asks before it runs a command, unless it runs with --yolo, which is how most people run it in a script or CI. There it runs every shell command and file change without asking anyone. Qwen Code runs a PreToolUse hook before every tool call, in every mode. With the VariantGate hook there, every shell command, file change, web fetch and MCP tool call waits for a person, who answers from the inbox, a phone notification, Slack or email. Rules answer the routine ones, and every answer lands on the same record as your other agents.

The quick way: one command

With Node.js on the computer it runs on, this does both steps for you. It opens the browser to sign in (or to make your free account), saves an agent key on this computer, and adds the approval hook to ~/.qwen/settings.json, keeping the old file beside it:

npx -y https://variantgate.com/sdk/variantgate-agent-0.15.0.tgz setup qwen-code

Restart Qwen Code and ask it to run echo hello: the command waits until you approve it in the inbox, and so does every file change, web fetch and MCP tool call, with --yolo too. A no blocks that one call. A file it cannot change safely, such as one with comments or one that links elsewhere, is left alone, and the command prints the entry to add yourself. The steps below do the same by hand.

1. Create a key

Sign in, open Agents and create a key for Qwen Code. Copy it: it is shown once. Put it in your shell profile, such as ~/.zshrc, as export VARIANTGATE_KEY=…. Qwen Code passes it on to its hooks.

2. Add the hook

Install the variantgate command (more on the SDK page):

npm install -g https://variantgate.com/sdk/variantgate-agent-latest.tgz

The hook reads VARIANTGATE_KEY from the environment Qwen Code starts in, or a key saved on this computer:

Or run variantgate login once on this computer instead. It opens the browser, you check a code and press Connect, and it saves a new agent key in ~/.config/variantgate/credentials.json, readable only by you. The hook uses that key, with the API address it was made for, whenever VARIANTGATE_KEY is not set. The MCP server still needs the key from step 1.

Add this to ~/.qwen/settings.json (under $QWEN_HOME when that is set), next to what is there:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "^(?!(?:read_file|zoom_image|grep_search|glob|list_directory|lsp|todo_write|search_memory|ask_user_question|enter_plan_mode|exit_plan_mode|agent|list_agents|task_list|tool_search|structured_output|get_goal|display_image)$).*$",
        "hooks": [
          {
            "type": "command",
            "command": "variantgate hook qwen-code",
            "name": "variantgate",
            "timeout": 60,
            "env": { "VARIANTGATE_QWEN_HOOK_WAIT": "30" }
          }
        ]
      }
    ]
  }
}

The matcher picks every tool but the ones that only look, read or plan: read_file, zoom_image, grep_search, glob, list_directory, lsp, todo_write, search_memory, ask_user_question, enter_plan_mode, exit_plan_mode, agent, list_agents, task_list, tool_search, structured_output, get_goal, display_image. Any other tool, including one a later version adds, waits for a yes. Starting a subagent (agent) is not asked about because its own tool calls reach the hook one by one. Restart Qwen Code, or open /hooks once in a running session, to load the change. Qwen Code hands the hook the call on stdin:

{
  "hook_event_name": "PreToolUse",
  "permission_mode": "yolo",
  "tool_name": "run_shell_command",
  "tool_input": { "command": "npm test" },
  "cwd": "/home/ada/shop"
}

Each call becomes a request with an action name that rules can match:

shell.run
A run_shell_command call, with the command and the folder. The same name the Claude Code, Codex, Gemini CLI, Cursor, Copilot and Cline hooks use, so one rule covers them all.
file.write
A write_file call, with its path and text.
file.edit
An edit or notebook_edit call.
web.fetch
A web_fetch call, with the address.
web.search
A web_search call, with the query.
mcp.<server>.<tool>
A call to an MCP tool, such as mcp.shop.refund_order for Qwen Code's mcp__shop__refund_order.
tool.<name>
Any other tool, such as save_memory, cron_create, exec in code mode, or one a later version adds.

A no blocks that one call: the model gets the reason the person gave as the tool’s result, and the session goes on. An approval does not skip Qwen Code’s own permissions, so a command your settings deny stays denied.

What Qwen Code does when a hook fails

We ran the hook in Qwen Code 0.28.3 against a scripted model and a local copy of the service, with --yolo. Qwen Code runs the call when a hook passes its timeout, exits with any code but 0 or 2, prints something that is not JSON, or cannot be started at all. We found no setting that makes it fail closed. And whatever a hook’s timeout says, Qwen Code stops waiting for its answer after 60 seconds and runs the call: a hook set to 150 seconds that refused at 70 was too late, and the command ran. So the hook always answers inside 60 seconds:

  • It waits VARIANTGATE_QWEN_HOOK_WAIT seconds for a person (30, or 25 when npx starts it), then withdraws the request and blocks the call. It never waits longer than 35, whatever the setting says, so that starting up, reading the call and taking back the request still fit inside the 60 seconds.
  • If the service cannot be reached, or stops answering, the call is blocked a few seconds after the wait, and a request it already opened is taken back.
  • With no key set or saved, the hook exits 2, which Qwen Code reads as a block.
  • What the hook cannot cover is itself not starting. If the variantgate command is missing, or npx cannot fetch it with no network, Qwen Code runs the call. Install the command rather than relying on npx for an agent that runs unattended.

A person has about half a minute to answer. Let rules approve the routine calls so the ones that reach a person are few, and keep notifications on your phone. A call nobody answered is blocked, and the model can ask again.

MCP tool names

Qwen Code hands the hook an MCP tool as mcp__<server>__<tool>, the shape Claude Code uses, so the request has the mcp.<server>.<tool> action the other hooks send, such as mcp.shop.refund_order, and one rule covers the tool in every agent. A name with characters other than letters, digits, _ and - has them replaced and a short hash added: a server named my.shop arrives as mcp__my_shop__refund_order_1jzbn48, so its action is mcp.my_shop.refund_order_1jzbn48. Name such a tool in a rule as it appears in the inbox, or rename the server in Qwen Code’s settings.

The hook puts every call in front of a person, including calls to the approvals MCP server if you add it to Qwen Code as well, so use one or the other.

In scripts and CI

A qwen -p run with --yolo asks nobody, so the hook is the whole approval step there. Without --yolo, a -p run refuses shell commands by itself, and a hook’s yes does not change that. Give the run a key, and install the variantgate command on the machine so the hook can always start:

export VARIANTGATE_KEY=...   # a key for this pipeline
qwen -p "Fix the failing test and push a branch" --yolo

A run that pauses for a person holds the pipeline for up to the wait. Plan for a no: the model is told the call was refused and may try something else, so check the job’s result rather than its exit code.

Other settings files

Qwen Code also reads hooks from a project’s .qwen/settings.json, but runs them only after you approve them once in an interactive session, and never in a -p run that has not. It does not read Claude Code’s hooks, so the Claude Code hook in ~/.claude/settings.json does not ask twice. disableAllHooks, --safe-mode and --bare turn every hook off, this one included, and so does running Qwen Code on an SSH workspace or as a read-only agent host; setup qwen-code warns when the settings turn hooks off. We tested the CLI on macOS; the IDE companions and Windows we have not tested.

Check that it works

Start Qwen Code and ask it to run echo hello. A request appears in the inbox; deny it and check that hello is not printed. Then ask again and approve it.