Stone aqueduct arches under a cloudy sky, with grass in the foreground

Official Cursor docs show how to configure agent hooks that can allow or deny shell commands

This isn’t a launch recap. Official Cursor hooks docs say hooks let you “observe, control, and extend the agent loop using custom scripts.” You define them in a hooks.json file at the project or user level. Hooks are “spawned processes that communicate over stdio using JSON in both directions.” They “run before or after defined stages of the agent loop and can observe, block, or modify behavior.” Official examples include running formatters after edits and gating risky operations. Here’s what the current official docs say.

Cream-paper schematic: a hooks.json document and a hook script, arrows into an agent loop with a file-edit stage and a shell-command gate, then an allow path and a deny path
A hooks.json file can run a script after a file edit, or allow or deny a shell command, as the official Cursor docs describe it. Original schematic by Tech & AI Pulse.

Where hooks.json goes

The quickstart says you can create the file at the project level, <project>/.cursor/hooks.json, or in your home directory, ~/.cursor/hooks.json. “Project-level hooks apply only to that specific project, while home directory hooks apply globally.” “Cursor watches hooks config files and reloads them automatically.”

To share hooks with a team, the docs say to put hooks.json at <project-root>/.cursor/hooks.json and commit it. “When team members open the project in a trusted workspace, Cursor automatically loads and runs the project hooks.” Project hooks “Require the workspace to be trusted to run (for security).”

Paths are easy to get wrong. “For project hooks, use paths like .cursor/hooks/script.sh (relative to project root), not ./hooks/script.sh (which would look for <project>/hooks/script.sh).” User-level hooks run from ~/.cursor/, so ./hooks/script.sh is the user-level form. If more than one source defines hooks, “all matching hooks from every source run,” and Cursor merges the responses: “any deny wins over ask, and ask wins over allow, regardless of source.” The documented priority, highest to lowest, is Enterprise, then Team, then Project, then User.

A first hook after a file edit

The official user-level quickstart starts with an afterFileEdit hook. Create ~/.cursor/hooks.json with:

{
  "version": 1,
  "hooks": {
    "afterFileEdit": [{ "command": "./hooks/format.sh" }]
  }
}

Create the script at ~/.cursor/hooks/format.sh. The docs show a minimal command hook that reads its JSON input and exits 0:

#!/bin/bash
# Read input, do something, exit 0
cat > /dev/null
exit 0

Then make it executable with chmod +x ~/.cursor/hooks/format.sh. “Your hook runs after every file edit.” For the same hook in a project file, change the command path to .cursor/hooks/format.sh and put the script there, as the docs describe for project hooks.

Allow or deny a shell command

Command hooks “execute shell scripts that receive JSON input via stdin and return JSON output via stdout.” “On Windows, the desktop app runs hook commands through PowerShell and pipes the JSON into the command, so a PowerShell script reads it from $input and other programs read it from stdin.”

To gate network tools, the official command-hook example uses beforeShellExecution with a matcher:

{
  "version": 1,
  "hooks": {
    "beforeShellExecution": [
      {
        "command": "./scripts/approve-network.sh",
        "timeout": 30,
        "matcher": "curl|wget|nc"
      }
    ]
  }
}

“The matcher runs against the shell command string.” Use it, the docs say, “to run hooks only when the command matches a pattern (e.g. network calls, file deletions).” In a project hooks.json file, point that command at a path under the project root, such as .cursor/hooks/approve-network.sh, rather than copying the user-level ./scripts/... path unchanged. The official output for this hook is a permission decision: "allow", "deny", or "ask", with optional user_message and agent_message strings. The docs do not publish the body of approve-network.sh; they show the hook definition and that output schema.

Exit codes and the fail-open default

This is the load-bearing caveat. Exit code 0 means the hook succeeded, and Cursor uses the JSON output. For permission hooks, including beforeShellExecution, “invalid JSON or a response that doesn’t match the hook’s schema blocks the action.” Exit code 2 blocks the action, “equivalent to returning permission: "deny".” Other exit codes mean the hook failed, and “action proceeds (fail-open by default).”

The same page is more specific: “Crashes, timeouts, and non-zero exit codes other than 2 fail open by default: Cursor logs the failure and allows the action through.” Set failClosed to true on the hook definition “to block on those failures too.” The option’s default is false. The docs call that setting “Useful for security-critical hooks,” and they also note that permission hooks “block on invalid JSON or an invalid response even when this is false.” A hook file is not a sandbox, and a hook that fails open is not a guarantee that a command will be stopped.

Prompt-based hooks are a different kind of check

Hooks also support a prompt-based type, which the docs describe as “LLM-evaluated.” “Prompt hooks use an LLM to evaluate a natural language condition.” An official example asks, “Does this command look safe to execute? Only allow read-only operations.” That’s a model judgment, not a hard control like a script that returns deny or exits 2. The docs also say cloud agents “run command-based hooks only,” because prompt-based hooks “require authentication wiring between the hook and the agent loop, which isn’t available in the cloud execution environment.”

What this does not guarantee

Project hooks only run in a trusted workspace. Hook scripts receive JSON that can include the full terminal command, and an afterShellExecution hook can receive the command’s output. Treat those scripts like other repo automation, and don’t assume a hook will redact secrets. The afterShellExecution input includes a sandbox field the docs describe as “Whether the command ran in a sandboxed environment.” That’s a reported flag, not proof that the command couldn’t reach anything else.

The same hooks page says that if you have hooks defined in .cursor/hooks.json at the root of your project, “cloud agents pick them up and run them during their work.” “Hooks do not run” during early read-only exploratory turns. “They start once the agent has a writable environment.” User-level hooks in ~/.cursor/hooks.json “are not available in cloud agents.” That is a limit on where those hooks run, not a walkthrough of starting a cloud-agent session.

To confirm a hook is active, the troubleshooting section points to “a Hooks tab in Customize and a Hooks output channel to debug configured and executed hooks and see errors.” “If hooks still do not load, restart Cursor.” And check that the script path matches the hook source, as in the project-versus-user note above. Official docs change; use the current hooks page for the commands and schema.

Hero photo: Larnaca Aqueduct, March 2022, by Qasinka, via Wikimedia Commons; CC0 1.0. Cropped and resized.