Skip to content

Commit f792d4d

Browse files
committed
Add test for UTF-8 encoded messages in STDIO transport
Verifies that multi-byte UTF-8 characters (Korean, emoji, accented Latin, Japanese) are correctly preserved through the STDIO inbound processing pipeline.
1 parent a4b1102 commit f792d4d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

mcp-test/src/test/java/io/modelcontextprotocol/server/transport/StdioServerTransportProviderTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,49 @@ void shouldHandleIncomingMessages() throws Exception {
135135
}).verifyComplete();
136136
}
137137

138+
@Test
139+
@SuppressWarnings("unchecked")
140+
void shouldHandleUtf8EncodedMessages() throws Exception {
141+
142+
String utf8Content = "한글 테스트 🎉 café 日本語";
143+
String jsonMessage = "{\"jsonrpc\":\"2.0\",\"method\":\"test\"," + "\"params\":{\"message\":\"" + utf8Content
144+
+ "\"},\"id\":1}\n";
145+
InputStream stream = new ByteArrayInputStream(jsonMessage.getBytes(StandardCharsets.UTF_8));
146+
147+
transportProvider = new StdioServerTransportProvider(McpJsonDefaults.getMapper(), stream, System.out);
148+
149+
AtomicReference<McpSchema.JSONRPCMessage> capturedMessage = new AtomicReference<>();
150+
CountDownLatch messageLatch = new CountDownLatch(1);
151+
152+
McpServerSession.Factory realSessionFactory = transport -> {
153+
McpServerSession session = mock(McpServerSession.class);
154+
when(session.handle(any())).thenAnswer(invocation -> {
155+
capturedMessage.set(invocation.getArgument(0));
156+
messageLatch.countDown();
157+
return Mono.empty();
158+
});
159+
when(session.closeGracefully()).thenReturn(Mono.empty());
160+
return session;
161+
};
162+
163+
transportProvider.setSessionFactory(realSessionFactory);
164+
165+
StepVerifier.create(Mono.fromCallable(() -> messageLatch.await(100, TimeUnit.SECONDS)).flatMap(success -> {
166+
if (!success) {
167+
return Mono.error(new AssertionError("Timeout waiting for message processing"));
168+
}
169+
return Mono.just(capturedMessage.get());
170+
})).assertNext(message -> {
171+
assertThat(message).isNotNull();
172+
assertThat(message).isInstanceOf(McpSchema.JSONRPCRequest.class);
173+
McpSchema.JSONRPCRequest request = (McpSchema.JSONRPCRequest) message;
174+
assertThat(request.method()).isEqualTo("test");
175+
assertThat(request.id()).isEqualTo(1);
176+
Map<String, Object> params = (Map<String, Object>) request.params();
177+
assertThat(params).containsEntry("message", utf8Content);
178+
}).verifyComplete();
179+
}
180+
138181
@Test
139182
void shouldNotifyClients() {
140183
// Set session factory

0 commit comments

Comments
 (0)