Skip to content

Commit 2b4e4b9

Browse files
committed
chore: Spring 5 compatibility
Signed-off-by: He-Pin <hepin1989@gmail.com>
1 parent 87bdf1e commit 2b4e4b9

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

mcp-spring/mcp-spring-webflux/src/main/java/io/modelcontextprotocol/client/transport/WebClientStreamableHttpTransport.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
308308

309309
// The spec mentions only ACCEPTED, but the existing SDKs can return
310310
// 200 OK for notifications
311-
if (response.statusCode().is2xxSuccessful()) {
311+
if (is2xx(response)) {
312312
Optional<MediaType> contentType = response.headers().contentType();
313313
long contentLength = response.headers().contentLength().orElse(-1);
314314
// Existing SDKs consume notifications with no response body nor
@@ -392,14 +392,15 @@ private Flux<McpSchema.JSONRPCMessage> extractError(ClientResponse response, Str
392392
}
393393
catch (IOException ex) {
394394
toPropagate = new McpTransportException("Sending request failed, " + e.getMessage(), e);
395-
logger.debug("Received content together with {} HTTP code response: {}", response.statusCode(), body);
395+
logger.debug("Received content together with {} HTTP code response: {}", response.rawStatusCode(),
396+
body);
396397
}
397398

398399
// Some implementations can return 400 when presented with a
399400
// session id that it doesn't know about, so we will
400401
// invalidate the session
401402
// https://github.com/modelcontextprotocol/typescript-sdk/issues/389
402-
if (responseException.getStatusCode().isSameCodeAs(HttpStatus.BAD_REQUEST)) {
403+
if (isBadRequest(responseException)) {
403404
if (!sessionRepresentation.equals(MISSING_SESSION_ID)) {
404405
return Mono.error(new McpTransportSessionNotFoundException(sessionRepresentation, toPropagate));
405406
}
@@ -419,16 +420,8 @@ private Flux<McpSchema.JSONRPCMessage> eventStream(McpTransportStream<Disposable
419420
return Flux.from(sessionStream.consumeSseStream(idWithMessages));
420421
}
421422

422-
private static boolean isNotFound(ClientResponse response) {
423-
return response.statusCode().isSameCodeAs(HttpStatus.NOT_FOUND);
424-
}
425-
426-
private static boolean isNotAllowed(ClientResponse response) {
427-
return response.statusCode().isSameCodeAs(HttpStatus.METHOD_NOT_ALLOWED);
428-
}
429-
430423
private static boolean isEventStream(ClientResponse response) {
431-
return response.statusCode().is2xxSuccessful() && response.headers().contentType().isPresent()
424+
return is2xx(response) && response.headers().contentType().isPresent()
432425
&& response.headers().contentType().get().isCompatibleWith(MediaType.TEXT_EVENT_STREAM);
433426
}
434427

@@ -607,4 +600,32 @@ public WebClientStreamableHttpTransport build() {
607600

608601
}
609602

603+
/**
604+
* Needed for Spring 5 compatibility
605+
*/
606+
private static boolean isBadRequest(final WebClientResponseException responseException) {
607+
return responseException.getRawStatusCode() == HttpStatus.BAD_REQUEST.value();
608+
}
609+
610+
/**
611+
* Needed for Spring 5 compatibility
612+
*/
613+
private static boolean isNotFound(ClientResponse response) {
614+
return response.rawStatusCode() == HttpStatus.NOT_FOUND.value();
615+
}
616+
617+
/**
618+
* Needed for Spring 5 compatibility
619+
*/
620+
private static boolean isNotAllowed(ClientResponse response) {
621+
return response.rawStatusCode() == HttpStatus.METHOD_NOT_ALLOWED.value();
622+
}
623+
624+
/**
625+
* Needed for Spring 5 compatibility
626+
*/
627+
private static boolean is2xx(final ClientResponse response) {
628+
return response.rawStatusCode() >= 200 && response.rawStatusCode() < 300;
629+
}
630+
610631
}

0 commit comments

Comments
 (0)