Client serves cached tools after its context exits with an exception

Offen Anfängerfreundlich
#3,968 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@jonxsong arbeitet bereits daran.

Seit 03.12.2026.

  • #3970 von @jonxsong — offen
  • #3971 von @mia03ther — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
65/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python

Rechercherichtung

Der Issue verweist auf src/mcp/client/client.py, genauer auf Client.__aexit__ in den Zeilen 480–496. Lies diese Methode und die bestehende Verbindungsprüfung und reproduziere dann das Beispiel mit einer zwischengespeicherten tools/list-Antwort und einer Ausnahme innerhalb des Contexts. Die Aufgabe ist erledigt, wenn Aufrufe nach einem normalen oder einem durch eine Ausnahme ausgelösten Verlassen des Contexts den geschlossenen Client zurückweisen und die ursprüngliche Ausnahme weiterhin weitergegeben wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Python
Sterne
25k
Forks
4.2k
Ø Merge
10 Min.
Gemergte PRs (30 T.)
1

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus modelcontextprotocol/python-sdk

Alle Issues in modelcontextprotocol/python-sdk

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.