refactor: process output writing

This commit is contained in:
Christian Tzolov 2024-12-12 23:27:46 +01:00
commit 59319e5509

View file

@ -19,6 +19,7 @@ package org.springframework.ai.mcp.client.stdio;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -212,9 +213,10 @@ public class StdioServerTransport extends AbstractMcpTransport {
.handle((message, s) -> { .handle((message, s) -> {
if (message != null) { if (message != null) {
try { try {
this.process.outputWriter().write(objectMapper.writeValueAsString(message)); String jsonMessage = objectMapper.writeValueAsString(message);
this.process.outputWriter().newLine(); this.process.getOutputStream().write(jsonMessage.getBytes(StandardCharsets.UTF_8));
this.process.outputWriter().flush(); this.process.getOutputStream().write("\n".getBytes(StandardCharsets.UTF_8));
this.process.getOutputStream().flush();
s.next(message); s.next(message);
} }
catch (IOException e) { catch (IOException e) {