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

mcp: StreamableHTTPHandler calls getServer twice for one new POST

Open Adebayo120 opened this issue 1 month ago • 0 comments

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

  1. Create a StreamableHTTPHandler whose getServer callback increments a counter and returns a server.
  2. Send one new POST to the handler with Content-Type: application/json, Accept: application/json, text/event-stream, and a valid initialize request body.
  3. 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.

Adebayo120 avatar Oct 18 '26 19:10 Adebayo120