Let extensions process MCP tool results (#41202)

## What changed

- Add `ToolLifecycleContributor::on_mcp_tool_result` with access to the
  executed MCP tool context, rewritten arguments, extension data stores, and
  mutable server result.
- Run contributors before publishing MCP completion and before preparing the
  result for the model, so extensions can inspect or replace successful and
  error results.

## Testing

- Cover unchanged, replaced, and error results for direct MCP calls, plus
  replaced and error results through Code Mode.
- Verify completion waits for result processing and the processed result flows
  to both completion events and subsequent model input.

GitOrigin-RevId: ece7acfe9c5eed4778cb78a29c3ffd06a74e1bcf
This commit is contained in:
Evan Fannin authored and copyberry committed 2026-08-27 20:39:39 +00:00
1 parent 124e560b93
commit 5bf0ba3dd6
6 files changed
+278 -4

No files matched your search

+26
View File
@@ -1,10 +1,12 @@
use std::sync::Arc;
use codex_extension_api::McpToolContext;
use codex_extension_api::McpToolResultInput;
use codex_extension_api::ToolCallOutcome;
use codex_extension_api::ToolCallSource as ExtensionToolCallSource;
use codex_extension_api::ToolFinishInput;
use codex_extension_api::ToolStartInput;
use codex_protocol::mcp::CallToolResult;
use codex_tools::ToolName;
use crate::session::session::Session;
@@ -45,6 +47,30 @@ pub(crate) async fn notify_tool_start(
}
}
pub(crate) async fn process_mcp_tool_result(
session: &Session,
turn: &TurnContext,
call_id: &str,
mcp_tool: &McpToolContext,
arguments: &serde_json::Value,
result: &mut CallToolResult,
) {
for contributor in session.services.extensions.tool_lifecycle_contributors() {
contributor
.on_mcp_tool_result(McpToolResultInput {
session_store: &session.services.session_extension_data,
thread_store: &session.services.thread_extension_data,
turn_store: turn.extension_data.as_ref(),
turn_id: &turn.sub_id,
call_id,
mcp_tool,
arguments,
result,
})
.await;
}
}
pub(crate) async fn notify_tool_finish(invocation: &ToolInvocation, outcome: ToolCallOutcome) {
notify_tool_finish_parts(
invocation.session.as_ref(),