Skip to content

Sync async client tests between mcp and mcp-test module #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ void tearDown() {
onClose();
}

<T> void verifyInitializationTimeout(Function<McpAsyncClient, Mono<T>> operation, String action) {
withClient(createMcpTransport(), mcpAsyncClient -> {
StepVerifier.withVirtualTime(() -> operation.apply(mcpAsyncClient))
.expectSubscription()
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before " + action))
.verify();
});
}

@Test
void testConstructorWithInvalidArguments() {
assertThatThrownBy(() -> McpClient.async(null).build()).isInstanceOf(IllegalArgumentException.class)
Expand All @@ -121,14 +132,7 @@ void testConstructorWithInvalidArguments() {

@Test
void testListToolsWithoutInitialization() {
withClient(createMcpTransport(), mcpAsyncClient -> {
StepVerifier.withVirtualTime(() -> mcpAsyncClient.listTools(null))
.expectSubscription()
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before listing tools"))
.verify();
});
verifyInitializationTimeout(client -> client.listTools(null), "listing tools");
}

@Test
Expand All @@ -148,14 +152,7 @@ void testListTools() {

@Test
void testPingWithoutInitialization() {
withClient(createMcpTransport(), mcpAsyncClient -> {
StepVerifier.withVirtualTime(() -> mcpAsyncClient.ping())
.expectSubscription()
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before pinging the " + "server"))
.verify();
});
verifyInitializationTimeout(client -> client.ping(), "pinging the server");
}

@Test
Expand All @@ -169,16 +166,8 @@ void testPing() {

@Test
void testCallToolWithoutInitialization() {
withClient(createMcpTransport(), mcpAsyncClient -> {
CallToolRequest callToolRequest = new CallToolRequest("echo", Map.of("message", ECHO_TEST_MESSAGE));

StepVerifier.withVirtualTime(() -> mcpAsyncClient.callTool(callToolRequest))
.expectSubscription()
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before calling tools"))
.verify();
});
CallToolRequest callToolRequest = new CallToolRequest("echo", Map.of("message", ECHO_TEST_MESSAGE));
verifyInitializationTimeout(client -> client.callTool(callToolRequest), "calling tools");
}

@Test
Expand Down Expand Up @@ -212,14 +201,7 @@ void testCallToolWithInvalidTool() {

@Test
void testListResourcesWithoutInitialization() {
withClient(createMcpTransport(), mcpAsyncClient -> {
StepVerifier.withVirtualTime(() -> mcpAsyncClient.listResources(null))
.expectSubscription()
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before listing resources"))
.verify();
});
verifyInitializationTimeout(client -> client.listResources(null), "listing resources");
}

@Test
Expand Down Expand Up @@ -250,14 +232,7 @@ void testMcpAsyncClientState() {

@Test
void testListPromptsWithoutInitialization() {
withClient(createMcpTransport(), mcpAsyncClient -> {
StepVerifier.withVirtualTime(() -> mcpAsyncClient.listPrompts(null))
.expectSubscription()
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before listing prompts"))
.verify();
});
verifyInitializationTimeout(client -> client.listPrompts(null), "listing " + "prompts");
}

@Test
Expand All @@ -281,16 +256,8 @@ void testListPrompts() {

@Test
void testGetPromptWithoutInitialization() {
withClient(createMcpTransport(), mcpAsyncClient -> {
GetPromptRequest request = new GetPromptRequest("simple_prompt", Map.of());

StepVerifier.withVirtualTime(() -> mcpAsyncClient.getPrompt(request))
.expectSubscription()
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before getting prompts"))
.verify();
});
GetPromptRequest request = new GetPromptRequest("simple_prompt", Map.of());
verifyInitializationTimeout(client -> client.getPrompt(request), "getting " + "prompts");
}

@Test
Expand All @@ -311,14 +278,8 @@ void testGetPrompt() {

@Test
void testRootsListChangedWithoutInitialization() {
withClient(createMcpTransport(), mcpAsyncClient -> {
StepVerifier.withVirtualTime(() -> mcpAsyncClient.rootsListChangedNotification())
.expectSubscription()
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before sending roots list changed notification"))
.verify();
});
verifyInitializationTimeout(client -> client.rootsListChangedNotification(),
"sending roots list changed notification");
}

@Test
Expand Down Expand Up @@ -392,14 +353,7 @@ void testReadResource() {

@Test
void testListResourceTemplatesWithoutInitialization() {
withClient(createMcpTransport(), mcpAsyncClient -> {
StepVerifier.withVirtualTime(() -> mcpAsyncClient.listResourceTemplates())
.expectSubscription()
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before listing resource templates"))
.verify();
});
verifyInitializationTimeout(client -> client.listResourceTemplates(), "listing resource templates");
}

@Test
Expand Down Expand Up @@ -492,14 +446,8 @@ void testInitializeWithAllCapabilities() {

@Test
void testLoggingLevelsWithoutInitialization() {
withClient(createMcpTransport(),
mcpAsyncClient -> StepVerifier
.withVirtualTime(() -> mcpAsyncClient.setLoggingLevel(McpSchema.LoggingLevel.DEBUG))
.expectSubscription()
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before setting logging level"))
.verify());
verifyInitializationTimeout(client -> client.setLoggingLevel(McpSchema.LoggingLevel.DEBUG),
"setting logging level");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import io.modelcontextprotocol.spec.ClientMcpTransport;
import io.modelcontextprotocol.spec.McpError;
Expand Down