@@ -1130,7 +1130,7 @@ func (c *streamableClientConn) sessionUpdated(state clientSessionState) {
11301130 // § 2.5: A server using the Streamable HTTP transport MAY assign a session
11311131 // ID at initialization time, by including it in an Mcp-Session-Id header
11321132 // on the HTTP response containing the InitializeResult.
1133- go c .handleSSE (nil , true , nil )
1133+ go c .handleSSE ("hanging GET" , nil , true , nil )
11341134}
11351135
11361136// fail handles an asynchronous error while reading.
@@ -1224,24 +1224,27 @@ func (c *streamableClientConn) Write(ctx context.Context, msg jsonrpc.Message) e
12241224 return nil
12251225 }
12261226
1227+ var requestSummary string
1228+ switch msg := msg .(type ) {
1229+ case * jsonrpc.Request :
1230+ requestSummary = fmt .Sprintf ("sending %q" , msg .Method )
1231+ case * jsonrpc.Response :
1232+ requestSummary = fmt .Sprintf ("sending jsonrpc response #%d" , msg .ID )
1233+ default :
1234+ panic ("unreachable" )
1235+ }
1236+
12271237 switch ct := resp .Header .Get ("Content-Type" ); ct {
12281238 case "application/json" :
1229- go c .handleJSON (resp )
1239+ go c .handleJSON (requestSummary , resp )
12301240
12311241 case "text/event-stream" :
12321242 jsonReq , _ := msg .(* jsonrpc.Request )
1233- go c .handleSSE (resp , false , jsonReq )
1243+ go c .handleSSE (requestSummary , resp , false , jsonReq )
12341244
12351245 default :
12361246 resp .Body .Close ()
1237- switch msg := msg .(type ) {
1238- case * jsonrpc.Request :
1239- return fmt .Errorf ("unsupported content type %q when sending %q (status: %d)" , ct , msg .Method , resp .StatusCode )
1240- case * jsonrpc.Response :
1241- return fmt .Errorf ("unsupported content type %q when sending jsonrpc response #%d (status: %d)" , ct , msg .ID , resp .StatusCode )
1242- default :
1243- panic ("unreachable" )
1244- }
1247+ return fmt .Errorf ("%s: unsupported content type %q" , requestSummary , ct )
12451248 }
12461249 return nil
12471250}
@@ -1265,16 +1268,16 @@ func (c *streamableClientConn) setMCPHeaders(req *http.Request) {
12651268 }
12661269}
12671270
1268- func (c * streamableClientConn ) handleJSON (resp * http.Response ) {
1271+ func (c * streamableClientConn ) handleJSON (requestSummary string , resp * http.Response ) {
12691272 body , err := io .ReadAll (resp .Body )
12701273 resp .Body .Close ()
12711274 if err != nil {
1272- c .fail (err )
1275+ c .fail (fmt . Errorf ( "%s: failed to read body: %v" , requestSummary , err ) )
12731276 return
12741277 }
12751278 msg , err := jsonrpc .DecodeMessage (body )
12761279 if err != nil {
1277- c .fail (fmt .Errorf ("failed to decode response: %v" , err ))
1280+ c .fail (fmt .Errorf ("%s: failed to decode response: %v" , requestSummary , err ))
12781281 return
12791282 }
12801283 select {
@@ -1289,12 +1292,12 @@ func (c *streamableClientConn) handleJSON(resp *http.Response) {
12891292//
12901293// If forReq is set, it is the request that initiated the stream, and the
12911294// stream is complete when we receive its response.
1292- func (c * streamableClientConn ) handleSSE (initialResp * http.Response , persistent bool , forReq * jsonrpc2.Request ) {
1295+ func (c * streamableClientConn ) handleSSE (requestSummary string , initialResp * http.Response , persistent bool , forReq * jsonrpc2.Request ) {
12931296 resp := initialResp
12941297 var lastEventID string
12951298 for {
12961299 if resp != nil {
1297- eventID , clientClosed := c .processStream (resp , forReq )
1300+ eventID , clientClosed := c .processStream (requestSummary , resp , forReq )
12981301 lastEventID = eventID
12991302
13001303 // If the connection was closed by the client, we're done.
@@ -1312,7 +1315,7 @@ func (c *streamableClientConn) handleSSE(initialResp *http.Response, persistent
13121315 newResp , err := c .reconnect (lastEventID )
13131316 if err != nil {
13141317 // All reconnection attempts failed: fail the connection.
1315- c .fail (err )
1318+ c .fail (fmt . Errorf ( "%s: failed to reconnect: %v" , requestSummary , err ) )
13161319 return
13171320 }
13181321 resp = newResp
@@ -1323,7 +1326,7 @@ func (c *streamableClientConn) handleSSE(initialResp *http.Response, persistent
13231326 }
13241327 if resp .StatusCode < 200 || resp .StatusCode >= 300 {
13251328 resp .Body .Close ()
1326- c .fail (fmt .Errorf ("failed to reconnect: %v" , http .StatusText (resp .StatusCode )))
1329+ c .fail (fmt .Errorf ("%s: failed to reconnect: %v" , requestSummary , http .StatusText (resp .StatusCode )))
13271330 return
13281331 }
13291332 // Reconnection was successful. Continue the loop with the new response.
@@ -1334,7 +1337,7 @@ func (c *streamableClientConn) handleSSE(initialResp *http.Response, persistent
13341337// incoming channel. It returns the ID of the last processed event and a flag
13351338// indicating if the connection was closed by the client. If resp is nil, it
13361339// returns "", false.
1337- func (c * streamableClientConn ) processStream (resp * http.Response , forReq * jsonrpc.Request ) (lastEventID string , clientClosed bool ) {
1340+ func (c * streamableClientConn ) processStream (requestSummary string , resp * http.Response , forReq * jsonrpc.Request ) (lastEventID string , clientClosed bool ) {
13381341 defer resp .Body .Close ()
13391342 for evt , err := range scanEvents (resp .Body ) {
13401343 if err != nil {
@@ -1347,7 +1350,7 @@ func (c *streamableClientConn) processStream(resp *http.Response, forReq *jsonrp
13471350
13481351 msg , err := jsonrpc .DecodeMessage (evt .Data )
13491352 if err != nil {
1350- c .fail (fmt .Errorf ("failed to decode event: %v" , err ))
1353+ c .fail (fmt .Errorf ("%s: failed to decode event: %v" , requestSummary , err ))
13511354 return "" , true
13521355 }
13531356
0 commit comments