Add scoped host metadata hooks for MCP tool requests (#58395)

## What changed

Expose `McpToolRequestMetaHook` and `McpToolRequestMetaProvider` so hosts can supply a per-invocation `_meta` value using the thread, turn, and call IDs. Apply the hook before dispatch only when the tool, server name, source, and full transport configuration match the selected local MCP registration.

Reject metadata key collisions and propagate provider errors to the tool caller without dispatching the call.

## Testing

Add integration coverage for matching registrations, different contributors and routing headers, unrelated tools and servers, provider failures, and existing metadata keys.

GitOrigin-RevId: 2bf51c375a04617d61b24ca1cbbcaa810fb06720
This commit is contained in:
rhan-oai
2026-10-30 17:32:09 +00:00
committed by copyberry
parent cbe16d525c
commit 5be57bedd0
4 changed files with 259 additions and 12 deletions
+17 -4
View File
@@ -40,6 +40,7 @@ use codex_features::Feature;
use codex_mcp::CODEX_APPS_MCP_SERVER_NAME;
use codex_mcp::MCP_TOOL_CODEX_APPS_META_KEY;
use codex_mcp::McpPermissionPromptAutoApproveContext;
use codex_mcp::McpToolRequestMetaHook;
use codex_mcp::PreparedMcpCall;
use codex_mcp::SandboxState;
use codex_mcp::ToolInfo;
@@ -584,10 +585,22 @@ async fn handle_approved_mcp_tool_call(
.services
.rollout_thread_trace
.start_mcp_call_trace(call_id);
Ok((
rewritten_arguments,
mcp_call_trace.add_request_meta(request_meta),
))
let mut request_meta = mcp_call_trace.add_request_meta(request_meta);
if let Some(hook) = sess
.services
.thread_extension_data
.get::<McpToolRequestMetaHook>()
{
hook.apply(
&prepared_call,
sess.thread_id,
&turn_context.sub_id,
call_id,
&mut request_meta,
)
.await?;
}
Ok((rewritten_arguments, request_meta))
})
.await
.map_err(|error| {