Python: Prefer MCP structuredContent over duplicate content - #7897
Draft
Shivani . (Shivani767) wants to merge 2 commits into
Draft
Python: Prefer MCP structuredContent over duplicate content#7897Shivani . (Shivani767) wants to merge 2 commits into
Shivani . (Shivani767) wants to merge 2 commits into
Conversation
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.
Shivani . (Shivani767)
deployed
to
github-app-auth
August 27, 2026 05:59 — with
GitHub Actions
Active
Shivani . (Shivani767)
deployed
to
github-app-auth
August 27, 2026 05:59 — with
GitHub Actions
Active
Shivani . (Shivani767)
deployed
to
github-app-auth
August 27, 2026 05:59 — with
GitHub Actions
Active
Shivani . (Shivani767)
deployed
to
github-app-auth
August 27, 2026 05:59 — with
GitHub Actions
Active
Contributor
There was a problem hiding this comment.
Pull request overview
Updates MCP tool-result parsing to avoid duplicated structured output while propagating server metadata.
Changes:
- Prefer
structuredContentover 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: |
There was a problem hiding this comment.
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.
Shivani . (Shivani767)
deployed
to
github-app-auth
August 27, 2026 06:11 — with
GitHub Actions
Active
Skip only text content that echoes structuredContent; keep images, audio, resources, and complementary summaries. Add a mixed-content regression test.
Shivani . (Shivani767)
deployed
to
github-app-auth
August 27, 2026 06:15 — with
GitHub Actions
Active
Shivani . (Shivani767)
deployed
to
github-app-auth
August 27, 2026 06:15 — with
GitHub Actions
Active
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation & Context
MCP
CallToolResultmay include bothcontentandstructuredContent. 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?
structuredContentfirst when present.contentblocks that demonstrably echo the structured payload (exact JSON match or string value present in the structured tree)._metaonto structured-contentContentitems.What is the impact of these changes?
contentis no longer dropped whenstructuredContentexists.parse_tool_results.What do you want reviewers to focus on?
structuredContentas an echo is the right equivalence check.Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.