Skip to content

Fix NPE caused by response without id #509

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*
* @author Christian Tzolov
* @author Dariusz Jędrzejczyk
* @author Yanming Zhou
*/
public class McpClientSession implements McpSession {

Expand Down Expand Up @@ -146,13 +147,18 @@ private void dismissPendingResponses() {

private void handle(McpSchema.JSONRPCMessage message) {
if (message instanceof McpSchema.JSONRPCResponse response) {
logger.debug("Received Response: {}", response);
var sink = pendingResponses.remove(response.id());
if (sink == null) {
logger.warn("Unexpected response for unknown id {}", response.id());
logger.debug("Received response: {}", response);
if (response.id() != null) {
var sink = pendingResponses.remove(response.id());
if (sink == null) {
logger.warn("Unexpected response for unknown id {}", response.id());
}
else {
sink.success(response);
}
}
else {
sink.success(response);
logger.debug("Discarded response without id");
}
}
else if (message instanceof McpSchema.JSONRPCRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import io.modelcontextprotocol.util.Assert;
import reactor.util.annotation.Nullable;

/**
* Based on the <a href="http://www.jsonrpc.org/specification">JSON-RPC 2.0
Expand All @@ -36,6 +37,7 @@
* @author Luca Chang
* @author Surbhi Bansal
* @author Anurag Pant
* @author Yanming Zhou
*/
public final class McpSchema {

Expand Down Expand Up @@ -281,7 +283,7 @@ public record JSONRPCNotification( // @formatter:off
// @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
public record JSONRPCResponse( // @formatter:off
@JsonProperty("jsonrpc") String jsonrpc,
@JsonProperty("id") Object id,
@JsonProperty("id") @Nullable Object id,
@JsonProperty("result") Object result,
@JsonProperty("error") JSONRPCError error) implements JSONRPCMessage { // @formatter:on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,18 @@ public Mono<Void> handle(McpSchema.JSONRPCMessage message) {
// TODO handle errors for communication to without initialization happening
// first
if (message instanceof McpSchema.JSONRPCResponse response) {
logger.debug("Received Response: {}", response);
var sink = pendingResponses.remove(response.id());
if (sink == null) {
logger.warn("Unexpected response for unknown id {}", response.id());
logger.debug("Received response: {}", response);
if (response.id() != null) {
var sink = pendingResponses.remove(response.id());
if (sink == null) {
logger.warn("Unexpected response for unknown id {}", response.id());
}
else {
sink.success(response);
}
}
else {
sink.success(response);
logger.debug("Discarded response without id");
}
return Mono.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* capability without the insight into the transport-specific details of HTTP handling.
*
* @author Dariusz Jędrzejczyk
* @author Yanming Zhou
*/
public class McpStreamableServerSession implements McpLoggableSession {

Expand Down Expand Up @@ -214,19 +215,25 @@ public Mono<Void> accept(McpSchema.JSONRPCNotification notification) {
*/
public Mono<Void> accept(McpSchema.JSONRPCResponse response) {
return Mono.defer(() -> {
var stream = this.requestIdToStream.get(response.id());
if (stream == null) {
return Mono.error(new McpError("Unexpected response for unknown id " + response.id())); // TODO
// JSONize
}
// TODO: encapsulate this inside the stream itself
var sink = stream.pendingResponses.remove(response.id());
if (sink == null) {
return Mono.error(new McpError("Unexpected response for unknown id " + response.id())); // TODO
// JSONize
logger.debug("Received response: {}", response);
if (response.id() != null) {
var stream = this.requestIdToStream.get(response.id());
if (stream == null) {
return Mono.error(new McpError("Unexpected response for unknown id " + response.id())); // TODO
// JSONize
}
// TODO: encapsulate this inside the stream itself
var sink = stream.pendingResponses.remove(response.id());
if (sink == null) {
return Mono.error(new McpError("Unexpected response for unknown id " + response.id())); // TODO
// JSONize
}
else {
sink.success(response);
}
}
else {
sink.success(response);
logger.debug("Discarded response without id");
}
return Mono.empty();
});
Expand Down
Loading