Skip to content

Commit 261920a

Browse files
committed
fix(server): preserve servlet transport error semantics
1 parent 0804ace commit 261920a

2 files changed

Lines changed: 8 additions & 13 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/server/transport/HttpServletStatelessServerTransport.java‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
204204
}
205205
catch (Exception e) {
206206
logger.error("Failed to handle request: {}", e.getMessage());
207-
if (e instanceof McpError mcpError) {
208-
this.responseError(response, HttpServletResponse.SC_OK, jsonrpcRequest.id(), mcpError);
209-
}
210-
else {
211-
this.responseError(response, HttpServletResponse.SC_OK, jsonrpcRequest.id(),
212-
McpError.builder(McpSchema.ErrorCodes.INTERNAL_ERROR)
213-
.message("Failed to handle request: " + e.getMessage())
214-
.build());
215-
}
207+
this.responseError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, jsonrpcRequest.id(),
208+
McpError.builder(McpSchema.ErrorCodes.INTERNAL_ERROR)
209+
.message("Failed to handle request: " + e.getMessage())
210+
.build());
216211
}
217212
}
218213
else if (message instanceof McpSchema.JSONRPCNotification jsonrpcNotification) {

‎mcp-core/src/test/java/io/modelcontextprotocol/server/transport/HttpServletJsonRpcErrorTests.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class HttpServletJsonRpcErrorTests {
4444
private static final McpJsonMapper JSON_MAPPER = new GsonMcpJsonMapper();
4545

4646
@Test
47-
void statelessTransportPreservesMcpErrorCodeAndRequestId() throws Exception {
47+
void statelessTransportMapsEscapedMcpErrorsToJsonRpcInternalError() throws Exception {
4848
HttpServletStatelessServerTransport transport = HttpServletStatelessServerTransport.builder()
4949
.jsonMapper(JSON_MAPPER)
5050
.build();
@@ -60,11 +60,11 @@ void statelessTransportPreservesMcpErrorCodeAndRequestId() throws Exception {
6060
""", HttpServletStatelessServerTransport.APPLICATION_JSON + ", "
6161
+ HttpServletStatelessServerTransport.TEXT_EVENT_STREAM), response);
6262

63-
verify(response).setStatus(HttpServletResponse.SC_OK);
63+
verify(response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
6464
McpSchema.JSONRPCResponse jsonResponse = readResponse(responseBody);
6565
assertThat(jsonResponse.id()).isEqualTo("missing-handler");
6666
assertThat(jsonResponse.error()).isNotNull();
67-
assertThat(jsonResponse.error().code()).isEqualTo(McpSchema.ErrorCodes.METHOD_NOT_FOUND);
67+
assertThat(jsonResponse.error().code()).isEqualTo(McpSchema.ErrorCodes.INTERNAL_ERROR);
6868
assertNoThrowableFields(responseBody);
6969
}
7070

@@ -82,7 +82,7 @@ void statelessTransportMapsUnexpectedRequestErrorsToJsonRpcInternalError() throw
8282
""", HttpServletStatelessServerTransport.APPLICATION_JSON + ", "
8383
+ HttpServletStatelessServerTransport.TEXT_EVENT_STREAM), response);
8484

85-
verify(response).setStatus(HttpServletResponse.SC_OK);
85+
verify(response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
8686
McpSchema.JSONRPCResponse jsonResponse = readResponse(responseBody);
8787
assertThat(jsonResponse.id()).isEqualTo("boom-request");
8888
assertThat(jsonResponse.error()).isNotNull();

0 commit comments

Comments
 (0)