|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.internal.mcp; |
| 7 | + |
| 8 | +import org.jspecify.annotations.NonNull; |
| 9 | +import org.jspecify.annotations.Nullable; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import io.jooby.Jooby; |
| 13 | +import io.jooby.SneakyThrows; |
| 14 | +import io.jooby.StatusCode; |
| 15 | +import io.jooby.mcp.McpChain; |
| 16 | +import io.jooby.mcp.McpInvoker; |
| 17 | +import io.jooby.mcp.McpOperation; |
| 18 | +import io.modelcontextprotocol.common.McpTransportContext; |
| 19 | +import io.modelcontextprotocol.server.McpSyncServerExchange; |
| 20 | +import io.modelcontextprotocol.spec.McpError; |
| 21 | +import io.modelcontextprotocol.spec.McpSchema; |
| 22 | + |
| 23 | +public class McpExecutor implements McpInvoker { |
| 24 | + private final Jooby application; |
| 25 | + |
| 26 | + public McpExecutor(Jooby application) { |
| 27 | + this.application = application; |
| 28 | + } |
| 29 | + |
| 30 | + @SuppressWarnings("unchecked") |
| 31 | + public @NonNull Object invoke( |
| 32 | + @Nullable McpSyncServerExchange exchange, |
| 33 | + @NonNull McpTransportContext transportContext, |
| 34 | + @NonNull McpOperation operation, |
| 35 | + @NonNull McpChain next) { |
| 36 | + try { |
| 37 | + return next.proceed(exchange, transportContext, operation); |
| 38 | + } catch (Throwable cause) { |
| 39 | + operation.exception(cause); |
| 40 | + log(operation, cause); |
| 41 | + if (SneakyThrows.isFatal(cause)) { |
| 42 | + throw SneakyThrows.propagate(cause); |
| 43 | + } |
| 44 | + var code = toMcpErrorCode(cause); |
| 45 | + if (operation.isTool()) { |
| 46 | + // Tool error |
| 47 | + var errorMessage = |
| 48 | + cause.getMessage() != null ? cause.getMessage() : "Unknown error occurred"; |
| 49 | + var textContent = new McpSchema.TextContent(errorMessage); |
| 50 | + return McpSchema.CallToolResult.builder().addContent(textContent).isError(true).build(); |
| 51 | + } |
| 52 | + if (cause instanceof McpError mcpError) { |
| 53 | + throw mcpError; |
| 54 | + } else { |
| 55 | + throw new McpError( |
| 56 | + new McpSchema.JSONRPCResponse.JSONRPCError(code, cause.getMessage(), null)); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void log(McpOperation operation, Throwable cause) { |
| 62 | + var log = LoggerFactory.getLogger(operation.getClassName()); |
| 63 | + var code = toMcpErrorCode(cause); |
| 64 | + if (isServerError(code)) { |
| 65 | + log.error("execution of {} resulted in exception", operation.getId(), cause); |
| 66 | + } else { |
| 67 | + log.debug("execution of {} resulted in exception", operation.getId(), cause); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + static boolean isServerError(int code) { |
| 72 | + // -32603 is Internal Error. Custom server errors usually fall outside the -32600 to -32699 |
| 73 | + // reserved range. |
| 74 | + return code == McpSchema.ErrorCodes.INTERNAL_ERROR || code < -32700; |
| 75 | + } |
| 76 | + |
| 77 | + private int toMcpErrorCode(Throwable cause) { |
| 78 | + if (cause instanceof McpError mcpError && mcpError.getJsonRpcError() != null) { |
| 79 | + return mcpError.getJsonRpcError().code(); |
| 80 | + } |
| 81 | + var statusCode = application.getRouter().errorCode(cause); |
| 82 | + return switch (statusCode.value()) { |
| 83 | + case StatusCode.BAD_REQUEST_CODE, StatusCode.CONFLICT_CODE -> |
| 84 | + McpSchema.ErrorCodes.INVALID_PARAMS; |
| 85 | + case StatusCode.NOT_FOUND_CODE -> McpSchema.ErrorCodes.RESOURCE_NOT_FOUND; |
| 86 | + |
| 87 | + default -> McpSchema.ErrorCodes.INTERNAL_ERROR; |
| 88 | + }; |
| 89 | + } |
| 90 | +} |
0 commit comments