Guard Hooks
Guard Hooks evaluate coding-agent prompts and tool activity against your runtime guards. Before the agent sends a prompt or runs a tool, the hook asks Willow for a verdict and applies it: the agent goes ahead, asks the user first, masks what a guard flagged, or stops.
There are two ways to install them:
- Through the Scan Agent (recommended). You configure hooks once in AI Discovery Settings > Guard hooks, and the Scan Agent writes them into each user's coding agents and keeps them up to date.
- With the Willow Guard Hooks plugin, for Claude Desktop and Cowork or for devices without the Scan Agent. See Install the plugin manually.
Use one or the other for Claude Code, Cursor, and Codex. Running both evaluates every prompt and tool call twice.
Supported agents
| Agent | Coverage |
|---|---|
| Claude Code | Prompts, tool calls, PII and secret masking on tool output, skill and slash-command use, and asking before Willow write tools |
| Cursor | Prompts, tool calls, and tool-output context |
| Codex | Prompts, tool calls, and PII and secret masking on tool output |
| GitHub Copilot CLI | Prompts, tool calls, and PII and secret masking on tool output |
| Pi | Prompts and tool calls, through the Willow Pi extension |
Prerequisites
- Admin access to Willow.
- At least one active runtime guard, so the hooks have something to enforce.
- The Scan Agent installed and connected on each device.
- For Claude Code, Cursor, and Codex: the Willow Guard Hooks plugin removed from your organization. See Remove the Guard Hooks plugin first.
Set up guard hooks with the Scan Agent
- Open Discover > Shadow AI, then More actions > Settings > Guard hooks. You can also open Control > Guards > Setup & Coverage and select Configure in AI Discovery beside a coding agent, which opens the same tab at that agent's card.
- Turn on the switch on the agent's card.
- Expand the card and choose which hooks run. For a tool hook, choose the tools it runs on.
- Optionally, select Everyone next to the switch to limit the agent's hooks to some groups.
- Wait for the devices to pick up the change, or select Sync Now in the Scan Agent menu bar app. Then restart the coding agent, which only loads hooks at startup. See When devices get changes.
- Send a prompt or run a tool that one of your guards flags, and check the decision in Runtime > Guard flags or Monitor > Logs.

