Skip to content

Commit 633dcfd

Browse files
committed
refactor(transport): replace Comparator-based max version lookup with Collections.max()
- Use method reference for Throwable::getCause in error mapping - Simplify orElseGet with orElse for null case
1 parent 082444e commit 633dcfd

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.net.http.HttpResponse.BodyHandler;
1313
import java.time.Duration;
1414
import java.util.Collections;
15-
import java.util.Comparator;
1615
import java.util.List;
1716
import java.util.Optional;
1817
import java.util.concurrent.CompletionException;
@@ -142,10 +141,7 @@ private HttpClientStreamableHttpTransport(McpJsonMapper jsonMapper, HttpClient h
142141
this.activeSession.set(createTransportSession());
143142
this.httpRequestCustomizer = httpRequestCustomizer;
144143
this.supportedProtocolVersions = Collections.unmodifiableList(supportedProtocolVersions);
145-
this.latestSupportedProtocolVersion = this.supportedProtocolVersions.stream()
146-
.sorted(Comparator.reverseOrder())
147-
.findFirst()
148-
.get();
144+
this.latestSupportedProtocolVersion = Collections.max(this.supportedProtocolVersions);
149145
}
150146

151147
@Override
@@ -367,7 +363,7 @@ else if (statusCode == BAD_REQUEST) {
367363
}).<McpSchema
368364
.JSONRPCMessage>flatMap(
369365
jsonrpcMessage -> this.handler.get().apply(Mono.just(jsonrpcMessage)))
370-
.onErrorMap(CompletionException.class, t -> t.getCause())
366+
.onErrorMap(CompletionException.class, Throwable::getCause)
371367
.onErrorComplete(t -> {
372368
this.handleException(t);
373369
return true;
@@ -463,11 +459,11 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage) {
463459
else {
464460
logger.debug("SSE connection established successfully");
465461
}
466-
})).onErrorMap(CompletionException.class, t -> t.getCause()).onErrorComplete().subscribe();
462+
})).onErrorMap(CompletionException.class, Throwable::getCause).onErrorComplete().subscribe();
467463

468464
})).flatMap(responseEvent -> {
469465
if (transportSession.markInitialized(
470-
responseEvent.responseInfo().headers().firstValue("mcp-session-id").orElseGet(() -> null))) {
466+
responseEvent.responseInfo().headers().firstValue("mcp-session-id").orElse(null))) {
471467
// Once we have a session, we try to open an async stream for
472468
// the server to send notifications and requests out-of-band.
473469

@@ -583,7 +579,7 @@ else if (statusCode == BAD_REQUEST) {
583579
new RuntimeException("Failed to send message: " + responseEvent));
584580
})
585581
.flatMap(jsonRpcMessage -> this.handler.get().apply(Mono.just(jsonRpcMessage)))
586-
.onErrorMap(CompletionException.class, t -> t.getCause())
582+
.onErrorMap(CompletionException.class, Throwable::getCause)
587583
.onErrorComplete(t -> {
588584
// handle the error first
589585
this.handleException(t);

0 commit comments

Comments
 (0)