@@ -133,7 +133,7 @@ func TestStreamableTransports(t *testing.T) {
133133 defer session .Close ()
134134 sid := session .ID ()
135135 if sid == "" {
136- t .Error ("empty session ID" )
136+ t .Fatalf ("empty session ID" )
137137 }
138138 if g , w := session .mcpConn .(* streamableClientConn ).initializedResult .ProtocolVersion , latestProtocolVersion ; g != w {
139139 t .Fatalf ("got protocol version %q, want %q" , g , w )
@@ -475,6 +475,8 @@ func resp(id int64, result any, err error) *jsonrpc.Response {
475475 }
476476}
477477
478+ var ()
479+
478480func TestStreamableServerTransport (t * testing.T ) {
479481 // This test checks detailed behavior of the streamable server transport, by
480482 // faking the behavior of a streamable client using a sequence of HTTP
@@ -502,7 +504,6 @@ func TestStreamableServerTransport(t *testing.T) {
502504 method : "POST" ,
503505 messages : []jsonrpc.Message {initializedMsg },
504506 wantStatusCode : http .StatusAccepted ,
505- wantSessionID : false , // TODO: should this be true?
506507 }
507508
508509 tests := []struct {
@@ -520,7 +521,6 @@ func TestStreamableServerTransport(t *testing.T) {
520521 messages : []jsonrpc.Message {req (2 , "tools/call" , & CallToolParams {Name : "tool" })},
521522 wantStatusCode : http .StatusOK ,
522523 wantMessages : []jsonrpc.Message {resp (2 , & CallToolResult {}, nil )},
523- wantSessionID : true ,
524524 },
525525 },
526526 },
@@ -535,30 +535,26 @@ func TestStreamableServerTransport(t *testing.T) {
535535 headers : http.Header {"Accept" : {"text/plain" , "application/*" }},
536536 messages : []jsonrpc.Message {req (3 , "tools/call" , & CallToolParams {Name : "tool" })},
537537 wantStatusCode : http .StatusBadRequest , // missing text/event-stream
538- wantSessionID : false ,
539538 },
540539 {
541540 method : "POST" ,
542541 headers : http.Header {"Accept" : {"text/event-stream" }},
543542 messages : []jsonrpc.Message {req (3 , "tools/call" , & CallToolParams {Name : "tool" })},
544543 wantStatusCode : http .StatusBadRequest , // missing application/json
545- wantSessionID : false ,
546544 },
547545 {
548546 method : "POST" ,
549547 headers : http.Header {"Accept" : {"text/plain" , "*/*" }},
550548 messages : []jsonrpc.Message {req (4 , "tools/call" , & CallToolParams {Name : "tool" })},
551549 wantStatusCode : http .StatusOK ,
552550 wantMessages : []jsonrpc.Message {resp (4 , & CallToolResult {}, nil )},
553- wantSessionID : true ,
554551 },
555552 {
556553 method : "POST" ,
557554 headers : http.Header {"Accept" : {"text/*, application/*" }},
558555 messages : []jsonrpc.Message {req (4 , "tools/call" , & CallToolParams {Name : "tool" })},
559556 wantStatusCode : http .StatusOK ,
560557 wantMessages : []jsonrpc.Message {resp (4 , & CallToolResult {}, nil )},
561- wantSessionID : true ,
562558 },
563559 },
564560 },
@@ -598,7 +594,6 @@ func TestStreamableServerTransport(t *testing.T) {
598594 req (0 , "notifications/progress" , & ProgressNotificationParams {}),
599595 resp (2 , & CallToolResult {}, nil ),
600596 },
601- wantSessionID : true ,
602597 },
603598 },
604599 },
@@ -620,7 +615,6 @@ func TestStreamableServerTransport(t *testing.T) {
620615 resp (1 , & ListRootsResult {}, nil ),
621616 },
622617 wantStatusCode : http .StatusAccepted ,
623- wantSessionID : false ,
624618 },
625619 {
626620 method : "POST" ,
@@ -632,7 +626,6 @@ func TestStreamableServerTransport(t *testing.T) {
632626 req (1 , "roots/list" , & ListRootsParams {}),
633627 resp (2 , & CallToolResult {}, nil ),
634628 },
635- wantSessionID : true ,
636629 },
637630 },
638631 },
@@ -663,7 +656,6 @@ func TestStreamableServerTransport(t *testing.T) {
663656 resp (1 , & ListRootsResult {}, nil ),
664657 },
665658 wantStatusCode : http .StatusAccepted ,
666- wantSessionID : false ,
667659 },
668660 {
669661 method : "GET" ,
@@ -674,7 +666,6 @@ func TestStreamableServerTransport(t *testing.T) {
674666 req (0 , "notifications/progress" , & ProgressNotificationParams {}),
675667 req (1 , "roots/list" , & ListRootsParams {}),
676668 },
677- wantSessionID : true ,
678669 },
679670 {
680671 method : "POST" ,
@@ -685,7 +676,6 @@ func TestStreamableServerTransport(t *testing.T) {
685676 wantMessages : []jsonrpc.Message {
686677 resp (2 , & CallToolResult {}, nil ),
687678 },
688- wantSessionID : true ,
689679 },
690680 {
691681 method : "DELETE" ,
@@ -724,7 +714,6 @@ func TestStreamableServerTransport(t *testing.T) {
724714 wantMessages : []jsonrpc.Message {resp (2 , nil , & jsonrpc2.WireError {
725715 Message : `method "tools/call" is invalid during session initialization` ,
726716 })},
727- wantSessionID : true , // TODO: this is probably wrong; we don't have a valid session
728717 },
729718 },
730719 },
@@ -951,7 +940,7 @@ func (s streamableRequest) do(ctx context.Context, serverURL, sessionID string,
951940 return "" , 0 , nil , fmt .Errorf ("creating request: %w" , err )
952941 }
953942 if sessionID != "" {
954- req .Header .Set ("Mcp-Session-Id" , sessionID )
943+ req .Header .Set (sessionIDHeader , sessionID )
955944 }
956945 req .Header .Set ("Content-Type" , "application/json" )
957946 req .Header .Set ("Accept" , "application/json, text/event-stream" )
@@ -963,7 +952,7 @@ func (s streamableRequest) do(ctx context.Context, serverURL, sessionID string,
963952 }
964953 defer resp .Body .Close ()
965954
966- newSessionID := resp .Header .Get ("Mcp-Session-Id" )
955+ newSessionID := resp .Header .Get (sessionIDHeader )
967956
968957 contentType := resp .Header .Get ("Content-Type" )
969958 var respBody []byte
@@ -1079,6 +1068,15 @@ func TestEventID(t *testing.T) {
10791068}
10801069
10811070func TestStreamableStateless (t * testing.T ) {
1071+ initReq := req (1 , methodInitialize , & InitializeParams {})
1072+ initResp := resp (1 , & InitializeResult {
1073+ Capabilities : & ServerCapabilities {
1074+ Logging : & LoggingCapabilities {},
1075+ Tools : & ToolCapabilities {ListChanged : true },
1076+ },
1077+ ProtocolVersion : latestProtocolVersion ,
1078+ ServerInfo : & Implementation {Name : "test" , Version : "v1.0.0" },
1079+ }, nil )
10821080 // This version of sayHi expects
10831081 // that request from our client).
10841082 sayHi := func (ctx context.Context , req * CallToolRequest , args hiParams ) (* CallToolResult , any , error ) {
@@ -1092,17 +1090,22 @@ func TestStreamableStateless(t *testing.T) {
10921090 AddTool (server , & Tool {Name : "greet" , Description : "say hi" }, sayHi )
10931091
10941092 requests := []streamableRequest {
1093+ {
1094+ method : "POST" ,
1095+ messages : []jsonrpc.Message {initReq },
1096+ wantStatusCode : http .StatusOK ,
1097+ wantMessages : []jsonrpc.Message {initResp },
1098+ wantSessionID : false , // sessionless
1099+ },
10951100 {
10961101 method : "POST" ,
10971102 wantStatusCode : http .StatusOK ,
10981103 messages : []jsonrpc.Message {req (1 , "tools/list" , struct {}{})},
10991104 wantBodyContaining : "greet" ,
1100- wantSessionID : false ,
11011105 },
11021106 {
11031107 method : "GET" ,
11041108 wantStatusCode : http .StatusMethodNotAllowed ,
1105- wantSessionID : false ,
11061109 },
11071110 {
11081111 method : "POST" ,
@@ -1116,7 +1119,6 @@ func TestStreamableStateless(t *testing.T) {
11161119 StructuredContent : json .RawMessage ("null" ),
11171120 }, nil ),
11181121 },
1119- wantSessionID : false ,
11201122 },
11211123 {
11221124 method : "POST" ,
@@ -1130,7 +1132,6 @@ func TestStreamableStateless(t *testing.T) {
11301132 StructuredContent : json .RawMessage ("null" ),
11311133 }, nil ),
11321134 },
1133- wantSessionID : false ,
11341135 },
11351136 }
11361137
@@ -1166,13 +1167,7 @@ func TestStreamableStateless(t *testing.T) {
11661167 //
11671168 // This can be used by tools to look up application state preserved across
11681169 // subsequent requests.
1169- for i , req := range requests {
1170- // Now, we want a session for all (valid) requests.
1171- if req .wantStatusCode != http .StatusMethodNotAllowed {
1172- req .wantSessionID = true
1173- }
1174- requests [i ] = req
1175- }
1170+ requests [0 ].wantSessionID = true // now expect a session ID for initialize
11761171 statelessHandler := NewStreamableHTTPHandler (func (* http.Request ) * Server { return server }, & StreamableHTTPOptions {
11771172 Stateless : true ,
11781173 })
0 commit comments