Handles message type Exception in lowlevel/server.py _handle_message function. Mentioned as TODO on line 528. (#786)

Co-authored-by: Felix Weinberger <fweinberger@anthropic.com>
Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com>
This commit is contained in:
AishwaryaKalloli 2025-10-13 18:59:03 +05:30 • committed by GitHub
commit b4e50aa9f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -642,13 +642,21 @@ class Server(Generic[LifespanResultT, RequestT]):
raise_exceptions: bool = False,
):
with warnings.catch_warnings(record=True) as w:
# TODO(Marcelo): We should be checking if message is Exception here.
match message: # type: ignore[reportMatchNotExhaustive]
match message:
case RequestResponder(request=types.ClientRequest(root=req)) as responder:
with responder:
await self._handle_request(message, req, session, lifespan_context, raise_exceptions)
case types.ClientNotification(root=notify):
await self._handle_notification(notify)
case Exception():
logger.error(f"Received exception from stream: {message}")
await session.send_log_message(
level="error",
data="Internal Server Error",
logger="mcp.server.exception_handler",
)
if raise_exceptions:
raise message
for warning in w:
logger.info("Warning: %s: %s", warning.category.__name__, warning.message)