Skip to content

Commit 3c1003e

Browse files
committed
refactor: reloacted the fix after rebasing
1 parent e6cf5be commit 3c1003e

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

mcp/transport.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,19 @@ func newIOConn(rwc io.ReadWriteCloser) *ioConn {
308308
for {
309309
var raw json.RawMessage
310310
err := dec.Decode(&raw)
311+
// If decoding was successful, check for trailing data at the end of the stream.
312+
if err == nil {
313+
// Read the next byte to check if there is trailing data.
314+
var tr [1]byte
315+
if n, readErr := dec.Buffered().Read(tr[:]); n > 0 {
316+
// If read byte is not a newline, it is an error.
317+
if tr[0] != '\n' {
318+
err = fmt.Errorf("invalid trailing data at the end of stream")
319+
}
320+
} else if readErr != nil && readErr != io.EOF {
321+
err = readErr
322+
}
323+
}
311324
select {
312325
case incoming <- msgOrErr{msg: raw, err: err}:
313326
case <-closed:
@@ -421,17 +434,6 @@ func (t *ioConn) Read(ctx context.Context) (jsonrpc.Message, error) {
421434
return nil, io.EOF
422435
}
423436

424-
// Read the next byte to check if there is trailing data.
425-
var tr [1]byte
426-
if n, err := in.Buffered().Read(tr[:]); n > 0 {
427-
// If read byte is not a newline, it is an error.
428-
if tr[0] != '\n' {
429-
return nil, fmt.Errorf("invalid trailing data at the end of stream")
430-
}
431-
} else if err != nil && err != io.EOF {
432-
return nil, err
433-
}
434-
435437
msgs, batch, err := readBatch(raw)
436438
if err != nil {
437439
return nil, err

0 commit comments

Comments
 (0)