|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.java.mcp.server; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import software.amazon.smithy.java.mcp.model.JsonRpcRequest; |
| 10 | +import software.amazon.smithy.java.mcp.model.JsonRpcResponse; |
| 11 | +import software.amazon.smithy.utils.SmithyUnstableApi; |
| 12 | + |
| 13 | +/** |
| 14 | + * Interceptor for MCP server request processing. Interceptors inject code into the |
| 15 | + * {@link McpService} request execution pipeline via hooks at specific stages. |
| 16 | + * |
| 17 | + * <p>Hooks are either "read" hooks (observe in-flight data) or "modify" hooks (transform |
| 18 | + * in-flight data). All hooks have default no-op implementations; override only the hooks |
| 19 | + * you need. |
| 20 | + * |
| 21 | + * <h2>Execution lifecycle</h2> |
| 22 | + * |
| 23 | + * <p>For every request: |
| 24 | + * <ol> |
| 25 | + * <li>{@link #readBeforeExecution} — observe the incoming request</li> |
| 26 | + * <li>{@link #modifyBeforeExecution} — optionally transform the request</li> |
| 27 | + * <li>For {@code tools/call} requests only: |
| 28 | + * <ol> |
| 29 | + * <li>{@link #readBeforeToolCall} — observe before tool dispatch</li> |
| 30 | + * <li>{@link #modifyBeforeToolCall} — optionally transform the request</li> |
| 31 | + * <li>Tool dispatch (local or proxy)</li> |
| 32 | + * <li>{@link #readAfterToolCall} — observe the tool result</li> |
| 33 | + * <li>{@link #modifyAfterToolCall} — optionally transform the response</li> |
| 34 | + * </ol> |
| 35 | + * </li> |
| 36 | + * <li>{@link #readAfterExecution} — observe the final result (ALWAYS fires)</li> |
| 37 | + * <li>{@link #modifyAfterExecution} — optionally transform the final response</li> |
| 38 | + * </ol> |
| 39 | + * |
| 40 | + * <h2>Error handling</h2> |
| 41 | + * |
| 42 | + * <p>Any hook may throw a {@link RuntimeException}. When a hook throws, remaining hooks |
| 43 | + * in that stage are skipped, and execution jumps to the after-execution hooks with the |
| 44 | + * error. The {@code readAfterExecution} and {@code modifyAfterExecution} hooks ALWAYS fire, |
| 45 | + * ensuring cleanup and telemetry logic runs regardless of errors. |
| 46 | + * |
| 47 | + * <h2>Async tool calls</h2> |
| 48 | + * |
| 49 | + * <p>For proxy tool calls, the after-tool-call and after-execution hooks fire on the |
| 50 | + * thread that receives the proxy response, not the original request thread. Hook |
| 51 | + * implementations must be thread-safe. |
| 52 | + * |
| 53 | + * <h2>Example: telemetry</h2> |
| 54 | + * <pre>{@code |
| 55 | + * public class TelemetryInterceptor implements McpServerInterceptor { |
| 56 | + * private static final Context.Key<Long> START = Context.key("start"); |
| 57 | + * |
| 58 | + * @Override |
| 59 | + * public void readBeforeExecution(McpExecutionHook hook) { |
| 60 | + * hook.context().put(START, System.nanoTime()); |
| 61 | + * } |
| 62 | + * |
| 63 | + * @Override |
| 64 | + * public void readAfterExecution(McpExecutionHook hook, |
| 65 | + * JsonRpcResponse response, RuntimeException error) { |
| 66 | + * long duration = System.nanoTime() - hook.context().get(START); |
| 67 | + * emitMetrics(hook.request().getMethod(), duration, error == null); |
| 68 | + * } |
| 69 | + * } |
| 70 | + * }</pre> |
| 71 | + * |
| 72 | + * <h2>Example: access control</h2> |
| 73 | + * <pre>{@code |
| 74 | + * public class AccessControlInterceptor implements McpServerInterceptor { |
| 75 | + * @Override |
| 76 | + * public void readBeforeToolCall(McpToolCallHook hook) { |
| 77 | + * if (isBlocked(hook.toolName(), hook.serverId())) { |
| 78 | + * throw new RuntimeException("Access denied: " + hook.toolName()); |
| 79 | + * } |
| 80 | + * } |
| 81 | + * } |
| 82 | + * }</pre> |
| 83 | + */ |
| 84 | +@SmithyUnstableApi |
| 85 | +public interface McpServerInterceptor { |
| 86 | + |
| 87 | + /** |
| 88 | + * An interceptor that does nothing. |
| 89 | + */ |
| 90 | + McpServerInterceptor NOOP = new McpServerInterceptor() {}; |
| 91 | + |
| 92 | + /** |
| 93 | + * Combines multiple interceptors into a single interceptor that invokes each one |
| 94 | + * in order. Hooks are called sequentially on each interceptor in list order. |
| 95 | + * |
| 96 | + * @param interceptors The interceptors to compose. |
| 97 | + * @return A single interceptor that delegates to all provided interceptors. |
| 98 | + */ |
| 99 | + static McpServerInterceptor chain(List<McpServerInterceptor> interceptors) { |
| 100 | + return switch (interceptors.size()) { |
| 101 | + case 0 -> NOOP; |
| 102 | + case 1 -> interceptors.get(0); |
| 103 | + default -> new McpServerInterceptorChain(List.copyOf(interceptors)); |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Combines multiple interceptors into a single interceptor that invokes each one |
| 109 | + * in order. Convenience overload of {@link #chain(List)}. |
| 110 | + * |
| 111 | + * @param interceptors The interceptors to compose. |
| 112 | + * @return A single interceptor that delegates to all provided interceptors. |
| 113 | + */ |
| 114 | + static McpServerInterceptor chain(McpServerInterceptor... interceptors) { |
| 115 | + return chain(List.of(interceptors)); |
| 116 | + } |
| 117 | + |
| 118 | + // --- Execution-level hooks (fire for all requests) --- |
| 119 | + |
| 120 | + /** |
| 121 | + * Called when a request is received, before any dispatch logic. |
| 122 | + * |
| 123 | + * @param hook Execution hook data containing the request, protocol version, and context. |
| 124 | + */ |
| 125 | + default void readBeforeExecution(McpExecutionHook hook) {} |
| 126 | + |
| 127 | + /** |
| 128 | + * Called before dispatch. Can return a modified request. |
| 129 | + * |
| 130 | + * @param hook Execution hook data. |
| 131 | + * @return The request to dispatch, or {@code hook.request()} to pass through unmodified. |
| 132 | + */ |
| 133 | + default JsonRpcRequest modifyBeforeExecution(McpExecutionHook hook) { |
| 134 | + return hook.request(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Called when execution completes. ALWAYS fires, even if an earlier hook threw. |
| 139 | + * |
| 140 | + * @param hook Execution hook data. |
| 141 | + * @param response The response, or {@code null} for notifications and async proxy calls |
| 142 | + * still in flight. |
| 143 | + * @param error The error if one occurred, or {@code null} on success. |
| 144 | + */ |
| 145 | + default void readAfterExecution(McpExecutionHook hook, JsonRpcResponse response, RuntimeException error) {} |
| 146 | + |
| 147 | + /** |
| 148 | + * Called when execution completes. Can modify the response or handle errors. |
| 149 | + * ALWAYS fires, even if an earlier hook threw. |
| 150 | + * |
| 151 | + * @param hook Execution hook data. |
| 152 | + * @param response The response, or {@code null} for notifications. |
| 153 | + * @param error The error if one occurred, or {@code null} on success. |
| 154 | + * @return The final response. |
| 155 | + * @throws RuntimeException to propagate or replace the error. |
| 156 | + */ |
| 157 | + default JsonRpcResponse modifyAfterExecution( |
| 158 | + McpExecutionHook hook, |
| 159 | + JsonRpcResponse response, |
| 160 | + RuntimeException error |
| 161 | + ) { |
| 162 | + if (error != null) { |
| 163 | + throw error; |
| 164 | + } |
| 165 | + return response; |
| 166 | + } |
| 167 | + |
| 168 | + // --- Tool-level hooks (fire only for tools/call) --- |
| 169 | + |
| 170 | + /** |
| 171 | + * Called before a tool is invoked. |
| 172 | + * |
| 173 | + * @param hook Tool call hook data containing tool name, server ID, and proxy status. |
| 174 | + */ |
| 175 | + default void readBeforeToolCall(McpToolCallHook hook) {} |
| 176 | + |
| 177 | + /** |
| 178 | + * Called before a tool is invoked. Can return a modified request. |
| 179 | + * |
| 180 | + * @param hook Tool call hook data. |
| 181 | + * @return The request to use for tool invocation, or {@code hook.request()} to pass |
| 182 | + * through unmodified. |
| 183 | + */ |
| 184 | + default JsonRpcRequest modifyBeforeToolCall(McpToolCallHook hook) { |
| 185 | + return hook.request(); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Called after a tool completes. For proxy tools, this fires on the callback thread. |
| 190 | + * |
| 191 | + * @param hook Tool call hook data. |
| 192 | + * @param response The tool call response. |
| 193 | + * @param error The error if one occurred, or {@code null} on success. |
| 194 | + */ |
| 195 | + default void readAfterToolCall(McpToolCallHook hook, JsonRpcResponse response, RuntimeException error) {} |
| 196 | + |
| 197 | + /** |
| 198 | + * Called after a tool completes. Can modify the response or handle errors. |
| 199 | + * For proxy tools, this fires on the callback thread. |
| 200 | + * |
| 201 | + * @param hook Tool call hook data. |
| 202 | + * @param response The tool call response. |
| 203 | + * @param error The error if one occurred, or {@code null} on success. |
| 204 | + * @return The response to return. |
| 205 | + * @throws RuntimeException to propagate or replace the error. |
| 206 | + */ |
| 207 | + default JsonRpcResponse modifyAfterToolCall( |
| 208 | + McpToolCallHook hook, |
| 209 | + JsonRpcResponse response, |
| 210 | + RuntimeException error |
| 211 | + ) { |
| 212 | + if (error != null) { |
| 213 | + throw error; |
| 214 | + } |
| 215 | + return response; |
| 216 | + } |
| 217 | +} |
0 commit comments