Skip to content

Commit 5c47844

Browse files
Move markInitialized/reconnect after status code check in Streamable HTTP transport
When the server returns a non-2xx status code (e.g. 405 Method Not Allowed), markInitialized() and reconnect() were called before the status code was checked. This caused an unnecessary GET request, which is problematic when using transport fallback (Streamable HTTP -> SSE) as it creates duplicate SSE sessions on the server. Move markInitialized() and reconnect() inside the 2xx success branch so they are only called when the server actually accepted the connection. Fixes #773 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 26304a7 commit 5c47844

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

mcp-core/src/main/java/io/modelcontextprotocol/client/transport/HttpClientStreamableHttpTransport.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,20 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage) {
478478
})).onErrorMap(CompletionException.class, t -> t.getCause()).onErrorComplete().subscribe();
479479

480480
})).flatMap(responseEvent -> {
481-
if (transportSession.markInitialized(
482-
responseEvent.responseInfo().headers().firstValue("mcp-session-id").orElseGet(() -> null))) {
483-
// Once we have a session, we try to open an async stream for
484-
// the server to send notifications and requests out-of-band.
485-
486-
reconnect(null).contextWrite(deliveredSink.contextView()).subscribe();
487-
}
488-
489481
String sessionRepresentation = sessionIdOrPlaceholder(transportSession);
490482

491483
int statusCode = responseEvent.responseInfo().statusCode();
492484

493485
if (statusCode >= 200 && statusCode < 300) {
486+
if (transportSession.markInitialized(responseEvent.responseInfo()
487+
.headers()
488+
.firstValue("mcp-session-id")
489+
.orElseGet(() -> null))) {
490+
// Once we have a session, we try to open an async stream
491+
// for the server to send notifications and requests
492+
// out-of-band.
493+
reconnect(null).contextWrite(deliveredSink.contextView()).subscribe();
494+
}
494495

495496
String contentType = responseEvent.responseInfo()
496497
.headers()

0 commit comments

Comments
 (0)