|
21 | 21 | import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport; |
22 | 22 | import io.modelcontextprotocol.server.transport.HttpServletSseServerTransportProvider; |
23 | 23 | import io.modelcontextprotocol.server.transport.TomcatTestUtil; |
| 24 | +import io.modelcontextprotocol.spec.McpError; |
24 | 25 | import io.modelcontextprotocol.spec.McpSchema; |
25 | 26 | import io.modelcontextprotocol.spec.McpSchema.CompleteRequest; |
26 | 27 | import io.modelcontextprotocol.spec.McpSchema.CompleteResult; |
27 | 28 | import io.modelcontextprotocol.spec.McpSchema.ErrorCodes; |
28 | 29 | import io.modelcontextprotocol.spec.McpSchema.InitializeResult; |
29 | 30 | import io.modelcontextprotocol.spec.McpSchema.Prompt; |
30 | 31 | import io.modelcontextprotocol.spec.McpSchema.PromptArgument; |
| 32 | +import io.modelcontextprotocol.spec.McpSchema.PromptReference; |
31 | 33 | import io.modelcontextprotocol.spec.McpSchema.ReadResourceResult; |
32 | 34 | import io.modelcontextprotocol.spec.McpSchema.Resource; |
33 | 35 | import io.modelcontextprotocol.spec.McpSchema.ResourceReference; |
34 | 36 | import io.modelcontextprotocol.spec.McpSchema.ResourceTemplate; |
35 | | -import io.modelcontextprotocol.spec.McpSchema.PromptReference; |
36 | 37 | import io.modelcontextprotocol.spec.McpSchema.ServerCapabilities; |
37 | | -import io.modelcontextprotocol.spec.McpError; |
38 | 38 |
|
39 | 39 | import static org.assertj.core.api.Assertions.assertThat; |
40 | 40 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 41 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 42 | +import static org.assertj.core.api.InstanceOfAssertFactories.type; |
41 | 43 |
|
42 | 44 | /** |
43 | 45 | * Tests for completion functionality with context support. |
@@ -273,6 +275,59 @@ void testResourceTemplateCompletionWithoutMatchingHandlerReturnsEmptyResult() { |
273 | 275 | mcpServer.close(); |
274 | 276 | } |
275 | 277 |
|
| 278 | + @Test |
| 279 | + void testCompletionForNonExistentPromptReturnsInvalidParams() { |
| 280 | + var mcpServer = McpServer.sync(mcpServerTransportProvider) |
| 281 | + .capabilities(ServerCapabilities.builder().completions().build()) |
| 282 | + .build(); |
| 283 | + |
| 284 | + try (var mcpClient = clientBuilder |
| 285 | + .clientInfo(McpSchema.Implementation.builder("Sample " + "client", "0.0.0").build()) |
| 286 | + .build()) { |
| 287 | + InitializeResult initResult = mcpClient.initialize(); |
| 288 | + assertThat(initResult).isNotNull(); |
| 289 | + |
| 290 | + CompleteRequest request = CompleteRequest |
| 291 | + .builder(new PromptReference("nonexistent-prompt"), new CompleteRequest.CompleteArgument("arg", "val")) |
| 292 | + .build(); |
| 293 | + |
| 294 | + assertThatThrownBy(() -> mcpClient.completeCompletion(request)).isInstanceOf(McpError.class) |
| 295 | + .asInstanceOf(type(McpError.class)) |
| 296 | + .extracting(McpError::getJsonRpcError) |
| 297 | + .extracting(McpSchema.JSONRPCResponse.JSONRPCError::code) |
| 298 | + .isEqualTo(ErrorCodes.INVALID_PARAMS); |
| 299 | + } |
| 300 | + |
| 301 | + mcpServer.close(); |
| 302 | + } |
| 303 | + |
| 304 | + @Test |
| 305 | + void testCompletionForNonExistentResourceReturnsResourceNotFound() { |
| 306 | + var mcpServer = McpServer.sync(mcpServerTransportProvider) |
| 307 | + .capabilities(ServerCapabilities.builder().completions().build()) |
| 308 | + .build(); |
| 309 | + |
| 310 | + try (var mcpClient = clientBuilder |
| 311 | + .clientInfo(McpSchema.Implementation.builder("Sample " + "client", "0.0.0").build()) |
| 312 | + .build()) { |
| 313 | + InitializeResult initResult = mcpClient.initialize(); |
| 314 | + assertThat(initResult).isNotNull(); |
| 315 | + |
| 316 | + CompleteRequest request = CompleteRequest |
| 317 | + .builder(new ResourceReference("test://nonexistent/{param}"), |
| 318 | + new CompleteRequest.CompleteArgument("param", "val")) |
| 319 | + .build(); |
| 320 | + |
| 321 | + assertThatThrownBy(() -> mcpClient.completeCompletion(request)).isInstanceOf(McpError.class) |
| 322 | + .asInstanceOf(type(McpError.class)) |
| 323 | + .extracting(McpError::getJsonRpcError) |
| 324 | + .extracting(McpSchema.JSONRPCResponse.JSONRPCError::code) |
| 325 | + .isEqualTo(McpSchema.ErrorCodes.RESOURCE_NOT_FOUND); |
| 326 | + } |
| 327 | + |
| 328 | + mcpServer.close(); |
| 329 | + } |
| 330 | + |
276 | 331 | @Test |
277 | 332 | void testDependentCompletionScenario() { |
278 | 333 | BiFunction<McpSyncServerExchange, CompleteRequest, CompleteResult> completionHandler = (exchange, request) -> { |
|
0 commit comments