Skip to content

Python: [Bug][Python][AG-UI] MCP tool results containing TextContent list are incorrectly serialized to object repr string #2509

@claude89757

Description

@claude89757

Describe the bug

When using MCP tools with the AG-UI protocol, tool results containing list[TextContent] are incorrectly serialized. Instead of
extracting the text content, the serialization produces Python object repr strings like:

[<agent_framework._types.TextContent object at 0x7f8e7c1cc730>]

This results in the frontend displaying raw object addresses instead of actual tool output data.

To Reproduce

  1. Set up an AG-UI endpoint using AgentFrameworkAgent with MCP tools
  2. Invoke any MCP tool that returns text content
  3. Observe the MessagesSnapshotEvent or tool result display

Expected behavior

Tool results should be properly serialized to display the actual text content, e.g.:

{"role": "tool", "content": "Actual tool output text here..."}

Root cause analysis

The issue is in agent_framework_ag_ui/_events.py around line 415-418:

 if isinstance(content.result, dict):
     result_content = json.dumps(content.result)
 elif content.result is not None:
     result_content = str(content.result)  # <- Bug here
 else:
     result_content = ""

When MCP tools return results, they go through _mcp_call_tool_result_to_ai_contents() in agent_framework/_mcp.py:69-73, which
converts them to list[Contents] (typically list[TextContent]).

The current serialization logic doesn't handle list[TextContent] - it just calls str() on the list, producing the object repr
string.

Suggested fix

Update the serialization logic to handle list[Contents]:

 if isinstance(content.result, dict):
     result_content = json.dumps(content.result)
 elif isinstance(content.result, list):
     # Handle list of Contents (e.g., from MCP tools)
     texts = []
     for item in content.result:
         if hasattr(item, 'text'):  # TextContent
             texts.append(item.text)
         elif hasattr(item, 'model_dump'):
             texts.append(json.dumps(item.model_dump(mode='json')))
         else:
             texts.append(str(item))
     result_content = '\n'.join(texts) if len(texts) == 1 else json.dumps(texts)
 elif content.result is not None:
     result_content = str(content.result)
 else:
     result_content = ""

Environment

  • OS: macOS Darwin 25.1.0
  • Python: 3.10
  • agent-framework-ag-ui version: 1.0.0b251120
  • agent-framework-core version: 1.0.0b251120

Additional context

This bug affects all MCP tool integrations when using the AG-UI protocol. It prevents proper display of tool results in frontend
applications like CopilotKit.

Metadata

Metadata

Assignees

Type

Projects

Status

Planned

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions