Skip to content

Commit 551e121

Browse files
committed
mcp: changed default StreamID's to empty starting
- Changed the default StreamID to StreamID("") - Added a test for empty streamID's in TestEventID - Removed the no-op change in streamable_test
1 parent 2ad24b9 commit 551e121

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

mcp/streamable.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,13 @@ func NewStreamableServerTransport(sessionID string, opts *StreamableServerTransp
176176
opts = &StreamableServerTransportOptions{}
177177
}
178178
t := &StreamableServerTransport{
179-
sessionID: sessionID,
180-
incoming: make(chan jsonrpc.Message, 10),
181-
done: make(chan struct{}),
182-
streams: make(map[StreamID]*stream),
183-
requestStreams: make(map[jsonrpc.ID]StreamID),
184-
defaultStreamID: StreamID(randText()),
185-
}
186-
t.streams[t.defaultStreamID] = newStream(t.defaultStreamID)
179+
sessionID: sessionID,
180+
incoming: make(chan jsonrpc.Message, 10),
181+
done: make(chan struct{}),
182+
streams: make(map[StreamID]*stream),
183+
requestStreams: make(map[jsonrpc.ID]StreamID),
184+
}
185+
t.streams[StreamID("")] = newStream(StreamID(""))
187186
if opts != nil {
188187
t.opts = *opts
189188
}
@@ -200,11 +199,10 @@ func (t *StreamableServerTransport) SessionID() string {
200199
// A StreamableServerTransport implements the [Transport] interface for a
201200
// single session.
202201
type StreamableServerTransport struct {
203-
sessionID string
204-
defaultStreamID StreamID
205-
opts StreamableServerTransportOptions
206-
incoming chan jsonrpc.Message // messages from the client to the server
207-
done chan struct{}
202+
sessionID string
203+
opts StreamableServerTransportOptions
204+
incoming chan jsonrpc.Message // messages from the client to the server
205+
done chan struct{}
208206

209207
mu sync.Mutex
210208
// Sessions are closed exactly once.
@@ -283,8 +281,7 @@ func signalChanPtr() *chan struct{} {
283281
return &c
284282
}
285283

286-
// A StreamID identifies a stream of SSE events. It is a random string
287-
// 26 characters in length
284+
// A StreamID identifies a stream of SSE events. It is globally unique.
288285
// [ServerSession].
289286
type StreamID string
290287

@@ -339,8 +336,8 @@ func (t *StreamableServerTransport) ServeHTTP(w http.ResponseWriter, req *http.R
339336
}
340337

341338
func (t *StreamableServerTransport) serveGET(w http.ResponseWriter, req *http.Request) (int, string) {
342-
// connID = t.defaultStreamID corresponds to the default GET request.
343-
id := t.defaultStreamID
339+
// connID = "" corresponds to the default GET request.
340+
id := StreamID("")
344341
// By default, we haven't seen a last index. Since indices start at 0, we represent
345342
// that by -1. This is incremented just before each event is written, in streamResponse
346343
// around L407.
@@ -547,9 +544,6 @@ func parseEventID(eventID string) (sid StreamID, idx int, ok bool) {
547544
return "", 0, false
548545
}
549546
stream := StreamID(parts[0])
550-
if len(stream) == 0 {
551-
return "", 0, false
552-
}
553547
idx, err := strconv.Atoi(parts[1])
554548
if err != nil || idx < 0 {
555549
return "", 0, false
@@ -614,7 +608,7 @@ func (t *StreamableServerTransport) Write(ctx context.Context, msg jsonrpc.Messa
614608

615609
stream := t.streams[forConn]
616610
if forConn == "" {
617-
stream = t.streams[t.defaultStreamID]
611+
stream = t.streams[""]
618612
}
619613
if stream == nil {
620614
return fmt.Errorf("no stream with ID %s", forConn)
@@ -624,7 +618,7 @@ func (t *StreamableServerTransport) Write(ctx context.Context, msg jsonrpc.Messa
624618
// done. This is a sequencing violation from the server, so we should report
625619
// a side-channel error here. Put the message on the general queue to avoid
626620
// dropping messages.
627-
stream = t.streams[t.defaultStreamID]
621+
stream = t.streams[""]
628622
}
629623

630624
// TODO: if there is nothing to send these messages to (as would happen, for example, if forConn == ""

mcp/streamable_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ func TestEventID(t *testing.T) {
773773
{"0", 1},
774774
{"1", 0},
775775
{"1", 1},
776+
{"", 1},
776777
{"1234", 5678},
777778
}
778779

@@ -793,7 +794,6 @@ func TestEventID(t *testing.T) {
793794
"",
794795
"_",
795796
"1_",
796-
"_1",
797797
"1_a",
798798
"1_-1",
799799
}

0 commit comments

Comments
 (0)