fix: make initialize idempotent instead of erroring on repeat calls#257
Open
VictorPuga wants to merge 1 commit into
Open
fix: make initialize idempotent instead of erroring on repeat calls#257VictorPuga wants to merge 1 commit into
VictorPuga wants to merge 1 commit into
Conversation
Repeated `initialize` requests previously failed with "Server is already initialized", breaking stateless HTTP servers and clients that retry initialization. Per the MCP spec, initialize should be safe to call again, so the server now re-negotiates and returns a fresh result instead of throwing. Fixes modelcontextprotocol#219 Fixes modelcontextprotocol#144 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
Repeated
initializerequests previously failed with "Server is already initialized", breaking stateless HTTP servers and clients that retry initialization. Per the MCP spec, initialize should be safe to call again, so the server now re-negotiates and returns a fresh result instead of throwing.Fixes #219
Fixes #144
Motivation and Context
Stateless HTTP deployments (and clients that retry
initializefor robustness) callinitializemore than once against the sameServerinstance.The SDK rejected every call after the first with
MCPError.invalidRequest("Server is already initialized"), which breaks protocol compliance and forced consumers to write middleware workarounds to synthesize a fake successful response.How Has This Been Tested?
Tests/MCPTests/ServerTests.swiftcovering:initializerequests over the same connection both succeed and returnserverInfo, with no"error"in the response.initializeHookis invoked again on each repeatedinitializecall, confirming re-negotiation happens every time rather than being skipped after the first call.Breaking Changes
None. Clients that never re-initialize see no change in behavior. Clients that previously relied on getting an error on a second
initializecall will now get a successful, re-negotiated result instead.Types of changes
Checklist
Additional context
The fix removes the
isInitializedguard inServer.swift'sinitializehandler so the server always re-runs version negotiation (and re-invokes theinitializeHook, if registered) and returns a freshInitialize.Result, rather than throwing on subsequent calls.This applies uniformly across all transports (stdio, in-memory, HTTP); no test or documented behavior relies on stdio rejecting a second
initialize, and tolerating a duplicate call is not a spec violation.