@@ -422,17 +422,23 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage) {
422422 return Mono .from (this .httpRequestCustomizer .customize (builder , "POST" , uri , jsonBody ));
423423 }).flatMapMany (requestBuilder -> Flux .<ResponseEvent >create (responseEventSink -> {
424424
425- // Create the async request with proper body subscriber selection
426- Mono .fromFuture (this .httpClient
427- .sendAsync (requestBuilder .build (), this .toSendMessageBodySubscriber (responseEventSink ))
428- .whenComplete ((response , throwable ) -> {
429- if (throwable != null ) {
430- responseEventSink .error (throwable );
431- }
432- else {
433- logger .debug ("SSE connection established successfully" );
434- }
435- })).onErrorMap (CompletionException .class , t -> t .getCause ()).onErrorComplete ().subscribe ();
425+ // Create the async request with proper error handling and timeout
426+ // The key insight: the response body is consumed by the BodySubscriber
427+ // and flows through responseEventSink
428+ // The CompletableFuture<HttpResponse<Void>> completes when headers are
429+ // received, not when body is consumed
430+ Mono .fromFuture (() -> this .httpClient .sendAsync (requestBuilder .build (),
431+ this .toSendMessageBodySubscriber (responseEventSink )))
432+ .doOnSuccess (response -> {
433+ logger .debug ("Success: " + response .statusCode ());
434+ })
435+ .doOnError (throwable -> {
436+ logger .error ("HTTP request failed with message {}" , throwable .getMessage (), throwable );
437+ // Ensure the sink gets the error if it hasn't been completed yet
438+ responseEventSink .error (throwable );
439+ })
440+ .onErrorMap (CompletionException .class , Throwable ::getCause )
441+ .subscribe ();
436442
437443 })).flatMap (responseEvent -> {
438444 if (transportSession .markInitialized (
@@ -500,6 +506,11 @@ else if (contentType.contains(APPLICATION_JSON)) {
500506 return Mono .empty ();
501507 }
502508
509+ if (!Utils .hasText (data )) {
510+ deliveredSink .success ();
511+ return Mono .empty ();
512+ }
513+
503514 try {
504515 return Mono .just (McpSchema .deserializeJsonRpcMessage (objectMapper , data ));
505516 }
0 commit comments