001/* 002 * Copyright 2022-2026 Revetware LLC. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.soklet; 018 019import org.jspecify.annotations.NonNull; 020 021import javax.annotation.concurrent.NotThreadSafe; 022 023import static java.util.Objects.requireNonNull; 024 025/** 026 * Signals an intentional client-visible JSON-RPC error from an MCP handler. 027 * 028 * <p>Unexpected application failures should be allowed to propagate normally 029 * so Soklet can map them to its safe internal-error response. A handler's 030 * exception passes through {@link McpHandlerContinuation#proceed()} unchanged. 031 * An {@link McpHandlerInterceptor} may inspect and rethrow that exact object 032 * during the same invocation to preserve the intentional error, or return a 033 * method-compatible recovery result. An interceptor-authored exception, 034 * including a new instance carrying the same error, a wrapper, or an exception 035 * retained from an earlier invocation, fails closed as an unexpected failure. 036 * 037 * @author <a href="https://www.revetkn.com">Mark Allen</a> 038 */ 039@NotThreadSafe 040public final class McpJsonRpcException extends RuntimeException { 041 private static final long serialVersionUID = 1L; 042 @NonNull 043 private final McpJsonRpcError error; 044 045 /** 046 * Creates an exception carrying an intentional JSON-RPC error. 047 * 048 * @param jsonRpcError client-visible JSON-RPC error 049 */ 050 public McpJsonRpcException(@NonNull McpJsonRpcError jsonRpcError) { 051 super("MCP handler produced a JSON-RPC error."); 052 this.error = requireNonNull(jsonRpcError); 053 } 054 055 /** @return client-visible JSON-RPC error */ 056 @NonNull 057 public McpJsonRpcError getError() { 058 return this.error; 059 } 060}