Skip to content

fix(mcp): correct file extension when image format mismatches#68

Open
haosenwang1018 wants to merge 1 commit intollmsresearch:mainfrom
haosenwang1018:fix/mcp-image-mime-type
Open

fix(mcp): correct file extension when image format mismatches#68
haosenwang1018 wants to merge 1 commit intollmsresearch:mainfrom
haosenwang1018:fix/mcp-image-mime-type

Conversation

@haosenwang1018
Copy link
Contributor

Fixes #58

The MCP generate_diagram tool returns JPEG image data with an image/png media type, causing the Anthropic API to reject the tool result.

Root Cause: The image generator may produce JPEG data but save it with a .png extension. While _compress_for_api correctly detects the actual format via magic-byte inspection, the fastmcp Image class may infer the MIME type from the file extension, resulting in a mismatch.

Fix: In _compress_for_api, after detecting the actual image format, check whether the file extension matches. If not, copy the file with the correct extension before returning.

No behavior change when the extension already matches the format.

When the image generator saves JPEG data with a .png extension,
the MCP tool result declares image/png but the actual content is
JPEG, causing Anthropic API validation errors.

Now _compress_for_api copies the file with the correct extension
when a mismatch is detected between the magic-byte format and the
file extension.

Fixes llmsresearch#58
@dippatel1994
Copy link
Member

@haosenwang1018 Thanks for the fix, the approach looks good and targeted.

I checked this against current main: no new functional regression from this change itself. One small blocker remains: there is a lint issue (E501 line too long in mcp_server/server.py) that should be fixed before merge.

After that, this looks good to go.

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.

[Bug]: MCP generate_diagram returns JPEG data with image/png media type

2 participants