@@ -433,73 +433,77 @@ async def run(
433433
434434 match message :
435435 case RequestResponder (request = types .ClientRequest (root = req )):
436- logger . info (
437- f"Processing request of type { type ( req ). __name__ } "
436+ await self . _handle_request (
437+ message , req , session , raise_exceptions
438438 )
439- if type (req ) in self .request_handlers :
440- handler = self .request_handlers [type (req )]
441- logger .debug (
442- f"Dispatching request of type { type (req ).__name__ } "
443- )
444-
445- token = None
446- try :
447- # Set our global state that can be retrieved via
448- # app.get_request_context()
449- token = request_ctx .set (
450- RequestContext (
451- message .request_id ,
452- message .request_meta ,
453- session ,
454- )
455- )
456- response = await handler (req )
457- except McpError as err :
458- response = err .error
459- except Exception as err :
460- if raise_exceptions :
461- raise err
462- response = types .ErrorData (
463- code = 0 , message = str (err ), data = None
464- )
465- finally :
466- # Reset the global state after we are done
467- if token is not None :
468- request_ctx .reset (token )
469-
470- await message .respond (response )
471- else :
472- await message .respond (
473- types .ErrorData (
474- code = types .METHOD_NOT_FOUND ,
475- message = "Method not found" ,
476- )
477- )
478-
479- logger .debug ("Response sent" )
480439 case types .ClientNotification (root = notify ):
481- if type (notify ) in self .notification_handlers :
482- assert type (notify ) in self .notification_handlers
483-
484- handler = self .notification_handlers [type (notify )]
485- logger .debug (
486- f"Dispatching notification of type "
487- f"{ type (notify ).__name__ } "
488- )
489-
490- try :
491- await handler (notify )
492- except Exception as err :
493- logger .error (
494- f"Uncaught exception in notification handler: "
495- f"{ err } "
496- )
440+ await self ._handle_notification (notify )
497441
498442 for warning in w :
499443 logger .info (
500444 f"Warning: { warning .category .__name__ } : { warning .message } "
501445 )
502446
447+ async def _handle_request (
448+ self ,
449+ message : RequestResponder ,
450+ req : Any ,
451+ session : ServerSession ,
452+ raise_exceptions : bool ,
453+ ):
454+ logger .info (f"Processing request of type { type (req ).__name__ } " )
455+ if type (req ) in self .request_handlers :
456+ handler = self .request_handlers [type (req )]
457+ logger .debug (f"Dispatching request of type { type (req ).__name__ } " )
458+
459+ token = None
460+ try :
461+ # Set our global state that can be retrieved via
462+ # app.get_request_context()
463+ token = request_ctx .set (
464+ RequestContext (
465+ message .request_id ,
466+ message .request_meta ,
467+ session ,
468+ )
469+ )
470+ response = await handler (req )
471+ except McpError as err :
472+ response = err .error
473+ except Exception as err :
474+ if raise_exceptions :
475+ raise err
476+ response = types .ErrorData (code = 0 , message = str (err ), data = None )
477+ finally :
478+ # Reset the global state after we are done
479+ if token is not None :
480+ request_ctx .reset (token )
481+
482+ await message .respond (response )
483+ else :
484+ await message .respond (
485+ types .ErrorData (
486+ code = types .METHOD_NOT_FOUND ,
487+ message = "Method not found" ,
488+ )
489+ )
490+
491+ logger .debug ("Response sent" )
492+
493+ async def _handle_notification (self , notify : Any ):
494+ if type (notify ) in self .notification_handlers :
495+ assert type (notify ) in self .notification_handlers
496+
497+ handler = self .notification_handlers [type (notify )]
498+ logger .debug (
499+ f"Dispatching notification of type " f"{ type (notify ).__name__ } "
500+ )
501+
502+ try :
503+ await handler (notify )
504+ except Exception as err :
505+ logger .error (f"Uncaught exception in notification handler: " f"{ err } " )
506+
503507
504508async def _ping_handler (request : types .PingRequest ) -> types .ServerResult :
505509 return types .ServerResult (types .EmptyResult ())
0 commit comments