diff --git a/mcp-core/src/main/java/io/modelcontextprotocol/client/transport/HttpClientStreamableHttpTransport.java b/mcp-core/src/main/java/io/modelcontextprotocol/client/transport/HttpClientStreamableHttpTransport.java index e56a23f08..67cc1035f 100644 --- a/mcp-core/src/main/java/io/modelcontextprotocol/client/transport/HttpClientStreamableHttpTransport.java +++ b/mcp-core/src/main/java/io/modelcontextprotocol/client/transport/HttpClientStreamableHttpTransport.java @@ -590,6 +590,13 @@ else if (contentType.contains(APPLICATION_JSON)) { // server does not recognise is already invalidated by the time // the caller learns about it. Consumed here so that it is not // handled a second time below. + // Detached from the session first: invalidating the session + // closes it, which would otherwise dispose this very exchange and + // complete sendMessage() through doOnCancel below. + Disposable ref = disposableRef.getAndSet(null); + if (ref != null) { + transportSession.removeConnection(ref); + } handleExceptionSafely(first.getThrowable()); deliveredSink.error(first.getThrowable()); return Flux.empty();