Skip to content

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

Closed

Description

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.

Activity

  1. changed the title [-][Bug][Python][AG-UI] MCP tool results containing TextContent list are incorrectly serialized to object repr string[/-] [+]Python: [Bug][Python][AG-UI] MCP tool results containing TextContent list are incorrectly serialized to object repr string[/+] on Nov 28, 2025
  2. added and removed
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Nov 28, 2025
  3. added a commit that references this issue on Nov 28, 2025
    12cbc42
  4. claude89757 commented on Nov 28, 2025

    @claude89757
    ContributorAuthor

    I've submitted a fix for this issue in PR #2523.

    The fix properly handles list[TextContent] results from MCP tools by:

    1. Detecting when the result is a list
    2. Extracting text content from items that have a text attribute
    3. Using model_dump() for items that support it
    4. Falling back to str() for other types

    This ensures that MCP tool results are correctly serialized and displayed in AG-UI frontends.

  5. added a commit that references this issue on Jan 6, 2026
    8a50b53
  6. added a commit that references this issue on Jan 7, 2026
    db283cd
  7. added a commit that references this issue on Jan 9, 2026
    1e36ba3
  8. added 2 commits that reference this issue on Jan 17, 2026
    cd77193
    172423a
  9. added a commit that references this issue on Jan 27, 2026
    787becf
  10. added a commit that references this issue on Feb 2, 2026
    d386f64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

pythonUsage: [Issues, PRs], Target: Python

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions