docs: clarify when Client(raise_exceptions=True) actually raises
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:
- Which failure modes are affected (in-process server exceptions vs transport/protocol errors)
- How this interacts with
ExceptionGroupwrapping fromasync with Client(...) - Recommended patterns vs catching
MCPErrorinside the context manager (asdocs/troubleshooting.mdalready teaches)
Proposed work
- Trace current behavior on
mainfor in-memory and HTTP clients - Document the real semantics in
docs/client/and/ordocs/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_exceptionsinsrc/mcp/client/client.py - #1401 ClientSession Error Handling (different surface; has an open PR)
- troubleshooting ExceptionGroup guidance
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.
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.