Skip to content

Python: Issue with MCP integration for tools that return image content #2900

@egeozanozyedek

Description

@egeozanozyedek

Where: agent_framework/_mcp.py, function _mcp_type_to_ai_content, here

Problem:
When converting MCP ImageContent or AudioContent to DataContent, the raw base64 data from mcp_type.data is passed directly to the uri parameter:

case types.ImageContent() | types.AudioContent():
    return_types.append(
        DataContent(
            uri=mcp_type.data,  # Raw base64 string
            media_type=mcp_type.mimeType,
            raw_representation=mcp_type,
        )
    )

Per the MCP specification, ImageContent.data contains only the raw base64-encoded data, and not passed as a uri. However, DataContent expects uri to be a valid in the format data:{media_type};base64,{base64_data}.

When an MCP tool returns raw base64 data, this conversion leads to a ValueError because of the validated_uri = self._validate_uri(uri) line in DataContent.

Fix:

Use the preexisting data parameter which accepts raw bytes and builds the URI automatically (I think this is quick and graceful):

import base64
DataContent(
    data=base64.b64decode(mcp_type.data),
    media_type=mcp_type.mimeType,
    raw_representation=mcp_type,
)

Metadata

Metadata

Labels

model context protocolIssue related to Model Context Protocolpythonv1.0Features being tracked for the version 1.0 GA

Type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions