File tree Expand file tree Collapse file tree
mcp/src/main/java/io/modelcontextprotocol/spec Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3535 *
3636 * @author Christian Tzolov
3737 * @author Dariusz Jędrzejczyk
38+ * @author Yanming Zhou
3839 */
3940public class McpClientSession implements McpSession {
4041
@@ -146,13 +147,18 @@ private void dismissPendingResponses() {
146147
147148 private void handle (McpSchema .JSONRPCMessage message ) {
148149 if (message instanceof McpSchema .JSONRPCResponse response ) {
149- logger .debug ("Received Response: {}" , response );
150- var sink = pendingResponses .remove (response .id ());
151- if (sink == null ) {
152- logger .warn ("Unexpected response for unknown id {}" , response .id ());
150+ logger .debug ("Received response: {}" , response );
151+ if (response .id () != null ) {
152+ var sink = pendingResponses .remove (response .id ());
153+ if (sink == null ) {
154+ logger .warn ("Unexpected response for unknown id {}" , response .id ());
155+ }
156+ else {
157+ sink .success (response );
158+ }
153159 }
154160 else {
155- sink . success ( response );
161+ logger . debug ( "Discarded response without id" );
156162 }
157163 }
158164 else if (message instanceof McpSchema .JSONRPCRequest request ) {
Original file line number Diff line number Diff line change 2525import com .fasterxml .jackson .databind .ObjectMapper ;
2626
2727import io .modelcontextprotocol .util .Assert ;
28+ import reactor .util .annotation .Nullable ;
2829
2930/**
3031 * Based on the <a href="http://www.jsonrpc.org/specification">JSON-RPC 2.0
3637 * @author Luca Chang
3738 * @author Surbhi Bansal
3839 * @author Anurag Pant
40+ * @author Yanming Zhou
3941 */
4042public final class McpSchema {
4143
@@ -281,7 +283,7 @@ public record JSONRPCNotification( // @formatter:off
281283 // @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
282284 public record JSONRPCResponse ( // @formatter:off
283285 @ JsonProperty ("jsonrpc" ) String jsonrpc ,
284- @ JsonProperty ("id" ) Object id ,
286+ @ JsonProperty ("id" ) @ Nullable Object id ,
285287 @ JsonProperty ("result" ) Object result ,
286288 @ JsonProperty ("error" ) JSONRPCError error ) implements JSONRPCMessage { // @formatter:on
287289
Original file line number Diff line number Diff line change @@ -202,13 +202,18 @@ public Mono<Void> handle(McpSchema.JSONRPCMessage message) {
202202 // TODO handle errors for communication to without initialization happening
203203 // first
204204 if (message instanceof McpSchema .JSONRPCResponse response ) {
205- logger .debug ("Received Response: {}" , response );
206- var sink = pendingResponses .remove (response .id ());
207- if (sink == null ) {
208- logger .warn ("Unexpected response for unknown id {}" , response .id ());
205+ logger .debug ("Received response: {}" , response );
206+ if (response .id () != null ) {
207+ var sink = pendingResponses .remove (response .id ());
208+ if (sink == null ) {
209+ logger .warn ("Unexpected response for unknown id {}" , response .id ());
210+ }
211+ else {
212+ sink .success (response );
213+ }
209214 }
210215 else {
211- sink . success ( response );
216+ logger . debug ( "Discarded response without id" );
212217 }
213218 return Mono .empty ();
214219 }
Original file line number Diff line number Diff line change 3333 * capability without the insight into the transport-specific details of HTTP handling.
3434 *
3535 * @author Dariusz Jędrzejczyk
36+ * @author Yanming Zhou
3637 */
3738public class McpStreamableServerSession implements McpLoggableSession {
3839
@@ -214,19 +215,25 @@ public Mono<Void> accept(McpSchema.JSONRPCNotification notification) {
214215 */
215216 public Mono <Void > accept (McpSchema .JSONRPCResponse response ) {
216217 return Mono .defer (() -> {
217- var stream = this .requestIdToStream .get (response .id ());
218- if (stream == null ) {
219- return Mono .error (new McpError ("Unexpected response for unknown id " + response .id ())); // TODO
220- // JSONize
221- }
222- // TODO: encapsulate this inside the stream itself
223- var sink = stream .pendingResponses .remove (response .id ());
224- if (sink == null ) {
225- return Mono .error (new McpError ("Unexpected response for unknown id " + response .id ())); // TODO
226- // JSONize
218+ logger .debug ("Received response: {}" , response );
219+ if (response .id () != null ) {
220+ var stream = this .requestIdToStream .get (response .id ());
221+ if (stream == null ) {
222+ return Mono .error (new McpError ("Unexpected response for unknown id " + response .id ())); // TODO
223+ // JSONize
224+ }
225+ // TODO: encapsulate this inside the stream itself
226+ var sink = stream .pendingResponses .remove (response .id ());
227+ if (sink == null ) {
228+ return Mono .error (new McpError ("Unexpected response for unknown id " + response .id ())); // TODO
229+ // JSONize
230+ }
231+ else {
232+ sink .success (response );
233+ }
227234 }
228235 else {
229- sink . success ( response );
236+ logger . debug ( "Discarded response without id" );
230237 }
231238 return Mono .empty ();
232239 });
You can’t perform that action at this time.
0 commit comments