99import java .util .Map ;
1010import java .util .Set ;
1111import java .util .concurrent .atomic .AtomicBoolean ;
12+ import java .util .concurrent .atomic .AtomicInteger ;
1213import java .util .function .BiFunction ;
1314
1415import io .modelcontextprotocol .modern .JsonRpc .JSONRPCNotification ;
2627import io .modelcontextprotocol .modern .McpSchema .TextContent ;
2728import io .modelcontextprotocol .spec .json .gson .GsonMcpJsonMapper ;
2829import org .junit .jupiter .api .Test ;
30+ import org .junit .jupiter .params .ParameterizedTest ;
31+ import org .junit .jupiter .params .provider .ValueSource ;
2932import reactor .core .publisher .Mono ;
3033import reactor .test .StepVerifier ;
3134
@@ -297,10 +300,11 @@ void streamingBodyRunsOnlyOnceTheStreamIsSubscribed() {
297300
298301 @ Test
299302 void streamingBodyNotificationsPrecedeTheResult () {
300- McpServer server = baseBuilder ().feature (feature ("tools/call" ,
301- (ctx , params ) -> Mono .just (McpAsyncResponse .streaming (notifier -> notifier .progress (1.0 , 2.0 , "half" )
302- .then (notifier .progress (2.0 , 2.0 , "done" ))
303- .thenReturn (ok ())))))
303+ McpServer server = baseBuilder ().feature (feature ("tools/call" , (ctx , params ) -> Mono
304+ .just (McpAsyncResponse .streaming (notifier -> notifier .progress (1.0 , 2.0 , "half" )
305+ .then (notifier .progress (2.0 , 2.0 , "done" ))
306+ .then (notifier .notify (McpSchema .METHOD_NOTIFICATION_MESSAGE , Map .of ("level" , "info" , "data" , "hello" )))
307+ .thenReturn (ok ())))))
304308 .build ();
305309
306310 McpTransportResponse invocation = invoke (server , request ("tools/call" , meta (MetaKeys .PROGRESS_TOKEN , "tok-1" )))
@@ -309,6 +313,8 @@ void streamingBodyNotificationsPrecedeTheResult() {
309313 StepVerifier .create (((McpTransportResponse .Streaming ) invocation ).messages ())
310314 .expectNextMatches (msg -> msg instanceof JSONRPCNotification )
311315 .expectNextMatches (msg -> msg instanceof JSONRPCNotification )
316+ .expectNextMatches (msg -> msg instanceof JSONRPCNotification notification
317+ && notification .method ().equals (McpSchema .METHOD_NOTIFICATION_MESSAGE ))
312318 .expectNextMatches (msg -> msg instanceof JSONRPCResponse )
313319 .verifyComplete ();
314320 }
@@ -328,6 +334,118 @@ void progressIsSuppressedWithoutProgressToken() {
328334 .verifyComplete ();
329335 }
330336
337+ @ Test
338+ void notifierFactoryFiltersUsingEachRequestsContextAndPreservesProgress () {
339+ AtomicInteger created = new AtomicInteger ();
340+ McpServer server = baseBuilder ().notifierFactory ((ctx , delegate ) -> {
341+ created .incrementAndGet ();
342+ return new McpAsyncNotifier () {
343+ @ Override
344+ public Mono <Void > progress (double progress , Double total , String message ) {
345+ return delegate .progress (progress , total , message );
346+ }
347+
348+ @ Override
349+ public Mono <Void > notify (String method , Object params ) {
350+ if (McpSchema .METHOD_NOTIFICATION_MESSAGE .equals (method )
351+ && !ctx .meta ().containsKey (MetaKeys .LOG_LEVEL )) {
352+ return Mono .empty ();
353+ }
354+ return delegate .notify (method , params );
355+ }
356+ };
357+ })
358+ .feature (feature ("tools/call" ,
359+ (ctx , params ) -> Mono .just (McpAsyncResponse .streaming (notifier -> notifier
360+ .notify (McpSchema .METHOD_NOTIFICATION_MESSAGE , Map .of ("level" , "info" , "data" , "hello" ))
361+ .then (notifier .progress (1.0 , 1.0 , "done" ))
362+ .thenReturn (ok ())))))
363+ .build ();
364+
365+ var quiet = (McpTransportResponse .Streaming ) invoke (server ,
366+ request ("tools/call" , meta (MetaKeys .PROGRESS_TOKEN , "quiet" )))
367+ .block ();
368+ var logging = (McpTransportResponse .Streaming ) invoke (server , new JSONRPCRequest ("tools/call" , 2 ,
369+ Map .of ("_meta" , meta (MetaKeys .PROGRESS_TOKEN , "logging" , MetaKeys .LOG_LEVEL , "info" ))))
370+ .block ();
371+
372+ assertThat (created ).hasValue (0 );
373+ StepVerifier .create (quiet .messages ())
374+ .expectNextMatches (msg -> msg instanceof JSONRPCNotification notification
375+ && notification .method ().equals (McpSchema .METHOD_NOTIFICATION_PROGRESS ))
376+ .expectNextMatches (msg -> msg instanceof JSONRPCResponse response && response .error () == null )
377+ .verifyComplete ();
378+ StepVerifier .create (logging .messages ())
379+ .expectNextMatches (msg -> msg instanceof JSONRPCNotification notification
380+ && notification .method ().equals (McpSchema .METHOD_NOTIFICATION_MESSAGE ))
381+ .expectNextMatches (msg -> msg instanceof JSONRPCNotification notification
382+ && notification .method ().equals (McpSchema .METHOD_NOTIFICATION_PROGRESS ))
383+ .expectNextMatches (msg -> msg instanceof JSONRPCResponse response && response .error () == null )
384+ .verifyComplete ();
385+ assertThat (created ).hasValue (2 );
386+ }
387+
388+ @ ParameterizedTest
389+ @ ValueSource (booleans = { false , true })
390+ void invalidNotifierFactoryIsAnInternalErrorInStream (boolean returnsNull ) {
391+ AtomicBoolean ran = new AtomicBoolean ();
392+ McpServer server = baseBuilder ().notifierFactory ((ctx , delegate ) -> {
393+ if (returnsNull ) {
394+ return null ;
395+ }
396+ throw new IllegalStateException ("factory failed" );
397+ }).feature (feature ("tools/call" , (ctx , params ) -> Mono .just (McpAsyncResponse .streaming (notifier -> {
398+ ran .set (true );
399+ return Mono .just (ok ());
400+ })))).build ();
401+
402+ var invocation = (McpTransportResponse .Streaming ) invoke (server , request ("tools/call" , meta ())).block ();
403+
404+ StepVerifier .create (invocation .messages ())
405+ .expectNextMatches (msg -> msg instanceof JSONRPCResponse response
406+ && response .error ().code () == ErrorCodes .INTERNAL_ERROR )
407+ .verifyComplete ();
408+ assertThat (ran ).isFalse ();
409+ }
410+
411+ @ Test
412+ void notifierFactoryCanRejectANotificationWithAnMcpError () {
413+ McpServer server = baseBuilder ().notifierFactory ((ctx , delegate ) -> new McpAsyncNotifier () {
414+ @ Override
415+ public Mono <Void > progress (double progress , Double total , String message ) {
416+ return delegate .progress (progress , total , message );
417+ }
418+
419+ @ Override
420+ public Mono <Void > notify (String method , Object params ) {
421+ return Mono .error (McpException .invalidParams ("Notification not allowed" ));
422+ }
423+ })
424+ .feature (feature ("tools/call" ,
425+ (ctx , params ) -> Mono .just (McpAsyncResponse
426+ .streaming (notifier -> notifier .notify ("extension/forbidden" , Map .of ()).thenReturn (ok ())))))
427+ .build ();
428+
429+ var invocation = (McpTransportResponse .Streaming ) invoke (server , request ("tools/call" , meta ())).block ();
430+
431+ StepVerifier .create (invocation .messages ())
432+ .expectNextMatches (msg -> msg instanceof JSONRPCResponse response
433+ && response .error ().code () == ErrorCodes .INVALID_PARAMS
434+ && response .error ().message ().equals ("Notification not allowed" ))
435+ .verifyComplete ();
436+ }
437+
438+ @ Test
439+ void notifierFactoryIsNotInvokedForSingleResults () {
440+ McpServer server = baseBuilder ().notifierFactory ((ctx , delegate ) -> {
441+ throw new IllegalStateException ("No notifier needed" );
442+ }).feature (echoFeature ("tools/call" )).build ();
443+
444+ StepVerifier .create (respond (server , request ("tools/call" , meta ())))
445+ .assertNext (response -> assertThat (response .error ()).isNull ())
446+ .verifyComplete ();
447+ }
448+
331449 @ Test
332450 void streamingBodyExceptionIsAnInternalErrorInStream () {
333451 McpServer server = baseBuilder ()
0 commit comments