feat: enforce MCP request ID validation requirements
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
parent
53f7b77edf
commit
1e93776354
2 changed files with 111 additions and 0 deletions
|
|
@ -236,6 +236,17 @@ public final class McpSchema {
|
|||
@JsonProperty("method") String method,
|
||||
@JsonProperty("id") Object id,
|
||||
@JsonProperty("params") Object params) implements JSONRPCMessage { // @formatter:on
|
||||
|
||||
/**
|
||||
* Constructor that validates MCP-specific ID requirements. Unlike base JSON-RPC,
|
||||
* MCP requires that: (1) Requests MUST include a string or integer ID; (2) The ID
|
||||
* MUST NOT be null
|
||||
*/
|
||||
public JSONRPCRequest {
|
||||
Assert.notNull(id, "MCP requests MUST include an ID - null IDs are not allowed");
|
||||
Assert.isTrue(id instanceof String || id instanceof Integer || id instanceof Long,
|
||||
"MCP requests MUST have an ID that is either a string or integer");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -76,4 +76,17 @@ public final class Assert {
|
|||
return (str != null && !str.isBlank());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert a boolean expression, throwing an {@code IllegalArgumentException} if the
|
||||
* expression evaluates to {@code false}.
|
||||
* @param expression a boolean expression
|
||||
* @param message the exception message to use if the assertion fails
|
||||
* @throws IllegalArgumentException if {@code expression} is {@code false}
|
||||
*/
|
||||
public static void isTrue(boolean expression, String message) {
|
||||
if (!expression) {
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue