Client serves cached tools after its context exits with an exception
Maintainers usually reply within 1 day
@jonxsong is already working on this.
Since Dec 3, 2026.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 65/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- python
- Domain
- backend-api-design
Research direction
The issue points to src/mcp/client/client.py, specifically Client.__aexit__ at lines 480–496. Read that method and the existing connection guard, then reproduce the example with a cached tools/list response and an exception inside the context. Done means calls after either normal or exceptional exit reject the closed client while the original exception still propagates.
Written by the indexing model from the issue text.
Description
Initial Checks
- Reproduced on the latest 2.x release.
- 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.
Example Code
import anyio
from mcp import Client
from mcp.server import CacheHint, MCPServer
class ApplicationError(Exception):
pass
async def main() -> None:
server = MCPServer("repro", cache_hints={"tools/list": CacheHint(ttl_ms=60_000)})
@server.tool()
def echo() -> str:
return "echo"
client = Client(server, mode="2026-07-28")
try:
async with client:
await client.list_tools()
raise ApplicationError
except* ApplicationError:
pass
print([tool.name for tool in (await client.list_tools()).tools])
anyio.run(main)
Python & MCP Python SDK
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.
- Dominant language
- Python
- Stars
- 25k
- Forks
- 4.2k
- Avg merge
- 10m
- Merged PRs (30d)
- 1
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from modelcontextprotocol/python-sdk
-
v2
Difficulty 1/5 Under an hour Newbie friendliness 86/100
modelcontextprotocol/python-sdk#3978 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
modelcontextprotocol/python-sdk#3957 ·
Maintainers usually reply within 1 day
-
v2
Difficulty 1/5 Under an hour Newbie friendliness 91/100
modelcontextprotocol/python-sdk#3934 · 2 comments ·
Maintainers usually reply within 1 day
-
bug v1
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
modelcontextprotocol/python-sdk#3929 · 2 comments ·
Maintainers usually reply within 1 day
-
DCR `RegistrationHandler` accepts `{"redirect_uris": null}` and returns `201 Created` for `authorization_code` clientsMay be free again A pull request for this issue was closed without being merged. Openbug v1 v2
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
modelcontextprotocol/python-sdk#3885 · 3 comments ·
Maintainers usually reply within 1 day
All issues in modelcontextprotocol/python-sdk
Similar issues
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwisePossibly taken @abdulwaarith0 claimed this today. Openneeds-triage
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
MakazhanAlpamys/Soup#3690 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug data-types native-format
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
ClickHouse/clickhouse-connect#1320 ·
Maintainers usually reply within 1 day
-
accepted
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
future-agi/future-agi#6392 ·
Maintainers usually reply within 1 day