Skip to content

Commit e6760af

Browse files
fix: improve misleading warning for progress callback exceptions
1 parent 2ca2de7 commit e6760af

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/mcp/shared/session.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,14 @@ async def _receive_loop(self) -> None:
379379
by_alias=True, mode="json", exclude_none=True
380380
)
381381
)
382+
except Exception as e:
383+
# For other validation errors, log and continue
384+
logging.warning(
385+
"Failed to validate notification: %s. " "Message was: %s",
386+
e,
387+
message.message.root,
388+
)
389+
else: # Notification is valid
382390
# Handle cancellation notifications
383391
if isinstance(notification.root, CancelledNotification):
384392
cancelled_id = notification.root.params.requestId
@@ -392,19 +400,19 @@ async def _receive_loop(self) -> None:
392400
# call it with the progress information
393401
if progress_token in self._progress_callbacks:
394402
callback = self._progress_callbacks[progress_token]
395-
await callback(
396-
notification.root.params.progress,
397-
notification.root.params.total,
398-
notification.root.params.message,
399-
)
403+
try:
404+
await callback(
405+
notification.root.params.progress,
406+
notification.root.params.total,
407+
notification.root.params.message,
408+
)
409+
except Exception as e:
410+
logging.warning(
411+
"Progress callback raised an exception: %s",
412+
e,
413+
)
400414
await self._received_notification(notification)
401415
await self._handle_incoming(notification)
402-
except Exception as e:
403-
# For other validation errors, log and continue
404-
logging.warning(
405-
f"Failed to validate notification: {e}. "
406-
f"Message was: {message.message.root}"
407-
)
408416
else: # Response or error
409417
stream = self._response_streams.pop(message.message.root.id, None)
410418
if stream:

0 commit comments

Comments
 (0)