-
Notifications
You must be signed in to change notification settings - Fork 870
fix: improve compatibility with non-compliant MCP servers #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -358,7 +358,7 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sendMessage) { | |
| String jsonBody = this.toString(sendMessage); | ||
|
|
||
| HttpRequest request = requestBuilder.uri(Utils.resolveUri(this.baseUri, this.endpoint)) | ||
| .header("Accept", TEXT_EVENT_STREAM + ", " + APPLICATION_JSON) | ||
| .header("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM) | ||
| .header("Content-Type", APPLICATION_JSON) | ||
| .header("Cache-Control", "no-cache") | ||
| .POST(HttpRequest.BodyPublishers.ofString(jsonBody)) | ||
|
|
@@ -436,11 +436,19 @@ else if (contentType.contains(TEXT_EVENT_STREAM)) { | |
| else if (contentType.contains(APPLICATION_JSON)) { | ||
| messageSink.success(); | ||
| String data = ((ResponseSubscribers.AggregateResponseEvent) responseEvent).data(); | ||
| try { | ||
| return Mono.just(McpSchema.deserializeJsonRpcMessage(objectMapper, data)); | ||
| if (Utils.hasText(data) && !data.trim().equals("{}")) { | ||
|
|
||
| try { | ||
| return Mono.just(McpSchema.deserializeJsonRpcMessage(objectMapper, data)); | ||
| } | ||
| catch (IOException e) { | ||
| return Mono.error(e); | ||
| } | ||
| } | ||
| catch (IOException e) { | ||
| return Mono.error(e); | ||
| else { | ||
| // No content type means no response body | ||
| logger.debug("No content type returned for POST in session {}", sessionRepresentation); | ||
| return Mono.empty(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as for the WebClient implementation.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is the same problem as above |
||
| } | ||
| } | ||
| logger.warn("Unknown media type {} returned for POST in session {}", contentType, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,36 +135,46 @@ protected void hookOnSubscribe(Subscription subscription) { | |
|
|
||
| @Override | ||
| protected void hookOnNext(String line) { | ||
| if (line.isEmpty()) { | ||
| // Empty line means end of event | ||
| if (this.eventBuilder.length() > 0) { | ||
| String eventData = this.eventBuilder.toString(); | ||
| SseEvent sseEvent = new SseEvent(currentEventId.get(), currentEventType.get(), eventData.trim()); | ||
|
|
||
| this.sink.next(new SseResponseEvent(responseInfo, sseEvent)); | ||
| this.eventBuilder.setLength(0); | ||
| } | ||
| } | ||
| else { | ||
| if (line.startsWith("data:")) { | ||
| var matcher = EVENT_DATA_PATTERN.matcher(line); | ||
| if (matcher.find()) { | ||
| this.eventBuilder.append(matcher.group(1).trim()).append("\n"); | ||
| if (this.responseInfo.statusCode() >= 200 && this.responseInfo.statusCode() < 300) { | ||
|
|
||
| if (line.isEmpty()) { | ||
| // Empty line means end of event | ||
| if (this.eventBuilder.length() > 0) { | ||
| String eventData = this.eventBuilder.toString(); | ||
| SseEvent sseEvent = new SseEvent(currentEventId.get(), currentEventType.get(), | ||
| eventData.trim()); | ||
|
|
||
| this.sink.next(new SseResponseEvent(responseInfo, sseEvent)); | ||
| this.eventBuilder.setLength(0); | ||
| } | ||
| } | ||
| else if (line.startsWith("id:")) { | ||
| var matcher = EVENT_ID_PATTERN.matcher(line); | ||
| if (matcher.find()) { | ||
| this.currentEventId.set(matcher.group(1).trim()); | ||
| else { | ||
| if (line.startsWith("data:")) { | ||
| var matcher = EVENT_DATA_PATTERN.matcher(line); | ||
| if (matcher.find()) { | ||
| this.eventBuilder.append(matcher.group(1).trim()).append("\n"); | ||
| } | ||
| } | ||
| } | ||
| else if (line.startsWith("event:")) { | ||
| var matcher = EVENT_TYPE_PATTERN.matcher(line); | ||
| if (matcher.find()) { | ||
| this.currentEventType.set(matcher.group(1).trim()); | ||
| else if (line.startsWith("id:")) { | ||
| var matcher = EVENT_ID_PATTERN.matcher(line); | ||
| if (matcher.find()) { | ||
| this.currentEventId.set(matcher.group(1).trim()); | ||
| } | ||
| } | ||
| else if (line.startsWith("event:")) { | ||
| var matcher = EVENT_TYPE_PATTERN.matcher(line); | ||
| if (matcher.find()) { | ||
| this.currentEventType.set(matcher.group(1).trim()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else { | ||
| // If the response is not successful, emit an error | ||
| System.out.println("Received non-successful response: " + this.responseInfo.statusCode()); | ||
| SseEvent sseEvent = new SseEvent(null, null, null); | ||
| this.sink.next(new SseResponseEvent(responseInfo, sseEvent)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks unfinished. Did you intend to use the logger and call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeh good catch. I've mistakenly commit some experiments. Above logic is incorrect. |
||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit worried that if the request body was an MCP Request then we will never complete a pending operation that awaits for it. Please consider adding a check whether the original message was a request and if so send an error to the Flux (using
handle's sink).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately if we receive an empty response message (e.g.
null," "or{}) there is no way to determine if it is a bogus response from a previous request, compliment notification response or bogus request.Here we assume that if an empty even is received thread it as incorrect notification response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have to cancel the request's sink though. We end up in this method if the server responded with an application/json content type. That means semantically we are to consume a response here. If there is nothing, we have to clean up the request sink, otherwise we are creating memory leaks and also not really doing the users of the client a favour not letting them know that the request has failed to fulfil the promise of a response.