Skip to content

Python: Isolate MCP lifecycle task context - #9451

Merged
Jose Alvarez (jpalvarezl) merged 3 commits into
mainfrom
jpalvarezl-mcp-header-isolation-tutoring
Oct 16, 2026
Merged

Jose Alvarez (jpalvarezl) merged 3 commits into
mainfrom
jpalvarezl-mcp-header-isolation-tutoring

Conversation

@jpalvarezl

@jpalvarezl Jose Alvarez (jpalvarezl) commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Motivation & Context

The MCP lifecycle owner may be created while request-scoped ContextVar values are active. Because asyncio.create_task() copies the current context, transient call-specific header state can otherwise become part of the long-lived connection task's context. Lifecycle-owned connection work must not retain that call-specific override, while unrelated request context must remain available during connection setup.

Description & Review Guide

  • What are the major changes? Temporarily masks _mcp_call_headers while creating the lifecycle-owner task, then immediately restores the caller's value. A regression test verifies that the owner does not inherit call-scoped headers while preserving unrelated ContextVar state.
  • What is the impact of these changes? Connection-lifetime work no longer retains transient call-header overrides, while legitimate request context remains available. There is no public API change, and the implementation remains compatible with the Python 3.10 support floor.
  • What do you want reviewers to focus on? The narrow context boundary around task creation and whether the regression test captures both header isolation and preservation of unrelated context.

Related Issue

N/A - trivial change. Using the right context for run.

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: Implementation, regression testing, and PR description.

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 links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" in the title prefix, before or after a language prefix) — a workflow keeps the label and title prefix in sync automatically.

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

Initial authentication breaks for documented ContextVar-backed header providers unless credentials are resolved before entering the isolated context.

1 open finding
What changed in this PR

Isolates Python MCP lifecycle tasks from transient caller context without changing the public API.

Changes:

  • Creates the lifecycle-owner task inside an empty Context.
  • Adds a regression test for call-header context isolation.
File Description
python/​packages/​core/​tests/​core/​test_mcp.py Tests that lifecycle work does not inherit call headers.
python/​packages/​core/​agent_framework/​_mcp.py Isolates lifecycle task context at creation.

🧠 Review effort: 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

@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)): 55f8deeeb88b
Model: gpt-5.6-sol

Overview

The PR creates the MCP lifecycle-owner task in a fresh context, and its focused regression test demonstrates that framework call-header state is not retained by the owner. Existing lifecycle locks, cancellation cleanup, and Python 3.10-compatible task creation are strong guardrails. However, clearing the entire context also removes the documented user ContextVar credential source for streamable HTTP header providers, which can break or mis-authenticate the initialization handshake.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 high) 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
@github-code-quality

Copy link
Copy Markdown
Contributor

Code coverage overview

Languages: Python

Python / code-coverage/python

main 8a365f4 jpalvarezl-mcp-heade... 080bc11 +/-
92% 92% 0%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.

@jpalvarezl
Jose Alvarez (jpalvarezl) added this pull request to the merge queue Oct 16, 2026
Merged via the queue into main with commit 17b1a14 Oct 16, 2026
60 of 67 checks passed
@jpalvarezl
Jose Alvarez (jpalvarezl) deleted the jpalvarezl-mcp-header-isolation-tutoring branch October 16, 2026 10:18

This branch was successfully deployed

1 active deployment
github-app-auth — 080bc112 Deployed Oct 16, 2026 by jpalvarezl via add_label #25589
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