Skip to content
← All guides

Cursor hooks: events, hooks.json and a blocking example

How Cursor hooks work. The hooks.json locations, the events, the allow, ask and deny output, exit code 2 and a working example that blocks a command.

4 min read

Cursor hooks run your own script at set points in the agent loop, such as before a shell command or before a file is read. You define them in a hooks.json file, either at ~/.cursor/hooks.json for you or .cursor/hooks.json in a project. A hook for beforeShellExecution receives the command as JSON and can answer allow, deny or ask. Exit code 2 also blocks the action. Other failures let the action proceed unless you set failClosed.

Where the config lives

Cursor watches hooks files and reloads them. If hooks do not load, restart Cursor. Project hooks run only in trusted workspaces. Hook output appears in the Hooks tab in Customize and in the Hooks output channel.

LevelLocation
Enterprise/Library/Application Support/Cursor/hooks.json on macOS
TeamWeb dashboard, enterprise only
Project<project-root>/.cursor/hooks.json
User~/.cursor/hooks.json

Priority runs Enterprise, then Team, then Project, then User. All matching hooks run and their answers merge. Deny beats ask, and ask beats allow.

The file needs "version": 1 and a hooks object that maps event names to arrays. Each entry takes a command, and optionally type, timeout in seconds, matcher as a regex, loop_limit for stop events and failClosed.

Events

Cursor documents 21 events. Agent events are sessionStart, sessionEnd, preToolUse, postToolUse, postToolUseFailure, subagentStart, subagentStop, beforeShellExecution, afterShellExecution, beforeMCPExecution, afterMCPExecution, beforeReadFile, afterFileEdit, beforeSubmitPrompt, preCompact, stop, afterAgentResponse and afterAgentThought. Tab events are beforeTabFileRead and afterTabFileEdit. workspaceOpen fires when a workspace opens.

These events return a permission field that can stop an action: preToolUse, beforeShellExecution, beforeMCPExecution, beforeReadFile, beforeTabFileRead and subagentStart. A few limits apply. ask is not enforced for preToolUse. subagentStart treats ask as deny. The two file-read events accept only allow or deny. beforeSubmitPrompt uses a continue boolean instead.

Example that blocks a command

This config runs a script for any shell command that starts with git.

{
  "version": 1,
  "hooks": {
    "beforeShellExecution": [
      {
        "command": ".cursor/hooks/block-git.sh",
        "matcher": "^git ",
        "failClosed": true,
        "timeout": 10
      }
    ]
  }
}

The script reads the input and prints a deny answer.

#!/bin/bash
# .cursor/hooks/block-git.sh
cat > /dev/null
cat << 'EOF'
{
  "permission": "deny",
  "user_message": "git is blocked by project policy.",
  "agent_message": "Do not run git directly. Use the approved tooling instead."
}
EOF
exit 0

user_message is shown to you. agent_message goes back to the agent. A real hook would parse the input, which includes command, cwd and sandbox, and decide per command.

Failure behavior

Exit 0 means Cursor uses your JSON. For the permission events, invalid JSON or a schema mismatch blocks the action. Exit 2 blocks it, like permission: "deny". Any other exit code, a crash or a timeout fails open by default, so the action proceeds. With failClosed: true those cases block instead. For a security check, set failClosed and keep the script fast.

When to use hooks

Use them for rules you want enforced without relying on the model to follow instructions, such as blocking pushes or reading secrets. A hook sees one event at a time, and your script is only as good as its matching, so test it against variants of the command. Pair hooks with the operating system controls you already use.

Checked against the vendor documentation on November 25, 2026. Cursor hooks (the older docs.cursor.com address redirects to cursor.com/docs)

Cursor and Excellent

Excellent does not read Cursor’s logs, so the free scan does not cover Cursor sessions. For the agents it does read, the free scan reads supported logs on the Mac and reports findings for review. Run the free scan · See a sample report

Excellent Pro adds a live check that runs in Cursor, as well as Claude Code, Codex, Grok Build, GitHub Copilot CLI, Gemini CLI, Qwen Code, Factory Droid and Devin. With Pro, it blocks or asks before the risky actions it checks for. Pro is US-only for now. Agent coverage · Pricing · One set of rules for your agents

Related guides

View all