fix: mark client initialized after initialize handshake#79
Open
pascualmg wants to merge 1 commit intophp-mcp:mainfrom
Open
fix: mark client initialized after initialize handshake#79pascualmg wants to merge 1 commit intophp-mcp:mainfrom
pascualmg wants to merge 1 commit intophp-mcp:mainfrom
Conversation
5374356 to
606704b
Compare
Many real-world MCP clients (Claude Code, Gemini/Antigravity) complete the initialize handshake and immediately call tools/list or tools/call without sending the notifications/initialized notification. Currently, initialized is only set to true in handleNotificationInitialized(), so these clients get permanently locked out with "Client session not initialized" errors. This change marks the session as initialized right after a successful initialize response. If a client still sends notifications/initialized, handleNotificationInitialized() sets the flag again — a harmless no-op. Discovered while running a production MCP server (Cohete blog) that serves Claude Code and other MCP clients over SSE transport.
606704b to
de7c087
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Marks the client session as initialized immediately after a successful
initializeresponse, instead of waiting for thenotifications/initializednotification that many real-world clients never send.This is a one-line change (
$session->set('initialized', true)inDispatcher::handleInitialize()). It fixes "Client session not initialized" errors for Claude Code, Gemini/Antigravity, and any other MCP client that treatsnotifications/initializedas optional.The problem
Protocol::processRequest()gates every method call behindassertSessionInitialized(), butinitializedis only set totrueinDispatcher::handleNotificationInitialized(). Clients that skip the notification are permanently locked out after a successful handshake.I'm using php-mcp/server on my blog at pascualmg.dev/blog with SSE transport, and my beta testers reported that MCP tools were failing with "Client session not initialized" errors right after a successful initialize handshake.
Steps to reproduce
Start any php-mcp/server instance with SSE transport:
Affected clients
notifications/initializedover SSEnotifications/initializedThe fix
Backwards compatibility
Fully backwards compatible. If a client still sends
notifications/initializedafterinitialize,handleNotificationInitialized()calls$session->set('initialized', true)again — a harmless no-op on an already-initialized session.No changes to
SessionInterface, no new config, no breaking API changes.Context
Discovered while running a production MCP server (Cohete) that serves Claude Code and other MCP clients over the SSE transport. We've been running with this fix patched locally via
cweagans/composer-patchesand it resolves the issue for all clients.🤖 Generated with Claude Code