forked from modelcontextprotocol/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStdioMcpSyncClientTests.java
More file actions
168 lines (138 loc) · 5.27 KB
/
StdioMcpSyncClientTests.java
File metadata and controls
168 lines (138 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright 2024-2024 the original author or authors.
*/
package io.modelcontextprotocol.client;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import io.modelcontextprotocol.client.transport.ServerParameters;
import io.modelcontextprotocol.client.transport.StdioClientTransport;
import io.modelcontextprotocol.spec.McpClientTransport;
import io.modelcontextprotocol.spec.McpSchema;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import reactor.core.publisher.Sinks;
import reactor.test.StepVerifier;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
/**
* Tests for the {@link McpSyncClient} with {@link StdioClientTransport}.
*
* @author Christian Tzolov
* @author Dariusz Jędrzejczyk
*/
@Timeout(15) // Giving extra time beyond the client timeout
class StdioMcpSyncClientTests extends AbstractMcpSyncClientTests {
@Override
protected McpClientTransport createMcpTransport() {
ServerParameters stdioParams;
if (System.getProperty("os.name").toLowerCase().contains("win")) {
stdioParams = ServerParameters.builder("cmd.exe")
.args("/c", "npx.cmd", "-y", "@modelcontextprotocol/server-everything", "stdio")
.build();
}
else {
stdioParams = ServerParameters.builder("npx")
.args("-y", "@modelcontextprotocol/server-everything", "stdio")
.build();
}
return new StdioClientTransport(stdioParams);
}
@Test
void customErrorHandlerShouldReceiveErrors() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<String> receivedError = new AtomicReference<>();
McpClientTransport transport = createMcpTransport();
StepVerifier.create(transport.connect(msg -> msg)).verifyComplete();
((StdioClientTransport) transport).setStdErrorHandler(error -> {
receivedError.set(error);
latch.countDown();
});
String errorMessage = "Test error";
((StdioClientTransport) transport).getErrorSink().emitNext(errorMessage, Sinks.EmitFailureHandler.FAIL_FAST);
assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
assertThat(receivedError.get()).isNotNull().isEqualTo(errorMessage);
StepVerifier.create(transport.closeGracefully()).expectComplete().verify(Duration.ofSeconds(5));
}
@Test
void testListReadResources() {
McpClientTransport transport = createMcpTransport();
withClient(transport, client -> {
client.initialize();
int i = 0;
String nextCursor = null;
do {
McpSchema.ListResourcesResult result = client.listResources(nextCursor);
nextCursor = result.nextCursor();
for (McpSchema.Resource resource : result.resources()) {
McpSchema.ReadResourceResult resourceResult = client.readResource(resource);
if (i % 2 == 0) {
assertThat(resourceResult.contents()).allSatisfy(content -> {
McpSchema.TextResourceContents text = assertInstanceOf(McpSchema.TextResourceContents.class,
content);
assertThat(text.mimeType()).isEqualTo("text/plain");
assertThat(text.uri()).isNotEmpty();
assertThat(text.text()).isNotEmpty();
});
}
else {
assertThat(resourceResult.contents()).allSatisfy(content -> {
McpSchema.BlobResourceContents blob = assertInstanceOf(McpSchema.BlobResourceContents.class,
content);
assertThat(blob.mimeType()).isEqualTo("application/octet-stream");
assertThat(blob.uri()).isNotEmpty();
assertThat(blob.blob()).isNotEmpty();
});
}
i++;
}
}
while (nextCursor != null);
});
}
@Test
void testListResourceTemplates() {
McpClientTransport transport = createMcpTransport();
withClient(transport, client -> {
client.initialize();
String nextCursor = null;
do {
McpSchema.ListResourceTemplatesResult result = client.listResourceTemplates(nextCursor);
nextCursor = result.nextCursor();
for (McpSchema.ResourceTemplate resourceTemplate : result.resourceTemplates()) {
// mimeType is null in @modelcontextprotocol/server-everything, but we
// don't assert that it's
// null in case they change that later.
assertThat(resourceTemplate.uriTemplate()).isNotEmpty();
assertThat(resourceTemplate.name()).isNotEmpty();
assertThat(resourceTemplate.description()).isNotEmpty();
}
}
while (nextCursor != null);
});
}
@Test
void testEmbeddedResources() {
McpClientTransport transport = createMcpTransport();
withClient(transport, client -> {
client.initialize();
McpSchema.CallToolResult result = client
.callTool(new McpSchema.CallToolRequest("getResourceReference", Map.of("resourceId", 1)));
assertThat(result.content()).hasAtLeastOneElementOfType(McpSchema.EmbeddedResource.class);
assertThat(result.content()).allSatisfy(content -> {
if (!(content instanceof McpSchema.EmbeddedResource resource))
return;
McpSchema.TextResourceContents text = assertInstanceOf(McpSchema.TextResourceContents.class,
resource.resource());
assertThat(text.mimeType()).isEqualTo("text/plain");
assertThat(text.uri()).isNotEmpty();
assertThat(text.text()).isNotEmpty();
});
});
}
protected Duration getInitializationTimeout() {
return Duration.ofSeconds(10);
}
}