Repository navigation
fix: keep text and image together in MCP tool results - #29332
Conversation
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 472914f967
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 472914f967
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 588402d336
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 36c480b5ae
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bcf4c7218f
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review Please review exact head
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5001de7c11
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review Please reassess exact head
|
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
| export const parseMediaToolResult = ( | ||
| result: unknown, | ||
| ): MediaToolResult | null => { | ||
| const rec = asRecord(result); | ||
| if ( | ||
| !rec || | ||
| typeof rec.data !== "string" || | ||
| typeof rec.mime_type !== "string" | ||
| ) { | ||
| return null; | ||
| } | ||
| const mimeType = rec.mime_type.split(";")[0].trim().toLowerCase(); | ||
| return { data: rec.data, mimeType, text: asString(rec.text) }; | ||
| }; |
There was a problem hiding this comment.
I still wish we had a better way of doing this, lol. This isn't me asking for it right now though!
There was a problem hiding this comment.
Noted. I left the parser unchanged in this pass.
Xum acted on Mike's (@ibetitsmike) behalf.
| func replaceUnsupportedToolMedia( | ||
| ctx context.Context, | ||
| logger slog.Logger, | ||
| messages []fantasy.Message, | ||
| model chatprovider.Model, | ||
| configuredProvider string, | ||
| ) []fantasy.Message { | ||
| replaced := 0 | ||
| provider := model.Provider() | ||
| out := messages | ||
| for i, msg := range messages { | ||
| var parts []fantasy.MessagePart | ||
| for j, part := range msg.Content { | ||
| result, ok := part.(fantasy.ToolResultPart) | ||
| if !ok { | ||
| continue | ||
| } | ||
| media, ok := fantasy.AsToolResultOutputType[fantasy.ToolResultOutputContentMedia](result.Output) | ||
| if !ok { | ||
| continue | ||
| } | ||
| note, omit := chatprovider.ToolResultMediaOmission(provider, configuredProvider, media.MediaType, base64DecodedLen(media.Data)) | ||
| if !omit { | ||
| continue | ||
| } | ||
| if replaced == 0 { | ||
| out = slices.Clone(messages) | ||
| } | ||
| if parts == nil { | ||
| parts = slices.Clone(msg.Content) | ||
| } | ||
| replaced++ | ||
| text := media.Text | ||
| if text != "" { | ||
| text += "\n" | ||
| } | ||
| result.Output = fantasy.ToolResultOutputContentText{Text: text + note} | ||
| parts[j] = result | ||
| } | ||
| if parts != nil { | ||
| out[i].Content = parts | ||
| } | ||
| } | ||
| if replaced > 0 { | ||
| logger.Debug(ctx, "replaced unsupported tool result media in prompt", | ||
| slog.F("provider", provider), | ||
| slog.F("replaced_parts", replaced), | ||
| ) | ||
| } | ||
| return out | ||
| } |
There was a problem hiding this comment.
what the heck are we doing here
There was a problem hiding this comment.
This keeps persisted tool media replayable when the chat or compaction model changes. Some provider serializers turn every media result into an image, so audio, unsupported image formats, or oversized images can make the request fail. The helper substitutes the accompanying text plus an omission note without changing stored history.
The lazy copy-on-write bookkeeping was unnecessary. 83e1a1094bf replaces it with the same fresh-slice loop as the neighboring sanitizers. Provider policy and the input-immutability assertions are unchanged.
Xum acted on Mike's (@ibetitsmike) behalf.
There was a problem hiding this comment.
Sadly. We do a similar thing for compaction too.
|
@codex review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 83e1a1094b
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review Please reassess unchanged head
Both probes intercepted HTTP requests; neither is live-provider UAT. No code was changed for these findings. Current-head CI passes; remaining live UAT acceptance and human re-review are separate, still-open gates.
|
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Pull coder/fantasy#58 so the Google provider sends media tool results as a text function response plus a sibling inline data part instead of dropping them. Without it, keeping both the text and the image of a mixed MCP tool result would leave Gemini with an unpaired function call.
Workspace-hosted MCP tools go through convertMCPToolResponse, which had the same text-first rule as mcpclient: any text block hid the image block. Return the first binary block with the joined text as its Content so both reach the model. Audio blocks are mapped to the "media" response type because the chat loop only recognizes "image" and "media".
convertCallResult now ignores image, audio, and blob items with no payload so accompanying text is not replaced by an empty media response. executeSingleTool keeps only the text plus an omission note when a tool image reaches the provider's inline image cap, because the image would fail the next request and every later turn once persisted.
Propagate is_media from tool result parts through message parsing and stream state into Tool so GenericToolRenderer only renders an image when the server persisted a media result, not whenever a JSON result happens to carry data and an image mime type. Add stories for the open lightbox and for a media-shaped text result.
fantasy renders Anthropic and Bedrock tool result media as image blocks, so audio or blob results from an MCP tool would fail the next request and every later turn once persisted. Add chatprovider.AcceptsToolResultMediaType and fold it into the chatloop text fallback alongside the inline image cap. Make the workspace MCP audio-to-media mapping explicit.
parseMediaToolResult owns the {data, mime_type, text} shape and
ToolResultMedia renders it, showing the text plus a short notice for
non-image media instead of falling through to the JSON viewer with the
raw base64 payload.
…build Move the provider media gate from tool execution to prompt preparation. Every generation action and compaction rebuilds the prompt from the persisted rows, so replacing media there with the accompanying text plus an omission note covers history recorded under another provider and keeps the payload persisted for the UI. chatprovider.ToolResultMediaOmission limits Anthropic and Bedrock to the JPEG, PNG, GIF, and WebP formats the Messages API accepts under the inline image cap and labels the note with the configured provider, so Bedrock users no longer read "Anthropic". The chatloop write-time gate is removed.
Bedrock routes non-Anthropic models through the OpenAI transport, so the media gate now decides on model.Provider() and only labels the note with the configured provider. MCP converters no longer promote binary items that lack a MIME type, since the prompt reader cannot rebuild them as media and would replay the raw base64 as text.
Tools may report image types with parameters or mixed case; the parser now lowercases the base type so ToolResultMedia recognizes them.
Tool media is now checked once at execution, the single chokepoint for MCP, workspace MCP, and computer-use results: payloads over the chat attachment cap and image types whose bytes are not an image fall back to the accompanying text plus a note, and a declared image type is replaced by the detected one. Provider-specific rules stay at prompt build. The compaction sanitizer now labels omission notes with the configured provider instead of the routed transport.
Rewrite the closure-style media tests as table tests, compare whole fantasy.ToolResponse values instead of field by field, and drop cases that duplicated the provider matrix already covered by TestToolResultMediaOmission or exercised trivial branches. The compaction and generation wiring tests keep one native and one bridged provider case each.
83e1a10 to
522bd76
Compare
|
@codex review Head
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 522bd76a58
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| {Role: fantasy.MessageRoleTool, Content: []fantasy.MessagePart{fantasy.ToolResultPart{ | ||
| ToolCallID: "call-1", |
There was a problem hiding this comment.
Keep coverage for multiple media results
Every table row now builds a tool message containing exactly one ToolResultPart, removing the previous case that supplied two unsupported media results and verified that both were converted while the input remained unchanged. replaceUnsupportedToolMedia is explicitly a loop over every part, so a regression that stops or mutates after sanitizing the first result would now pass this test and leave a later unsupported result to be submitted to the provider. Retain a case with multiple media result parts in the same message.
Useful? React with 👍 / 👎.
|
@codex review
|
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Problem
MCP tool results that mix text and an image (Playwright's default
browser_take_screenshotreturns[text, image]) were reduced to text only in both MCP conversion paths, so screenshots never reached the model. The generic tool renderer also showed media results as a JSON dump of base64.Fix
mcpclientandchattool: keep all text plus the first usable binary item. Error flags survive empty and MIME-less results. Only one binary payload is represented; additional images are still dropped.chatloop: at execution time, cap tool media at the 10 MiB attachment limit and correct or reject declared image types by byte signature (sniffing, not decoding).chatdprompt build andchatprovider: at generation and compaction, replace media the destination provider cannot accept with the text plus an omission note (Anthropic/Bedrock: JPEG, PNG, GIF, WebP under the inline cap; OpenAI/Azure and Google: their image allowlists, applying the configured vendor's rules behindopenai-compat). Persisted history is unchanged, so the UI keeps the payload and provider switches keep working.site: results flaggedis_mediarender throughToolResultMedia(image with the existing lightbox, or a short notice for non-image media, plus the text). Unflagged media-shaped JSON stays in the JSON viewer.go.mod: pin coder/fantasy9a3598480a71, main'sa511353f83ebplus the batch-order fix460d4fdd113b(Chat Completions and compatible transports emit every tool result before synthetic user images). The fix lives onmike/mcp-tool-media-batch-order; no upstream PR yet.Validation
Round 3 dogfood UAT on this head,
522bd76a58a: PASS (chat, real Playwright MCP, Claude Haiku 4.5 and GPT-4o mini through the AI gateway). Verified in the UI and on the wire: image plus text render together, the lightbox opens and closes, both survive a reload; audio shows a notice instead of base64; media-shaped JSON stays in the JSON viewer; BMP on OpenAI Responses and Chat Completions reaches the model as text plus an omission note; SVG and PNG histories switch from Anthropic to OpenAI; cross-provider compaction through the organization override works; a result over 10 MiB persists only text plus a note, and a 5.5 MiB PNG is substituted for Anthropic yet still renders in the UI. Google is not routable on the dogfood gateway (404), so it was not exercised. The remote agent's own FAIL was a probe artifact (the code viewer renders in a shadow root) and was overturned on its screenshots by the independent runner. Rounds 1 and 2 (65db84a,5001de7) covered the same API surface earlier.Known limits: signature sniffing does not catch a truncated PNG with a valid header (providers return 400 for it), and only the first binary item of a result is kept. Pre-existing and out of scope: chatd opens a new MCP session per generation action, so Playwright's default per-session browser loses page state between steps (UAT used
--cdp-endpoint).Reviews:
472914ftobcf4c728a9a9e0,5001de783e1a10522bd76472914f,5001de7Local pre-push hooks were skipped per Mike's instruction; CI is the gate.