Skip to content

Support proper conversion from MCP's image content to OpenAI's image content part #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

StreetLamb
Copy link
Collaborator

@StreetLamb StreetLamb commented May 17, 2025

Fixes #201

)
if content.resource.mimeType.startswith("image/"):
return ChatCompletionContentPartImageParam(
type="image_url",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it always base64 based btw, or can it be a URI as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, MCP's ImageContent only provides base64 encoded data.

)
)
else:
raise ValueError(f"Unexpected content type: {c.type}")
raise ValueError(f"Unexpected content type: {c["type"]}")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a syntax error in this f-string where double quotes are nested inside double quotes when accessing the dictionary key. This will cause a Python syntax error at runtime.

Consider using single quotes inside the f-string:

raise ValueError(f"Unexpected content type: {c['type']}")

Or alternatively, use single quotes for the f-string itself:

raise ValueError(f'Unexpected content type: {c["type"]}')
Suggested change
raise ValueError(f"Unexpected content type: {c["type"]}")
raise ValueError(f"Unexpected content type: {c['type']}")

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

"""
import re

match = re.match(r"data:(image/\w+);base64,(.*)", image_url)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex pattern r"data:(image/\w+);base64,(.*)" is too restrictive for image MIME types. The \w+ only matches alphanumeric characters and underscores, but valid MIME types like image/svg+xml contain additional characters such as +.

Consider updating to a more inclusive pattern like:

r"data:(image/[\w.+-]+);base64,(.*)"

This will properly handle all standard image MIME types including SVG, PNG, JPEG, and others that may contain special characters in their subtype.

Suggested change
match = re.match(r"data:(image/\w+);base64,(.*)", image_url)
match = re.match(r"data:(image/[\w.+-]+);base64,(.*)", image_url)

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

Copy link
Collaborator

@saqadri saqadri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving to unblock @StreetLamb. Please update with conflicts resolved and merge at your convenience!

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.

Dealing with images
2 participants