Skip to content

Commit b7c7139

Browse files
committed
wip
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
1 parent 8fd9903 commit b7c7139

2 files changed

Lines changed: 131 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public void awaitForExit() {
180180
private void startErrorProcessing() {
181181
this.errorScheduler.schedule(() -> {
182182
try (BufferedReader processErrorReader = new BufferedReader(
183-
new InputStreamReader(process.getErrorStream()))) {
183+
new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) {
184184
String line;
185185
while (!isClosing && (line = processErrorReader.readLine()) != null) {
186186
try {
@@ -246,7 +246,8 @@ public Mono<Void> sendMessage(JSONRPCMessage message) {
246246
*/
247247
private void startInboundProcessing() {
248248
this.inboundScheduler.schedule(() -> {
249-
try (BufferedReader processReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
249+
try (BufferedReader processReader = new BufferedReader(
250+
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
250251
String line;
251252
while (!isClosing && (line = processReader.readLine()) != null) {
252253
try {
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package io.modelcontextprotocol.client.transport;
2+
3+
import io.modelcontextprotocol.json.McpJsonMapper;
4+
import io.modelcontextprotocol.spec.McpSchema;
5+
import io.modelcontextprotocol.spec.json.gson.GsonMcpJsonMapper;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.io.File;
9+
import java.util.concurrent.CountDownLatch;
10+
import java.util.concurrent.TimeUnit;
11+
import java.util.concurrent.atomic.AtomicReference;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
/**
16+
* Tests for {@link StdioClientTransport} to ensure it correctly handles character
17+
* encodings.
18+
*/
19+
class StdioClientTransportCharsetTests {
20+
21+
@Test
22+
void testUtf8DecodingWithNonUtf8DefaultCharset() throws Exception {
23+
String javaHome = System.getProperty("java.home");
24+
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
25+
String classpath = System.getProperty("java.class.path");
26+
27+
// We MUST run a separate JVM to test this because the bug only manifests
28+
// when the JVM's default charset is not UTF-8. Charset.defaultCharset() is
29+
// cached at JVM startup and cannot be changed at runtime for the background
30+
// threads spawned by StdioClientTransport.
31+
// We use -Xmx32m to ensure the child JVM uses minimal memory on CI.
32+
ProcessBuilder pb = new ProcessBuilder(javaBin, "-Xmx32m", "-Dfile.encoding=ISO-8859-1", "-cp", classpath,
33+
ClientMain.class.getName());
34+
35+
pb.redirectErrorStream(true);
36+
Process process = pb.start();
37+
38+
try {
39+
// Wait with a timeout to prevent the test from hanging indefinitely
40+
boolean finished = process.waitFor(15, TimeUnit.SECONDS);
41+
String output = new String(process.getInputStream().readAllBytes());
42+
43+
assertThat(finished).as("Client process timed out. Output:\n" + output).isTrue();
44+
assertThat(process.exitValue()).as("Client process failed with output:\n" + output).isEqualTo(0);
45+
assertThat(output).contains("SUCCESS_MATCH");
46+
}
47+
finally {
48+
// Guarantee the child JVM is killed even if the test fails or is aborted
49+
if (process.isAlive()) {
50+
process.destroyForcibly();
51+
}
52+
}
53+
}
54+
55+
public static class ClientMain {
56+
57+
public static void main(String[] args) {
58+
try {
59+
McpJsonMapper jsonMapper = new GsonMcpJsonMapper();
60+
61+
String javaHome = System.getProperty("java.home");
62+
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
63+
String classpath = System.getProperty("java.class.path");
64+
65+
// We use -Xmx32m to ensure the child JVM uses minimal memory on CI.
66+
ServerParameters params = ServerParameters.builder(javaBin)
67+
.args("-Xmx32m", "-cp", classpath, ServerMain.class.getName())
68+
.build();
69+
70+
StdioClientTransport transport = new StdioClientTransport(params, jsonMapper);
71+
72+
CountDownLatch latch = new CountDownLatch(1);
73+
AtomicReference<McpSchema.JSONRPCMessage> receivedMessage = new AtomicReference<>();
74+
75+
transport.connect(msgMono -> msgMono.doOnNext(msg -> {
76+
receivedMessage.set(msg);
77+
latch.countDown();
78+
})).block();
79+
80+
boolean received = latch.await(10, TimeUnit.SECONDS);
81+
if (!received) {
82+
System.err.println("Did not receive message in time");
83+
System.exit(1);
84+
}
85+
86+
transport.closeGracefully().block();
87+
88+
McpSchema.JSONRPCMessage msg = receivedMessage.get();
89+
if (msg instanceof McpSchema.JSONRPCNotification notif) {
90+
if ("こんにちは".equals(notif.method())) {
91+
System.out.println("SUCCESS_MATCH");
92+
System.exit(0);
93+
}
94+
else {
95+
System.err.println("Method mismatch: " + notif.method());
96+
System.exit(1);
97+
}
98+
}
99+
else {
100+
System.err.println("Wrong message type: " + msg);
101+
System.exit(1);
102+
}
103+
}
104+
catch (Exception e) {
105+
// Catch any unexpected errors to ensure the JVM doesn't hang
106+
e.printStackTrace();
107+
System.exit(1);
108+
}
109+
}
110+
111+
}
112+
113+
public static class ServerMain {
114+
115+
public static void main(String[] args) throws Exception {
116+
// Write a UTF-8 JSON-RPC message containing Japanese characters
117+
String json = "{\"jsonrpc\":\"2.0\",\"method\":\"こんにちは\"}\n";
118+
System.out.write(json.getBytes(java.nio.charset.StandardCharsets.UTF_8));
119+
System.out.flush();
120+
// Sleep to keep the process alive long enough for the client to read the
121+
// message.
122+
// It will automatically exit after 5 seconds, preventing any permanent leaks.
123+
Thread.sleep(5000);
124+
}
125+
126+
}
127+
128+
}

0 commit comments

Comments
 (0)