|
| 1 | +/* |
| 2 | + * Copyright 2024-2024 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.server.transport; |
| 6 | + |
| 7 | +import java.io.OutputStream; |
| 8 | +import java.io.PrintStream; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.concurrent.CountDownLatch; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | + |
| 14 | +import io.modelcontextprotocol.json.McpJsonDefaults; |
| 15 | +import io.modelcontextprotocol.spec.McpSchema; |
| 16 | +import io.modelcontextprotocol.spec.McpServerSession; |
| 17 | +import reactor.core.publisher.Mono; |
| 18 | + |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +/** |
| 24 | + * Minimal STDIO server process for testing UTF-8 encoding behavior. |
| 25 | + * |
| 26 | + * <p> |
| 27 | + * This class is spawned as a subprocess with {@code -Dfile.encoding=ISO-8859-1} to |
| 28 | + * simulate a non-UTF-8 default charset environment. It uses |
| 29 | + * {@link StdioServerTransportProvider} to read a JSON-RPC message from stdin and echoes |
| 30 | + * the received {@code params.message} value back to stdout, allowing the parent test to |
| 31 | + * verify that multi-byte UTF-8 characters are preserved regardless of the JVM default |
| 32 | + * charset. |
| 33 | + * |
| 34 | + * @see StdioServerTransportProviderTests#shouldHandleUtf8MessagesWithNonUtf8DefaultCharset |
| 35 | + */ |
| 36 | +public class StdioUtf8TestServer { |
| 37 | + |
| 38 | + @SuppressWarnings("unchecked") |
| 39 | + public static void main(String[] args) throws Exception { |
| 40 | + // Capture the original stdout for echoing the result later |
| 41 | + PrintStream originalOut = System.out; |
| 42 | + |
| 43 | + // Redirect System.out to stderr so that logger output does not |
| 44 | + // interfere with the test result written to stdout |
| 45 | + System.setOut(new PrintStream(System.err, true)); |
| 46 | + |
| 47 | + CountDownLatch messageLatch = new CountDownLatch(1); |
| 48 | + StringBuilder receivedMessage = new StringBuilder(); |
| 49 | + |
| 50 | + StdioServerTransportProvider transportProvider = new StdioServerTransportProvider(McpJsonDefaults.getMapper(), |
| 51 | + System.in, OutputStream.nullOutputStream()); |
| 52 | + |
| 53 | + McpServerSession.Factory sessionFactory = transport -> { |
| 54 | + McpServerSession session = mock(McpServerSession.class); |
| 55 | + when(session.handle(any())).thenAnswer(invocation -> { |
| 56 | + McpSchema.JSONRPCMessage msg = invocation.getArgument(0); |
| 57 | + if (msg instanceof McpSchema.JSONRPCRequest request) { |
| 58 | + Map<String, Object> params = (Map<String, Object>) request.params(); |
| 59 | + receivedMessage.append(params.get("message")); |
| 60 | + } |
| 61 | + messageLatch.countDown(); |
| 62 | + return Mono.empty(); |
| 63 | + }); |
| 64 | + when(session.closeGracefully()).thenReturn(Mono.empty()); |
| 65 | + return session; |
| 66 | + }; |
| 67 | + |
| 68 | + // Start processing stdin |
| 69 | + transportProvider.setSessionFactory(sessionFactory); |
| 70 | + |
| 71 | + // Wait for the message to be processed |
| 72 | + if (messageLatch.await(10, TimeUnit.SECONDS)) { |
| 73 | + // Write the received message to the original stdout in UTF-8 |
| 74 | + originalOut.write(receivedMessage.toString().getBytes(StandardCharsets.UTF_8)); |
| 75 | + originalOut.write('\n'); |
| 76 | + originalOut.flush(); |
| 77 | + } |
| 78 | + |
| 79 | + transportProvider.closeGracefully().block(java.time.Duration.ofSeconds(5)); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments