Support MCP tools in hooks (#18385)

## Summary

Lifecycle hooks currently treat `PreToolUse`, `PostToolUse`, and
`PermissionRequest` as Bash-only flows
- hook schema constrains `tool_name` to `Bash`
- hook input assumes a command-shaped `tool_input`
- core hook dispatch path passes only shell command strings

That means hooks cannot target MCP tools even though MCP tool names are
model-visible and stable

This change generalizes those hook paths so they can match and receive
payloads for MCP tools while preserving the existing Bash behavior.

## Reviewer Notes

I think these are the key files
- `codex-rs/core/src/tools/handlers/mcp.rs`
- `codex-rs/core/src/mcp_tool_call.rs`

Otherwise the changes across apply_patch, shell, and unified_exec are
mainly to rewire everything to be `tool_input` based instead of just
`command` so that it'll make sense for MCP tools.

## Changes

- Allow `PreToolUse`, `PostToolUse`, and `PermissionRequest` hook inputs
to carry arbitrary `tool_name` and `tool_input` values instead of
hard-coding `Bash` and command-only payloads.
- Add MCP hook payload support through `McpHandler`, using the
model-visible tool name from `ToolInvocation` and the raw MCP arguments
as `tool_input`.
- Include MCP tool responses in `PostToolUse` by serializing
`McpToolOutput` into the hook response payload.
- Run `PermissionRequest` hooks for MCP approval requests after
remembered approval checks and before falling back to user-facing MCP
elicitation.
- Preserve exact matching for literal hook matchers like `Bash` and
`mcp__memory__create_entities`, while keeping regex matcher support for
patterns like `mcp__memory__.*` and `mcp__.*__write.*`.

---------

Co-authored-by: Andrei Eternal <eternal@openai.com>
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
authored and GitHub committed 2026-04-23 07:33:57 +00:00
1 parent 8bc667b07b
commit 305825abd9
34 files changed
+1293 -361

No files matched your search

+3 -26
View File
@@ -210,13 +210,6 @@ pub(crate) enum PreToolUseDecisionWire {
Block,
}
#[derive(Debug, Clone, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub(crate) struct PreToolUseToolInput {
pub command: String,
}
#[derive(Debug, Clone, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[schemars(rename = "pre-tool-use.command.input")]
@@ -232,19 +225,10 @@ pub(crate) struct PreToolUseCommandInput {
#[schemars(schema_with = "permission_mode_schema")]
pub permission_mode: String,
pub tool_name: String,
pub tool_input: PreToolUseToolInput,
pub tool_input: Value,
pub tool_use_id: String,
}
#[derive(Debug, Clone, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub(crate) struct PermissionRequestToolInput {
pub command: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
#[derive(Debug, Clone, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[schemars(rename = "permission-request.command.input")]
@@ -260,14 +244,7 @@ pub(crate) struct PermissionRequestCommandInput {
#[schemars(schema_with = "permission_mode_schema")]
pub permission_mode: String,
pub tool_name: String,
pub tool_input: PermissionRequestToolInput,
}
#[derive(Debug, Clone, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub(crate) struct PostToolUseToolInput {
pub command: String,
pub tool_input: Value,
}
#[derive(Debug, Clone, Serialize, JsonSchema)]
@@ -285,7 +262,7 @@ pub(crate) struct PostToolUseCommandInput {
#[schemars(schema_with = "permission_mode_schema")]
pub permission_mode: String,
pub tool_name: String,
pub tool_input: PostToolUseToolInput,
pub tool_input: Value,
pub tool_response: Value,
pub tool_use_id: String,
}