Skip to content

Python: Make MCP connection-loss recovery ownership-aware - #9583

Merged
Jose Alvarez (jpalvarezl) merged 3 commits into
mainfrom
jpalvarezl-fix-mcp-ownership-recovery
Oct 22, 2026
Merged

Jose Alvarez (jpalvarezl) merged 3 commits into
mainfrom
jpalvarezl-fix-mcp-ownership-recovery

Conversation

@jpalvarezl

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

Copy link
Copy Markdown
Member

Motivation & Context

Caller-supplied MCP Client and ClientSession connections are preserved by reset and close because Agent Framework does not own their lifecycle. Existing connection-loss paths nevertheless logged that they were reconnecting, called connect(reset=True), and retried against the same ended caller-owned object. This contradicted the documented ownership boundary and could issue a second RPC without performing a real reconnection.

This change makes connection-loss handling ownership-aware while preserving existing recovery for framework-owned connections.

Description & Review Guide

  • What are the major changes? Added one internal ownership decision for connection-loss recovery and applied it to tool/prompt discovery, liveness checks, tool calls, and prompt calls. Caller-owned losses now raise an actionable error before reconnect logging or a second RPC. Added regression coverage for both supplied high-level Clients and low-level ClientSessions, and made existing framework-owned recovery fixtures explicit.
  • What is the impact of these changes? Framework-owned reconnection behavior is unchanged. Caller-owned connections retain their existing ownership and reset semantics, but definitive connection loss now tells the caller to supply a replacement connection rather than retrying the same ended object. There is no public API change.
  • What do you want reviewers to focus on? Please verify that the shared ownership check is placed before every automatic recovery attempt while leaving operation-specific retry behavior and framework-owned recovery unchanged.

Related Issue

Fixes #9426

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: Copilot assisted with implementation, regression tests, historical/design analysis, and this 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 the title prefix in sync automatically.

Fixes: #9426

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

Disconnected-wrapper discovery regresses, and modern caller-owned connection-loss coverage is missing.

2 open issues
What changed in this PR

Makes Python MCP connection-loss recovery respect lifecycle ownership, preventing retries against ended caller-supplied connections.

Changes:

  • Adds a shared ownership check across discovery, liveness, tool calls, and prompt calls.
  • Adds caller-owned regression tests and explicitly marks framework-owned recovery fixtures.
File Description
python/​packages/​core/​tests/​core/​test_mcp.py Adds ownership-aware recovery tests and updates fixtures.
python/​packages/​core/​agent_framework/​_mcp.py Guards automatic recovery and reports caller replacement responsibilities.

Review effort: Balanced

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)): 18d099a5113d
Model: gpt-5.6-sol

Overview

The PR centralizes ownership-aware recovery and places the guard before each existing automatic reconnect path, while preserving framework-owned retries and caller-owned object lifecycles. Its ownership initialization, reset/close behavior, and operation-specific regression coverage are strong. One residual issue remains in liveness checks: failures that do not establish connection loss are now reported as requiring replacement of a caller-owned connection.

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
@KNIHAL

Copy link
Copy Markdown

Thanks for clarifying the scope and addressing the review feedback!

I was interested in contributing to this issue, and I’ve been following the implementation. Since this PR now covers the identified gaps, is there any related testing or follow-up work where an additional contributor could help? I’d be happy to take on a focused task to avoid duplicating your work.

@github-code-quality

Copy link
Copy Markdown
Contributor

Code coverage overview

Languages: Python

Python / code-coverage/python

main 0200599 jpalvarezl-fix-mcp-o... 64bf47f +/-
92% 92% 0%
View coverage of the 1 most impacted file.
File main 0200599 jpalvarezl-fix-mcp-o... 64bf47f +/-
packages/core/a...amework/_mcp.py 90% 91% +1%

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

@jpalvarezl
Jose Alvarez (jpalvarezl) added this pull request to the merge queue Oct 22, 2026
Merged via the queue into main with commit 1fe5467 Oct 22, 2026
53 checks passed
@jpalvarezl
Jose Alvarez (jpalvarezl) deleted the jpalvarezl-fix-mcp-ownership-recovery branch October 22, 2026 07:42

This branch was successfully deployed

1 active deployment
github-app-auth — 64bf47f9 Deployed Oct 21, 2026 by jpalvarezl via add_label #25868
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.

Python: Make MCP connection-loss recovery ownership-aware

4 participants