Skip to content

fix(mcp-py): fall back to json for unsupported mcp image mime types - #4596

Open
BlueX888 wants to merge 1 commit into
strands-agents:mainfrom
BlueX888:fix/prep-mcp-client-image-mime-keyerror
Open

BlueX888 wants to merge 1 commit into
strands-agents:mainfrom
BlueX888:fix/prep-mcp-client-image-mime-keyerror

Conversation

@BlueX888

Copy link
Copy Markdown
Contributor

Description

MCPClient.map_mcp_content_to_tool_result_content indexed MIME_TO_FORMAT directly with the mime type supplied by the MCP server. That map only covers image/jpeg, image/jpg, image/png, image/gif and image/webp, so a successful tool result carrying a top-level ImageContent block with any other image mime type (e.g. image/svg+xml from a chart-rendering server) raised KeyError: 'image/svg+xml'. call_tool_sync/call_tool_async catch that exception and turn the whole result into {"status": "error", "content": [{"text": "Tool execution failed: 'image/svg+xml'"}]} — discarding every other content block of a call that actually succeeded.

This falls back to a json content block carrying the full image payload and logs a warning, mirroring the TypeScript SDK's _mapMcpImageContent (strands-ts/src/tools/mcp-tool.ts), which does the same fallback with the same warning text.

Related Issues

None open. #2218 previously proposed this fallback and was closed without merging; the unguarded MIME_TO_FORMAT[image_mime] lookup is still on main.

Documentation PR

Not needed — no public API change, and no documentation currently enumerates the supported image mime types.

Type of Change

Bug fix

Testing

hatch test / hatch run prepare cannot run on this machine: hatch 1.18.1's environment dependency sync fails with RecursionError: maximum recursion depth exceeded deep in its dependency-in-sync walk (packaging marker parsing). It reproduces with hatch on Python 3.12 and 3.13, with sys.setrecursionlimit raised, and identically on the unpatched tree, so it is unrelated to this change. The commands below are the ones the hatch scripts define (pytest {args}, ruff format --check, ruff check), run against an equivalent environment: Python 3.13.15, project installed editable with the all extra plus the hatch-test dependency set.

  • Regression test on the unpatched tree (git checkout origin/main -- src/strands/tools/mcp/mcp_client.py): pytest -n 0 -vv tests/strands/tools/mcp/test_mcp_client.py -k "unsupported_mime" → 1 failed — the result degrades to {'status': 'error', 'content': [{'text': "Tool execution failed: 'image/svg+xml'"}]}

  • New test on the patched tree: same command → 1 passed

  • Module tests: pytest -n auto tests/strands/tools/mcp/ → 357 passed, 31 skipped

  • Full unit suite: pytest -n auto tests/ → 6949 passed, 31 skipped

  • ruff format --check → 724 files already formatted

  • ruff check → All checks passed!

  • mypy scoped to the changed module with --python-version 3.13 (the repo-wide run cannot complete in this ad-hoc environment because the latest numpy/smithy_core stubs use Python 3.12+ syntax under the pinned python_version = "3.10"; pre-existing and unrelated, and mypy is not part of the CI lint gate) → Success: no issues found in 1 source file

  • I ran hatch run prepare
    — not run: see note above; the checks it wraps (format, lint, full test suite) were run individually.

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@BlueX888
BlueX888 requested a review from a team as a code owner September 25, 2026 07:53
@github-actions github-actions Bot added area-mcp MCP related bug Something isn't working python Pull requests that update python code labels Sep 25, 2026
map_mcp_content_to_tool_result_content indexed MIME_TO_FORMAT directly,
so a successful tool result carrying an ImageContent block with an image
mime type outside the supported set (e.g. image/svg+xml) raised a
KeyError that call_tool_sync/call_tool_async turned into an error result,
losing every other content block in the same result. Mirror the typescript
sdk and fall back to a json content block with a warning instead.
@BlueX888
BlueX888 force-pushed the fix/prep-mcp-client-image-mime-keyerror branch from d0ac137 to 1ce6966 Compare October 22, 2026 18:02

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

area-mcp MCP related bug Something isn't working python Pull requests that update python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant