Skip to content

Python: Scope MCP headers to transport requests - #8039

Open
SergeyMenshykh wants to merge 4 commits into
microsoft:mainfrom
SergeyMenshykh:sergeymenshykh-mcp-header-isolation
Open

Python: Scope MCP headers to transport requests#8039
SergeyMenshykh wants to merge 4 commits into
microsoft:mainfrom
SergeyMenshykh:sergeymenshykh-mcp-header-isolation

Conversation

@SergeyMenshykh

Copy link
Copy Markdown
Contributor

Motivation & Context

When multiple MCP tools reuse the same HTTP client, provider output should remain associated with the transport request that initiated it. This keeps shared-client behavior deterministic while retaining the existing construction and connection model.

Description & Review Guide

  • What are the major changes?
    • Add a private HTTP client proxy that associates MCP transport requests with their originating tool.
    • Scope header_provider request-hook processing to matching transport requests.
    • Remove tool-specific request hooks on close and restore them on reconnect.
    • Preserve provider-header handling across same-origin requests and redirects.
    • Add regression coverage for shared clients, tools without providers, hook lifecycle, delegation, and redirects.
  • What is the impact of these changes?
    • Tools can continue sharing an httpx.AsyncClient with consistent per-tool provider behavior. There are no public API changes.
  • What do you want reviewers to focus on?
    • The private proxy delegation and the request-hook lifecycle during connection, redirects, close, and reconnect.

Related Issue

N/A — no public issue. Related PR #7892 adds static connect-time headers; this PR instead scopes existing header_provider processing when an HTTP client is reused.

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 the title prefix in sync automatically.

Keep provider header processing associated with the MCP tool that issued each request, including when HTTP clients are shared. Clean up request hooks on close and preserve redirect handling.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 4fca7430-5806-4fb4-a190-729e2473c5bb
Copilot AI balanced review requested due to automatic review settings September 3, 2026 15:04
@SergeyMenshykh
SergeyMenshykh deployed to github-app-auth September 3, 2026 15:05 — with GitHub Actions Active
@SergeyMenshykh
SergeyMenshykh deployed to github-app-auth September 3, 2026 15:05 — with GitHub Actions Active
@SergeyMenshykh
SergeyMenshykh deployed to github-app-auth September 3, 2026 15:05 — with GitHub Actions Active
@agent-framework-automation agent-framework-automation Bot added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python labels Sep 3, 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.

🟡 Changes recommended

Redirect chains can retain and expose previously injected headers when the provider’s key set changes.

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

Pull request overview

Scopes MCP provider headers per tool when sharing an HTTP client.

Changes:

  • Adds a transport proxy and tool-specific request ownership.
  • Manages hooks across close, reconnect, and redirects.
  • Adds regression tests and guidance.
File summaries
File Description
python/packages/core/agent_framework/_mcp.py Implements scoped header injection and cleanup.
python/packages/core/tests/core/test_mcp.py Tests sharing, delegation, lifecycle, and redirects.
python/packages/core/AGENTS.md Documents header-provider request scoping.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • 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
Remove previously injected provider headers before applying the latest set so redirected requests retain only current provider output.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 4fca7430-5806-4fb4-a190-729e2473c5bb
@SergeyMenshykh
SergeyMenshykh deployed to github-app-auth September 3, 2026 15:13 — with GitHub Actions Active

@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)): 9e90a73c3951
Model: gpt-5.6-sol-fast

Overview

The PR scopes shared-client header providers by tagging transport requests with a per-tool owner, delegates the underlying HTTP client surface, and removes and restores request hooks across normal close and reconnect. Owner checks, cross-origin stripping, and shared-client lifecycle tests provide strong coverage for ordinary request and redirect flows. A cancellation race remains: the caller can detach the authentication hook while the lifecycle owner is still performing transport teardown.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 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
Keep the request hook attached while lifecycle-owner transport cleanup runs, including when the caller awaiting close is cancelled.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 4fca7430-5806-4fb4-a190-729e2473c5bb
@SergeyMenshykh
SergeyMenshykh deployed to github-app-auth September 3, 2026 16:38 — with GitHub Actions Active
Comment thread python/packages/core/agent_framework/_mcp.py Outdated
Replace the shared request-hook list during cleanup so in-flight HTTPX hook iteration is not disrupted.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 4fca7430-5806-4fb4-a190-729e2473c5bb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

3 participants