From 1bec3c60a0229df71933fdea7d5046702fc8cc4b Mon Sep 17 00:00:00 2001 From: isfzhang Date: Tue, 2 Dec 2025 01:16:39 +0800 Subject: [PATCH] fix: handle Windows CRLF in MCP client(#664) (#665) This change updates the MCP client stdio JSON decoder to correctly handle both Unix (`\n`) and Windows (`\r\n`) line endings. Fixes #664 Co-authored-by: qysz --- mcp/transport.go | 3 ++- mcp/transport_test.go | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/mcp/transport.go b/mcp/transport.go index d6359f5..ec57b38 100644 --- a/mcp/transport.go +++ b/mcp/transport.go @@ -380,7 +380,8 @@ func newIOConn(rwc io.ReadWriteCloser) *ioConn { var tr [1]byte if n, readErr := dec.Buffered().Read(tr[:]); n > 0 { // If read byte is not a newline, it is an error. - if tr[0] != '\n' { + // Support both Unix (\n) and Windows (\r\n) line endings. + if tr[0] != '\n' && tr[0] != '\r' { err = fmt.Errorf("invalid trailing data at the end of stream") } } else if readErr != nil && readErr != io.EOF { diff --git a/mcp/transport_test.go b/mcp/transport_test.go index aeff366..515b8c1 100644 --- a/mcp/transport_test.go +++ b/mcp/transport_test.go @@ -83,6 +83,11 @@ func TestIOConnRead(t *testing.T) { want: "", protocolVersion: "", }, + { + name: "windows newline at the end of first valid json input", + input: "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"test\",\"params\":{}}\r\n", + want: "", + }, { name: "batching old protocol", input: `[{"jsonrpc":"2.0","id":1,"method":"test1"},{"jsonrpc":"2.0","id":2,"method":"test2"}]`,