go-sdk
go-sdk copied to clipboard
mcp: StreamableHTTPHandler calls getServer twice for one new POST
Describe the bug
StreamableHTTPHandler calls the getServer callback once while validating the protocol-version header, then calls it again when a POST creates a new stateful or stateless session. A callback with side effects runs twice for one request, and a callback that returns different server instances can validate against one server and handle the request with another.
To Reproduce
- Create a
StreamableHTTPHandlerwhosegetServercallback increments a counter and returns a server. - Send one new POST to the handler with
Content-Type: application/json,Accept: application/json, text/event-stream, and a validinitializerequest body. - Observe that the callback count is 2 (for both stateful and stateless handlers).
Expected behavior The callback is evaluated once per request and the selected server is reused for protocol-version validation and new-session handling.
Additional context
The duplicate invocation is in mcp/streamable.go (ServeHTTP and the POST handling paths). I verified this locally with a regression test.