You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Client serves cached tools after its context exits with an exception #3968
Searched existing issues and pull requests for this behavior.
Release line
2.x (current stable)
Description
After an exception leaves an async with Client(...) block, client.list_tools() can still return a cached tool list even though the connection has closed. The same call after a normal exit raises RuntimeError: Client must be used within an async context manager. Both exits should make subsequent calls reject the closed client.
Client.aexit clears _session after awaiting the exit stack. If the stack propagates an exception, the assignment is skipped and the cache's connection check still passes.
The example prints ['echo'] on both versions below. Remove the line that raises ApplicationError and the final call raises RuntimeError as expected.
Windows, CPython 3.12.4, MCP 2.3.0 and main at 77001c96.
I'd like to work on the fix if maintainers are open to an outside PR. Clearing the session reference even when the exit stack raises would preserve the original exception and let the existing connection guard reject later calls.
Codex helped with implementation. Report written and reviewed by Jon Zhang.
Reproduced on main at 77001c9: after the exception leaves the async with, list_tools() returns the cached ['echo'] instead of raising, exactly as described — the await self._exit_stack.__aexit__(...) in Client.__aexit__ propagates the exception, so self._session = None never runs.
The fix I have ready clears _session in a finally block, so the existing connection guard rejects later calls while the original exception still propagates unchanged, plus a regression test (fails without the fix, passes with it). It's on my fork at mia03ther/python-sdk branch fix/3968-cached-tools-after-exit (commit ed836e7) — happy to open the PR if a maintainer wants it. (I tried opening one, but the API returns a permissions error for PR creation from my account, so the branch is here for now.)
Noting the reporter @jonxsong already offered to fix this and has first call — deferring to them if they'd like to take it.
Activity
mia03ther commentedon Dec 3, 2026
Reproduced on main at
77001c9: after the exception leaves theasync with,list_tools()returns the cached['echo']instead of raising, exactly as described — theawait self._exit_stack.__aexit__(...)inClient.__aexit__propagates the exception, soself._session = Nonenever runs.The fix I have ready clears
_sessionin afinallyblock, so the existing connection guard rejects later calls while the original exception still propagates unchanged, plus a regression test (fails without the fix, passes with it). It's on my fork atmia03ther/python-sdkbranchfix/3968-cached-tools-after-exit(commited836e7) — happy to open the PR if a maintainer wants it. (I tried opening one, but the API returns a permissions error for PR creation from my account, so the branch is here for now.)Noting the reporter @jonxsong already offered to fix this and has first call — deferring to them if they'd like to take it.