|
4 | 4 |
|
5 | 5 | package io.modelcontextprotocol.client;
|
6 | 6 |
|
| 7 | +import static org.assertj.core.api.Assertions.assertThat; |
| 8 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 9 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 10 | +import static org.assertj.core.api.Assertions.fail; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 12 | + |
7 | 13 | import java.time.Duration;
|
| 14 | +import java.util.ArrayList; |
8 | 15 | import java.util.Map;
|
9 | 16 | import java.util.Objects;
|
10 | 17 | import java.util.concurrent.atomic.AtomicBoolean;
|
|
13 | 20 | import java.util.function.Consumer;
|
14 | 21 | import java.util.function.Function;
|
15 | 22 |
|
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.ValueSource; |
| 28 | + |
16 | 29 | import io.modelcontextprotocol.spec.McpClientTransport;
|
17 | 30 | import io.modelcontextprotocol.spec.McpError;
|
18 | 31 | import io.modelcontextprotocol.spec.McpSchema;
|
| 32 | +import io.modelcontextprotocol.spec.McpSchema.BlobResourceContents; |
19 | 33 | import io.modelcontextprotocol.spec.McpSchema.CallToolRequest;
|
20 | 34 | import io.modelcontextprotocol.spec.McpSchema.ClientCapabilities;
|
21 | 35 | import io.modelcontextprotocol.spec.McpSchema.CreateMessageRequest;
|
|
24 | 38 | import io.modelcontextprotocol.spec.McpSchema.ElicitResult;
|
25 | 39 | import io.modelcontextprotocol.spec.McpSchema.GetPromptRequest;
|
26 | 40 | import io.modelcontextprotocol.spec.McpSchema.Prompt;
|
| 41 | +import io.modelcontextprotocol.spec.McpSchema.ReadResourceResult; |
27 | 42 | import io.modelcontextprotocol.spec.McpSchema.Resource;
|
| 43 | +import io.modelcontextprotocol.spec.McpSchema.ResourceContents; |
28 | 44 | import io.modelcontextprotocol.spec.McpSchema.Root;
|
29 | 45 | import io.modelcontextprotocol.spec.McpSchema.SubscribeRequest;
|
| 46 | +import io.modelcontextprotocol.spec.McpSchema.TextResourceContents; |
30 | 47 | import io.modelcontextprotocol.spec.McpSchema.Tool;
|
31 | 48 | import io.modelcontextprotocol.spec.McpSchema.UnsubscribeRequest;
|
32 | 49 | import io.modelcontextprotocol.spec.McpTransport;
|
33 |
| -import org.junit.jupiter.api.AfterEach; |
34 |
| -import org.junit.jupiter.api.BeforeEach; |
35 |
| -import org.junit.jupiter.api.Disabled; |
36 |
| -import org.junit.jupiter.api.Test; |
37 | 50 | import reactor.core.publisher.Flux;
|
38 | 51 | import reactor.core.publisher.Mono;
|
39 | 52 | import reactor.test.StepVerifier;
|
40 | 53 |
|
41 |
| -import static org.assertj.core.api.Assertions.assertThat; |
42 |
| -import static org.assertj.core.api.Assertions.assertThatCode; |
43 |
| -import static org.assertj.core.api.Assertions.assertThatThrownBy; |
44 |
| -import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
45 |
| - |
46 | 54 | /**
|
47 | 55 | * Test suite for the {@link McpAsyncClient} that can be used with different
|
48 | 56 | * {@link McpTransport} implementations.
|
@@ -208,6 +216,64 @@ void testCallToolWithInvalidTool() {
|
208 | 216 | });
|
209 | 217 | }
|
210 | 218 |
|
| 219 | + @ParameterizedTest |
| 220 | + @ValueSource(strings = { "success", "error", "debug" }) |
| 221 | + void testCallToolWithMessageAnnotations(String messageType) { |
| 222 | + McpClientTransport transport = createMcpTransport(); |
| 223 | + |
| 224 | + withClient(transport, mcpAsyncClient -> { |
| 225 | + StepVerifier.create(mcpAsyncClient.initialize() |
| 226 | + .then(mcpAsyncClient.callTool(new McpSchema.CallToolRequest("annotatedMessage", |
| 227 | + Map.of("messageType", messageType, "includeImage", true))))) |
| 228 | + .consumeNextWith(result -> { |
| 229 | + assertThat(result).isNotNull(); |
| 230 | + assertThat(result.isError()).isNotEqualTo(true); |
| 231 | + assertThat(result.content()).isNotEmpty(); |
| 232 | + assertThat(result.content()).allSatisfy(content -> { |
| 233 | + switch (content.type()) { |
| 234 | + case "text": |
| 235 | + McpSchema.TextContent textContent = assertInstanceOf(McpSchema.TextContent.class, |
| 236 | + content); |
| 237 | + assertThat(textContent.text()).isNotEmpty(); |
| 238 | + assertThat(textContent.annotations()).isNotNull(); |
| 239 | + |
| 240 | + switch (messageType) { |
| 241 | + case "error": |
| 242 | + assertThat(textContent.annotations().priority()).isEqualTo(1.0); |
| 243 | + assertThat(textContent.annotations().audience()) |
| 244 | + .containsOnly(McpSchema.Role.USER, McpSchema.Role.ASSISTANT); |
| 245 | + break; |
| 246 | + case "success": |
| 247 | + assertThat(textContent.annotations().priority()).isEqualTo(0.7); |
| 248 | + assertThat(textContent.annotations().audience()) |
| 249 | + .containsExactly(McpSchema.Role.USER); |
| 250 | + break; |
| 251 | + case "debug": |
| 252 | + assertThat(textContent.annotations().priority()).isEqualTo(0.3); |
| 253 | + assertThat(textContent.annotations().audience()) |
| 254 | + .containsExactly(McpSchema.Role.ASSISTANT); |
| 255 | + break; |
| 256 | + default: |
| 257 | + throw new IllegalStateException("Unexpected value: " + content.type()); |
| 258 | + } |
| 259 | + break; |
| 260 | + case "image": |
| 261 | + McpSchema.ImageContent imageContent = assertInstanceOf(McpSchema.ImageContent.class, |
| 262 | + content); |
| 263 | + assertThat(imageContent.data()).isNotEmpty(); |
| 264 | + assertThat(imageContent.annotations()).isNotNull(); |
| 265 | + assertThat(imageContent.annotations().priority()).isEqualTo(0.5); |
| 266 | + assertThat(imageContent.annotations().audience()).containsExactly(McpSchema.Role.USER); |
| 267 | + break; |
| 268 | + default: |
| 269 | + fail("Unexpected content type: " + content.type()); |
| 270 | + } |
| 271 | + }); |
| 272 | + }) |
| 273 | + .verifyComplete(); |
| 274 | + }); |
| 275 | + } |
| 276 | + |
211 | 277 | @Test
|
212 | 278 | void testListResourcesWithoutInitialization() {
|
213 | 279 | verifyCallSucceedsWithImplicitInitialization(client -> client.listResources(null), "listing resources");
|
@@ -345,18 +411,59 @@ void testRemoveNonExistentRoot() {
|
345 | 411 | }
|
346 | 412 |
|
347 | 413 | @Test
|
348 |
| - @Disabled |
349 | 414 | void testReadResource() {
|
350 |
| - withClient(createMcpTransport(), mcpAsyncClient -> { |
351 |
| - StepVerifier.create(mcpAsyncClient.listResources()).consumeNextWith(resources -> { |
352 |
| - if (!resources.resources().isEmpty()) { |
353 |
| - Resource firstResource = resources.resources().get(0); |
354 |
| - StepVerifier.create(mcpAsyncClient.readResource(firstResource)).consumeNextWith(result -> { |
355 |
| - assertThat(result).isNotNull(); |
356 |
| - assertThat(result.contents()).isNotNull(); |
357 |
| - }).verifyComplete(); |
| 415 | + withClient(createMcpTransport(), client -> { |
| 416 | + Flux<McpSchema.ReadResourceResult> resources = client.initialize() |
| 417 | + .then(client.listResources(null)) |
| 418 | + .flatMapMany(r -> Flux.fromIterable(r.resources())) |
| 419 | + .flatMap(r -> client.readResource(r)); |
| 420 | + |
| 421 | + StepVerifier.create(resources).recordWith(ArrayList::new).consumeRecordedWith(readResourceResults -> { |
| 422 | + |
| 423 | + for (ReadResourceResult result : readResourceResults) { |
| 424 | + |
| 425 | + assertThat(result).isNotNull(); |
| 426 | + assertThat(result.contents()).isNotNull().isNotEmpty(); |
| 427 | + |
| 428 | + // Validate each content item |
| 429 | + for (ResourceContents content : result.contents()) { |
| 430 | + assertThat(content).isNotNull(); |
| 431 | + assertThat(content.uri()).isNotNull().isNotEmpty(); |
| 432 | + assertThat(content.mimeType()).isNotNull().isNotEmpty(); |
| 433 | + |
| 434 | + // Validate content based on its type with more comprehensive |
| 435 | + // checks |
| 436 | + switch (content.mimeType()) { |
| 437 | + case "text/plain" -> { |
| 438 | + TextResourceContents textContent = assertInstanceOf(TextResourceContents.class, |
| 439 | + content); |
| 440 | + assertThat(textContent.text()).isNotNull().isNotEmpty(); |
| 441 | + assertThat(textContent.uri()).isNotEmpty(); |
| 442 | + } |
| 443 | + case "application/octet-stream" -> { |
| 444 | + BlobResourceContents blobContent = assertInstanceOf(BlobResourceContents.class, |
| 445 | + content); |
| 446 | + assertThat(blobContent.blob()).isNotNull().isNotEmpty(); |
| 447 | + assertThat(blobContent.uri()).isNotNull().isNotEmpty(); |
| 448 | + // Validate base64 encoding format |
| 449 | + assertThat(blobContent.blob()).matches("^[A-Za-z0-9+/]*={0,2}$"); |
| 450 | + } |
| 451 | + default -> { |
| 452 | + |
| 453 | + // Still validate basic properties |
| 454 | + if (content instanceof TextResourceContents textContent) { |
| 455 | + assertThat(textContent.text()).isNotNull(); |
| 456 | + } |
| 457 | + else if (content instanceof BlobResourceContents blobContent) { |
| 458 | + assertThat(blobContent.blob()).isNotNull(); |
| 459 | + } |
| 460 | + } |
| 461 | + } |
| 462 | + } |
358 | 463 | }
|
359 |
| - }).verifyComplete(); |
| 464 | + }) |
| 465 | + .expectNextCount(10) // Expect 10 elements |
| 466 | + .verifyComplete(); |
360 | 467 | });
|
361 | 468 | }
|
362 | 469 |
|
|
0 commit comments