mcp/streamable: remove StreamableReconnectOptions (#319)

Moves MaxRetries into the parent struct and assume a negative integer
signifies to not retry.

Fixes #308
This commit is contained in:
Sam Thanawalla 2025-08-18 13:53:34 -04:00 • committed by GitHub
commit 767dacb820
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 43 deletions

View file

@ -849,26 +849,15 @@ func (c *streamableServerConn) Close() error {
// endpoint serving the streamable HTTP transport defined by the 2025-03-26 // endpoint serving the streamable HTTP transport defined by the 2025-03-26
// version of the spec. // version of the spec.
type StreamableClientTransport struct { type StreamableClientTransport struct {
Endpoint string Endpoint string
HTTPClient *http.Client HTTPClient *http.Client
ReconnectOptions *StreamableReconnectOptions
}
// StreamableReconnectOptions defines parameters for client reconnect attempts.
type StreamableReconnectOptions struct {
// MaxRetries is the maximum number of times to attempt a reconnect before giving up. // MaxRetries is the maximum number of times to attempt a reconnect before giving up.
// A value of 0 or less means never retry. // It defaults to 5. To disable retries, use a negative number.
MaxRetries int MaxRetries int
} }
// DefaultReconnectOptions provides sensible defaults for reconnect logic.
var DefaultReconnectOptions = &StreamableReconnectOptions{
MaxRetries: 5,
}
// These settings are not (yet) exposed to the user in // These settings are not (yet) exposed to the user in
// StreamableReconnectOptions. Since they're invisible, keep them const rather // StreamableClientTransport.
// than requiring the user to start from DefaultReconnectOptions and mutate.
const ( const (
// reconnectGrowFactor is the multiplicative factor by which the delay increases after each attempt. // reconnectGrowFactor is the multiplicative factor by which the delay increases after each attempt.
// A value of 1.0 results in a constant delay, while a value of 2.0 would double it each time. // A value of 1.0 results in a constant delay, while a value of 2.0 would double it each time.
@ -887,8 +876,10 @@ const (
type StreamableClientTransportOptions struct { type StreamableClientTransportOptions struct {
// HTTPClient is the client to use for making HTTP requests. If nil, // HTTPClient is the client to use for making HTTP requests. If nil,
// http.DefaultClient is used. // http.DefaultClient is used.
HTTPClient *http.Client HTTPClient *http.Client
ReconnectOptions *StreamableReconnectOptions // MaxRetries is the maximum number of times to attempt a reconnect before giving up.
// It defaults to 5. To disable retries, use a negative number.
MaxRetries int
} }
// NewStreamableClientTransport returns a new client transport that connects to // NewStreamableClientTransport returns a new client transport that connects to
@ -901,7 +892,7 @@ func NewStreamableClientTransport(url string, opts *StreamableClientTransportOpt
t := &StreamableClientTransport{Endpoint: url} t := &StreamableClientTransport{Endpoint: url}
if opts != nil { if opts != nil {
t.HTTPClient = opts.HTTPClient t.HTTPClient = opts.HTTPClient
t.ReconnectOptions = opts.ReconnectOptions t.MaxRetries = opts.MaxRetries
} }
return t return t
} }
@ -919,34 +910,36 @@ func (t *StreamableClientTransport) Connect(ctx context.Context) (Connection, er
if client == nil { if client == nil {
client = http.DefaultClient client = http.DefaultClient
} }
reconnOpts := t.ReconnectOptions maxRetries := t.MaxRetries
if reconnOpts == nil { if maxRetries == 0 {
reconnOpts = DefaultReconnectOptions maxRetries = 5
} else if maxRetries < 0 {
maxRetries = 0
} }
// Create a new cancellable context that will manage the connection's lifecycle. // Create a new cancellable context that will manage the connection's lifecycle.
// This is crucial for cleanly shutting down the background SSE listener by // This is crucial for cleanly shutting down the background SSE listener by
// cancelling its blocking network operations, which prevents hangs on exit. // cancelling its blocking network operations, which prevents hangs on exit.
connCtx, cancel := context.WithCancel(context.Background()) connCtx, cancel := context.WithCancel(context.Background())
conn := &streamableClientConn{ conn := &streamableClientConn{
url: t.Endpoint, url: t.Endpoint,
client: client, client: client,
incoming: make(chan jsonrpc.Message, 10), incoming: make(chan jsonrpc.Message, 10),
done: make(chan struct{}), done: make(chan struct{}),
ReconnectOptions: reconnOpts, maxRetries: maxRetries,
ctx: connCtx, ctx: connCtx,
cancel: cancel, cancel: cancel,
failed: make(chan struct{}), failed: make(chan struct{}),
} }
return conn, nil return conn, nil
} }
type streamableClientConn struct { type streamableClientConn struct {
url string url string
ReconnectOptions *StreamableReconnectOptions client *http.Client
client *http.Client ctx context.Context
ctx context.Context cancel context.CancelFunc
cancel context.CancelFunc incoming chan jsonrpc.Message
incoming chan jsonrpc.Message maxRetries int
// Guard calls to Close, as it may be called multiple times. // Guard calls to Close, as it may be called multiple times.
closeOnce sync.Once closeOnce sync.Once
@ -1222,7 +1215,7 @@ func (c *streamableClientConn) reconnect(lastEventID string) (*http.Response, er
attempt = 1 attempt = 1
} }
for ; attempt <= c.ReconnectOptions.MaxRetries; attempt++ { for ; attempt <= c.maxRetries; attempt++ {
select { select {
case <-c.done: case <-c.done:
return nil, fmt.Errorf("connection closed by client during reconnect") return nil, fmt.Errorf("connection closed by client during reconnect")
@ -1244,9 +1237,9 @@ func (c *streamableClientConn) reconnect(lastEventID string) (*http.Response, er
} }
// If the loop completes, all retries have failed. // If the loop completes, all retries have failed.
if finalErr != nil { if finalErr != nil {
return nil, fmt.Errorf("connection failed after %d attempts: %w", c.ReconnectOptions.MaxRetries, finalErr) return nil, fmt.Errorf("connection failed after %d attempts: %w", c.maxRetries, finalErr)
} }
return nil, fmt.Errorf("connection failed after %d attempts", c.ReconnectOptions.MaxRetries) return nil, fmt.Errorf("connection failed after %d attempts", c.maxRetries)
} }
// isResumable checks if an HTTP response indicates a valid SSE stream that can be processed. // isResumable checks if an HTTP response indicates a valid SSE stream that can be processed.

View file

@ -193,8 +193,8 @@ func TestStreamableTransports(t *testing.T) {
// outage. // outage.
func TestClientReplay(t *testing.T) { func TestClientReplay(t *testing.T) {
for _, test := range []clientReplayTest{ for _, test := range []clientReplayTest{
{"default", nil, true}, {"default", 0, true},
{"no retries", &StreamableReconnectOptions{}, false}, {"no retries", -1, false},
} { } {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
testClientReplay(t, test) testClientReplay(t, test)
@ -204,7 +204,7 @@ func TestClientReplay(t *testing.T) {
type clientReplayTest struct { type clientReplayTest struct {
name string name string
options *StreamableReconnectOptions maxRetries int
wantRecovered bool wantRecovered bool
} }
@ -258,8 +258,8 @@ func testClientReplay(t *testing.T, test clientReplayTest) {
}, },
}) })
clientSession, err := client.Connect(ctx, &StreamableClientTransport{ clientSession, err := client.Connect(ctx, &StreamableClientTransport{
Endpoint: proxy.URL, Endpoint: proxy.URL,
ReconnectOptions: test.options, MaxRetries: test.maxRetries,
}, nil) }, nil)
if err != nil { if err != nil {
t.Fatalf("client.Connect() failed: %v", err) t.Fatalf("client.Connect() failed: %v", err)