SSE Connection Race Condition in MCP Server
🐛 Describe the bug
###video https://www.loom.com/share/8a15efc80194431eb9aea4e65fa1c888?sid=0818810b-fd56-4f1d-b8a8-36bcc10c940a
Description
Server-Sent Events (SSE) connection is failing due to a race condition where requests are being received before the server initialization is complete. This results in a RuntimeError being thrown with the message "Received request before initialization was complete".
Environment
- Python Version: 3.12
- FastAPI Version: [version]
- Operating System: Docker Container
- MCP Server Version: [version]
Steps to Reproduce
- Start the MCP server
- Client attempts to establish SSE connection via
/mcp/cursor/sse/[username] - Client sends message through
/mcp/messages/?session_id=[session_id] - Server fails with initialization error
Current Behavior
The server throws a RuntimeError when receiving requests before initialization is complete, resulting in failed SSE connections.
Expected Behavior
The server should either:
- Queue incoming requests until initialization is complete, or
- Return a proper 503 Service Unavailable status until ready, or
- Ensure initialization is complete before accepting any connections
Error Log
RuntimeError: Received request before initialization was complete
Traceback:
File "/usr/local/lib/python3.12/site-packages/mcp/server/session.py", line 147, in _receive_loop
await super()._receive_loop()
File "/usr/local/lib/python3.12/site-packages/mcp/shared/session.py", line 370, in _receive_loop
await self._received_request(responder)
File "/usr/local/lib/python3.12/site-packages/mcp/server/session.py", line 175, in _received_request
raise RuntimeError("Received request before initialization was complete")
existing MCP issue Try restarting the client after restarting openMemory. I just had to quit Cursor and reopen and it worked.
existing MCP issue Try restarting the client after restarting openMemory. I just had to quit Cursor and reopen and it worked.
thanks, i reopen and it work