Sub topic 5
Scoping a Hook with Matchers
This chapter assumes you can write a short Python script, read JSON, and know that an agent calls tools such as a shell command or a file edit. You should also know that a pre-action hook receives a tool-call event on stdin and signals its decision with an exit code.
Why scope a hook
A hook with no scope runs before every tool call. That includes harmless ones like reading a file or listing a directory. Each run costs time, and a shell-command check has nothing useful to say about a file read. A matcher is a pattern attached to a hook registration that tells the agent which tools the hook applies to. If the tool name does not match, the agent skips the hook.
Scoping gives you three things:
- Less overhead, because unrelated tool calls never start your script.
- Simpler scripts, because a shell guard only sees shell events.
- Clearer intent, because the configuration shows what each hook protects.
Matching on the tool name
Most hook systems match against the tool name in the event, such as shell or write. The matcher is usually an exact name, a pattern, or a wildcard. Exact syntax and tool names differ between agents, so check your agent's documentation. The example below only shows the general shape of a registration.
{
"hooks": {
"preToolUse": [
{ "matcher": "shell", "command": "python3 guard.py" },
{ "matcher": "write", "command": "python3 path_guard.py" }
]
}
}In this setup the shell guard from the previous chapter runs only for shell calls. A second script handles file writes.
Matchers do not replace checks inside the script
A matcher is a coarse filter. It cannot look inside the command or the path. The script still has to inspect the input.
Be careful with two common errors:
- Matching too narrowly. A matcher for
shellmisses any other tool that can run commands or change files. Agents often have several: a file-write tool, an edit tool, or tools provided by external servers. List every tool that can reach what you want to protect. - Trusting the name in the event. If one script is registered for several tools, read
tool_namein the script and handle each tool's input shape. A shell event hascommand. A write event usually has a path field, whose name varies by agent.
A defensive way to share one script is to branch on the tool name and fail closed on any name you did not expect:
#!/usr/bin/env python3
import json
import sys
try:
event = json.load(sys.stdin)
tool = event["tool_name"]
tool_input = event["tool_input"]
except (json.JSONDecodeError, KeyError, TypeError):
print("Blocked: hook could not read the tool call.", file=sys.stderr)
sys.exit(2)
if tool == "shell":
command = tool_input.get("command", "")
print(f"Checking shell command: {command}", file=sys.stderr)
# run the deny-pattern checks here
elif tool == "write":
path = tool_input.get("path", "")
print(f"Checking write path: {path}", file=sys.stderr)
# run the protected-path checks here
else:
print(f"Blocked: no rule for tool {tool!r}.", file=sys.stderr)
sys.exit(2)
sys.exit(0)Blocking unknown tools is safe only if the matcher already limits which tools reach the script. Otherwise this rule would block every tool.
Exercise
Your hook is registered with the matcher "shell" and blocks writes to the .env file. A user reports the agent still changed .env. What is the most likely cause, and how do you fix it?
Show answer
The agent used a different tool, such as a file-write or edit tool, which the "shell" matcher never sends to the hook. Add matchers for every tool that can modify files, and make each script read the right input field for that tool.