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:
parent
0e29cc4130
commit
b4e50aa9f9
1 changed files with 84 additions and 2 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue