Fix MCP session header timing per HTTP protocol specification
Set Mcp-Session-Id header before writing response body to ensure proper HTTP protocol compliance and MCP 2025-06-18 specification adherence. Headers must be sent before any response data per HTTP standards.
Showing
1 changed file
with
5 additions
and
5 deletions
| ... | @@ -724,6 +724,11 @@ logger.info("Handling Enhanced SSE connection from ${request.remoteAddr}") | ... | @@ -724,6 +724,11 @@ logger.info("Handling Enhanced SSE connection from ${request.remoteAddr}") |
| 724 | // Process MCP method using Moqui services with session ID if available | 724 | // Process MCP method using Moqui services with session ID if available |
| 725 | def result = processMcpMethod(rpcRequest.method, rpcRequest.params, ec, sessionId) | 725 | def result = processMcpMethod(rpcRequest.method, rpcRequest.params, ec, sessionId) |
| 726 | 726 | ||
| 727 | // Set Mcp-Session-Id header BEFORE any response data (per MCP 2025-06-18 spec) | ||
| 728 | if (result?.sessionId) { | ||
| 729 | response.setHeader("Mcp-Session-Id", result.sessionId) | ||
| 730 | } | ||
| 731 | |||
| 727 | // Build JSON-RPC response | 732 | // Build JSON-RPC response |
| 728 | def rpcResponse = [ | 733 | def rpcResponse = [ |
| 729 | jsonrpc: "2.0", | 734 | jsonrpc: "2.0", |
| ... | @@ -734,11 +739,6 @@ logger.info("Handling Enhanced SSE connection from ${request.remoteAddr}") | ... | @@ -734,11 +739,6 @@ logger.info("Handling Enhanced SSE connection from ${request.remoteAddr}") |
| 734 | response.setContentType("application/json") | 739 | response.setContentType("application/json") |
| 735 | response.setCharacterEncoding("UTF-8") | 740 | response.setCharacterEncoding("UTF-8") |
| 736 | 741 | ||
| 737 | // Set Mcp-Session-Id header if result contains sessionId (per MCP 2025-06-18 spec) | ||
| 738 | if (rpcResponse.result?.sessionId) { | ||
| 739 | response.setHeader("Mcp-Session-Id", rpcResponse.result.sessionId) | ||
| 740 | } | ||
| 741 | |||
| 742 | response.writer.write(groovy.json.JsonOutput.toJson(rpcResponse)) | 742 | response.writer.write(groovy.json.JsonOutput.toJson(rpcResponse)) |
| 743 | } | 743 | } |
| 744 | 744 | ... | ... |
-
Please register or sign in to post a comment