@@ -6,6 +6,7 @@ package mcp
66
77import (
88 "bytes"
9+ "cmp"
910 "context"
1011 "errors"
1112 "fmt"
@@ -773,6 +774,10 @@ type streamableClientConn struct {
773774 err error
774775}
775776
777+ func (c * streamableClientConn ) httpClient () * http.Client {
778+ return cmp .Or (c .authClient , c .opts .HTTPClient )
779+ }
780+
776781func (c * streamableClientConn ) setProtocolVersion (s string ) {
777782 c .mu .Lock ()
778783 defer c .mu .Unlock ()
@@ -865,22 +870,13 @@ func (s *streamableClientConn) postMessage(ctx context.Context, sessionID string
865870
866871 // Use an HTTP client that does authentication, if there is one.
867872 // Otherwise, use the one provided by the user.
868- client := s .authClient
869- if client == nil {
870- client = s .opts .HTTPClient
871- }
873+ client := s .httpClient ()
872874 // TODO: Resource Indicators, as in
873875 // https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization#resource-parameter-implementation
874876 resp , err := client .Do (req )
875877 if err != nil {
876878 return "" , err
877879 }
878- bodyClosed := false // avoid a second call to Close: undefined behavior (see [io.Closer])
879- defer func () {
880- if resp != nil && ! bodyClosed {
881- resp .Body .Close ()
882- }
883- }()
884880
885881 if resp .StatusCode == http .StatusUnauthorized {
886882 if client == s .authClient {
@@ -1055,7 +1051,7 @@ func (s *streamableClientConn) Close() error {
10551051 req .Header .Set (protocolVersionHeader , s .protocolVersion )
10561052 }
10571053 req .Header .Set (sessionIDHeader , s ._sessionID )
1058- if _ , err := s .opts . HTTPClient .Do (req ); err != nil {
1054+ if _ , err := s .httpClient () .Do (req ); err != nil {
10591055 s .closeErr = err
10601056 }
10611057 }
@@ -1081,7 +1077,7 @@ func (s *streamableClientConn) establishSSE(lastEventID string) (*http.Response,
10811077 }
10821078 req .Header .Set ("Accept" , "text/event-stream" )
10831079
1084- return s .opts . HTTPClient .Do (req )
1080+ return s .httpClient () .Do (req )
10851081}
10861082
10871083// calculateReconnectDelay calculates a delay using exponential backoff with full jitter.
0 commit comments