|
1 | 1 | """Tests for MCP exception classes.""" |
2 | 2 |
|
| 3 | +import pickle |
| 4 | + |
3 | 5 | import pytest |
4 | 6 |
|
5 | 7 | from mcp.shared.exceptions import MCPError, UrlElicitationRequiredError |
@@ -162,3 +164,41 @@ def test_url_elicitation_required_error_exception_message() -> None: |
162 | 164 |
|
163 | 165 | # The exception's string representation should match the message |
164 | 166 | assert str(error) == "URL elicitation required" |
| 167 | + |
| 168 | + |
| 169 | +def test_mcp_error_pickle_roundtrip() -> None: |
| 170 | + """Test that MCPError survives a normal pickle round-trip.""" |
| 171 | + original = MCPError( |
| 172 | + code=-32600, |
| 173 | + message="Authentication Required", |
| 174 | + data={"scope": "files.read"}, |
| 175 | + ) |
| 176 | + |
| 177 | + restored = pickle.loads(pickle.dumps(original)) |
| 178 | + |
| 179 | + assert isinstance(restored, MCPError) |
| 180 | + assert restored.code == -32600 |
| 181 | + assert restored.message == "Authentication Required" |
| 182 | + assert restored.data == {"scope": "files.read"} |
| 183 | + assert str(restored) == "Authentication Required" |
| 184 | + |
| 185 | + |
| 186 | +def test_url_elicitation_required_error_pickle_roundtrip() -> None: |
| 187 | + """Test that specialized MCPError subclasses survive pickle too.""" |
| 188 | + original = UrlElicitationRequiredError( |
| 189 | + [ |
| 190 | + ElicitRequestURLParams( |
| 191 | + mode="url", |
| 192 | + message="Auth required", |
| 193 | + url="https://example.com/auth", |
| 194 | + elicitation_id="test-123", |
| 195 | + ) |
| 196 | + ] |
| 197 | + ) |
| 198 | + |
| 199 | + restored = pickle.loads(pickle.dumps(original)) |
| 200 | + |
| 201 | + assert isinstance(restored, UrlElicitationRequiredError) |
| 202 | + assert restored.elicitations[0].elicitation_id == "test-123" |
| 203 | + assert restored.elicitations[0].url == "https://example.com/auth" |
| 204 | + assert restored.message == "URL elicitation required" |
0 commit comments