feat: enforce MCP request ID validation requirements

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
Christian Tzolov 2025-07-16 11:06:20 +02:00
commit 1e93776354
2 changed files with 111 additions and 0 deletions

View file

@ -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");
}
}
/**

View file

@ -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);
}
}
}