mcp: simplify ServerSession.initialize

Make the simplifications described in #222. I don't know why we
originally had such fine granularity of locking: perhaps we were going
to execute 'oninitialized' callbacks.

Also update the TODO with a bit more information about jsonrpc2
behavior.

Fixes #222
This commit is contained in:
Rob Findley 2025-08-07 16:19:14 +00:00 • committed by Robert Findley
commit 4608401cc4

View file

@ -753,19 +753,16 @@ func (ss *ServerSession) initialize(ctx context.Context, params *InitializeParam
return nil, fmt.Errorf("%w: \"params\" must be be provided", jsonrpc2.ErrInvalidParams)
}
ss.mu.Lock()
defer ss.mu.Unlock()
ss.initializeParams = params
ss.mu.Unlock()
// Mark the connection as initialized when this method exits.
// TODO: Technically, the server should not be considered initialized until it has
// *responded*, but we don't have adequate visibility into the jsonrpc2
// connection to implement that easily. In any case, once we've initialized
// here, we can handle requests.
defer func() {
ss.mu.Lock()
ss.initialized = true
ss.mu.Unlock()
}()
// TODO(#26): Technically, the server should not be considered initialized
// until it has *responded*, but since jsonrpc2 is currently serialized we
// can mark the session as initialized here. If we ever implement a
// concurrency model (#26), we need to guarantee that initialize is not
// handled concurrently to other requests.
ss.initialized = true
// If we support the client's version, reply with it. Otherwise, reply with our
// latest version.