refactor: simplify StdioServerTransport to only use System streams

Removes custom stream injection capability from StdioServerTransport and
simplifies it to only use System.in/out.
Updates related tests to use System streams instead of piped streams.
This commit is contained in:
Christian Tzolov 2024-12-30 11:52:43 +01:00
commit fcf0ff0ae2
3 changed files with 34 additions and 115 deletions

View file

@ -16,8 +16,6 @@
package org.springframework.ai.mcp.server;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.mcp.server.transport.StdioServerTransport;
import org.springframework.ai.mcp.spec.McpTransport;
@ -30,7 +28,7 @@ class StdioMcpAsyncServerTests extends AbstractMcpAsyncServerTests {
@Override
protected McpTransport createMcpTransport() {
return new StdioServerTransport(new ObjectMapper());
return new StdioServerTransport();
}
}

View file

@ -16,11 +16,6 @@
package org.springframework.ai.mcp.server;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.mcp.server.transport.StdioServerTransport;
import org.springframework.ai.mcp.spec.McpTransport;
@ -31,53 +26,9 @@ import org.springframework.ai.mcp.spec.McpTransport;
*/
class StdioMcpSyncServerTests extends AbstractMcpSyncServerTests {
private PipedInputStream testInput;
private PipedOutputStream testOutput;
private PipedOutputStream writeToInput;
private PipedInputStream readFromOutput;
@Override
protected McpTransport createMcpTransport() {
try {
testInput = new PipedInputStream();
writeToInput = new PipedOutputStream(testInput);
testOutput = new PipedOutputStream();
readFromOutput = new PipedInputStream(testOutput);
return new StdioServerTransport(new ObjectMapper(), testInput, testOutput);
}
catch (Exception e) {
throw new RuntimeException("Failed to initialize test streams", e);
}
}
@Override
protected void onStart() {
// No special setup needed for stdio transport
}
@Override
protected void onClose() {
try {
if (testInput != null) {
testInput.close();
}
if (testOutput != null) {
testOutput.close();
}
if (writeToInput != null) {
writeToInput.close();
}
if (readFromOutput != null) {
readFromOutput.close();
}
}
catch (Exception e) {
// Log but don't throw since this is cleanup
e.printStackTrace();
}
return new StdioServerTransport();
}
}

View file

@ -92,7 +92,7 @@ class StdioServerTransportTests {
testIn = new ByteArrayInputStream((jsonMessage + "\n").getBytes(StandardCharsets.UTF_8));
// Create transport with test streams
transport = new StdioServerTransport(objectMapper, testIn, testOutPrintStream);
transport = new StdioServerTransport(objectMapper);
// Parse expected message
McpSchema.JSONRPCRequest expected = objectMapper.readValue(jsonMessage, McpSchema.JSONRPCRequest.class);
@ -109,9 +109,9 @@ class StdioServerTransportTests {
@Disabled
void shouldHandleOutgoingMessages() throws Exception {
// Create transport with test streams
transport = new StdioServerTransport(objectMapper, new BlockingInputStream(), testOutPrintStream);
// transport = new StdioServerTransport(objectMapper, new ByteArrayInputStream(new
// byte[0]), testOutPrintStream);
transport = new StdioServerTransport(objectMapper);
// transport = new StdioServerTransport(objectMapper, new BlockingInputStream(),
// testOutPrintStream);
// Create test messages
JSONRPCRequest initMessage = new JSONRPCRequest(McpSchema.JSONRPC_VERSION, "init", "init-id",
@ -137,7 +137,7 @@ class StdioServerTransportTests {
@Test
void shouldWaitForProcessorsBeforeSendingMessage() {
// Create transport with test streams
transport = new StdioServerTransport(objectMapper, new ByteArrayInputStream(new byte[0]), testOutPrintStream);
transport = new StdioServerTransport(objectMapper);
// Create test message
JSONRPCRequest testMessage = new JSONRPCRequest(McpSchema.JSONRPC_VERSION, "test", "test-id",
@ -154,7 +154,7 @@ class StdioServerTransportTests {
@Test
void shouldCloseGracefully() {
// Create transport with test streams
transport = new StdioServerTransport(objectMapper, new ByteArrayInputStream(new byte[0]), testOutPrintStream);
transport = new StdioServerTransport(objectMapper);
// Create test message
JSONRPCRequest initMessage = new JSONRPCRequest(McpSchema.JSONRPC_VERSION, "init", "init-id",