Skip to content

Commit 1bec3c6

Browse files
isfzhangqysz
andauthored
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 <[email protected]>
1 parent 2ce44d4 commit 1bec3c6

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

mcp/transport.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ func newIOConn(rwc io.ReadWriteCloser) *ioConn {
380380
var tr [1]byte
381381
if n, readErr := dec.Buffered().Read(tr[:]); n > 0 {
382382
// If read byte is not a newline, it is an error.
383-
if tr[0] != '\n' {
383+
// Support both Unix (\n) and Windows (\r\n) line endings.
384+
if tr[0] != '\n' && tr[0] != '\r' {
384385
err = fmt.Errorf("invalid trailing data at the end of stream")
385386
}
386387
} else if readErr != nil && readErr != io.EOF {

mcp/transport_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ func TestIOConnRead(t *testing.T) {
8383
want: "",
8484
protocolVersion: "",
8585
},
86+
{
87+
name: "windows newline at the end of first valid json input",
88+
input: "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"test\",\"params\":{}}\r\n",
89+
want: "",
90+
},
8691
{
8792
name: "batching old protocol",
8893
input: `[{"jsonrpc":"2.0","id":1,"method":"test1"},{"jsonrpc":"2.0","id":2,"method":"test2"}]`,

0 commit comments

Comments
 (0)