fix(sdtio-client) Escape any embedded newlines in the JSON message

Resolves #21
This commit is contained in:
Christian Tzolov 2024-12-19 12:21:21 +01:00
commit 9adcc5b9c4

View file

@ -287,6 +287,12 @@ public class StdioClientTransport implements McpTransport {
if (message != null) {
try {
String jsonMessage = objectMapper.writeValueAsString(message);
// Escape any embedded newlines in the JSON message as per spec:
// https://spec.modelcontextprotocol.io/specification/basic/transports/#stdio
// - Messages are delimited by newlines, and MUST NOT contain
// embedded newlines.
jsonMessage = jsonMessage.replace("\r\n", "\\n").replace("\n", "\\n").replace("\r", "\\n");
this.process.getOutputStream().write(jsonMessage.getBytes(StandardCharsets.UTF_8));
this.process.getOutputStream().write("\n".getBytes(StandardCharsets.UTF_8));
this.process.getOutputStream().flush();