@@ -351,11 +351,21 @@ async def _receive_loop(self) -> None:
351351 if isinstance (message , Exception ):
352352 await self ._handle_incoming (message )
353353 elif isinstance (message .message .root , JSONRPCRequest ):
354- validated_request = self ._receive_request_type .model_validate (
355- message .message .root .model_dump (
356- by_alias = True , mode = "json" , exclude_none = True
354+ try :
355+ validated_request = self ._receive_request_type .model_validate (
356+ message .message .root .model_dump (
357+ by_alias = True , mode = "json" , exclude_none = True
358+ )
357359 )
358- )
360+ except Exception as e :
361+ # For other validation errors, log and continue
362+ logging .warning (
363+ "Failed to validate request: %s. Message was: %s" ,
364+ e ,
365+ message .message .root ,
366+ )
367+ continue
368+
359369 responder = RequestResponder (
360370 request_id = message .message .root .id ,
361371 request_meta = validated_request .root .params .meta
@@ -386,33 +396,33 @@ async def _receive_loop(self) -> None:
386396 e ,
387397 message .message .root ,
388398 )
389- else : # Notification is valid
390- # Handle cancellation notifications
391- if isinstance (notification .root , CancelledNotification ):
392- cancelled_id = notification .root .params .requestId
393- if cancelled_id in self ._in_flight :
394- await self ._in_flight [cancelled_id ].cancel ()
395- else :
396- # Handle progress notifications callback
397- if isinstance (notification .root , ProgressNotification ):
398- progress_token = notification .root .params .progressToken
399- # If there is a progress callback for this token,
400- # call it with the progress information
401- if progress_token in self ._progress_callbacks :
402- callback = self ._progress_callbacks [progress_token ]
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- )
414- await self ._received_notification (notification )
415- await self ._handle_incoming (notification )
399+ continue
400+ # Handle cancellation notifications
401+ if isinstance (notification .root , CancelledNotification ):
402+ cancelled_id = notification .root .params .requestId
403+ if cancelled_id in self ._in_flight :
404+ await self ._in_flight [cancelled_id ].cancel ()
405+ else :
406+ # Handle progress notifications callback
407+ if isinstance (notification .root , ProgressNotification ):
408+ progress_token = notification .root .params .progressToken
409+ # If there is a progress callback for this token,
410+ # call it with the progress information
411+ if progress_token in self ._progress_callbacks :
412+ callback = self ._progress_callbacks [progress_token ]
413+ try :
414+ await callback (
415+ notification .root .params .progress ,
416+ notification .root .params .total ,
417+ notification .root .params .message ,
418+ )
419+ except Exception as e :
420+ logging .warning (
421+ "Progress callback raised an exception: %s" ,
422+ e ,
423+ )
424+ await self ._received_notification (notification )
425+ await self ._handle_incoming (notification )
416426 else : # Response or error
417427 stream = self ._response_streams .pop (message .message .root .id , None )
418428 if stream :
0 commit comments