@@ -427,6 +427,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
427427
428428 McpTransportContext transportContext = this .contextExtractor .extract (request );
429429
430+ McpSchema .JSONRPCMessage message = null ;
430431 try {
431432 BufferedReader reader = request .getReader ();
432433 StringBuilder body = new StringBuilder ();
@@ -435,14 +436,14 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
435436 body .append (line );
436437 }
437438
438- McpSchema . JSONRPCMessage message = McpSchema .deserializeJsonRpcMessage (jsonMapper , body .toString ());
439+ message = McpSchema .deserializeJsonRpcMessage (jsonMapper , body .toString ());
439440
440441 // Handle initialization request
441442 if (message instanceof McpSchema .JSONRPCRequest jsonrpcRequest
442443 && jsonrpcRequest .method ().equals (McpSchema .METHOD_INITIALIZE )) {
443444 if (!badRequestErrors .isEmpty ()) {
444445 String combinedMessage = String .join ("; " , badRequestErrors );
445- this .responseError (response , HttpServletResponse .SC_BAD_REQUEST ,
446+ this .responseError (response , HttpServletResponse .SC_BAD_REQUEST , jsonrpcRequest . id (),
446447 McpError .builder (McpSchema .ErrorCodes .METHOD_NOT_FOUND ).message (combinedMessage ).build ());
447448 return ;
448449 }
@@ -472,7 +473,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
472473 }
473474 catch (Exception e ) {
474475 logger .error ("Failed to initialize session: {}" , e .getMessage ());
475- this .responseError (response , HttpServletResponse .SC_INTERNAL_SERVER_ERROR ,
476+ this .responseError (response , HttpServletResponse .SC_INTERNAL_SERVER_ERROR , jsonrpcRequest . id (),
476477 McpError .builder (McpSchema .ErrorCodes .INTERNAL_ERROR )
477478 .message ("Failed to initialize session: " + e .getMessage ())
478479 .build ());
@@ -488,15 +489,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
488489
489490 if (!badRequestErrors .isEmpty ()) {
490491 String combinedMessage = String .join ("; " , badRequestErrors );
491- this .responseError (response , HttpServletResponse .SC_BAD_REQUEST ,
492+ this .responseError (response , HttpServletResponse .SC_BAD_REQUEST , requestId ( message ),
492493 McpError .builder (McpSchema .ErrorCodes .METHOD_NOT_FOUND ).message (combinedMessage ).build ());
493494 return ;
494495 }
495496
496497 McpStreamableServerSession session = this .sessions .get (sessionId );
497498
498499 if (session == null ) {
499- this .responseError (response , HttpServletResponse .SC_NOT_FOUND ,
500+ this .responseError (response , HttpServletResponse .SC_NOT_FOUND , requestId ( message ),
500501 McpError .builder (McpSchema .ErrorCodes .INTERNAL_ERROR )
501502 .message ("Session not found: " + sessionId )
502503 .build ());
@@ -539,21 +540,21 @@ else if (message instanceof McpSchema.JSONRPCRequest jsonrpcRequest) {
539540 }
540541 }
541542 else {
542- this .responseError (response , HttpServletResponse .SC_INTERNAL_SERVER_ERROR ,
543+ this .responseError (response , HttpServletResponse .SC_INTERNAL_SERVER_ERROR , requestId ( message ),
543544 McpError .builder (McpSchema .ErrorCodes .INVALID_REQUEST ).message ("Unknown message type" ).build ());
544545 }
545546 }
546547 catch (IllegalArgumentException | IOException e ) {
547548 logger .error ("Failed to deserialize message: {}" , e .getMessage ());
548- this .responseError (response , HttpServletResponse .SC_BAD_REQUEST ,
549+ this .responseError (response , HttpServletResponse .SC_BAD_REQUEST , requestId ( message ),
549550 McpError .builder (McpSchema .ErrorCodes .INVALID_REQUEST )
550551 .message ("Invalid message format: " + e .getMessage ())
551552 .build ());
552553 }
553554 catch (Exception e ) {
554555 logger .error ("Error handling message: {}" , e .getMessage ());
555556 try {
556- this .responseError (response , HttpServletResponse .SC_INTERNAL_SERVER_ERROR ,
557+ this .responseError (response , HttpServletResponse .SC_INTERNAL_SERVER_ERROR , requestId ( message ),
557558 McpError .builder (McpSchema .ErrorCodes .INTERNAL_ERROR )
558559 .message ("Error processing message: " + e .getMessage ())
559560 .build ());
@@ -638,14 +639,16 @@ protected void doDelete(HttpServletRequest request, HttpServletResponse response
638639 }
639640
640641 public void responseError (HttpServletResponse response , int httpCode , McpError mcpError ) throws IOException {
641- response .setContentType (APPLICATION_JSON );
642- response .setCharacterEncoding (UTF_8 );
643- response .setStatus (httpCode );
644- String jsonError = jsonMapper .writeValueAsString (mcpError );
645- PrintWriter writer = response .getWriter ();
646- writer .write (jsonError );
647- writer .flush ();
648- return ;
642+ this .responseError (response , httpCode , null , mcpError );
643+ }
644+
645+ private void responseError (HttpServletResponse response , int httpCode , Object requestId , McpError mcpError )
646+ throws IOException {
647+ HttpServletJsonRpcErrorWriter .writeError (this .jsonMapper , response , httpCode , requestId , mcpError );
648+ }
649+
650+ private static Object requestId (McpSchema .JSONRPCMessage message ) {
651+ return (message instanceof McpSchema .JSONRPCRequest request ) ? request .id () : null ;
649652 }
650653
651654 /**
0 commit comments