Skip to content

Transform selected native MCP results before publication - #6133

Open
markstuart-oai wants to merge 2 commits into
feat/simple-codexfrom
feat/native-mcp-result-transform
Open

markstuart-oai wants to merge 2 commits into
feat/simple-codexfrom
feat/native-mcp-result-transform

Conversation

@markstuart-oai

@markstuart-oai markstuart-oai commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This pull request adds an opt-in callback for replacing a native MCP result before Codex publishes completion events, records history or sends the result to the model. The existing text hook runs after native completion events and skips peer errors.

Select a configured tool by its exact server and tool names. This example replaces the entire result with a status message and discards all media and structured content.

from agents import ToolExecutionConfig


async def summarize(call, result):
    text = "Lookup failed." if result.get("isError") else "Lookup completed."
    return {"content": [{"type": "text", "text": text}]}


settings = ToolExecutionConfig(
    native_mcp_result_transforms={("catalog", "lookup"): summarize}
)

Pass settings as RunConfig(tool_execution=settings) when using the Codex provider. To keep media, edit the detached result and return it. Check structuredContent and _meta for other copies of sensitive values.

  • Selected results bypass prepare_tool_output. Unselected calls keep their existing behavior.
  • The SDK validates the replacement, limits it to 1 MiB and preserves a peer's error flag. History keeps confirmed input rewrites even on failure.
  • Callback and validation errors fail the run. Core can still sample with a fixed error placeholder. Earlier Core traces may contain the original result.
  • Selection covers configured peers in the current native thread. Hosted tools, SDK-discovered tools and separate child threads are outside this scope.

A callback exception or a replacement that cannot be encoded as UTF-8 can carry tool data into error diagnostics. With tool-data redaction enabled (the default), Runner reports a fixed UserError for ordinary callback, encoding and validation errors. Setting OPENAI_AGENTS_DONT_LOG_TOOL_DATA=0 preserves the original exception and can expose tool data. Cancellation and process-control exceptions still propagate. Application logs and arbitrary user tracebacks are outside this protection.

Test plan

Checks

  • Regression tests
  • .agents/skills/code-change-verification/scripts/run.sh
  • All local verification steps passing
  • Two independent reviews of the complete diff

@markstuart-oai
markstuart-oai requested review from a team, rm-openai and seratch as code owners November 21, 2026 14:01
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T14:45:43.063950Z 4e2da1c New commits
🔒 Security Review ✅ Completed 2026-11-21T14:47:08.160435Z 4e2da1c New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@jbeckwith-oai jbeckwith-oai left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved after comprehensive static review and independent native and Python reviews. Reviewed exact configured-peer selection, per-thread request ownership and one-shot replies, pinned Core callback ordering before completion/history/model publication, detached callback data, MCP replacement validation and the 1 MiB limit, preservation of peer errors, legacy-output-hook bypass, confirmed input rewrites, and failure/cancellation cleanup. The original result is replaced with a withheld placeholder before awaiting the callback, so a lost or rejected reply does not restore it. No actionable findings.

The documented limits are material and accurately stated: earlier Core traces may retain the original result; independently started child threads and non-configured tools are outside selection; a failed transform fails the SDK run, but Core may sample using the fixed placeholder. Reviewed regression coverage for media, structured content, error flags, history, native events, next model requests, invalid/oversized replacements, and cancellation.

Hosted CI is complete: 33 passed and 2 skipped; no failures. Static review only; no candidate code or local tests executed.

@jbeckwith-oai jbeckwith-oai left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at 4e2da1c860929a5d60990ea80a6d169cc93b9ca7. Re-reviewed the two-file increment against the previously approved commit, with an independent second review. The shared diagnostics boundary now covers callback failures, replacement JSON serialization, and native validation, while retaining explicit diagnostic disclosure, cancellation semantics, and pending-result cleanup. Regression coverage includes rejected oversized and invalid replacements, lone surrogates in streamed runs, and detached redacted exceptions. No actionable findings.

Hosted CI is complete: 33 checks passed and 2 skipped; no failures. Static review only; no candidate code or local tests executed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants