|
8 | 8 | import java.util.Map; |
9 | 9 | import java.util.Objects; |
10 | 10 | import java.util.concurrent.atomic.AtomicBoolean; |
| 11 | +import java.util.concurrent.atomic.AtomicInteger; |
11 | 12 | import java.util.concurrent.atomic.AtomicReference; |
12 | 13 | import java.util.function.Consumer; |
13 | 14 | import java.util.function.Function; |
|
40 | 41 | import static org.assertj.core.api.Assertions.assertThat; |
41 | 42 | import static org.assertj.core.api.Assertions.assertThatCode; |
42 | 43 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
43 | 45 |
|
44 | 46 | /** |
45 | 47 | * Test suite for the {@link McpAsyncClient} that can be used with different |
@@ -194,22 +196,6 @@ void testCallTool() { |
194 | 196 | }); |
195 | 197 | } |
196 | 198 |
|
197 | | - @Test |
198 | | - void testSampling() { |
199 | | - withClient(createMcpTransport(), mcpAsyncClient -> { |
200 | | - CallToolRequest callToolRequest = new CallToolRequest("sampleLLM", |
201 | | - Map.of("prompt", "Hello MCP Spring AI!")); |
202 | | - StepVerifier.create(mcpAsyncClient.initialize().then(mcpAsyncClient.callTool(callToolRequest))) |
203 | | - .consumeNextWith(callToolResult -> { |
204 | | - assertThat(callToolResult).isNotNull().satisfies(result -> { |
205 | | - assertThat(result.content()).isNotNull(); |
206 | | - assertThat(result.isError()).isNull(); |
207 | | - }); |
208 | | - }) |
209 | | - .verifyComplete(); |
210 | | - }); |
211 | | - } |
212 | | - |
213 | 199 | @Test |
214 | 200 | void testCallToolWithInvalidTool() { |
215 | 201 | withClient(createMcpTransport(), mcpAsyncClient -> { |
@@ -486,7 +472,6 @@ void testInitializeWithAllCapabilities() { |
486 | 472 | // --------------------------------------- |
487 | 473 | // Logging Tests |
488 | 474 | // --------------------------------------- |
489 | | - |
490 | 475 | @Test |
491 | 476 | void testLoggingLevelsWithoutInitialization() { |
492 | 477 | verifyNotificationSucceedsWithImplicitInitialization( |
@@ -526,4 +511,52 @@ void testLoggingWithNullNotification() { |
526 | 511 | }); |
527 | 512 | } |
528 | 513 |
|
| 514 | + @Test |
| 515 | + void testSampling() { |
| 516 | + McpClientTransport transport = createMcpTransport(); |
| 517 | + |
| 518 | + final String message = "Hello, world!"; |
| 519 | + final String response = "Goodbye, world!"; |
| 520 | + final int maxTokens = 100; |
| 521 | + |
| 522 | + AtomicReference<String> receivedPrompt = new AtomicReference<>(); |
| 523 | + AtomicReference<String> receivedMessage = new AtomicReference<>(); |
| 524 | + AtomicInteger receivedMaxTokens = new AtomicInteger(); |
| 525 | + |
| 526 | + withClient(transport, spec -> spec.capabilities(McpSchema.ClientCapabilities.builder().sampling().build()) |
| 527 | + .sampling(request -> { |
| 528 | + McpSchema.TextContent messageText = assertInstanceOf(McpSchema.TextContent.class, |
| 529 | + request.messages().get(0).content()); |
| 530 | + receivedPrompt.set(request.systemPrompt()); |
| 531 | + receivedMessage.set(messageText.text()); |
| 532 | + receivedMaxTokens.set(request.maxTokens()); |
| 533 | + |
| 534 | + return Mono |
| 535 | + .just(new McpSchema.CreateMessageResult(McpSchema.Role.USER, new McpSchema.TextContent(response), |
| 536 | + "modelId", McpSchema.CreateMessageResult.StopReason.END_TURN)); |
| 537 | + }), client -> { |
| 538 | + StepVerifier.create(client.initialize()).expectNextMatches(Objects::nonNull).verifyComplete(); |
| 539 | + |
| 540 | + StepVerifier.create(client.callTool( |
| 541 | + new McpSchema.CallToolRequest("sampleLLM", Map.of("prompt", message, "maxTokens", maxTokens)))) |
| 542 | + .consumeNextWith(result -> { |
| 543 | + // Verify tool response to ensure our sampling response was passed |
| 544 | + // through |
| 545 | + assertThat(result.content()).hasAtLeastOneElementOfType(McpSchema.TextContent.class); |
| 546 | + assertThat(result.content()).allSatisfy(content -> { |
| 547 | + if (!(content instanceof McpSchema.TextContent text)) |
| 548 | + return; |
| 549 | + |
| 550 | + assertThat(text.text()).endsWith(response); // Prefixed |
| 551 | + }); |
| 552 | + |
| 553 | + // Verify sampling request parameters received in our callback |
| 554 | + assertThat(receivedPrompt.get()).isNotEmpty(); |
| 555 | + assertThat(receivedMessage.get()).endsWith(message); // Prefixed |
| 556 | + assertThat(receivedMaxTokens.get()).isEqualTo(maxTokens); |
| 557 | + }) |
| 558 | + .verifyComplete(); |
| 559 | + }); |
| 560 | + } |
| 561 | + |
529 | 562 | } |
0 commit comments