python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

docs: clarify when Client(raise_exceptions=True) actually raises

Open HaoChiBao opened this issue 3 months ago • 2 comments

Description

Client exposes raise_exceptions: bool = False with a code TODO asking when raise_exceptions=True actually raises:

https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/client/client.py (around the raise_exceptions field)

What's missing

There is no clear user-facing documentation of:

  1. Which failure modes are affected (in-process server exceptions vs transport/protocol errors)
  2. How this interacts with ExceptionGroup wrapping from async with Client(...)
  3. Recommended patterns vs catching MCPError inside the context manager (as docs/troubleshooting.md already teaches)

Proposed work

  • Trace current behavior on main for in-memory and HTTP clients
  • Document the real semantics in docs/client/ and/or docs/troubleshooting.md
  • Add a small docs_src example and/or regression test if behavior is surprising
  • Only change runtime behavior if docs reveal a clear bug; prefer docs-first

Related

  • TODO(Marcelo) on Client.raise_exceptions in src/mcp/client/client.py
  • #1401 ClientSession Error Handling (different surface; has an open PR)
  • troubleshooting ExceptionGroup guidance

HaoChiBao avatar Aug 11 '26 17:08 HaoChiBao

I'd like to take this docs-first clarification of Client.raise_exceptions. Plan: trace current behavior on main, document real semantics, add a small example/test only if needed, and avoid runtime changes unless a clear bug appears.

I searched for open PRs/issues specifically about Client.raise_exceptions documentation and did not find an active duplicate (related error-handling issues like #1401 are a different surface and already have PRs).

AI-assisted drafting disclosed; I will own the final write-up.

HaoChiBao avatar Aug 11 '26 17:08 HaoChiBao

The semantics worth documenting: raise_exceptions governs server-side exceptions raised while the server executes a request (tool, prompt, or resource handlers). With it False, those become CallToolResult with isError set; with it True, the exception propagates out of the await. Transport and protocol failures (connection refused, timeouts, invalid JSON-RPC framing) raise regardless of the flag, surfaced as McpError or the transport's own exception, so those belong inside the async with block and are not controlled by this flag.

The ExceptionGroup interaction comes from asyncio semantics rather than the SDK itself: when a client session is torn down with in-flight work, asyncio can group the raised exceptions, so callers often see an ExceptionGroup wrapping the actual MCPError. Documenting the unwrap pattern (except* MCPError, or iterating eg.exceptions) next to the flag docs addresses the most common "why did my except not catch it" complaint.

Practical guidance for the docs: keep raise_exceptions=True for in-process server scenarios where you want the traceback at the call site, and catch MCPError inside the context manager for anything transport related. We standardize on that split across the connectors at corpusiq.io; it is the difference between a debuggable traceback and a swallowed isError result.

Ben-Home avatar Aug 18 '26 00:08 Ben-Home