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.ThreadSafe; 022import java.util.Optional; 023 024/** 025 * Immutable context supplied to an MCP rate limiter. 026 * <p> 027 * Implementations may combine the normalized endpoint path, the accepted 028 * identity's stable partition key, and the target to select local or 029 * distributed rate-limit state. Client-reported identity information is not 030 * a trusted partition key. 031 * 032 * @author <a href="https://www.revetkn.com">Mark Allen</a> 033 */ 034@ThreadSafe 035public interface McpRateLimitContext { 036 /** 037 * Returns the original Soklet request. 038 * 039 * @return request 040 */ 041 @NonNull 042 Request getRequest(); 043 044 /** 045 * Returns the resolved MCP endpoint. 046 * 047 * @return endpoint 048 */ 049 @NonNull 050 McpEndpoint getEndpoint(); 051 052 /** @return exact validated protocol revision for this acquisition */ 053 @NonNull 054 McpProtocolVersion getProtocolVersion(); 055 056 /** 057 * Returns the identity accepted by request admission. 058 * 059 * @return admitted identity 060 */ 061 @NonNull 062 McpAdmissionIdentity getAdmissionIdentity(); 063 064 /** 065 * Returns the rate-limit stage requesting this acquisition. 066 * 067 * @return request or tool target 068 */ 069 @NonNull 070 McpRateLimitTarget getTarget(); 071 072 /** 073 * Returns the JSON-RPC method. 074 * 075 * @return JSON-RPC method 076 */ 077 @NonNull 078 String getJsonRpcMethod(); 079 080 /** 081 * Returns the high-level type of the MCP operation being rate-limited. 082 * 083 * <p>This classification is suitable for application branching. The exact 084 * validated wire value remains available from {@link #getJsonRpcMethod()}. 085 * 086 * @return operation type 087 */ 088 @NonNull 089 default McpOperationType getOperationType() { 090 return McpOperationType.fromJsonRpcMethod(getJsonRpcMethod()); 091 } 092 093 /** 094 * Returns the resolved operation name, such as a tool name or task ID, when 095 * present. 096 * <p> 097 * On session-enabled 2025 views, a request with malformed operation parameters 098 * can reach the request limiter with an empty operation name. Its parameter 099 * error is returned after admission, applicable request limiting and session 100 * binding. Branch on operation type without requiring this optional value. 101 * 102 * @return operation name, or the empty optional 103 */ 104 @NonNull 105 Optional<@NonNull String> getOperationName(); 106}