Skip to content

Commit dca8a04

Browse files
authored
Merge pull request #14 from explyt/imuromtsev/thread-safe-stdio-send
fix: Make STDIO sendMessage thread-safe and run STDIO tests with node (backport upstream modelcontextprotocol#304, dc952a2)
2 parents cb86c74 + 575d722 commit dca8a04

6 files changed

Lines changed: 253 additions & 32 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/client/transport/StdioClientTransport.java‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,13 @@ private void handleIncomingMessages(Function<Mono<JSONRPCMessage>, Mono<JSONRPCM
252252

253253
@Override
254254
public Mono<Void> sendMessage(JSONRPCMessage message) {
255-
if (this.outboundSink.tryEmitNext(message).isSuccess()) {
256-
// TODO: essentially we could reschedule ourselves in some time and make
257-
// another attempt with the already read data but pause reading until
258-
// success
259-
// In this approach we delegate the retry and the backpressure onto the
260-
// caller. This might be enough for most cases.
255+
try {
256+
// busyLooping retries FAIL_NON_SERIALIZED under concurrent senders
257+
this.outboundSink.emitNext(message, Sinks.EmitFailureHandler.busyLooping(Duration.ofMillis(100)));
261258
return Mono.empty();
262259
}
263-
else {
264-
return Mono.error(new RuntimeException("Failed to enqueue message"));
260+
catch (Sinks.EmissionException e) {
261+
return Mono.error(new RuntimeException("Failed to enqueue message", e));
265262
}
266263
}
267264

‎mcp-core/src/main/java/io/modelcontextprotocol/server/transport/StdioServerTransportProvider.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import java.io.InputStreamReader;
1111
import java.io.OutputStream;
1212
import java.nio.charset.StandardCharsets;
13+
import java.time.Duration;
1314
import java.util.List;
1415
import java.util.concurrent.Executors;
1516
import java.util.concurrent.atomic.AtomicBoolean;
1617
import java.util.function.Function;
1718

1819
import io.modelcontextprotocol.json.TypeRef;
19-
import io.modelcontextprotocol.spec.McpError;
2020
import io.modelcontextprotocol.spec.McpSchema;
2121
import io.modelcontextprotocol.spec.McpSchema.JSONRPCMessage;
2222
import io.modelcontextprotocol.spec.McpServerSession;
@@ -161,11 +161,12 @@ public StdioMcpSessionTransport() {
161161
public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
162162

163163
return Mono.zip(inboundReady.asMono(), outboundReady.asMono()).then(Mono.defer(() -> {
164-
if (outboundSink.tryEmitNext(message).isSuccess()) {
164+
try {
165+
outboundSink.emitNext(message, Sinks.EmitFailureHandler.busyLooping(Duration.ofMillis(100)));
165166
return Mono.empty();
166167
}
167-
else {
168-
return Mono.error(new RuntimeException("Failed to enqueue message"));
168+
catch (Sinks.EmissionException e) {
169+
return Mono.error(new RuntimeException("Failed to enqueue message", e));
169170
}
170171
}));
171172
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2026 - 2026 the original author or authors.
3+
*/
4+
5+
package io.modelcontextprotocol.client.transport;
6+
7+
import java.time.Duration;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.concurrent.CountDownLatch;
11+
import java.util.concurrent.TimeUnit;
12+
13+
import io.modelcontextprotocol.spec.McpSchema;
14+
import io.modelcontextprotocol.spec.json.gson.GsonMcpJsonMapper;
15+
import org.junit.jupiter.api.AfterEach;
16+
import org.junit.jupiter.api.Test;
17+
import reactor.core.publisher.Flux;
18+
import reactor.core.scheduler.Schedulers;
19+
import reactor.test.StepVerifier;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
class StdioClientTransportConcurrentSendTests {
24+
25+
private static final int MESSAGE_COUNT = 500;
26+
27+
private final StdioClientTransport transport = new StdioClientTransport(
28+
ServerParameters.builder("sh").args(List.of("-c", "cat")).build(), new GsonMcpJsonMapper());
29+
30+
@AfterEach
31+
void closeTransport() {
32+
this.transport.closeGracefully().block(Duration.ofSeconds(5));
33+
}
34+
35+
@Test
36+
void concurrentSendersAreAllDeliveredToTheServerProcess() throws InterruptedException {
37+
CountDownLatch echoedMessages = new CountDownLatch(MESSAGE_COUNT);
38+
StepVerifier.create(this.transport.connect(message -> message.doOnNext(echoed -> echoedMessages.countDown())))
39+
.verifyComplete();
40+
41+
Flux<Integer> concurrentSends = Flux.range(0, MESSAGE_COUNT)
42+
.parallel(16)
43+
.runOn(Schedulers.parallel())
44+
.flatMap(i -> this.transport
45+
.sendMessage(
46+
new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION, "test/notification", Map.of()))
47+
.thenReturn(i))
48+
.sequential();
49+
50+
StepVerifier.create(concurrentSends).expectNextCount(MESSAGE_COUNT).verifyComplete();
51+
assertThat(echoedMessages.await(30, TimeUnit.SECONDS))
52+
.as("%d of %d messages never reached the server process", echoedMessages.getCount(), MESSAGE_COUNT)
53+
.isTrue();
54+
}
55+
56+
}
Lines changed: 175 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,188 @@
1+
/*
2+
* Copyright 2024-2026 the original author or authors.
3+
*/
4+
15
package io.modelcontextprotocol.client;
26

7+
import java.io.IOException;
8+
import java.io.UncheckedIOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.nio.file.StandardCopyOption;
13+
import java.util.ArrayList;
14+
import java.util.Comparator;
15+
import java.util.List;
16+
import java.util.concurrent.TimeUnit;
17+
import java.util.stream.Stream;
18+
319
import io.modelcontextprotocol.client.transport.ServerParameters;
420

21+
/**
22+
* Provides the {@link ServerParameters} used to launch the {@code server-everything} MCP
23+
* server for the stdio client tests.
24+
*
25+
* <p>
26+
* Those tests spawn a fresh server process per test, so the launch cost is paid dozens of
27+
* times per build. Going through {@code npx} costs roughly 3 seconds per spawn even with
28+
* a warm cache, because npx re-resolves the package every time, and it inserts two extra
29+
* processes ({@code npx} &rarr; {@code npm exec} &rarr; {@code node}) between the test
30+
* and the server. The latter also means the process the transport owns is not the server,
31+
* so closing the transport leaves the server behind.
32+
*
33+
* <p>
34+
* Instead, the package is installed once per JVM into a version-keyed directory under the
35+
* temporary directory, and the server is launched directly with {@code node}, which
36+
* brings the per-spawn cost down to roughly 0.4 seconds.
37+
*/
538
public final class ServerParameterUtils {
639

40+
private static final String SERVER_EVERYTHING_VERSION = "2025.12.18";
41+
42+
private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().contains("win");
43+
44+
private static final Path SERVER_SCRIPT = resolveServerScript();
45+
46+
private static final String NODE_EXECUTABLE = resolveNodeExecutable();
47+
748
private ServerParameterUtils() {
849
}
950

1051
public static ServerParameters createServerParameters() {
11-
if (System.getProperty("os.name").toLowerCase().contains("win")) {
12-
return ServerParameters.builder("cmd.exe")
13-
.args("/c", "npx.cmd", "-y", "@modelcontextprotocol/[email protected]", "stdio")
14-
.build();
15-
}
16-
return ServerParameters.builder("npx")
17-
.args("-y", "@modelcontextprotocol/[email protected]", "stdio")
18-
.build();
52+
return ServerParameters.builder(NODE_EXECUTABLE).args(SERVER_SCRIPT.toString(), "stdio").build();
53+
}
54+
55+
private static Path resolveServerScript() {
56+
Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir"));
57+
Path installDir = tmpDir.resolve("mcp-server-everything-" + SERVER_EVERYTHING_VERSION);
58+
if (!Files.isRegularFile(serverScriptIn(installDir))) {
59+
install(tmpDir, installDir);
60+
}
61+
Path script = serverScriptIn(installDir);
62+
if (!Files.isRegularFile(script)) {
63+
throw new IllegalStateException("server-everything was not installed at " + script);
64+
}
65+
return script;
66+
}
67+
68+
private static Path serverScriptIn(Path installDir) {
69+
return installDir
70+
.resolve(Paths.get("node_modules", "@modelcontextprotocol", "server-everything", "dist", "index.js"));
71+
}
72+
73+
/**
74+
* Installs into a staging directory and moves it into place atomically, so that
75+
* concurrent builds sharing the temporary directory can never observe a partially
76+
* installed tree.
77+
*/
78+
private static void install(Path tmpDir, Path installDir) {
79+
Path staging;
80+
try {
81+
staging = Files.createTempDirectory(tmpDir, "mcp-server-everything-staging-");
82+
}
83+
catch (IOException e) {
84+
throw new UncheckedIOException(e);
85+
}
86+
try {
87+
run(npmCommand("install", "--prefix", staging.toString(), "--no-save", "--no-audit", "--no-fund",
88+
"--loglevel=error", "@modelcontextprotocol/server-everything@" + SERVER_EVERYTHING_VERSION));
89+
try {
90+
Files.move(staging, installDir, StandardCopyOption.ATOMIC_MOVE);
91+
}
92+
catch (IOException e) {
93+
// Another build won the race and installed it first, which is fine as
94+
// long as the result is usable.
95+
if (!Files.isRegularFile(serverScriptIn(installDir))) {
96+
throw new UncheckedIOException("Failed to install server-everything to " + installDir, e);
97+
}
98+
}
99+
}
100+
finally {
101+
deleteRecursively(staging);
102+
}
103+
}
104+
105+
private static void deleteRecursively(Path path) {
106+
if (!Files.exists(path)) {
107+
return;
108+
}
109+
try (Stream<Path> paths = Files.walk(path)) {
110+
paths.sorted(Comparator.reverseOrder()).forEach(p -> {
111+
try {
112+
Files.deleteIfExists(p);
113+
}
114+
catch (IOException e) {
115+
// Leftovers in the temporary directory are harmless.
116+
}
117+
});
118+
}
119+
catch (IOException e) {
120+
// Leftovers in the temporary directory are harmless.
121+
}
122+
}
123+
124+
/**
125+
* Asks node for its own absolute path, so that spawning the server does not go
126+
* through a {@code node} wrapper script on the {@code PATH} (as installed by nvm and
127+
* friends), which would add an extra shell process per spawn.
128+
*/
129+
private static String resolveNodeExecutable() {
130+
String node = IS_WINDOWS ? "node.exe" : "node";
131+
try {
132+
return run(List.of(node, "-p", "process.execPath")).trim();
133+
}
134+
catch (RuntimeException e) {
135+
return node;
136+
}
137+
}
138+
139+
private static List<String> npmCommand(String... args) {
140+
List<String> command = new ArrayList<>();
141+
if (IS_WINDOWS) {
142+
command.add("cmd.exe");
143+
command.add("/c");
144+
command.add("npm.cmd");
145+
}
146+
else {
147+
command.add("npm");
148+
}
149+
command.addAll(List.of(args));
150+
return command;
151+
}
152+
153+
private static String run(List<String> command) {
154+
try {
155+
// Note: the output is redirected to a file rather than inherited, because
156+
// writing to the native streams from a forked JVM corrupts the Surefire fork
157+
// channel. A file rather than a pipe also keeps the timeout below effective,
158+
// which draining the pipe first would not.
159+
Path log = Files.createTempFile("mcp-test-", ".log");
160+
try {
161+
Process process = new ProcessBuilder(command).redirectErrorStream(true)
162+
.redirectOutput(log.toFile())
163+
.start();
164+
if (!process.waitFor(5, TimeUnit.MINUTES)) {
165+
process.destroyForcibly();
166+
throw new IllegalStateException("Timed out running " + command);
167+
}
168+
String output = Files.readString(log);
169+
if (process.exitValue() != 0) {
170+
throw new IllegalStateException(
171+
command + " failed with exit code " + process.exitValue() + ":\n" + output);
172+
}
173+
return output;
174+
}
175+
finally {
176+
Files.deleteIfExists(log);
177+
}
178+
}
179+
catch (IOException e) {
180+
throw new UncheckedIOException(e);
181+
}
182+
catch (InterruptedException e) {
183+
Thread.currentThread().interrupt();
184+
throw new IllegalStateException(e);
185+
}
19186
}
20187

21188
}

‎mcp-test/src/test/java/io/modelcontextprotocol/client/StdioMcpAsyncClientTests.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
* Tests for the {@link McpAsyncClient} with {@link StdioClientTransport}.
1919
*
2020
* <p>
21-
* These tests use npx to download and run the MCP "everything" server locally. The first
22-
* test execution will download the everything server scripts and cache them locally,
23-
* which can take more than 15 seconds. Subsequent test runs will use the cached version
24-
* and execute faster.
21+
* These tests run the MCP "everything" server locally, spawning a fresh server process
22+
* per test. The server package is installed once and launched directly with node, see
23+
* {@link ServerParameterUtils}. The first test execution installs the package, which can
24+
* take more than 15 seconds; subsequent runs reuse the installed copy.
2525
*
2626
* @author Christian Tzolov
2727
* @author Dariusz Jędrzejczyk
2828
*/
29-
@Timeout(25) // Giving extra time beyond the client timeout to account for initial server
30-
// download
29+
@Timeout(25) // Giving extra time beyond the client timeout to account for the one-time
30+
// install of the server package
3131
class StdioMcpAsyncClientTests extends AbstractMcpAsyncClientTests {
3232

3333
@Override

‎mcp-test/src/test/java/io/modelcontextprotocol/client/StdioMcpSyncClientTests.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525
* Tests for the {@link McpSyncClient} with {@link StdioClientTransport}.
2626
*
2727
* <p>
28-
* These tests use npx to download and run the MCP "everything" server locally. The first
29-
* test execution will download the everything server scripts and cache them locally,
30-
* which can take more than 15 seconds. Subsequent test runs will use the cached version
31-
* and execute faster.
28+
* These tests run the MCP "everything" server locally, spawning a fresh server process
29+
* per test. The server package is installed once and launched directly with node, see
30+
* {@link ServerParameterUtils}. The first test execution installs the package, which can
31+
* take more than 15 seconds; subsequent runs reuse the installed copy.
3232
*
3333
* @author Christian Tzolov
3434
* @author Dariusz Jędrzejczyk
3535
*/
36-
@Timeout(25) // Giving extra time beyond the client timeout to account for initial server
37-
// download
36+
@Timeout(25) // Giving extra time beyond the client timeout to account for the one-time
37+
// install of the server package
3838
class StdioMcpSyncClientTests extends AbstractMcpSyncClientTests {
3939

4040
@Override

0 commit comments

Comments
 (0)