|
1 | 1 | """Tests for MCP exception classes.""" |
2 | 2 |
|
| 3 | +import pickle |
| 4 | + |
3 | 5 | import pytest |
4 | 6 | from mcp_types import URL_ELICITATION_REQUIRED, ElicitRequestURLParams, ErrorData, JSONRPCError |
5 | 7 |
|
@@ -173,3 +175,38 @@ def test_from_jsonrpc_error_preserves_code_message_and_data() -> None: |
173 | 175 | ) |
174 | 176 | error = MCPError.from_jsonrpc_error(wire) |
175 | 177 | assert error.error == ErrorData(code=URL_ELICITATION_REQUIRED, message="go elsewhere", data={"hint": "y"}) |
| 178 | + |
| 179 | + |
| 180 | +def test_mcp_error_pickle_roundtrip() -> None: |
| 181 | + """MCPError preserves its structured payload across a pickle round-trip.""" |
| 182 | + original = MCPError(code=-32600, message="Invalid request", data={"detail": "bad"}) |
| 183 | + |
| 184 | + restored = pickle.loads(pickle.dumps(original)) |
| 185 | + |
| 186 | + assert type(restored) is MCPError |
| 187 | + assert restored.error == original.error |
| 188 | + |
| 189 | + |
| 190 | +def test_url_elicitation_required_error_pickle_roundtrip() -> None: |
| 191 | + """UrlElicitationRequiredError preserves its typed state when pickled.""" |
| 192 | + elicitations = [ |
| 193 | + ElicitRequestURLParams( |
| 194 | + mode="url", |
| 195 | + message="First authorization", |
| 196 | + url="https://example.com/auth/first", |
| 197 | + elicitation_id="auth-1", |
| 198 | + ), |
| 199 | + ElicitRequestURLParams( |
| 200 | + mode="url", |
| 201 | + message="Second authorization", |
| 202 | + url="https://example.com/auth/second", |
| 203 | + elicitation_id="auth-2", |
| 204 | + ), |
| 205 | + ] |
| 206 | + original = UrlElicitationRequiredError(elicitations, message="Authorization required") |
| 207 | + |
| 208 | + restored = pickle.loads(pickle.dumps(original)) |
| 209 | + |
| 210 | + assert type(restored) is UrlElicitationRequiredError |
| 211 | + assert restored.error == original.error |
| 212 | + assert restored.elicitations == original.elicitations |
0 commit comments