core: MCP result sanitization deep-clones unchanged content blocks
What variant of Codex are you using?
CLI (core MCP tool calls)
What feature would you like to see?
I found a potential performance concern in current source rather than a confirmed user-visible bug. sanitize_mcp_tool_result_for_model owns the CallToolResult, but when the model lacks image or audio input support it iterates content by reference and calls block.clone() for every block that does not need replacement.
Please consider moving unchanged content blocks through an owned iteration while still replacing unsupported image and audio blocks with the existing text placeholders. Content order, placeholders, structured_content, meta, and is_error should remain unchanged.
Additional information
Evidence
- The sanitizer at the audited commit uses
content.iter()andblock.clone()for every unchanged JSON value when either modality is unsupported. - The MCP handler entrypoint reaches the sanitizer after each approved result, using the model's
input_modalities.CallToolResultowns its content and metadata fields. - A realistic case is a screenshot or document MCP tool returning a large mixed
content[]list containing media, text, resource, and metadata JSON blocks. If the model lacks one modality, the pass recursively clones all unchanged blocks while constructing an equivalent result. - Related prior art: #4819, #5600, #4391, #10334, and #9251 concern image support or
content/structuredContentsemantics, not this ownership path.
Impact
Not measured. The allocation volume and latency of recursively cloning unchanged JSON blocks are unknown. The cost may become material for large multi-block results or frequent MCP calls, but no production impact or benchmark number is claimed.
Question
Would maintainers accept an ownership-preserving sanitizer pass that moves unchanged blocks and allocates only the required unsupported-media placeholders?
I checked all relevant issues, comments, pull requests, discussions, and release notes; this report is not a duplicate.
I am reporting this finding only and am not proposing a pull request unless a maintainer invites one.
Disclosure
Investigated thoroughly with GPT-5.6 (high reasoning effort), using Oh My Pi as the agent framework.
This report is not generic or unreviewed AI-generated output. Its claims were checked against the cited evidence, and it includes the relevant detail intended to help maintainers resolve the issue.
If reports like this are not useful to the project, please let me know and I will refrain from submitting similar ones. My intent is to help without wasting maintainer time or energy or discouraging their work.
Thank you for your work.