|
37 | 37 | import io.modelcontextprotocol.modern.JsonRpc.JSONRPCNotification; |
38 | 38 | import io.modelcontextprotocol.modern.JsonRpc.JSONRPCRequest; |
39 | 39 | import io.modelcontextprotocol.modern.JsonRpc.JSONRPCResponse; |
| 40 | +import io.modelcontextprotocol.modern.McpSchema; |
40 | 41 | import io.modelcontextprotocol.modern.McpSchema.CacheScope; |
41 | 42 | import io.modelcontextprotocol.modern.McpSchema.ErrorCodes; |
42 | 43 | import io.modelcontextprotocol.modern.server.McpTransportResponse; |
|
45 | 46 | import io.modelcontextprotocol.modern.server.feature.McpChangePublisher; |
46 | 47 | import io.modelcontextprotocol.modern.server.feature.ServerChange; |
47 | 48 | import io.modelcontextprotocol.modern.server.feature.ToolsFeature; |
| 49 | +import io.modelcontextprotocol.spec.ProtocolVersions; |
48 | 50 | import io.modelcontextprotocol.spec.json.gson.GsonMcpJsonMapper; |
49 | 51 | import org.junit.jupiter.api.AfterEach; |
50 | 52 | import org.junit.jupiter.api.Test; |
| 53 | +import org.junit.jupiter.params.ParameterizedTest; |
| 54 | +import org.junit.jupiter.params.provider.ValueSource; |
51 | 55 | import reactor.core.publisher.Flux; |
52 | 56 | import reactor.core.publisher.Mono; |
53 | 57 | import reactor.core.publisher.Sinks; |
@@ -150,6 +154,50 @@ public Mono<Void> handleNotification(McpTransportContext transportContext, |
150 | 154 | }; |
151 | 155 | } |
152 | 156 |
|
| 157 | + @Test |
| 158 | + void legacyInitializeErrorNamesSupportedVersions() throws Exception { |
| 159 | + List<String> supportedVersions = List.of(McpSchema.LATEST_PROTOCOL_VERSION, "2099-01-01"); |
| 160 | + McpServer server = McpServer.builder() |
| 161 | + .serverInfo(SERVER_INFO) |
| 162 | + .jsonMapper(this.jsonMapper) |
| 163 | + .supportedVersions(supportedVersions) |
| 164 | + .build(); |
| 165 | + start(server); |
| 166 | + |
| 167 | + send("initialize", 1, Map.of("protocolVersion", ProtocolVersions.MCP_2025_11_25, "capabilities", Map.of(), |
| 168 | + "clientInfo", Map.of("name", "legacy-client", "version", "1.0.0"))); |
| 169 | + |
| 170 | + Map<String, Object> response = this.jsonMapper.readValue(readLineWithTimeout(), Map.class); |
| 171 | + assertThat(((Number) response.get("id")).intValue()).isEqualTo(1); |
| 172 | + assertThat(errorCode(response)).isEqualTo(ErrorCodes.INVALID_PARAMS); |
| 173 | + Map<?, ?> error = (Map<?, ?>) response.get("error"); |
| 174 | + assertThat(error.get("message")).isEqualTo("Unsupported protocol version: " + ProtocolVersions.MCP_2025_11_25 |
| 175 | + + ". Supported versions: " + String.join(", ", supportedVersions)); |
| 176 | + Map<?, ?> data = (Map<?, ?>) error.get("data"); |
| 177 | + assertThat(data.get("supported")).isEqualTo(supportedVersions); |
| 178 | + assertThat(data.get("requested")).isEqualTo(ProtocolVersions.MCP_2025_11_25); |
| 179 | + } |
| 180 | + |
| 181 | + @ParameterizedTest |
| 182 | + @ValueSource(strings = { "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\"}", |
| 183 | + "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}", |
| 184 | + "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{\"protocolVersion\":42}}" }) |
| 185 | + void legacyInitializeWithoutRequestedVersionStillNamesSupportedVersions(String request) throws Exception { |
| 186 | + start(McpServer.builder().serverInfo(SERVER_INFO).jsonMapper(this.jsonMapper).build()); |
| 187 | + |
| 188 | + sendRaw(request); |
| 189 | + |
| 190 | + Map<String, Object> response = this.jsonMapper.readValue(readLineWithTimeout(), Map.class); |
| 191 | + assertThat(((Number) response.get("id")).intValue()).isEqualTo(1); |
| 192 | + assertThat(errorCode(response)).isEqualTo(ErrorCodes.INVALID_PARAMS); |
| 193 | + Map<?, ?> error = (Map<?, ?>) response.get("error"); |
| 194 | + assertThat((String) error.get("message")).contains(McpSchema.LATEST_PROTOCOL_VERSION); |
| 195 | + assertThat((String) error.get("message")).doesNotContain("null"); |
| 196 | + Map<?, ?> data = (Map<?, ?>) error.get("data"); |
| 197 | + assertThat(data.get("supported")).isEqualTo(List.of(McpSchema.LATEST_PROTOCOL_VERSION)); |
| 198 | + assertThat(data.get("requested")).isNull(); |
| 199 | + } |
| 200 | + |
153 | 201 | @Test |
154 | 202 | void fastRequestIsNotBlockedByASlowerConcurrentOne() throws Exception { |
155 | 203 | CountDownLatch slowStarted = new CountDownLatch(1); |
|
0 commit comments