From 4608401cc4b146e934c9f7e06da63a45a57964ef Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Thu, 7 Aug 2025 16:19:14 +0000 Subject: [PATCH] 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 --- mcp/server.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/mcp/server.go b/mcp/server.go index c8878da..b34cfe8 100644 --- a/mcp/server.go +++ b/mcp/server.go @@ -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.