Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit 9adcc5b

Browse files
committed
fix(sdtio-client) Escape any embedded newlines in the JSON message
Resolves #21
1 parent 05085b7 commit 9adcc5b

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

spring-ai-mcp-core/src/main/java/org/springframework/ai/mcp/client/stdio/StdioClientTransport.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ private void startOutboundProcessing() {
287287
if (message != null) {
288288
try {
289289
String jsonMessage = objectMapper.writeValueAsString(message);
290+
// Escape any embedded newlines in the JSON message as per spec:
291+
// https://spec.modelcontextprotocol.io/specification/basic/transports/#stdio
292+
// - Messages are delimited by newlines, and MUST NOT contain
293+
// embedded newlines.
294+
jsonMessage = jsonMessage.replace("\r\n", "\\n").replace("\n", "\\n").replace("\r", "\\n");
295+
290296
this.process.getOutputStream().write(jsonMessage.getBytes(StandardCharsets.UTF_8));
291297
this.process.getOutputStream().write("\n".getBytes(StandardCharsets.UTF_8));
292298
this.process.getOutputStream().flush();

0 commit comments

Comments
 (0)