mirror of
https://github.com/openai/codex.git
synced 2026-10-30 19:18:49 +00:00
Add MCP tool handler support to the hooks engine (#38705)
## What changed - Discover synchronous `mcp_tool` hook handlers and invoke their configured MCP server and tool through a supplied executor. - Expand nested hook-event placeholders in MCP tool inputs while preserving JSON types, and process tool output through the existing hook output contract. - Represent hook details as handler-specific metadata in `hooks/list`, including MCP server and tool fields, and show those details in the TUI hooks browser. - Skip unsupported `SessionEnd` MCP hooks and runtimes without MCP invocation support with startup warnings. ## Testing - Cover argument expansion, missing placeholders, MCP invocation and hook decisions, discovery warnings, `hooks/list` metadata, and TUI rendering. GitOrigin-RevId: 295b845471fe92bd7ad7cd272fbcd2c3713912e0
This commit is contained in:
@@ -23,7 +23,7 @@ v2_enum_from_core!(
|
||||
|
||||
v2_enum_from_core!(
|
||||
pub enum HookHandlerType from CoreHookHandlerType {
|
||||
Command, Prompt, Agent
|
||||
Command, McpTool, Prompt, Agent
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use super::AppSummary;
|
||||
use super::HookEventName;
|
||||
use super::HookExecutionMode;
|
||||
use super::HookHandlerType;
|
||||
use super::HookSource;
|
||||
use super::HookTrustStatus;
|
||||
use crate::JsonSchema;
|
||||
@@ -518,17 +516,32 @@ pub struct HooksListEntry {
|
||||
pub errors: Vec<HookErrorInfo>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
|
||||
#[serde(tag = "handlerType", rename_all = "camelCase")]
|
||||
#[ts(tag = "handlerType", export_to = "v2/")]
|
||||
pub enum HookHandlerMetadata {
|
||||
Command {
|
||||
command: String,
|
||||
#[serde(default)]
|
||||
r#async: bool,
|
||||
},
|
||||
McpTool {
|
||||
server: String,
|
||||
tool: String,
|
||||
},
|
||||
Prompt {},
|
||||
Agent {},
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(export_to = "v2/")]
|
||||
pub struct HookMetadata {
|
||||
pub key: String,
|
||||
pub event_name: HookEventName,
|
||||
pub handler_type: HookHandlerType,
|
||||
#[serde(default)]
|
||||
pub execution_mode: HookExecutionMode,
|
||||
#[serde(flatten)]
|
||||
pub handler: HookHandlerMetadata,
|
||||
pub matcher: Option<String>,
|
||||
pub command: Option<String>,
|
||||
pub timeout_sec: u64,
|
||||
pub status_message: Option<String>,
|
||||
/// Configured `additionalContext` spill threshold.
|
||||
|
||||
@@ -3396,6 +3396,45 @@ fn user_input_into_core_preserves_media_fields() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hook_handler_metadata_only_exposes_async_for_commands() {
|
||||
assert_eq!(
|
||||
serde_json::to_value(HookHandlerMetadata::Command {
|
||||
command: "echo hello".to_string(),
|
||||
r#async: true,
|
||||
})
|
||||
.unwrap(),
|
||||
json!({
|
||||
"handlerType": "command",
|
||||
"command": "echo hello",
|
||||
"async": true,
|
||||
}),
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_value::<HookHandlerMetadata>(json!({
|
||||
"handlerType": "command",
|
||||
"command": "echo hello",
|
||||
}))
|
||||
.unwrap(),
|
||||
HookHandlerMetadata::Command {
|
||||
command: "echo hello".to_string(),
|
||||
r#async: false,
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::to_value(HookHandlerMetadata::McpTool {
|
||||
server: "security".to_string(),
|
||||
tool: "scan".to_string(),
|
||||
})
|
||||
.unwrap(),
|
||||
json!({
|
||||
"handlerType": "mcpTool",
|
||||
"server": "security",
|
||||
"tool": "scan",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skills_list_params_serialization_uses_force_reload() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user