mcp: SSEClientTransport ties the event stream to the context passed to Connect
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
- Connect to an SSE server with a context that has a short timeout, as above.
- Let the timeout fire (or call
cancel). - 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.
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?
Hi @chaholl feel free to open the PR!
I'd like to work on this issue.