---
title: Can Cursor hooks block risky agent commands?
url: https://opencontroller.lyzr.com/blog/cursor-hooks-to-block-agent-commands
published: 2026-11-14
updated: 2026-11-14
author: Lyzr
---

# Can Cursor hooks block risky agent commands?

**Yes. Cursor's `beforeShellExecution` and `beforeMCPExecution` hooks run your script before the agent executes a shell command or MCP tool call, and the script can return `allow`, `ask` or `deny`. The catch is failure handling: by default, a hook that crashes or times out fails open, so security-critical hooks need `failClosed: true` and testing.**

This guide covers which hooks matter, how to push them to every developer, and how to test that they actually block.

## Which Cursor hooks matter for security?

Cursor exposes agent lifecycle events in a `hooks.json` file. Three are the core of a guardrail setup:

| Hook | Runs before | Input you can inspect | Can block? |
|---|---|---|---|
| `beforeShellExecution` | Any terminal command the agent runs | `command`, `cwd`, `sandbox` | Yes: `allow` / `ask` / `deny` |
| `beforeMCPExecution` | Any MCP tool call | `tool_name`, `tool_input`, `mcp_server_name`, server URL or stdio command | Yes: `allow` / `ask` / `deny` |
| `beforeReadFile` | The agent reading a file | File path and content | Yes, for keeping secrets out of context |

There are also `after*` hooks (for example `afterShellExecution`, `afterFileEdit`) that are useful for logging but cannot prevent the action.

Each hook entry can set a `matcher` regex, a `timeout` in seconds, and `failClosed`. A hook can also return `user_message` and `agent_message`, so you can explain the denial to the developer and steer the agent.

## Where do hooks live, and which one wins?

Cursor loads hooks from four levels, and all matching hooks run:

1. **Enterprise (MDM-managed):** `/Library/Application Support/Cursor/hooks.json` on macOS, `/etc/cursor/hooks.json` on Linux and WSL, `C:\ProgramData\Cursor\hooks.json` on Windows.
2. **Team:** configured in the Cursor Enterprise dashboard and synced to members roughly every 30 minutes.
3. **Project:** `.cursor/hooks.json` in the repository.
4. **User:** `~/.cursor/hooks.json`.

When hooks disagree, `deny` beats `ask` and `ask` beats `allow`. That means a developer's own user hook cannot override an enterprise `deny`. Cursor does not deploy files through your MDM for you: you push the enterprise file and its scripts with your own MDM tool.

Note one limit: dashboard team hooks do not support `failClosed` or `matcher`. For hard blocks, prefer the MDM-managed enterprise file.

## What does a blocking policy look like?

A practical starter policy:

- **Shell:** deny `curl`/`wget` piped to a shell, `rm -rf` outside the workspace, writes to `~/.ssh` or cloud credential files, and `git push --force` to protected branches. Use `ask` for package installs.
- **MCP:** deny any `mcp_server_name` that is not on your approved list. Cursor's own documentation advises treating an unknown or missing server name as a deny.
- **File reads:** deny `.env`, private keys and cloud credential files so their contents never enter the model context.

Keep the decision logic in a small, versioned script, and log every decision with the developer, repository and command so you have evidence later.

## How do you test for fail-open behaviour?

This is the step most rollouts skip. Cursor's documented behaviour:

- Exit code 0 with valid JSON: the output is used. For permission hooks, invalid JSON or a schema mismatch blocks the action.
- Exit code 2: blocks, same as `deny`.
- Other exit codes, crashes or timeouts: **fail open** by default. The action proceeds and the failure is logged.
- With `failClosed: true`: crashes, timeouts, non-zero exits or missing output block the action.

Run these tests on a managed test machine before rollout:

1. Make the script sleep past its timeout. Without `failClosed`, the command should run; with it, it should be blocked.
2. Make the script exit with code 1. Same check.
3. Delete the script but leave `hooks.json` in place. Confirm the result matches your intent.
4. Return malformed JSON. Confirm it blocks.
5. Add a conflicting user-level `allow` hook. Confirm the enterprise `deny` still wins.
6. Turn on the dashboard option to block prompts until hooks are loaded, so a fresh install cannot run before policy arrives.

Repeat after each Cursor update, because hook behaviour is versioned with the app.

## What can hooks not do?

Hooks are a strong local control, but they have edges:

- They only see Cursor. Developers using another IDE, a terminal agent or a browser assistant are outside them.
- They inspect commands as text. Obfuscated commands or scripts that call other scripts can slip past simple pattern matching.
- They do not give a central inventory of which MCP servers and skills exist across your developer fleet.
- Local logs are only useful if you collect them.

For broader coverage, pair hooks with fleet discovery of coding agents and local MCP servers. See [how to govern Cursor in the enterprise](/blog/govern-cursor-in-the-enterprise) and [how to find local MCP servers on developer laptops](/blog/find-local-mcp-servers-on-developer-laptops).

## How Opencontroller does this

Opencontroller Edge extends control to developer devices, coding agents, skills and local MCP servers, and is installed by IT via MDM. It gives you a cross-tool view that sits alongside per-tool controls like Cursor hooks, feeding the same governed registry used for the rest of your agent estate. See the [AI agent control plane pillar](/blog/how-to-govern-ai-agents-across-platforms) or [book a demo](https://www.lyzr.ai/book-demo/).

## Frequently asked questions

**Do Cursor hooks fail open or closed?**
By default they fail open on crashes, timeouts and unexpected exit codes. Set `failClosed: true` on security-critical hooks so failures block the action. Invalid JSON from a permission hook already blocks.

**Can a developer override an enterprise hook?**
All matching hooks run and the most restrictive answer wins: `deny` beats `ask`, and `ask` beats `allow`. A user-level hook cannot turn an enterprise deny into an allow.

**Can Cursor hooks block MCP servers?**
Yes. `beforeMCPExecution` receives the server name, tool name and arguments, and can deny calls to any server not on your approved list.

## Key facts

Cursor beforeShellExecution and beforeMCPExecution hooks return allow, ask or deny. Hooks fail open on crashes and timeouts unless failClosed is true. Enterprise hooks.json paths: /Library/Application Support/Cursor/hooks.json (macOS), /etc/cursor/hooks.json (Linux), C:\ProgramData\Cursor\hooks.json (Windows). Opencontroller Edge extends control to developer devices, coding agents, skills and local MCP servers, installed via MDM. Opencontroller pricing starts from $250K per deployment per year.

## Related

- [How do you govern Cursor across an engineering organisation?](https://opencontroller.lyzr.com/blog/govern-cursor-in-the-enterprise.md)
- [How do you find local MCP servers on developer laptops?](https://opencontroller.lyzr.com/blog/find-local-mcp-servers-on-developer-laptops.md)
- [Can Claude Code hooks enforce security policy across an enterprise?](https://opencontroller.lyzr.com/blog/enforce-claude-code-policy-with-hooks.md)
- [More on Platform Guides](https://opencontroller.lyzr.com/blog/cluster/platform-guides)
