Repository navigation
Conversation
McpTool dumps the CallToolResult to a plain dict, so an ImageContent item
stayed a {"type", "data", "mimeType"} dict. _extract_multimodal_parts only
lifts types.Part values, so the base64 reached the model as JSON text on
Gemini and as a printed dict on Claude. Convert each image item to a
types.Part right after the dump, so the existing extraction moves it into
function_response.parts. Data that is not valid base64 is left as it was.
Fixes google#8118
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
ImageContentstill never reached the model)Problem:
McpTool._run_async_impldumps theCallToolResultinto a plain dict (result = _dump_mcp_model(response)), so anImageContentitem becomes{"type": "image", "data": "<base64>", "mimeType": "image/png"}._extract_multimodal_partsinflows/llm_flows/tools/_caller.pyonly liftstypes.Partvalues (_as_function_response_part), so nothing reachesfunction_response.parts. Gemini receives the base64 inside the function response JSON; on Claude,_part_to_message_blockturns the item intostr(item), a printed dict with the full base64. The model can't see the image, and a real screenshot costs a large number of text tokens.MultimodalToolResultsPlugindoesn't help, since it only acts ontypes.Partresults.Solution:
Right after the dump,
_convert_image_content_to_partsreplaces eachimageitem that has stringdataandmimeTypewithtypes.Part.from_bytes(...). The existing_extract_multimodal_partsthen moves it intofunction_response.parts, which the Gemini and Anthropic adapters already send as media. The dump already uses the same keys under MCP SDK 1.x and 2.x, so the conversion readsmimeTypeand needs no version check. Data that isn't valid base64 (validate=True) is logged and left as it was, so a tool call that succeeded never fails here.Behavior notes:
after_tool_callbacknow sees atypes.Partin place of an image dict, the same as for function tools that return parts. Text items,structuredContent,isErrorandmetaare unchanged.TestResultDictKeys.test_nested_content_fields_keep_their_camel_case_keysused an image item as its example of a nested camelCase key. It now uses anEmbeddedResourcewithTextResourceContents, which carriesmimeTypein both SDK majors.AudioContentand blobEmbeddedResourceare out of scope here and could follow the same path in a separate PR.Testing Plan
Unit Tests:
New class
TestImageContentPartsintests/unittests/tools/mcp_tool/test_mcp_tool.py, using a real 1x1 PNG:test_image_content_becomes_a_partPartwith the decoded bytes andimage/png; the text item andisErrorare unchangedtest_gemini_path_gets_the_image_as_a_function_response_part_extract_multimodal_partslifts exactly oneimage/pngpart; no base64 is left in the responsetest_anthropic_path_sends_an_image_blockcontent_to_message_paramproduces atool_resultwith animageblock and the text, and no base64 texttest_invalid_base64_image_stays_a_dictWithout the fix (src change stashed, tests kept):
With the fix:
pre-commit runon both files passes (ruff, isort, pyink, addlicense, codespell, ADK compliance checks). Tested locally with MCP SDK 2.3.0; the conversion reads the dumped keys, which are the same under 1.x.Manual End-to-End (E2E) Tests:
The repro from #8118 (MCP stdio server returning a PNG,
MockModel, second request inspected), Python 3.11.18, mcp 2.3.0:Before:
After:
Checklist