Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions mcp/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,18 @@ func scanEventsLimited(r io.Reader, maxEventSize int) iter.Seq2[Event, error] {
if b, a, found := bytes.Cut(line, []byte{':'}); found {
before, after = b, a
}
// A field value is everything after the colon, with at most one
// leading U+0020 removed; any other whitespace is part of the value.
value := bytes.TrimPrefix(after, []byte{' '})
switch {
case bytes.Equal(before, eventKey):
evt.Name = strings.TrimSpace(string(after))
evt.Name = string(value)
case bytes.Equal(before, idKey):
evt.ID = strings.TrimSpace(string(after))
evt.ID = string(value)
case bytes.Equal(before, retryKey):
evt.Retry = strings.TrimSpace(string(after))
evt.Retry = string(value)
case bytes.Equal(before, dataKey):
data := bytes.TrimSpace(after)
data := value
if dataBuf == nil {
dataBuf = new(bytes.Buffer)
} else {
Expand Down
23 changes: 23 additions & 0 deletions mcp/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ func TestScanEvents(t *testing.T) {
{Data: []byte("hello\n")},
},
},
{
// Per the SSE parsing rules, only a single leading space is
// removed from a field value, and no trailing whitespace.
name: "data values keep all but one leading space",
input: "data: {\"a\":\ndata: 1}\n\n",
want: []Event{
{Data: []byte("{\"a\":\n 1}")},
},
},
{
name: "data value keeps trailing whitespace",
input: "data: hello \n\n",
want: []Event{
{Data: []byte("hello ")},
},
},
{
name: "field values keep all but one leading space",
input: "event: message\nid: 1 \nretry: 500\ndata: x\n\n",
want: []Event{
{Name: " message", ID: "1 ", Retry: " 500", Data: []byte("x")},
},
},
}

for _, tt := range tests {
Expand Down
Loading