Skip to content

fix: let models see images returned by MCP tools - #8363

Open
omnomkar wants to merge 1 commit into
google:mainfrom
omnomkar:fix/mcp-image-content-parts
Open

omnomkar wants to merge 1 commit into
google:mainfrom
omnomkar:fix/mcp-image-content-parts

Conversation

@omnomkar

Copy link
Copy Markdown

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
McpTool._run_async_impl dumps the CallToolResult into a plain dict (result = _dump_mcp_model(response)), so an ImageContent item becomes {"type": "image", "data": "<base64>", "mimeType": "image/png"}. _extract_multimodal_parts in flows/llm_flows/tools/_caller.py only lifts types.Part values (_as_function_response_part), so nothing reaches function_response.parts. Gemini receives the base64 inside the function response JSON; on Claude, _part_to_message_block turns the item into str(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. MultimodalToolResultsPlugin doesn't help, since it only acts on types.Part results.

Solution:
Right after the dump, _convert_image_content_to_parts replaces each image item that has string data and mimeType with types.Part.from_bytes(...). The existing _extract_multimodal_parts then moves it into function_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 reads mimeType and 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:

  • An after_tool_callback now sees a types.Part in place of an image dict, the same as for function tools that return parts. Text items, structuredContent, isError and meta are unchanged.
  • TestResultDictKeys.test_nested_content_fields_keep_their_camel_case_keys used an image item as its example of a nested camelCase key. It now uses an EmbeddedResource with TextResourceContents, which carries mimeType in both SDK majors.
  • AudioContent and blob EmbeddedResource are out of scope here and could follow the same path in a separate PR.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

New class TestImageContentParts in tests/unittests/tools/mcp_tool/test_mcp_tool.py, using a real 1x1 PNG:

Test Checks
test_image_content_becomes_a_part the image item is a Part with the decoded bytes and image/png; the text item and isError are unchanged
test_gemini_path_gets_the_image_as_a_function_response_part _extract_multimodal_parts lifts exactly one image/png part; no base64 is left in the response
test_anthropic_path_sends_an_image_block content_to_message_param produces a tool_result with an image block and the text, and no base64 text
test_invalid_base64_image_stays_a_dict invalid base64 is left as the original dict without raising

Without the fix (src change stashed, tests kept):

FAILED tests/unittests/tools/mcp_tool/test_mcp_tool.py::TestImageContentParts::test_image_content_becomes_a_part
FAILED tests/unittests/tools/mcp_tool/test_mcp_tool.py::TestImageContentParts::test_gemini_path_gets_the_image_as_a_function_response_part
FAILED tests/unittests/tools/mcp_tool/test_mcp_tool.py::TestImageContentParts::test_anthropic_path_sends_an_image_block
3 failed, 499 passed

With the fix:

$ pytest tests/unittests/tools/mcp_tool -q
502 passed

$ pytest tests/unittests/tools tests/unittests/flows tests/unittests/models -q
7137 passed, 2 skipped

pre-commit run on 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:

function_response.parts: None
response content types: ['image', 'text']

After:

function_response.parts: [FunctionResponsePart(inline_data=FunctionResponseBlob(data=b'\x89PNG...', mime_type='image/png'))]
response content types: ['text']

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

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
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.

MCP tool results containing ImageContent reach the model as base64 text instead of image parts

1 participant