@@ -639,81 +639,43 @@ public Task RunAsync(CancellationToken cancellationToken = default) =>
639639 [ Fact ]
640640 public async Task NotifyProgress_Should_Be_Handled ( )
641641 {
642- var taskCompletionSource = new TaskCompletionSource ( ) ;
643- bool notificationHandled = false ;
644- await Notifications_Are_Handled (
645- serverCapabilities : null ,
646- method : NotificationMethods . ProgressNotification ,
647- parameters : new ProgressNotification ( )
642+ await using TestServerTransport transport = new ( ) ;
643+ var options = CreateOptions ( ) ;
644+
645+ var notificationReceived = new TaskCompletionSource < JsonRpcNotification > ( ) ;
646+
647+ var server = McpServerFactory . Create ( transport , options , LoggerFactory , _serviceProvider ) ;
648+ server . AddNotificationHandler ( NotificationMethods . ProgressNotification , notification =>
649+ {
650+ notificationReceived . SetResult ( notification ) ;
651+ return Task . CompletedTask ;
652+ } ) ;
653+
654+ Task serverTask = server . RunAsync ( TestContext . Current . CancellationToken ) ;
655+
656+ await transport . SendMessageAsync ( new JsonRpcNotification
657+ {
658+ Method = NotificationMethods . ProgressNotification ,
659+ Params = new ProgressNotification ( )
648660 {
649- ProgressToken = new ( ) ,
661+ ProgressToken = new ( "abc" ) ,
650662 Progress = new ( )
651663 {
652664 Progress = 50 ,
653665 Total = 100 ,
654666 Message = "Progress message" ,
655667 } ,
656668 } ,
657- configureOptions : null ,
658- configureServer : server =>
659- {
660- server . AddNotificationHandler ( NotificationMethods . ProgressNotification ,
661- ( notification ) =>
662- {
663- notificationHandled = true ;
664- var progress = ( ProgressNotificationValue ? ) notification . Params ;
665- Assert . NotNull ( progress ) ;
666- var progressValue = progress . Value ;
667- taskCompletionSource . SetResult ( ) ;
668- Assert . Equal ( 50 , progressValue . Progress ) ;
669- Assert . Equal ( 100 , progressValue . Total ) ;
670- Assert . Equal ( "Progress message" , progressValue . Message ) ;
671- return Task . CompletedTask ;
672- } ) ;
673- } ,
674- assertResult : async response =>
675- {
676- //Note: awaiting here so handlers are guaranteed to be called first.
677- await taskCompletionSource . Task . WaitAsync ( TimeSpan . FromSeconds ( 1 ) ) ;
678- Assert . True ( notificationHandled ) ;
679- } ) ;
680- }
681-
682- private async Task Notifications_Are_Handled (
683- ServerCapabilities ? serverCapabilities ,
684- string method , object ? parameters ,
685- Action < McpServerOptions > ? configureOptions ,
686- Action < IMcpServer > ? configureServer ,
687- Action < JsonRpcNotification > assertResult )
688- {
689- await using TestServerTransport transport = new ( ) ;
690- var options = CreateOptions ( serverCapabilities ) ;
691- configureOptions ? . Invoke ( options ) ;
692-
693- await using var server = McpServerFactory . Create (
694- transport , options , LoggerFactory , _serviceProvider ) ;
695-
696- configureServer ? . Invoke ( server ) ;
697- await server . RunAsync ( ) ;
698-
699- TaskCompletionSource < JsonRpcNotification > receivedMessage = new ( ) ;
700-
701- transport . OnMessageSent = ( message ) =>
702- {
703- Assert . NotNull ( message ) ;
704- if ( message is JsonRpcNotification notification && notification . Method == method )
705- {
706- assertResult ( notification ) ;
707- receivedMessage . SetResult ( notification ) ;
708- }
709- } ;
669+ } , TestContext . Current . CancellationToken ) ;
710670
711- await transport . SendMessageAsync ( new JsonRpcNotification
712- {
713- Method = method ,
714- Params = parameters ,
715- } ) ;
671+ var notification = await notificationReceived . Task ;
672+ var progress = ( ProgressNotification ) notification . Params ! ;
673+ Assert . Equal ( "\" abc\" " , progress . ProgressToken . ToString ( ) ) ;
674+ Assert . Equal ( 50 , progress . Progress . Progress ) ;
675+ Assert . Equal ( 100 , progress . Progress . Total ) ;
676+ Assert . Equal ( "Progress message" , progress . Progress . Message ) ;
716677
717- var response = await receivedMessage . Task . WaitAsync ( TimeSpan . FromSeconds ( 1 ) ) ;
678+ await server . DisposeAsync ( ) ;
679+ await serverTask ;
718680 }
719681}
0 commit comments