The Guard hooks tab
The tab has one card per coding agent. Depending on your beta features, it also shows Evaluate on the device above the cards, and a warning when the Guard Hooks plugin is still installed.
Agent cards
Each card has:
- The switch, which turns the agent's hooks on or off for every device in scope. When it's on, the card shows how many hooks are included.
- The groups button, which reads Everyone until you pick groups. A device whose linked user isn't in any of the groups doesn't get the agent's hooks, and neither does a device with no linked user.
- A row per hook. Include or exclude each one. Prompt hooks run on every prompt.
- Runs on tools, under each tool hook: the tools the hook runs on. Pick from the agent's own tool names, select Add a custom tool for one that isn't listed, or open Raw matcher expression to edit the pipe-separated matcher directly, for example
Read|Write|Edit. - Block when Willow can't be reached, under the Tool calls hook. See When Willow can't be reached.
- Ask before write tools, on the Claude Code card only, when the beta is on for your organization. See Ask before write tools in Claude Code.
| Agent | Hooks | Default tools |
|---|---|---|
| Claude Code | Prompts, Tool calls, Tool output, Skills and commands | Tool calls: Read|Write|Edit|MultiEdit|NotebookEdit. Tool output: Bash|Read. Skills and commands records which skills and slash commands people use, for Analytics. |
| Cursor | Prompts, Tool calls, Tool output | Tool calls: Read|Write. Tool output: Read|Shell. Cursor can't rewrite tool output, so this hook adds context for the model after Read or Shell instead. |
| Codex | Prompts, Tool calls, Tool output | Tool calls: Read|Write|Edit|apply_patch. Tool output: Shell|Read. |
| GitHub Copilot CLI | Prompts, Tool calls, Tool output | Tool calls: view|create|edit|str_replace_editor|apply_patch. Tool output: bash|powershell|view. |
| Pi | Prompts, Tool calls | Tool calls: bash|read|write|edit. |
In the GitHub Copilot CLI, a blocked prompt is refused before it reaches the model, and a redacted prompt reaches the model with the matched text masked. Copilot can't rewrite a tool call's arguments, so when a guard redacts a tool call, the call is denied and the model is asked to retry it with the flagged values replaced by a placeholder.
Hooks Willow adds for you
Some features need a hook on tools you didn't pick. The Scan Agent adds it to what it installs, without changing what the card shows:
- An active CLI guard, or a runtime policy rule on a CLI, needs the agent's shell tool:
Bashin Claude Code,Shellin Cursor,Bash|Shellin Codex,bash|powershellin the Copilot CLI, andbashin Pi. - A runtime policy rule on an MCP tool needs the agent's MCP tools in Claude Code, Cursor, and Codex.
- Ask before write tools needs Claude Code's MCP tools (
mcp__.*).
You don't need to change the Tool calls matcher for any of these.
Evaluate on the device
Evaluate on the device appears when the Run Guards on the Device beta is on. With it on, the Scan Agent checks Claude Code, Cursor, and Codex hooks against your regex and JSONata guards on the device, without calling Willow. Willow is called only when a guard needs an AI model or a built-in detector that the device check didn't settle. Everything a guard catches is still logged and alerted on.
It doesn't apply to the GitHub Copilot CLI, Pi, or the gateway. Devices send every hook to Willow instead while your organization has active runtime policy rules or Ask before write tools is on, because those are evaluated on the server.
Remove the Guard Hooks plugin first
If the Willow Guard Hooks plugin is still in your organization's plugin list, the tab shows Remove the Guard Hooks plugin first, and turning on Claude Code, Cursor, or Codex asks you to remove it. The plugin and the Scan Agent hooks subscribe to the same events, so keeping both evaluates every prompt and tool call twice. Pi and the GitHub Copilot CLI are not affected.
- Select Remove plugin to remove it from Plugins.
- On machines that already have the plugin, uninstall it and restart the agent. In Claude Code, run
/plugin uninstall willow-guard-hooks@willow-guards-marketplace. In Cursor, remove it from the plugin list. In Codex, runcodex plugin uninstall willow-guard-hooks@willow-guards-marketplace. - Turn on the agents' switches on the Guard hooks tab.
Ask before write tools in Claude Code
This needs the Ask Before Write Tools in Claude Code beta feature. Contact Willow Support to turn it on for your organization.
With Ask before write tools on, Claude Code asks the user before it runs a Willow tool that can change data. Read-only tools run without a prompt.
Turn it on
- Open Discover > Shadow AI, then More actions > Settings > Guard hooks.
- Turn on Claude Code and expand its card.
- Turn on Ask before write tools, at the bottom of the card. It saves right away.
- Optionally, write an Approval message (up to 500 characters). It saves when you leave the field.
- Let devices pick up the change, or select Sync Now, then restart Claude Code.
You don't need to change the Tool calls matcher: the Scan Agent adds a Claude Code hook on MCP tools (mcp__.*) for this feature.
Which tools ask
Each MCP server's Tools tab splits its tools into Read-only tools and Write/delete tools. Tools in Write/delete tools ask, and tools in Read-only tools don't. A tool is read-only when its readOnlyHint annotation is set. Disabled tools never ask, and neither do tools set to Require Approval: Willow already asks the user to approve those, so Claude Code doesn't ask a second time.
Willow recognizes its tools by the end of the name Claude Code gives them. Claude Code names an MCP tool mcp__<server>__<tool>, and Willow names its tools <integration>__<tool>, so mcp__claude_ai_Slack__slack__send_message is the Send message tool of the slack MCP server. This works whatever name the user gave the Willow server in Claude Code. Tools of servers that aren't in Willow are left alone.
Through Willow mesh, every tool runs through one execute_tool tool, so Willow reads which tool it runs from the call's mcp_slug and tool_slug. A mesh call to a write tool asks, and the lookup tools (search_tools, get_mcp_servers, get_mcp_tools, and get_tool_details) never do.
What the user sees
Claude Code shows its permission prompt for the call, with the reason:
Approval required by Willow guard(s): Willow write tool slack/send_message. Confirm you want to proceed.
With an approval message, the message replaces Willow write tool slack/send_message. Write {{matchedText}} where the tool should go. For example, Check {{matchedText}} before you continue. shows:
Approval required by Willow guard(s): Check slack/send_message before you continue. Confirm you want to proceed.
If one of your guards blocks the same call, the block wins.
Good to know
- Each ask is logged in Monitor > Logs and sends alerts like a guard's Warning.
- Changes to the switch or the message reach devices that already have the hook within a minute. A new tool, or a tool moved between Read-only tools and Write/delete tools, takes up to 5 minutes.
- While it's on, devices don't evaluate on the device.
- It covers Claude Code only. Other agents, and calls made straight to the Willow gateway, aren't asked.
- A tool from a server outside Willow is also asked if its name ends with the same
<integration>__<tool>as a Willow write tool.
When devices get changes
The Scan Agent installs and updates hooks as part of its scheduled scan: when it starts, then every 30 minutes by default, and only while the user is active. Willow can take up to 5 minutes to serve new settings to a scheduled scan.
Sync Now in the Scan Agent menu bar app gets the latest settings right away. Coding agents load hooks at startup, so restart the agent afterwards.
When you turn an agent off, the Scan Agent removes its own entries on the next scan and leaves the rest of the file, including your own hooks, in place. For where each agent's hooks are written, see Provision guard hooks.
When Willow can't be reached
Guard hooks fail open by default: if Willow can't be reached or doesn't answer within the 15-second timeout, the agent goes ahead, so a network problem never stalls a session.
To deny tool calls instead, turn on Block when Willow can't be reached under a Tool calls hook. A tool call the hook matches is then denied when the guard check can't complete (offline, timeout, or an evaluation error). It's off by default, isn't offered on Prompts, Tool output, or Pi hooks, and takes effect once the device runs a Scan Agent version that supports it.
Troubleshooting
Nothing is evaluated. Check that the agent's switch is on and the device's user is in the card's groups. Run Sync Now, restart the coding agent, and make sure a guard that should fire is active.
Every prompt or tool call is evaluated twice. The Guard Hooks plugin is still installed alongside the Scan Agent hooks. Remove it.
A tool you expected isn't checked. Add it under Runs on tools on the Tool calls hook, using the name the agent uses for it.
Claude Code doesn't ask before a Willow write tool. Check that Ask before write tools is on, that the tool is listed under Write/delete tools on its MCP server's Tools tab, that the tool is enabled, and that it isn't set to Require Approval. Then run Sync Now and restart Claude Code.
Install the plugin manually
The Willow Guard Hooks plugin provides downloadable hooks for Claude Code, Cursor, and Codex. Use it for client-side enforcement in Claude Desktop and Cowork, or where the Scan Agent doesn't run. Settings on the Guard hooks tab, including Ask before write tools, don't apply to the plugin.
Step 1: Activate the plugin
Open Control > Guards > Setup & Coverage. Beside Claude Desktop & Cowork, select Set up, then Guard Hooks plugin.
- If the plugin has not been created yet, a dialog explains that guard hooks aren't active. Click Activate hooks to create the Willow Guard Hooks plugin for your organization and open its plugin page.
- If the plugin already exists, you go straight to its plugin page.
The plugin starts as a draft. Publish it when you are ready to make it available through your marketplace.
Step 2: Choose which hooks run
Choose which hooks to include and configure their matchers:
- Toggle each hook Included / Excluded.
- Tune the tool matcher for tool-use hooks (a pipe-separated list of tool names the hook runs on, e.g.
Read|Write|Edit). Prompt hooks run on every prompt and have no matcher. - For Claude Code tool-use hooks, optionally turn on Block when Willow can't be reached. If the hook times out or fails, Claude Code blocks the tool call instead of letting it run. It is off by default and needs Claude Code 2.1.295 or later.
- Edit the plugin name and description, then Save.
- Download an installation bundle for Claude Code, Cursor, or Codex. The bundle includes your organization token.
These settings apply to the managed configuration (Step 4) and the plugin bundle.
Logging
Two organization-wide switches in Admin > Settings > Log Settings control hook records in Monitor > Logs:
- Log Passed Evaluations: record passing evaluations as well as flagged evaluations. Passing evaluations do not send alerts.
- Log Content: when off, the scanned prompt or tool content and matched snippets are stripped from logged messages. The verdict and which guards fired are still recorded.
The plugin page's Logging card reflects the current state of both and links to the settings page. They apply to Claude Code, Cursor, and Codex hooks.
Step 3: Deliver the org token via MDM
The hooks authenticate with your organization token. Distribute it through the Scan Agent MDM profile.
If a machine already has the Willow Scan Agent profile, the token is already present and the guard hook reads it directly. You can skip this step for those machines.
macOS: configuration profile (.mobileconfig)
Download the profile from the Scan Agent setup flow (pre-filled for your organization) and push it through your MDM (Jamf, Kandji, Mosyle, or Intune). It writes the token to the com.mcp-s-scan.agent managed-preferences payload. See Deploy the Scan Agent with MDM.
Windows: registry policy (.reg)
Import the registry policy via GPO or deploy it with Intune. It writes the token to HKLM\SOFTWARE\Policies\MCP-S-Scan, which the PowerShell hook reads.
These files contain your organization auth token. Treat them as secrets and distribute them only through your MDM.
Token resolution order
The hook uses the first available token source in this order:
- Willow Scan Agent profile (
com.mcp-s-scan.agent): the token delivered by the Scan Agent's MDM profile, either the macOS.mobileconfigor the Windows.reg(HKLM\SOFTWARE\Policies\MCP-S-Scan). Any machine that already runs the Scan Agent needs nothing extra. - Plugin bundle token file: included in the downloaded plugin for marketplace or local installation (Step 4).
WILLOW_GUARD_TOKENenvironment variable: a fallback for dev shells and env-based deploys (for example Linux).
Device identity is detected automatically at runtime (the same identity source as the Scan Agent). Optionally set WILLOW_USER_EMAIL to attribute audit decisions to a user; Cursor also forwards the authenticated user's email in the hook payload when available.
Step 4: Enforce org-wide
Deploy the configured hooks through managed configuration or your plugin marketplace.
Managed configuration
- Claude Code: push the plugin's hooks through Claude Code's
managed-settings.jsonusing the policy builder, which validates the full schema and exports an MDM-ready profile. The token comes from Step 3. - Cursor: deploy the hooks to Cursor's enterprise
hooks.jsonsystem path via MDM so they apply to every user on the machine. - Codex: deploy
hooks.jsonto~/.codex/hooks.jsonor merge the generated hooks into the[hooks]table inconfig.toml.
Plugin marketplace
Publish the Willow Guard Hooks plugin to your marketplace and mark it enforced. Agents install it on launch, with the token already delivered from Step 3.
All three agents fail open by default: on a hook timeout or if Willow is unreachable, the agent proceeds. Blocks apply only when Willow responds with a guard block, unless you turned on Block when Willow can't be reached for a tool-call hook.
Plugin hooks reference
Claude Code: merge the copied fragment into your Claude Code settings (user, project, or managed), keeping any existing hook groups.
| Event | Tools | Purpose |
|---|---|---|
UserPromptSubmit | — | Evaluate every prompt |
PreToolUse | Read, Write, Edit, MultiEdit, NotebookEdit | Evaluate file tool calls |
PostToolUse | Bash, Read | Mask PII and secrets in tool output |
UserPromptExpansion | — | Record skill and slash-command use for Analytics |
Cursor: save the hooks.json as project .cursor/hooks.json, user ~/.cursor/hooks.json, or the enterprise system path, keeping any existing hooks.
| Event | Tools | Purpose |
|---|---|---|
beforeSubmitPrompt | — | Evaluate every prompt |
preToolUse | Read, Write | Evaluate file tool calls |
postToolUse | Read, Shell | Add context for the model after tool output |
Codex: save the generated hooks.json as ~/.codex/hooks.json, keeping any existing hook groups.
| Event | Tools | Purpose |
|---|---|---|
UserPromptSubmit | — | Evaluate every prompt |
PreToolUse | Read, Write, Edit, apply_patch | Evaluate file tool calls |
PostToolUse | Shell, Read | Mask PII and secrets in tool output |
The hooks authenticate with Authorization: Bearer <org-token>, using the token source described above. Hooks load at startup, so restart or reload the agent after changing settings.