Skip to content

fix: surface MCP structuredContent-only results#8695

Open
Success6666 wants to merge 1 commit into
agno-agi:mainfrom
Success6666:fix-mcp-structured-content-output
Open

fix: surface MCP structuredContent-only results#8695
Success6666 wants to merge 1 commit into
agno-agi:mainfrom
Success6666:fix-mcp-structured-content-output

Conversation

@Success6666

Copy link
Copy Markdown
Contributor

Summary

MCP tool results that only return structuredContent now surface that payload through ToolResult.content when the normal content array produces no text output.

This keeps the existing metadata preservation from #8580, while making structured-only successful MCP responses visible to the standard tool-result -> model-message flow.

Fixes #8653

Type of change

  • Bug fix

Details

  • If MCP content already contains text, that text remains the primary tool output.
  • If content is empty and structuredContent is present, the structured payload is serialized as JSON for ToolResult.content.
  • metadata[structured_content] still preserves the original structured object.
  • The structured-only regression test parses the serialized content as JSON before comparing the payload.

Tests

.venv\Scripts\python.exe -m pytest libs\agno\tests\unit\tools\test_mcp.py::test_mcp_tool_result_preserves_structured_content libs\agno\tests\unit\tools\test_mcp.py::test_mcp_tool_result_uses_structured_content_when_content_is_empty libs\agno\tests\unit\tools\test_mcp.py::test_mcp_tool_error_result_preserves_structured_content -q
.venv\Scripts\ruff.exe check libs\agno\agno\utils\mcp.py libs\agno\tests\unit\tools\test_mcp.py
.venv\Scripts\ruff.exe format --check libs\agno\agno\utils\mcp.py libs\agno\tests\unit\tools\test_mcp.py
git diff --check

Signed-off-by: 李政达 <1242427577@qq.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes how MCP tool results are surfaced through Agno’s standard tool-result → model-message pipeline when an MCP server returns a successful response with an empty content array but a populated structuredContent. In that structured-only case, the structured payload is now serialized to JSON and placed into ToolResult.content while still preserving the original object in ToolResult.metadata["structured_content"].

Changes:

  • Serialize structuredContent into ToolResult.content when no text output is produced from the MCP content array.
  • Add a helper (_structured_content_as_tool_output) to perform safe JSON serialization with fallbacks.
  • Add/extend unit tests to cover both the text-content and structured-only success cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
libs/agno/agno/utils/mcp.py Adds structuredContent-to-JSON fallback so structured-only MCP successes become visible in ToolResult.content.
libs/agno/tests/unit/tools/test_mcp.py Adds a regression test ensuring structured-only results serialize into content and remain preserved in metadata.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Sebastiao-Assuncao

Copy link
Copy Markdown
Contributor

Beautiful, thank you @Success6666

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 structuredContent-only tool results leave ToolResult.content empty — agent loop sees no output

3 participants