mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
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:
34 files changed
+1293
-361
No files matched your search
@@ -8,6 +8,7 @@ use codex_protocol::protocol::HookOutputEntryKind;
|
||||
use codex_protocol::protocol::HookRunStatus;
|
||||
use codex_protocol::protocol::HookRunSummary;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use serde_json::Value;
|
||||
|
||||
use super::common;
|
||||
use crate::engine::CommandShell;
|
||||
@@ -28,7 +29,7 @@ pub struct PreToolUseRequest {
|
||||
pub tool_name: String,
|
||||
pub matcher_aliases: Vec<String>,
|
||||
pub tool_use_id: String,
|
||||
pub command: String,
|
||||
pub tool_input: Value,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -124,7 +125,8 @@ pub(crate) async fn run(
|
||||
///
|
||||
/// Handler selection may include internal matcher aliases, but hook stdin keeps
|
||||
/// the canonical `tool_name` so audit logs and downstream policy decisions stay
|
||||
/// stable.
|
||||
/// stable. Shell-like tools pass `{ "command": ... }` as `tool_input`; MCP
|
||||
/// tools pass their resolved JSON arguments.
|
||||
fn command_input_json(request: &PreToolUseRequest) -> Result<String, serde_json::Error> {
|
||||
serde_json::to_string(&PreToolUseCommandInput {
|
||||
session_id: request.session_id.to_string(),
|
||||
@@ -135,9 +137,7 @@ fn command_input_json(request: &PreToolUseRequest) -> Result<String, serde_json:
|
||||
model: request.model.clone(),
|
||||
permission_mode: request.permission_mode.clone(),
|
||||
tool_name: request.tool_name.clone(),
|
||||
tool_input: crate::schema::PreToolUseToolInput {
|
||||
command: request.command.clone(),
|
||||
},
|
||||
tool_input: request.tool_input.clone(),
|
||||
tool_use_id: request.tool_use_id.clone(),
|
||||
})
|
||||
}
|
||||
@@ -568,7 +568,7 @@ mod tests {
|
||||
tool_name: "Bash".to_string(),
|
||||
matcher_aliases: Vec::new(),
|
||||
tool_use_id: tool_use_id.to_string(),
|
||||
command: "echo hello".to_string(),
|
||||
tool_input: serde_json::json!({ "command": "echo hello" }),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user