mcp: don't persist failed streamable sessions (#605)

If a stateful streamable session fails to initialize, it is unusable.
Avoid allocating resources by closing the session immediately.

Also:
- Fix a bug where InitializeParams is not guarded with its mutex.
- Fix a resource leak where sessions are persisted with session id "",
even though they are unaddressable.
- Add a relevant benchmark, and update tests.

Fixes #578
This commit is contained in:
Robert Findley 2025-10-27 16:29:16 -04:00 • committed by GitHub
commit d256a9c109
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1145,7 +1145,13 @@ func (ss *ServerSession) handle(ctx context.Context, req *jsonrpc.Request) (any,
return handleReceive(ctx, ss, req)
}
func (ss *ServerSession) InitializeParams() *InitializeParams { return ss.state.InitializeParams }
// InitializeParams returns the InitializeParams provided during the client's
// initial connection.
func (ss *ServerSession) InitializeParams() *InitializeParams {
ss.mu.Lock()
defer ss.mu.Unlock()
return ss.state.InitializeParams
}
func (ss *ServerSession) initialize(ctx context.Context, params *InitializeParams) (*InitializeResult, error) {
if params == nil {