Skip to content

Commit

Permalink
fix(sdtio-client) Escape any embedded newlines in the JSON message
Browse files Browse the repository at this point in the history
Resolves #21
  • Loading branch information
tzolov committed Dec 19, 2024
1 parent 05085b7 commit 9adcc5b
Showing 1 changed file with 6 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ private void startOutboundProcessing() {
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();
Expand Down

0 comments on commit 9adcc5b

Please sign in to comment.