Repository navigation
Python: Isolate MCP lifecycle task context - #9451
Conversation
There was a problem hiding this comment.
🟡 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.
There was a problem hiding this comment.
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

Motivation & Context
The MCP lifecycle owner may be created while request-scoped
ContextVarvalues are active. Becauseasyncio.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
_mcp_call_headerswhile 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 unrelatedContextVarstate.Related Issue
N/A - trivial change. Using the right context for run.
AI Assistance
AI assistance details: Implementation, regression testing, and PR description.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" in the title prefix, before or after a language prefix) — a workflow keeps the label and title prefix in sync automatically.