Skip to main content
hook

subagent-read-only-guard — Claude Code hook: setup and features

PreToolUse hook that restricts Claude Code subagents to read-only shell commands and blocks file modifications.

Install into Claude Code · claude-code-templates All methods and platforms ↓
npx claude-code-templates@latest --hook security/subagent-read-only-guard
Open on GitHub

What subagent-read-only-guard does

The hook inspects the agent_id attribute, which Claude Code attaches strictly to subagent tool calls. When a call originates from a subagent, the script denies Edit, Write, MultiEdit, NotebookEdit, and PowerShell. Your primary conversation thread remains untouched.

Terminal runs through Bash and Monitor must match an allowlist containing basic inspection binaries such as ls, cat, grep, rg, find, jq, sort, cd, and non-destructive git subcommands. Subshells, file redirections, shell substitutions, $VAR expansions, and git -c triggers are blocked immediately. Any command the parser fails to classify is rejected by default.

MCP tools and repository-level git diff drivers fall outside this hook boundary. Subagents can still inspect .env files unless you add another guard layer.

What it helps with

  • prevent subagents from modifying files and notebooks
  • restrict terminal commands to a read-only list like rg and cat
  • stop shell expansions, variable injections, and file redirections
  • keep exploratory subagents safely isolated from codebase changes

Example prompts for the agent

  • Launch a subagent to search for all authentication handlers under src/
  • Have a subagent review recent git commits without editing any files
  • Inspect project dependencies across subdirectories using an exploratory subagent

How to install subagent-read-only-guard

1claude-code-templates

npx claude-code-templates@latest --hook security/subagent-read-only-guard

2Manually: put the files in place

.claude/settings.json

The hooks block: event (PreToolUse, PostToolUse, Stop…) → command. Personal ones go to ~/.claude/settings.json

Step by step, with commands for each platform: Claude Code hooks: what they are, where to configure them, how to block a risky command

Questions about subagent-read-only-guard

How do I install subagent-read-only-guard in Claude Code?

Run npx claude-code-templates@latest --hook security/subagent-read-only-guard. It saves subagent-read-only-guard.py to .claude/hooks/ and configures the PreToolUse matcher in .claude/settings.json.

What does subagent-read-only-guard do?

It intercepts tool calls and checks for the subagent agent_id field. Subagents get denied whenever they attempt file edits or execute terminal commands outside a small list of read-only utilities.

What limitations does this guard have?

It does not restrict MCP tools, and it does not block subagents from reading sensitive files like .env. You will need python3 installed to run the hook script.

Source text

subagent-read-only-guard.json · MIT
{
  "description": "Enforces read-only subagents. A PreToolUse hook denies Edit, Write, MultiEdit, NotebookEdit and PowerShell, and any Bash or Monitor command that is not read-only, when the call comes from a subagent (detected by the agent_id field Claude Code adds only inside subagents; tested on Claude Code 2.1.296). Shell commands pass only when every command is a bare program from a short read-only allowlist (ls, cat, grep, rg, find, jq, sort, sed -n 'N,Mp', cd, git log/diff/status/show/blame/branch/tag and similar) without the options that make it write or run other programs, including abbreviated long options. Redirection to files, command substitution, shell expansion ($VAR, {a,b}), subshells, env-var prefixes, git -c and anything the guard cannot classify are denied, including its own errors. The main conversation is never affected. Not covered: MCP tools, and git diff/blame running diff or textconv drivers already configured in the repo. Reading secrets (.env, printenv) is still allowed, so pair it with env-file-protection. Pairs with the hooks/subagents-explore-only setting, which tells subagents up front that they are exploration-only.",
  "supportingFiles": [
    {
      "source": "subagent-read-only-guard.py",
      "destination": ".claude/hooks/subagent-read-only-guard.py",
      "executable": true
    }
  ],
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write|MultiEdit|NotebookEdit|PowerShell|Bash|Monitor",
        "hooks": [
          {
            "type": "command",
            "command": "python3 .claude/hooks/subagent-read-only-guard.py"
          }
        ]
      }
    ]
  }
}

© davila7 · MIT · davila7/claude-code-templates

Similar hooks

All hooks for Agent setup: memory, context, orchestration →

From the same set804

The whole set →

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor