Skip to content

Commit 642ca88

Browse files
committed
fix: add HttpError exception class for HTTP error propagation
1 parent 7ba41dc commit 642ca88

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/mcp/shared/exceptions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ def __str__(self) -> str:
4141
return self.message
4242

4343

44+
class HttpError(Exception):
45+
"""Raised when an MCP HTTP transport receives a non-2xx response.
46+
47+
Preserves the original HTTP status code so callers can distinguish
48+
auth errors (401/403) from other failures (404, 5xx, etc.).
49+
"""
50+
51+
def __init__(self, status_code: int, message: str | None = None, body: str | None = None):
52+
self.status_code = status_code
53+
self.body = body
54+
if message is None:
55+
message = f"HTTP {status_code}"
56+
super().__init__(message)
57+
58+
@property
59+
def is_auth_error(self) -> bool:
60+
"""True for 401 Unauthorized or 403 Forbidden responses."""
61+
return self.status_code in (401, 403)
62+
63+
4464
class StatelessModeNotSupported(RuntimeError):
4565
"""Raised when attempting to use a method that is not supported in stateless mode.
4666

0 commit comments

Comments
 (0)