Let extensions attach metadata to MCP tool requests (#62435)

## What changed

Add `ToolLifecycleContributor::mcp_tool_request_meta` so an extension can
use thread and turn data and the captured tool registration to attach an
entry to the outgoing request's `_meta`. Codex rejects duplicate keys.
Expose the registering plugin's package ID in `McpToolContext` so
contributors can scope metadata to that plugin.

Export `PERSISTED_MCP_RESULT_MAX_BYTES`, the existing 64 KiB limit for
untruncated MCP results in durable history.

## Testing

Extend the multi-environment MCP scenario to check that only the call
for the selected plugin receives the metadata and that the result
callback sees the same plugin context.

GitOrigin-RevId: 25a2afc5402a5c9323de184313d614aede3692fb
This commit is contained in:
Oliver Tepman
2026-11-19 05:37:58 +00:00
committed by copyberry
parent 524e15989b
commit c764c44a05
6 changed files with 82 additions and 7 deletions
+23 -4
View File
@@ -553,6 +553,10 @@ async fn handle_approved_mcp_tool_call(
}
Ok(())
};
let mcp_tool = McpToolContext::from_prepared_call(
&prepared_call,
turn_context.config.mcp_servers.get().get(&server),
);
let result = async {
let result = async {
let result = prepared_call
@@ -702,6 +706,25 @@ async fn handle_approved_mcp_tool_call(
)
.await?;
}
for contributor in sess.services.extensions.tool_lifecycle_contributors() {
if let Some((key, value)) = contributor.mcp_tool_request_meta(
&sess.services.thread_extension_data,
&turn_context.extension_data,
&mcp_tool,
) {
let meta = request_meta
.get_or_insert_with(|| serde_json::json!({}))
.as_object_mut()
.ok_or_else(|| {
anyhow::anyhow!("missing MCP request metadata object")
})?;
anyhow::ensure!(
!meta.contains_key(key),
"MCP request metadata already contains {key}"
);
meta.insert(key.to_owned(), value);
}
}
Ok((rewritten_arguments, request_meta))
})
.await
@@ -735,10 +758,6 @@ async fn handle_approved_mcp_tool_call(
);
// Capture trusted server metadata before result callbacks or model-facing rewrites.
elicitation_type = mcp_tool_call_auth_elicitation_type(&server, connector_id, &result);
let mcp_tool = McpToolContext::from_prepared_call(
&prepared_call,
turn_context.config.mcp_servers.get().get(&server),
);
process_mcp_tool_result(
sess,
turn_context,