Skip to content

Python: Prefer MCP structuredContent over duplicate content - #7897

Draft
Shivani . (Shivani767) wants to merge 2 commits into
microsoft:mainfrom
Shivani767:fix/7866-mcp-structured-content-dedupe
Draft

Python: Prefer MCP structuredContent over duplicate content#7897
Shivani . (Shivani767) wants to merge 2 commits into
microsoft:mainfrom
Shivani767:fix/7866-mcp-structured-content-dedupe

Conversation

@Shivani767

@Shivani767 Shivani . (Shivani767) commented Aug 27, 2026

Copy link
Copy Markdown

Motivation & Context

MCP CallToolResult may include both content and structuredContent. Many servers (MS Learn, DeepWiki) echo an equivalent payload in both fields. Agent Framework previously appended both, so agents saw duplicated text and paid ~2x tokens.

Fixes #7866

Note: Pavlo Natalenko (@Pavnat) reported this and shared a custom-parser workaround that prefers structuredContent; there was no open PR when this was started. Happy to coordinate if preferred.

Description & Review Guide

  • What are the major changes?

    • Emit structuredContent first when present.
    • Skip only text / embedded-text content blocks that demonstrably echo the structured payload (exact JSON match or string value present in the structured tree).
    • Retain complementary text and all non-text blocks (images, audio, resources).
    • Stamp server _meta onto structured-content Content items.
    • Add regression tests for DeepWiki-style duplication, complementary summary text, and image + structured mixed results.
  • What is the impact of these changes?

    • Equivalent duplicated text no longer doubles token usage.
    • Rich / complementary content is no longer dropped when structuredContent exists.
    • Callers who need different merge semantics can still supply parse_tool_results.
  • What do you want reviewers to focus on?

    • Whether treating any matching string leaf in structuredContent as an echo is the right equivalence check.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

When CallToolResult includes both content and structuredContent, return
only the structured payload so agents are not charged for duplicated
tokens from servers that echo the same result in both fields.
Copilot AI balanced review requested due to automatic review settings August 27, 2026 05:59
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Aug 27, 2026

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

Updates MCP tool-result parsing to avoid duplicated structured output while propagating server metadata.

Changes:

  • Prefer structuredContent over parallel content blocks.
  • Add regression tests for deduplication and metadata propagation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
python/packages/core/agent_framework/_mcp.py Changes MCP result parsing precedence.
python/packages/core/tests/core/test_mcp.py Updates and adds parser regression tests.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

# each newly constructed Content; empty when the server provided no meta.
additional_kwargs: dict[str, Any] = {"additional_properties": {"_meta": meta}} if meta else {}

if mcp_type.structuredContent is not None:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed — dropping all content was too aggressive.

Updated to emit structuredContent first, skip only text (or embedded text) that demonstrably echoes the structured payload, and keep complementary text plus images/audio/resources. Added a mixed image + structured regression test.

Skip only text content that echoes structuredContent; keep images,
audio, resources, and complementary summaries. Add a mixed-content
regression test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: MCP tools return double output if CallToolResult contains both content and structuredContent

2 participants