go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

mcp: SSEClientTransport ties the event stream to the context passed to Connect

Open chaholl opened this issue 2 months ago • 3 comments

Describe the bug

SSEClientTransport.Connect opens the long-lived event stream with http.NewRequestWithContext(ctx, "GET", c.Endpoint, nil), using the context given to Client.Connect (mcp/sse.go; present in v1.8.0 and on main at 53effc0). Callers usually bound the handshake with a timeout:

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
session, err := client.Connect(ctx, &mcp.SSEClientTransport{Endpoint: url}, nil)

The session works until cancel runs or the timeout fires. After that the stream is closed and every later call fails. StreamableClientTransport detaches its connection from the Connect context, so the same code works on one transport and silently breaks on the other.

To Reproduce

  1. Connect to an SSE server with a context that has a short timeout, as above.
  2. Let the timeout fire (or call cancel).
  3. Call session.ListTools: it fails because the event stream is gone.

Expected behavior

The stream's lifetime is tied to the connection, ended by Close, as with the streamable transport. If the current behavior is intended, document it on Client.Connect and SSEClientTransport. Happy to send a PR.

Additional context

Found while moving PromptKit's MCP client onto go-sdk v1.8.0 (AltairaLabs/PromptKit#2141). We work around it by passing Connect a context that outlives the handshake: dial.

chaholl avatar Oct 02 '26 09:10 chaholl

I have a fix ready: chaholl/go-sdk@sse-connect-context. It detaches the GET from the Connect context, as StreamableClientTransport does. The GET is cancelled if that context ends before the endpoint event arrives, so Connect still honors its deadline, and by Close afterwards. There are tests for both, and the first fails without the change. Can I open the PR?

chaholl avatar Oct 02 '26 09:10 chaholl

Hi @chaholl feel free to open the PR!

guglielmo-san avatar Oct 02 '26 13:10 guglielmo-san

I'd like to work on this issue.

beiryu avatar Oct 16 '26 02:10 beiryu