|
6 | 6 |
|
7 | 7 | import java.time.Duration; |
8 | 8 | import java.util.Map; |
| 9 | +import java.util.Objects; |
9 | 10 | import java.util.concurrent.ConcurrentHashMap; |
10 | 11 | import java.util.concurrent.atomic.AtomicInteger; |
11 | 12 | import java.util.concurrent.atomic.AtomicLong; |
@@ -224,10 +225,7 @@ public Mono<Void> handle(McpSchema.JSONRPCMessage message) { |
224 | 225 | else if (message instanceof McpSchema.JSONRPCRequest request) { |
225 | 226 | logger.debug("Received request: {}", request); |
226 | 227 | return handleIncomingRequest(request, transportContext).onErrorResume(error -> { |
227 | | - McpSchema.JSONRPCResponse.JSONRPCError jsonRpcError = (error instanceof McpError mcpError |
228 | | - && mcpError.getJsonRpcError() != null) ? mcpError.getJsonRpcError() |
229 | | - : new McpSchema.JSONRPCResponse.JSONRPCError(McpSchema.ErrorCodes.INTERNAL_ERROR, |
230 | | - error.getMessage(), McpError.aggregateExceptionMessages(error)); |
| 228 | + McpSchema.JSONRPCResponse.JSONRPCError jsonRpcError = toJsonRpcError(request, error); |
231 | 229 | var errorResponse = new McpSchema.JSONRPCResponse(McpSchema.JSONRPC_VERSION, request.id(), null, |
232 | 230 | jsonRpcError); |
233 | 231 | // TODO: Should the error go to SSE or back as POST return? |
@@ -285,18 +283,28 @@ private Mono<McpSchema.JSONRPCResponse> handleIncomingRequest(McpSchema.JSONRPCR |
285 | 283 | } |
286 | 284 | return resultMono |
287 | 285 | .map(result -> new McpSchema.JSONRPCResponse(McpSchema.JSONRPC_VERSION, request.id(), result, null)) |
288 | | - .onErrorResume(error -> { |
289 | | - McpSchema.JSONRPCResponse.JSONRPCError jsonRpcError = (error instanceof McpError mcpError |
290 | | - && mcpError.getJsonRpcError() != null) ? mcpError.getJsonRpcError() |
291 | | - // TODO: add error message through the data field |
292 | | - : new McpSchema.JSONRPCResponse.JSONRPCError(McpSchema.ErrorCodes.INTERNAL_ERROR, |
293 | | - error.getMessage(), McpError.aggregateExceptionMessages(error)); |
294 | | - return Mono.just( |
295 | | - new McpSchema.JSONRPCResponse(McpSchema.JSONRPC_VERSION, request.id(), null, jsonRpcError)); |
296 | | - }); |
| 286 | + .onErrorResume(error -> Mono.just(new McpSchema.JSONRPCResponse(McpSchema.JSONRPC_VERSION, request.id(), |
| 287 | + null, toJsonRpcError(request, error)))); |
297 | 288 | }); |
298 | 289 | } |
299 | 290 |
|
| 291 | + /** |
| 292 | + * Converts a request handler failure into the JSON-RPC error sent to the client. |
| 293 | + * Errors other than {@link McpError} are logged, and only their message is sent: |
| 294 | + * their cause chain may contain internal details that must not reach the client. |
| 295 | + * @param request the request that failed |
| 296 | + * @param error the failure |
| 297 | + * @return the JSON-RPC error to send to the client |
| 298 | + */ |
| 299 | + private McpSchema.JSONRPCResponse.JSONRPCError toJsonRpcError(McpSchema.JSONRPCRequest request, Throwable error) { |
| 300 | + if (error instanceof McpError mcpError && mcpError.getJsonRpcError() != null) { |
| 301 | + return mcpError.getJsonRpcError(); |
| 302 | + } |
| 303 | + logger.warn("Error handling request {} [{}]", request.id(), request.method(), error); |
| 304 | + return new McpSchema.JSONRPCResponse.JSONRPCError(McpSchema.ErrorCodes.INTERNAL_ERROR, |
| 305 | + Objects.requireNonNullElse(error.getMessage(), "Internal error"), null); |
| 306 | + } |
| 307 | + |
300 | 308 | /** |
301 | 309 | * Handles an incoming JSON-RPC notification by routing it to the appropriate handler. |
302 | 310 | * @param notification The incoming JSON-RPC notification |
|
0 commit comments