{"slug": "codex-hooks-how-to-block-a-tool-call-before-it-runs", "title": "Codex Hooks: How to Block a Tool Call Before It Runs", "summary": "OpenAI's Codex hooks framework, documented as of 2 Dec 2026, runs scripts or MCP tools at 14 session events, and since the rust-v0.173.0 release on 2 Dec 2026 a hook with onFailure set to block stops a tool call when its PreToolUse check itself fails. Hooks are enabled by default, only command and mcp_tool handlers execute while prompt and agent handlers are parsed but skipped, and new or changed hooks must be reviewed and trusted in /hooks before they run unless an admin policy manages them. The change matters to developers running Codex agents because a failing guard now fails closed rather than letting the tool call proceed.", "body_md": "# Codex Hooks: How to Block a Tool Call Before It Runs\n\nCodex hooks run your scripts or MCP tools at 14 events. Since rust-v0.173.0, onFailure block stops a tool when its PreToolUse check fails.\n\nCodex hooks are scripts or MCP tools that Codex runs at fixed points in a session, such as `PreToolUse` before a tool call, and a hook there can deny the call. Since [rust-v0.173.0](https://github.com/openai/codex/releases/tag/rust-v0.173.0), released 2 Dec 2026, a hook with `onFailure` set to `block` also stops the tool when the check itself fails. Checked 2 Dec 2026.\n\n## What are Codex hooks? [#](#what-are-codex-hooks)\n\nA Codex hook is a rule that says “when this event fires, run this handler”. OpenAI’s [hooks docs](https://learn.chatgpt.com/docs/hooks) call hooks “an extensibility framework for Codex” that let you “run scripts or MCP tools during the agentic loop”. Only `command` and `mcp_tool` handlers run; `prompt` and `agent` handlers “are parsed but skipped”. New to Codex? Start with [what Codex is](https://munderdiffl.in/blog/what-is-codex/).\n\nHooks are on by default, and matching command hooks for one event start at the same time. Hooks that no admin policy manages “must be reviewed and trusted before they run”, so a new or changed hook is skipped until you trust it in `/hooks`.\n\n## Which events can a Codex hook use? [#](#which-events-can-a-codex-hook-use)\n\nThe hooks docs list 14 events, checked 2 Dec 2026. For tool events, the matcher filters on the tool name.\n\n| Event | When it runs | What it can do | Source | \n|---|---|---|---|\n| `SessionStart` | A session starts or resumes | Add context | [Docs](https://learn.chatgpt.com/docs/hooks) | \n| `SubagentStart` | A subagent starts | Add context; can’t stop it | [Docs](https://learn.chatgpt.com/docs/hooks) | \n| `UserPromptSubmit` | Before your prompt is sent | Add context or block the prompt | [Docs](https://learn.chatgpt.com/docs/hooks) | \n| `PreToolUse` | Before a tool call | Deny it, rewrite its input, add context | [Docs](https://learn.chatgpt.com/docs/hooks) | \n| `PermissionRequest` | Codex is about to ask for approval | Allow or deny | [Docs](https://learn.chatgpt.com/docs/hooks) | \n| `PostToolUse` | After a tool produces output | Replace the result with feedback; can’t undo it | [Docs](https://learn.chatgpt.com/docs/hooks) | \n| `PreCompact` ,`PostCompact` | Before and after compaction | Stop with `continue: false` | [Docs](https://learn.chatgpt.com/docs/hooks) | \n| `PreModelRequest` ,`PostModelRequest` | Each model request attempt | Log only | [Docs](https://learn.chatgpt.com/docs/hooks) | \n| `SubagentStop` | A subagent finishes | Keep the subagent going | [Docs](https://learn.chatgpt.com/docs/hooks) | \n| `Stop` | A turn stops | Keep Codex going with your reason as a new prompt | [Docs](https://learn.chatgpt.com/docs/hooks) | \n| `Interrupt` | You interrupt a turn | Record it; can’t prevent it | [Docs](https://learn.chatgpt.com/docs/hooks) | \n| `SessionEnd` | The main thread ends | Advisory only | [Docs](https://learn.chatgpt.com/docs/hooks) | \n\n## Where do Codex hooks go? [#](#where-do-codex-hooks-go)\n\nCodex reads a `hooks.json` file, or the same hooks inline under `[hooks]` in `config.toml`, in each of these places:\n\n- **System:**`/etc/codex/hooks.json` on macOS and Linux,`%ProgramData%\\OpenAI\\Codex\\hooks.json` on Windows\n- **User:**`$CODEX_HOME/hooks.json` , which is`~/.codex/hooks.json` on macOS and Linux when`CODEX_HOME` is unset\n- **Project:**`.codex/hooks.json` , loaded only from trusted project layers\n\nEnabled plugins can also bring hooks, by default from `hooks/hooks.json` in the plugin root. If one layer has both `hooks.json` and `[hooks]` in `config.toml`, Codex loads both and warns at startup. To turn hooks off, set `hooks = false` under `[features]`.\n\nOur example: this `config.toml` entry follows the inline TOML in the hooks docs, plus `onFailure` from the rust-v0.173.0 notes, spelled as in that tag’s source. It runs a guard before every Bash command:\n\n```\n[[hooks.PreToolUse]]\nmatcher = \"^Bash$\"\n\n[[hooks.PreToolUse.hooks]]\ntype = \"command\"\ncommand = '/bin/bash \"$(git rev-parse --show-toplevel)/.codex/hooks/check_secrets.sh\"'\ntimeout = 30\nstatusMessage = \"Checking Bash command\"\nonFailure = \"block\"\n```\n\n`timeout` is in seconds, 600 by default for most hooks. The git root path follows the docs’ advice.\n\n## How do you block a tool call with a Codex hook? [#](#how-do-you-block-a-tool-call-with-a-codex-hook)\n\nA `PreToolUse` hook blocks a call in one of three documented ways: print JSON with `hookSpecificOutput.permissionDecision` set to `\"deny\"` and a `permissionDecisionReason`, print the older `decision: \"block\"` shape with a `reason`, or exit with code 2 and write the reason to stderr. The hook gets `tool_name` and `tool_input` on stdin; for Bash and `apply_patch`, the command is in `tool_input.command`.\n\nBy default a hook that fails does not stop the action. The docs page, checked 2 Dec 2026, says MCP hook errors “don’t block the operation” and does not mention `onFailure`. The [rust-v0.173.0 release notes](https://github.com/openai/codex/releases/tag/rust-v0.173.0), published 2 Dec 2026 at 02:16 UTC, say: “Supported synchronous command and MCP tool hooks can set `onFailure` to `block`. A failed `PreToolUse` check then prevents the tool from running.”\n\n[Pull request #62831](https://github.com/openai/codex/pull/62831), merged 20 Nov 2026, fills in the rest. The default is `continue`, which keeps the old behaviour, and “a failed `PostToolUse` check withholds the result of a tool that already ran”. In the [rust-v0.173.0 source](https://github.com/openai/codex/blob/rust-v0.173.0/codex-rs/hooks/src/events/pre_tool_use.rs), a `PreToolUse` command hook that exits with any code other than 0 or 2 is marked failed. Checks that passed behave as before. Codex does not load a hook with `onFailure = \"block\"` that is `async` or runs on `SessionEnd`, `PreModelRequest` or `PostModelRequest`, and warns “onFailure:block requires a synchronous hook that supports blocking”.\n\n## What does a Codex hook that guards secrets look like? [#](#what-does-a-codex-hook-that-guards-secrets-look-like)\n\nOur example: this is the `check_secrets.sh` script that the config above points to. It reads the event JSON on stdin and blocks any Bash command that mentions a `.env` file:\n\n``` bash\n#!/bin/bash\nset -e\ncmd=$(jq -r '.tool_input.command')\ncase \"$cmd\" in\n  *.env*)\n    echo \"Blocked: this command touches a .env file.\" >&2\n    exit 2 ;;\nesac\nexit 0\n```\n\nFor `git status` the script exits 0 and the command runs. For `cat .env` it exits 2, so Codex blocks the call with the stderr line as the reason. `set -e` makes the script exit with an error if `jq` is missing, and `onFailure = \"block\"` turns that failure into a block instead of a pass. To cover file edits too, widen the matcher to `^(Bash|apply_patch)$`, since `apply_patch` also puts its patch in `tool_input.command`.\n\nOur view: matching text in a command is easy to get around. The docs make a similar point: “Treat tool hooks as a useful guardrail, not a complete enforcement boundary”.\n\n## How do Codex hooks compare with Claude Code hooks? [#](#how-do-codex-hooks-compare-with-claude-code-hooks)\n\nBoth use events such as `PreToolUse` and `Stop`, three levels of event, matcher group and handler, and exit code 2 to block. The differences, from the Codex docs and the [Claude Code hooks reference](https://code.claude.com/docs/en/hooks), both checked 2 Dec 2026:\n\n|  | Codex | Claude Code | \n|---|---|---|\n| Config | `hooks.json` or`[hooks]` in`config.toml` ([docs](https://learn.chatgpt.com/docs/hooks) ) | `hooks` in`settings.json` files ([reference](https://code.claude.com/docs/en/hooks) ) | \n| Events listed | 14 ( [docs](https://learn.chatgpt.com/docs/hooks) ) | 34 ( [reference](https://code.claude.com/docs/en/hooks) ) | \n| Handlers that run | `command` ,`mcp_tool` ([docs](https://learn.chatgpt.com/docs/hooks) ) | `command` ,`http` ,`mcp_tool` ,`prompt` ,`agent` ([reference](https://code.claude.com/docs/en/hooks) ) | \n| Block when a hook fails | `onFailure` , since rust-v0.173.0 ([notes](https://github.com/openai/codex/releases/tag/rust-v0.173.0) ) | `onFailure` , since v2.1.295; v2.1.328 for`mcp_tool` ([reference](https://code.claude.com/docs/en/hooks) ) | \n\nOur [Claude Code hooks guide](https://munderdiffl.in/blog/claude-code-hooks-explained/) covers the other side, and [how to install Codex CLI](https://munderdiffl.in/blog/how-to-install-codex-cli/) gets Codex ready.\n\n## Where does Munder Difflin fit? [#](#where-does-munder-difflin-fit)\n\nWe think it fits if you run Codex next to Claude Code or Gemini CLI. [Munder Difflin](https://harnessmd.com/download) is free and open source: a desktop app that runs a team of AI agents for all kinds of knowledge work, coding included, on your own computer. Its [README](https://github.com/HarnessMD/munder-difflin), read 2 Dec 2026, says of engines: “Each agent picks its own, so one office can mix them.” The [install guide](https://munderdiffl.in/blog/how-to-install-and-use-munder-difflin/) covers setup.\n\n## FAQ\n\nWhat are Codex hooks?\n\nScripts or MCP tools that Codex runs at fixed points in a session, such as before a tool call or when a turn stops. OpenAI's hooks docs, checked 2 Dec 2026, list 14 events, from SessionStart to SessionEnd. Command and mcp_tool handlers run; prompt and agent handlers are parsed but skipped.\n\nWhere do I put Codex hooks?\n\nIn a hooks.json file, or inline under [hooks] in config.toml. The user file is $CODEX_HOME/hooks.json, which is ~/.codex/hooks.json on macOS and Linux when CODEX_HOME is unset. A trusted project uses .codex/hooks.json, and the system file on macOS and Linux is /etc/codex/hooks.json. A new or changed hook is skipped until you trust it in /hooks.\n\nHow do I block a command with a Codex PreToolUse hook?\n\nPrint JSON with hookSpecificOutput.permissionDecision set to \"deny\" and a permissionDecisionReason, or exit with code 2 and write the reason to stderr. Codex also accepts the older shape with decision set to \"block\" and a reason.\n\nWhat does onFailure block do in Codex?\n\nIt makes a hook that fails stop the action instead of letting it go ahead. The rust-v0.173.0 release notes, published 2 Dec 2026, say supported synchronous command and MCP tool hooks can set it, and a failed PreToolUse check then prevents the tool from running. The default is continue. The hooks docs page did not mention onFailure when we checked it on 2 Dec 2026.\n\nHow are Codex hooks different from Claude Code hooks?\n\nBoth use events such as PreToolUse and Stop, the same three levels of event, matcher group and handler, and exit code 2 to block. Codex reads hooks.json or config.toml and runs command and mcp_tool handlers. Claude Code reads settings.json files and also runs http, prompt and agent handlers. Both now offer onFailure with a block value.", "url": "https://wpnews.pro/news/codex-hooks-how-to-block-a-tool-call-before-it-runs", "canonical_source": "https://munderdiffl.in/blog/codex-hooks/", "published_at": "2026-12-02 18:47:22.495296+00:00", "updated_at": "2026-12-02 18:47:24.714252+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "agent-protocols"], "entities": ["OpenAI", "Codex", "rust-v0.173.0", "PreToolUse", "MCP"], "also_reported_by": [], "takeaway": "Set onFailure to block on PreToolUse hooks in Codex rust-v0.173.0 or later so a failing guard denies the tool call instead of letting it run, and trust new or changed hooks in /hooks before they execute.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/codex-hooks-how-to-block-a-tool-call-before-it-runs", "markdown": "https://wpnews.pro/news/codex-hooks-how-to-block-a-tool-call-before-it-runs.md", "text": "https://wpnews.pro/news/codex-hooks-how-to-block-a-tool-call-before-it-runs.txt", "jsonld": "https://wpnews.pro/news/codex-hooks-how-to-block-a-tool-call-before-it-runs.jsonld"}}