FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: reject invalid JSON-RPC response versions by Shubchynskyi · Pull Request #1162 · modelcontextprotocol/java-sdk · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public record JSONRPCResponse( // @formatter:off

public JSONRPCResponse {
Assert.hasText(jsonrpc, "jsonrpc must not be empty");
Assert.isTrue(JSONRPC_VERSION.equals(jsonrpc), "jsonrpc must be 2.0");
Assert.notNull(id, "MCP responses MUST include an ID - null IDs are not allowed");
Assert.isTrue(id instanceof String || id instanceof Integer || id instanceof Long,
"MCP responses MUST have an ID that is either a string or integer");
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static io.modelcontextprotocol.util.McpJsonMapperUtils.JSON_MAPPER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import java.util.Map;
Expand Down Expand Up @@ -81,6 +82,28 @@ void dispatchesErrorResponse() throws IOException {
assertThat(resp.result()).isNull();
}

@Test
void rejectsSuccessResponseWithInvalidJsonRpcVersion() {
String json = """
{"jsonrpc":"1.0","id":"req-1","result":{"content":[{"type":"text","text":"hi"}]}}
""";

assertThatThrownBy(() -> McpSchema.deserializeJsonRpcMessage(mapper, json)).rootCause()
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("jsonrpc must be 2.0");
}

@Test
void rejectsErrorResponseWithInvalidJsonRpcVersion() {
String json = """
{"jsonrpc":"1.0","id":"req-1","error":{"code":-32601,"message":"Method not found"}}
""";

assertThatThrownBy(() -> McpSchema.deserializeJsonRpcMessage(mapper, json)).rootCause()
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("jsonrpc must be 2.0");
}

@Test
void paramsMapSurvivesConvertValue() throws IOException {
String json = """
Expand Down

Back | FazBrowse Home | New Git URL