forked from modelcontextprotocol/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonRpcDispatchTests.java
More file actions
123 lines (97 loc) · 4.12 KB
/
Copy pathJsonRpcDispatchTests.java
File metadata and controls
123 lines (97 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2026 - 2026 the original author or authors.
*/
package io.modelcontextprotocol.spec;
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;
import io.modelcontextprotocol.json.McpJsonMapper;
import io.modelcontextprotocol.json.TypeRef;
import org.junit.jupiter.api.Test;
/**
* Verifies that {@link McpSchema#deserializeJsonRpcMessage} dispatches to the correct
* concrete subtype for all four JSON-RPC message shapes, and that {@code params} /
* {@code result} survive the round-trip.
*/
class JsonRpcDispatchTests {
private final McpJsonMapper mapper = JSON_MAPPER;
@Test
void dispatchesRequest() throws IOException {
String json = """
{"jsonrpc":"2.0","id":"req-1","method":"tools/call","params":{"name":"echo","arguments":{"x":1}}}
""";
McpSchema.JSONRPCMessage msg = McpSchema.deserializeJsonRpcMessage(mapper, json);
assertThat(msg).isInstanceOf(McpSchema.JSONRPCRequest.class);
McpSchema.JSONRPCRequest req = (McpSchema.JSONRPCRequest) msg;
assertThat(req.jsonrpc()).isEqualTo("2.0");
assertThat(req.method()).isEqualTo("tools/call");
assertThat(req.id()).isEqualTo("req-1");
assertThat(req.params()).isNotNull();
}
@Test
void dispatchesNotification() throws IOException {
String json = """
{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}
""";
McpSchema.JSONRPCMessage msg = McpSchema.deserializeJsonRpcMessage(mapper, json);
assertThat(msg).isInstanceOf(McpSchema.JSONRPCNotification.class);
McpSchema.JSONRPCNotification notif = (McpSchema.JSONRPCNotification) msg;
assertThat(notif.method()).isEqualTo("notifications/initialized");
}
@Test
void dispatchesSuccessResponse() throws IOException {
String json = """
{"jsonrpc":"2.0","id":"req-1","result":{"content":[{"type":"text","text":"hi"}]}}
""";
McpSchema.JSONRPCMessage msg = McpSchema.deserializeJsonRpcMessage(mapper, json);
assertThat(msg).isInstanceOf(McpSchema.JSONRPCResponse.class);
McpSchema.JSONRPCResponse resp = (McpSchema.JSONRPCResponse) msg;
assertThat(resp.error()).isNull();
assertThat(resp.result()).isNotNull();
}
@Test
void dispatchesErrorResponse() throws IOException {
String json = """
{"jsonrpc":"2.0","id":"req-1","error":{"code":-32601,"message":"Method not found"}}
""";
McpSchema.JSONRPCMessage msg = McpSchema.deserializeJsonRpcMessage(mapper, json);
assertThat(msg).isInstanceOf(McpSchema.JSONRPCResponse.class);
McpSchema.JSONRPCResponse resp = (McpSchema.JSONRPCResponse) msg;
assertThat(resp.error()).isNotNull();
assertThat(resp.error().code()).isEqualTo(-32601);
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 = """
{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"echo","arguments":{"x":42}}}
""";
McpSchema.JSONRPCRequest req = (McpSchema.JSONRPCRequest) McpSchema.deserializeJsonRpcMessage(mapper, json);
McpSchema.CallToolRequest call = mapper.convertValue(req.params(), new TypeRef<McpSchema.CallToolRequest>() {
});
assertThat(call.name()).isEqualTo("echo");
@SuppressWarnings("unchecked")
Map<String, Object> args = (Map<String, Object>) call.arguments();
assertThat(((Number) args.get("x")).intValue()).isEqualTo(42);
}
}