66
77import java .io .IOException ;
88import java .net .InetSocketAddress ;
9+ import java .net .URI ;
910import java .net .http .HttpResponse ;
1011import java .time .Duration ;
1112import java .util .ArrayList ;
@@ -403,9 +404,12 @@ void invokeHandler(int httpStatus) {
403404 AtomicReference <HttpResponse .ResponseInfo > capturedResponseInfo = new AtomicReference <>();
404405 AtomicReference <McpTransportContext > capturedContext = new AtomicReference <>();
405406
407+ AtomicReference <URI > capturedUri = new AtomicReference <>();
408+
406409 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
407- .authorizationErrorHandler ((responseInfo , context ) -> {
410+ .authorizationErrorHandler ((responseInfo , requestUri , context ) -> {
408411 capturedResponseInfo .set (responseInfo );
412+ capturedUri .set (requestUri );
409413 capturedContext .set (context );
410414 return Mono .just (false );
411415 })
@@ -417,6 +421,8 @@ void invokeHandler(int httpStatus) {
417421 assertThat (processedMessagesCount .get ()).isEqualTo (1 );
418422 assertThat (capturedResponseInfo .get ()).isNotNull ();
419423 assertThat (capturedResponseInfo .get ().statusCode ()).isEqualTo (httpStatus );
424+ assertThat (capturedUri .get ()).isNotNull ();
425+ assertThat (capturedUri .get ().toString ()).isEqualTo (HOST + "/mcp" );
420426 assertThat (capturedContext .get ()).isNotNull ();
421427
422428 StepVerifier .create (authTransport .closeGracefully ()).verifyComplete ();
@@ -440,7 +446,7 @@ void defaultHandler() {
440446 void retry () {
441447 serverResponseStatus .set (401 );
442448 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
443- .authorizationErrorHandler ((responseInfo , context ) -> {
449+ .authorizationErrorHandler ((responseInfo , requestUri , context ) -> {
444450 serverResponseStatus .set (200 );
445451 return Mono .just (true );
446452 })
@@ -456,7 +462,7 @@ void retry() {
456462 void retryAtMostOnce () {
457463 serverResponseStatus .set (401 );
458464 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
459- .authorizationErrorHandler ((responseInfo , context ) -> Mono .just (true ))
465+ .authorizationErrorHandler ((responseInfo , requestUri , context ) -> Mono .just (true ))
460466 .build ();
461467 StepVerifier .create (authTransport .sendMessage (createTestRequestMessage ()))
462468 .expectErrorMatches (authorizationError (401 ))
@@ -473,7 +479,7 @@ void customMaxRetries() {
473479 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
474480 .authorizationErrorHandler (new McpHttpClientAuthorizationErrorHandler () {
475481 @ Override
476- public Publisher <Boolean > handle (HttpResponse .ResponseInfo responseInfo ,
482+ public Publisher <Boolean > handle (HttpResponse .ResponseInfo responseInfo , URI requestUri ,
477483 McpTransportContext context ) {
478484 return Mono .just (true );
479485 }
@@ -498,7 +504,7 @@ void noRetry() {
498504 serverResponseStatus .set (401 );
499505
500506 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
501- .authorizationErrorHandler ((responseInfo , context ) -> Mono .just (false ))
507+ .authorizationErrorHandler ((responseInfo , requestUri , context ) -> Mono .just (false ))
502508 .build ();
503509
504510 StepVerifier .create (authTransport .sendMessage (createTestRequestMessage ()))
@@ -513,8 +519,8 @@ void noRetry() {
513519 void propagateHandlerError () {
514520 serverResponseStatus .set (401 );
515521 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
516- .authorizationErrorHandler (
517- ( responseInfo , context ) -> Mono .error (new IllegalStateException ("handler error" )))
522+ .authorizationErrorHandler (( responseInfo , requestUri , context ) -> Mono
523+ .error (new IllegalStateException ("handler error" )))
518524 .build ();
519525
520526 StepVerifier .create (authTransport .sendMessage (createTestRequestMessage ()))
@@ -529,7 +535,7 @@ void propagateHandlerError() {
529535 void emptyHandler () {
530536 serverResponseStatus .set (401 );
531537 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
532- .authorizationErrorHandler ((responseInfo , context ) -> Mono .empty ())
538+ .authorizationErrorHandler ((responseInfo , requestUri , context ) -> Mono .empty ())
533539 .build ();
534540
535541 StepVerifier .create (authTransport .sendMessage (createTestRequestMessage ()))
@@ -552,11 +558,13 @@ void invokeHandler(int httpStatus) {
552558 AtomicReference <Throwable > capturedException = new AtomicReference <>();
553559
554560 AtomicReference <HttpResponse .ResponseInfo > capturedResponseInfo = new AtomicReference <>();
561+ AtomicReference <URI > capturedUri = new AtomicReference <>();
555562 AtomicReference <McpTransportContext > capturedContext = new AtomicReference <>();
556563
557564 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
558- .authorizationErrorHandler ((responseInfo , context ) -> {
565+ .authorizationErrorHandler ((responseInfo , requestUri , context ) -> {
559566 capturedResponseInfo .set (responseInfo );
567+ capturedUri .set (requestUri );
560568 capturedContext .set (context );
561569 return Mono .just (false );
562570 })
@@ -572,6 +580,8 @@ void invokeHandler(int httpStatus) {
572580 assertThat (messages ).isEmpty ();
573581 assertThat (capturedResponseInfo .get ()).isNotNull ();
574582 assertThat (capturedResponseInfo .get ().statusCode ()).isEqualTo (httpStatus );
583+ assertThat (capturedUri .get ()).isNotNull ();
584+ assertThat (capturedUri .get ().toString ()).isEqualTo (HOST + "/mcp" );
575585 assertThat (capturedContext .get ()).isNotNull ();
576586 assertThat (capturedException .get ()).hasMessage ("Authorization error connecting to SSE stream" )
577587 .asInstanceOf (type (McpHttpClientTransportAuthorizationException .class ))
@@ -606,7 +616,7 @@ void retry() {
606616 AtomicReference <Throwable > capturedException = new AtomicReference <>();
607617 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
608618 .openConnectionOnStartup (true )
609- .authorizationErrorHandler ((responseInfo , context ) -> {
619+ .authorizationErrorHandler ((responseInfo , requestUri , context ) -> {
610620 serverSseResponseStatus .set (200 );
611621 return Mono .just (true );
612622 })
@@ -636,7 +646,7 @@ void retryAtMostOnce() {
636646 AtomicReference <Throwable > capturedException = new AtomicReference <>();
637647 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
638648 .openConnectionOnStartup (true )
639- .authorizationErrorHandler ((responseInfo , context ) -> {
649+ .authorizationErrorHandler ((responseInfo , requestUri , context ) -> {
640650 return Mono .just (true );
641651 })
642652 .build ();
@@ -663,7 +673,7 @@ void customMaxRetries() {
663673 .openConnectionOnStartup (true )
664674 .authorizationErrorHandler (new McpHttpClientAuthorizationErrorHandler () {
665675 @ Override
666- public Publisher <Boolean > handle (HttpResponse .ResponseInfo responseInfo ,
676+ public Publisher <Boolean > handle (HttpResponse .ResponseInfo responseInfo , URI requestUri ,
667677 McpTransportContext context ) {
668678 return Mono .just (true );
669679 }
@@ -695,7 +705,7 @@ void noRetry() {
695705 AtomicReference <Throwable > capturedException = new AtomicReference <>();
696706 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
697707 .openConnectionOnStartup (true )
698- .authorizationErrorHandler ((responseInfo , context ) -> {
708+ .authorizationErrorHandler ((responseInfo , requestUri , context ) -> {
699709 // if there was a retry, the request would succeed.
700710 serverSseResponseStatus .set (200 );
701711 return Mono .just (false );
@@ -720,7 +730,7 @@ void emptyHandler() {
720730 AtomicReference <Throwable > capturedException = new AtomicReference <>();
721731 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
722732 .openConnectionOnStartup (true )
723- .authorizationErrorHandler ((responseInfo , context ) -> Mono .empty ())
733+ .authorizationErrorHandler ((responseInfo , requestUri , context ) -> Mono .empty ())
724734 .build ();
725735 authTransport .setExceptionHandler (capturedException ::set );
726736
@@ -741,8 +751,8 @@ void propagateHandlerError() {
741751 AtomicReference <Throwable > capturedException = new AtomicReference <>();
742752 var authTransport = HttpClientStreamableHttpTransport .builder (HOST )
743753 .openConnectionOnStartup (true )
744- .authorizationErrorHandler (
745- ( responseInfo , context ) -> Mono .error (new IllegalStateException ("handler error" )))
754+ .authorizationErrorHandler (( responseInfo , requestUri , context ) -> Mono
755+ .error (new IllegalStateException ("handler error" )))
746756 .build ();
747757 authTransport .setExceptionHandler (capturedException ::set );
748758
0 commit comments