Repository navigation
Description
Feature Request: Idempotent initialize for Stateless HTTP MCP Servers
Problem
According to the MCP protocol, clients (including agents and LLMs) may call initialize multiple times for robustness, retries, or stateless HTTP operation. The protocol expects repeated initialize calls to always succeed and return the current server capabilities and info, not an error.
However, the MCP Swift SDK currently returns an error (Server is already initialized) on repeated initialize calls. This breaks compatibility with stateless HTTP clients, RESTful integrations, and robust agent implementations that expect idempotency.
This is a protocol-level requirement, not just a convenience: stateless HTTP deployments (including REST, serverless, and agent-facing APIs) must support idempotent initialize calls to be compliant and reliable.
Why This Is a Problem
- Clients and agents expect to be able to call
initializeon every request or retry, and always get a valid result. - Returning an error breaks protocol compliance and causes failures in real-world integrations.
- Workarounds (like custom middleware) add complexity and risk misconfiguration.
- The transport layer (e.g.,
StatelessHTTPServerTransport) should guarantee idempotency forinitializeby default.
Current Workaround
- We implemented
MCPIdempotentInitializeMiddlewareto intercept repeatedinitializecalls and synthesize a valid result, but this should not be necessary for stateless HTTP servers.
Proposed Solution
- When using
StatelessHTTPServerTransport, the SDK should automatically make repeatedinitializecalls idempotent:- If the server is already initialized, return the current
initializeresult (capabilities, info, etc.) instead of an error. - This should be the default behavior for stateless transports, with no middleware required.
- If the server is already initialized, return the current
- Optionally, document that
MCPIdempotentInitializeMiddlewareis only needed for stateful transports.
Benefits
- Ensures MCP protocol compliance for all stateless HTTP deployments.
- Simplifies server implementation and reduces boilerplate.
- Prevents subtle bugs and integration failures for agents and clients.
Summary
Please make repeated initialize calls idempotent by default when using StatelessHTTPServerTransport, so that custom middleware is not required and MCP servers are robust, compliant, and agent-friendly out of the box.
Activity
mynona commentedon Apr 19, 2026
Connected bug
#144
mynona commentedon Apr 25, 2026
Besides this is the Middleware I use as workaround to overcome the problem. You see, I have to fully create a fake initialize response
fix: make initialize idempotent instead of erroring on repeat calls