Skip to content

Commit c85c43f

Browse files
committed
Simplify _restore_mcp_error: every MCPError subclass inherits from_error_data
The defensive hasattr/fallback branch was unreachable in practice — every MCPError subclass inherits from_error_data() from the base class, so the exc_type.__new__(...) fallback path never fires. Removing it brings coverage to 100% and keeps the reconstructor to 2 lines. Behavior unchanged: - MCPError pickle round-trip - UrlElicitationRequiredError pickle round-trip (specialized path) - Arbitrary MCPError subclass pickle round-trip All 11 tests/shared/test_exceptions.py tests pass.
1 parent 5cf4cd2 commit c85c43f

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

src/mcp/shared/exceptions.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66

77

88
def _restore_mcp_error(exc_type: type[MCPError], error: ErrorData) -> MCPError:
9-
"""Reconstruct a pickled MCPError or subclass from ErrorData."""
9+
"""Reconstruct a pickled MCPError (or subclass) from ErrorData.
10+
11+
Every MCPError subclass inherits :meth:`MCPError.from_error_data`, so the
12+
generic path is sufficient. :class:`UrlElicitationRequiredError` needs its
13+
specialized reconstructor because it interprets ``error.data`` as a list
14+
of elicitations, not free-form data.
15+
"""
1016
if exc_type is UrlElicitationRequiredError:
1117
return exc_type.from_error(error)
12-
13-
if hasattr(exc_type, "from_error_data"):
14-
return exc_type.from_error_data(error)
15-
16-
restored = exc_type.__new__(exc_type)
17-
Exception.__init__(restored, error.code, error.message, error.data)
18-
restored.error = error
19-
return restored
18+
return exc_type.from_error_data(error)
2019

2120

2221
class MCPError(Exception):

0 commit comments

Comments
 (0)