Repository navigation
fix: make initialize idempotent instead of erroring on repeat calls - #257
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 <[email protected]>
Issue titles are now bare 'Upstream PR#<n>'; the merge/investigate/decline label carries the decision, so the title does not duplicate it. Two places named the old convention and are updated. The investigate count was also stale: it said twelve, which was true at triage on 2026-09-10. Four were declined since (modelcontextprotocol#280, modelcontextprotocol#226, modelcontextprotocol#118, modelcontextprotocol#204) and two were promoted and merged (modelcontextprotocol#266 as entries 9/9a, modelcontextprotocol#275 as 10/10a/10b), leaving six — modelcontextprotocol#178, modelcontextprotocol#213, modelcontextprotocol#216, modelcontextprotocol#257, modelcontextprotocol#258, modelcontextprotocol#259 — now listed by number rather than by count, so the sentence cannot drift again. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Eb9yGSXH1TVkg5Afsu9phk
|
Hi @VictorPuga, thank you for this PR, and for the care you put into the initialize fix! We have just opened the Swift SDK 1.0.0 release (#372, branch Could you check whether 1.0.0 covers your case? If it does, we would suggest closing this PR. If something is still missing, please open a new PR against Thank you again for contributing. We would love to see more from you on the new stack! |
|
Thanks @varun06! I tested We can safely close this PR. |
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.