Skip to content

Python: Preserve bounded MCP Host payload metadata - #8128

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
mainfrom
mcp-host-payload-core
Sep 8, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
mainfrom
mcp-host-payload-core

Conversation

@eavanvalkenburg

Copy link
Copy Markdown
Member

Motivation & Context

Generated MCP FunctionTool calls need to retain the complete CallToolResult for Host transports even when the model-facing parser intentionally projects only a bounded summary. Without a core-owned capture contract, fields such as structuredContent are unavailable to downstream transports and persisted history after tool execution.

This is layer 1 (the bottom layer) of the split replacement for #7971. It establishes only the core MCP Host-payload capture contract and remains orthogonal to #7897: it does not change which MCP content the model sees, or its ordering, preference, or deduplication.

Description & Review Guide

  • What are the major changes? Generated MCP functions now scope Host-payload capture with a private ContextVar, attach one JSON-safe complete result under a private core marker after built-in or custom parsing, preserve _meta on every model-facing item, and carry the marker through generic function-error conversion. Normal, long-running fallback, completed-task, and MCP error paths share the contract. A configurable per-result size cap rejects oversized payloads before full-copy materialization and retains an exact final serialization guard; None disables the cap. The option is keyword-only on MCPTool after all existing positional parameters and is forwarded by every concrete transport.
  • What is the impact of these changes? Host transports can consume complete MCP result metadata without changing direct call_tool() custom-parser return shapes, model-visible content, transport subclass behavior, or header-provider behavior. Oversized Host payloads are omitted while bounded model projections and _meta remain intact.
  • What do you want reviewers to focus on? Please focus on the generated-call-only scoping, exactly-once marker behavior, early size-bound enforcement, constructor compatibility, and preservation across task/error paths.

Related Issue

Part of #7959.

This draft is the core-only first layer replacing the broader open source PR #7971. Draft #7897 addresses model-facing MCP content selection and is intentionally independent from this Host-only metadata contract.

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.

@github-code-quality

github-code-quality Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Code Coverage Overview

Languages: Python

Python / code-coverage/python

The overall line coverage in commit 57db79e in the mcp-host-payload-cor... branch is 91%. Line coverage data for the main branch is not yet available.

Show a line coverage summary of the most covered files.
File main mcp-host-payload-cor... 57db79e +/-
packages/core/a...work/_skills.py 95%
packages/core/a...ework/_tools.py 94%
packages/core/a.../_compaction.py 94%
packages/core/a...rk/_sessions.py 94%
packages/core/a...ework/_types.py 93%
packages/core/a...bservability.py 93%
packages/openai..._chat_client.py 92%
packages/core/a...amework/_mcp.py 91%
packages/ag-ui/...i/_agent_run.py 91%
packages/foundr...g/_responses.py 87%

Updated September 08, 2026 08:20 UTC

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.

🟡 Changes recommended

Oversized errors lose metadata, custom function parsers discard markers, and error capture is not limited to generated calls.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds bounded, Host-only MCP result metadata capture while preserving model-facing output.

Changes:

  • Adds scoped, size-limited Host payload markers.
  • Preserves markers through task and error paths.
  • Adds payload, transport, and compatibility tests.
File summaries
File Description
python/packages/core/agent_framework/_mcp.py Implements MCP Host payload capture and propagation.
python/packages/core/agent_framework/_tools.py Transfers exception metadata into function results.
python/packages/core/tests/core/test_mcp.py Adds MCP payload regression coverage.
Review details

Suppressed comments (2)

python/packages/core/agent_framework/_mcp.py:2619

  • The legacy/fallback error path also captures a complete Host payload for direct call_tool_as_task() calls even though preserve_host_payload is false. Apply the generated-call guard here as well so fallback behavior remains scoped consistently.
                    _mcp_tool_result_host_payload(
                        fallback_result,
                        max_size_bytes=self.max_host_payload_size_bytes,
                    ),

python/packages/core/agent_framework/_mcp.py:2836

  • Completed-task errors likewise serialize and retain the Host payload when the public direct task API requested no preservation. Gate this capture on preserve_host_payload to keep all task paths generated-call-only.
                    _mcp_tool_result_host_payload(
                        payload,
                        max_size_bytes=self.max_host_payload_size_bytes,
                    ),
  • Files reviewed: 3/3 changed files
  • Comments generated: 4
  • Review effort level: Balanced

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

Comment thread python/packages/core/agent_framework/_mcp.py Outdated
Comment thread python/packages/core/agent_framework/_mcp.py Outdated
Comment thread python/packages/core/agent_framework/_mcp.py Outdated
Comment thread python/packages/core/tests/core/test_mcp.py Outdated

@github-actions github-actions Bot 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.

MAF Automated Review — Iteration 1

Result: Findings reported
Scope: full PR (1 commit(s)): 5aebc2d700f4
Model: gpt-5.6-sol-fast

Overview

The PR cleanly scopes successful Host-payload capture to generated MCP functions, uses early and final serialization-size guards, and covers normal, error, fallback, and completed-task paths with targeted tests. The residual risks are that the per-result cap is not an aggregate bound, raw _meta can bypass it, oversized errors lose metadata, required-task dispatch bypasses public overrides, and an empty custom parser result changes the model-facing projection.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
5 verified findings remained after source verification (5 medium) across 1 file. Details are attached to the affected lines below.

Affected areas: python/packages/core/agent_framework/_mcp.py

Comment thread python/packages/core/agent_framework/_mcp.py Outdated
Comment thread python/packages/core/agent_framework/_mcp.py Outdated
Comment thread python/packages/core/agent_framework/_mcp.py
Comment thread python/packages/core/agent_framework/_mcp.py Outdated
Comment thread python/packages/core/agent_framework/_mcp.py Outdated
Comment thread python/packages/core/agent_framework/_mcp.py Outdated
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

@github-actions github-actions Bot 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.

MAF Automated Review — Iteration 2

Result: Findings reported
Scope: full PR (1 commit(s)): 57db79e933bd
Model: gpt-5.6-sol-fast

Overview

The PR separates complete MCP Host payloads from model-facing projections with a context-local capture, a function-result carrier, per-result serialization limits, and broad streaming, error, middleware, and task-path tests. Its strongest guards are the finally-scoped capture reset, exact post-serialization checks, and an aggregate request budget for complete payloads. Residual risks remain where server metadata crosses the security-label boundary, bypasses the aggregate budget, makes approval state non-durable, and is lost when result parsing fails.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
4 verified findings remained after source verification (3 high, 1 medium) across 2 files. Details are attached to the affected lines below.

Affected areas: python/packages/core/agent_framework/_mcp.py, python/packages/core/agent_framework/_tools.py

Comment thread python/packages/core/agent_framework/_mcp.py
Comment thread python/packages/core/agent_framework/_mcp.py
Comment thread python/packages/core/agent_framework/_tools.py
Comment thread python/packages/core/agent_framework/_mcp.py
Merged via the queue into main with commit 25959b7 Sep 8, 2026
43 checks passed
@baywet
Vincent Biret (baywet) deleted the mcp-host-payload-core branch September 17, 2026 14:53
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.

3 participants