fastmcp icon indicating copy to clipboard operation
fastmcp copied to clipboard

RuntimeError: Received request before initialization was complete

Open world520ai opened this issue 8 months ago • 6 comments

ERROR: Exception in ASGI application Traceback (most recent call last): File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi result = await app( # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in call return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/applications.py", line 112, in call await self.middleware_stack(scope, receive, send) File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 187, in call raise exc File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 165, in call await self.app(scope, receive, _send) File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 62, in call await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app raise exc File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app await app(scope, receive, sender) File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/routing.py", line 714, in call await self.middleware_stack(scope, receive, send) File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/routing.py", line 734, in app await route.handle(scope, receive, send) File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/routing.py", line 460, in handle await self.app(scope, receive, send) File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/mcp/server/sse.py", line 175, in handle_post_message await writer.send(message) File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/anyio/streams/memory.py", line 242, in send self.send_nowait(item) File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/anyio/streams/memory.py", line 213, in send_nowait raise BrokenResourceError anyio.BrokenResourceError ERROR: Exception in ASGI application

  • Exception Group Traceback (most recent call last): | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi | result = await app( # type: ignore[func-returns-value] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in call | return await self.app(scope, receive, send) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/applications.py", line 112, in call | await self.middleware_stack(scope, receive, send) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 187, in call | raise exc | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 165, in call | await self.app(scope, receive, _send) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 62, in call | await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app | raise exc | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app | await app(scope, receive, sender) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/routing.py", line 714, in call | await self.middleware_stack(scope, receive, send) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/routing.py", line 734, in app | await route.handle(scope, receive, send) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/routing.py", line 288, in handle | await self.app(scope, receive, send) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/routing.py", line 76, in app | await wrap_app_handling_exceptions(app, request)(scope, receive, send) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app | raise exc | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app | await app(scope, receive, sender) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/starlette/routing.py", line 73, in app | response = await f(request) | ^^^^^^^^^^^^^^^^ | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/fastmcp/server/server.py", line 717, in handle_sse | async with sse.connect_sse( | ^^^^^^^^^^^^^^^^ | File "/Users/lucas/.local/share/uv/python/cpython-3.12.9-macos-aarch64-none/lib/python3.12/contextlib.py", line 231, in aexit | await self.gen.athrow(value) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/mcp/server/sse.py", line 123, in connect_sse | async with anyio.create_task_group() as tg: | ^^^^^^^^^^^^^^^^^^^^^^^^^ | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 772, in aexit | raise BaseExceptionGroup( | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception) +-+---------------- 1 ---------------- | Exception Group Traceback (most recent call last): | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/mcp/server/sse.py", line 131, in connect_sse | yield (read_stream, write_stream) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/fastmcp/server/server.py", line 722, in handle_sse | await self._mcp_server.run( | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/mcp/server/lowlevel/server.py", line 483, in run | async with AsyncExitStack() as stack: | ^^^^^^^^^^^^^^^^ | File "/Users/lucas/.local/share/uv/python/cpython-3.12.9-macos-aarch64-none/lib/python3.12/contextlib.py", line 754, in aexit | raise exc_details[1] | File "/Users/lucas/.local/share/uv/python/cpython-3.12.9-macos-aarch64-none/lib/python3.12/contextlib.py", line 737, in aexit | cb_suppress = await cb(*exc_details) | ^^^^^^^^^^^^^^^^^^^^^^ | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/mcp/shared/session.py", line 210, in aexit | return await self._task_group.aexit(exc_type, exc_val, exc_tb) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 772, in aexit | raise BaseExceptionGroup( | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception) +-+---------------- 1 ---------------- | Traceback (most recent call last): | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/mcp/shared/session.py", line 324, in _receive_loop | await self._received_request(responder) | File "/Users/lucas/IdeaProjects/mcp_servers_ops/.venv/lib/python3.12/site-packages/mcp/server/session.py", line 163, in _received_request | raise RuntimeError( | RuntimeError: Received request before initialization was complete +------------------------------------ INFO: 127.0.0.1:62923 - "GET /sse HTTP/1.1" 200 OK INFO: 127.0.0.1:62938 - "POST /messages/?session_id=fb7f7269ad3a4e93b67768c2fed4bd3c HTTP/1.1" 202 Accepted

world520ai avatar Apr 17 '25 03:04 world520ai

what is the problem? when i run the fastmcp app as server then restart it again, when i access from client, I had got the error like above comment.

world520ai avatar Apr 17 '25 03:04 world520ai

The client must reconnect after server restart.

The MCP Server starts a new "Server" for every session. When you restart the server there is no longer an active session with that client.

strawgate avatar Apr 17 '25 04:04 strawgate

If that's the case, if I restart the production environment, then all users will have to close the client and reconnect once again, doesn't that seem a bit unfriendly?

world520ai avatar Apr 17 '25 08:04 world520ai

Can you describe the behavior and expected behavior as an MRE please?

jlowin avatar Apr 21 '25 00:04 jlowin

Can you describe the behavior and expected behavior as an MRE please?

for example, if i used cline( a AI agent plugin for visual studio code) to run a task, when i paused the task and rerun it after a time duration(the MCP SSE server already restarted), it will be get error cause by above connection error.

world520ai avatar Apr 22 '25 06:04 world520ai

I suspect this is related to this issue and can be solved with this workaround

silverspace avatar Apr 25 '25 17:04 silverspace

I believe this race condition is fixed by https://github.com/modelcontextprotocol/python-sdk/pull/1478

jlowin avatar Oct 15 '25 15:10 jlowin