@@ -102,6 +102,8 @@ public McpHttpFeature(McpHttpFeatureConfig config) {
102102 builder .method (McpJsonRPC .METHOD_NOTIFICATION_INITIALIZED , this ::notificationInitRpc );
103103 builder .method (McpJsonRPC .METHOD_NOTIFICATION_CANCELED , this ::notificationCancelRpc );
104104
105+ builder .error (this ::handleErrorRequest );
106+
105107 jsonRpcHandlers = builder .build ();
106108 }
107109
@@ -169,6 +171,30 @@ private McpSession findSession(JsonRpcRequest req) {
169171 }
170172 }
171173
174+ /**
175+ * If we receive what looks like a response on the error handler,
176+ * pass it to the session.
177+ *
178+ * @param req the HTTP request
179+ * @param object the invalid JSON-RPC request
180+ * @return whether error was handled or not
181+ */
182+ private boolean handleErrorRequest (ServerRequest req , JsonObject object ) {
183+ try {
184+ if (object .containsKey ("result" ) || object .containsKey ("error" )) {
185+ String sessionId = req .query ().get ("sessionId" );
186+ McpSession session = sessions .get (sessionId );
187+ if (session != null ) {
188+ session .handleResponse (object );
189+ return true ;
190+ }
191+ }
192+ } catch (Exception e ) {
193+ // falls through
194+ }
195+ return false ;
196+ }
197+
172198 private void notificationInitRpc (JsonRpcRequest req , JsonRpcResponse res ) {
173199 McpSession session = findSession (req );
174200 if (session == null ) {
@@ -284,7 +310,7 @@ private void resourcesListRpc(JsonRpcRequest req, JsonRpcResponse res) {
284310 this .config .resources ().stream ()
285311 .map (Jsonable ::json )
286312 .forEach (builder ::add );
287- JsonObject result = Json .createObjectBuilder ()
313+ JsonObject result = Json .createObjectBuilder ()
288314 .add ("resources" , builder .build ())
289315 .build ();
290316
@@ -351,7 +377,7 @@ private void promptsListRpc(JsonRpcRequest req, JsonRpcResponse res) {
351377 this .config .prompts ().stream ()
352378 .map (Jsonable ::json )
353379 .forEach (builder ::add );
354- JsonObject result = Json .createObjectBuilder ()
380+ JsonObject result = Json .createObjectBuilder ()
355381 .add ("prompts" , builder .build ())
356382 .build ();
357383
@@ -370,7 +396,7 @@ private void promptsGetRpc(JsonRpcRequest req, JsonRpcResponse res) {
370396 .filter (p -> Objects .equals (p .name (), params .getString ("name" )))
371397 .findFirst ();
372398 McpParameters parameters = new McpParameters (params .getJsonObject ("arguments" ), "arguments" );
373- JsonObject result = prompt .map (value -> Json .createObjectBuilder ()
399+ JsonObject result = prompt .map (value -> Json .createObjectBuilder ()
374400 .add ("description" , value .description ())
375401 .add ("messages" , Json .createArrayBuilder ()
376402 .add (value .prompt (parameters ).json ()))
@@ -395,7 +421,7 @@ private void completionRpc(JsonRpcRequest req, JsonRpcResponse res) {
395421 JsonObject reference = params .getJsonObject ("ref" );
396422 Optional <String > search = parseCompletionName (reference );
397423 if (search .isEmpty ()) {
398- JsonObject result = Json .createObjectBuilder ()
424+ JsonObject result = Json .createObjectBuilder ()
399425 .add ("error" , Json .createObjectBuilder ()
400426 .add ("code" , McpJsonRPC .INVALID_REQUEST )
401427 .add ("message" , "Completion reference not found" ))
0 commit comments