@@ -694,23 +694,14 @@ public async Task NotifyCancel_Should_Be_Handled()
694
694
{
695
695
// Arrange
696
696
TaskCompletionSource < JsonRpcNotification > notificationReceived = new ( ) ;
697
- TaskCompletionSource notificationIntercepted = new ( ) ;
698
697
await using TestServerTransport transport = new ( LoggerFactory ) ;
699
698
transport . OnMessageSent = ( message ) =>
700
699
{
701
700
if ( message is JsonRpcNotification notification
702
701
&& notification . Method == NotificationMethods . CancelledNotification )
703
702
notificationReceived . TrySetResult ( notification ) ;
704
703
} ;
705
- var options = CreateOptions ( new ( )
706
- {
707
- NotificationHandlers = [ new ( NotificationMethods . CancelledNotification , ( notification , cancellationToken ) =>
708
- {
709
- InvalidOperationException exception = new ( "The sender of a notification shouldn't handle the notification." ) ;
710
- notificationIntercepted . TrySetException ( exception ) ;
711
- return notificationIntercepted . Task ;
712
- } ) ] ,
713
- } ) ;
704
+ var options = CreateOptions ( ) ;
714
705
await using var server = McpServerFactory . Create ( transport , options , LoggerFactory ) ;
715
706
716
707
// Act
@@ -732,10 +723,46 @@ await server.SendNotificationAsync(
732
723
Assert . NotNull ( cancelled ) ;
733
724
Assert . Equal ( "abc" , cancelled . RequestId . ToString ( ) ) ;
734
725
Assert . Equal ( "Cancelled" , cancelled . Reason ) ;
726
+ }
735
727
736
- Assert . Throws < TimeoutException > ( ( ) => notificationIntercepted . Task
737
- . Wait ( TimeSpan . FromSeconds ( 5 ) , token ) ) ;
738
- Assert . False ( notificationIntercepted . Task . IsCompleted ,
739
- "Notifications should not be intercepted by the sender of the notification." ) ;
728
+ [ Fact ]
729
+ public async Task SendRequest_Should_Notify_When_Cancelled ( )
730
+ {
731
+ // Arrange
732
+ TaskCompletionSource < JsonRpcNotification > notificationReceived = new ( ) ;
733
+ await using TestServerTransport transport = new ( LoggerFactory ) ;
734
+ transport . OnMessageSent = ( message ) =>
735
+ {
736
+ if ( message is JsonRpcNotification notification
737
+ && notification . Method == NotificationMethods . CancelledNotification )
738
+ notificationReceived . TrySetResult ( notification ) ;
739
+ } ;
740
+ var options = CreateOptions ( ) ;
741
+ await using var server = McpServerFactory . Create ( transport , options , LoggerFactory ) ;
742
+
743
+ // Act
744
+ var token = TestContext . Current . CancellationToken ;
745
+ Task serverTask = server . RunAsync ( token ) ;
746
+ using CancellationTokenSource cts = new ( ) ;
747
+ await cts . CancelAsync ( ) ;
748
+
749
+ await Assert . ThrowsAsync < TaskCanceledException > ( async ( ) =>
750
+ {
751
+ await server . SendRequestAsync ( new JsonRpcRequest
752
+ {
753
+ Method = RequestMethods . Ping ,
754
+ Id = new ( "abc" ) ,
755
+ } , cts . Token ) ;
756
+ } ) ;
757
+
758
+ await server . DisposeAsync ( ) ;
759
+ await serverTask . WaitAsync ( TimeSpan . FromSeconds ( 1 ) , token ) ;
760
+ var notification = await notificationReceived . Task . WaitAsync ( TimeSpan . FromSeconds ( 1 ) , token ) ;
761
+
762
+ // Assert
763
+ var cancelled = JsonSerializer . Deserialize < CancelledNotification > ( notification . Params ) ;
764
+ Assert . NotNull ( cancelled ) ;
765
+ Assert . Equal ( "abc" , cancelled . RequestId . ToString ( ) ) ;
766
+ Assert . Null ( cancelled . Reason ) ;
740
767
}
741
768
}
0 commit comments