Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mcp server always get initialization error

I create a mcp server by FastMCP, I can ensure that the mcp server has already finished the initialization, due to the server has already process several tool request, but I also get following error:

INFO:     192.168.182.1:2520 - "GET /sse HTTP/1.1" 200 OK
INFO:     192.168.182.1:2691 - "POST /messages/?session_id=aa9fa14634144092aed00e8a04f84ca4 HTTP/1.1" 202 Accepted
INFO:     192.168.182.1:2760 - "POST /messages/?session_id=aa9fa14634144092aed00e8a04f84ca4 HTTP/1.1" 202 Accepted
ERROR:    Exception in ASGI application
  + Exception Group Traceback (most recent call last):
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
  |     result = await app(  # type: ignore[func-returns-value]
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |         self.scope, self.receive, self.send
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |     )
  |     ^
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
  |     return await self.app(scope, receive, send)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/applications.py", line 112, in __call__
  |     await self.middleware_stack(scope, receive, send)
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/middleware/errors.py", line 187, in __call__
  |     raise exc
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/middleware/errors.py", line 165, in __call__
  |     await self.app(scope, receive, _send)
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
  |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
  |     raise exc
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
  |     await app(scope, receive, sender)
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/routing.py", line 714, in __call__
  |     await self.middleware_stack(scope, receive, send)
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/routing.py", line 734, in app
  |     await route.handle(scope, receive, send)
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/routing.py", line 288, in handle
  |     await self.app(scope, receive, send)
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/routing.py", line 76, in app
  |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
  |     raise exc
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
  |     await app(scope, receive, sender)
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/starlette/routing.py", line 73, in app
  |     response = await f(request)
  |                ^^^^^^^^^^^^^^^^
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/fastmcp/server.py", line 485, in handle_sse
  |     async with sse.connect_sse(
  |                ~~~~~~~~~~~~~~~^
  |         request.scope,
  |         ^^^^^^^^^^^^^^
  |         request.receive,
  |         ^^^^^^^^^^^^^^^^
  |         request._send,  # type: ignore[reportPrivateUsage]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |     ) as streams:
  |     ^
  |   File "/home/lewis/.local/share/uv/python/cpython-3.13.2-linux-x86_64-gnu/lib/python3.13/contextlib.py", line 235, in __aexit__
  |     await self.gen.athrow(value)
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/sse.py", line 123, in connect_sse
  |     async with anyio.create_task_group() as tg:
  |                ~~~~~~~~~~~~~~~~~~~~~~~^^
  |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
  |     raise BaseExceptionGroup(
  |         "unhandled errors in a TaskGroup", self._exceptions
  |     ) from None
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Exception Group Traceback (most recent call last):
    |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/sse.py", line 131, in connect_sse
    |     yield (read_stream, write_stream)
    |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/fastmcp/server.py", line 490, in handle_sse
    |     await self._mcp_server.run(
    |     ...<3 lines>...
    |     )
    |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/lowlevel/server.py", line 483, in run
    |     async with AsyncExitStack() as stack:
    |                ~~~~~~~~~~~~~~^^
    |   File "/home/lewis/.local/share/uv/python/cpython-3.13.2-linux-x86_64-gnu/lib/python3.13/contextlib.py", line 768, in __aexit__
    |     raise exc
    |   File "/home/lewis/.local/share/uv/python/cpython-3.13.2-linux-x86_64-gnu/lib/python3.13/contextlib.py", line 751, in __aexit__
    |     cb_suppress = await cb(*exc_details)
    |                   ^^^^^^^^^^^^^^^^^^^^^^
    |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/shared/session.py", line 210, in __aexit__
    |     return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
    |     raise BaseExceptionGroup(
    |         "unhandled errors in a TaskGroup", self._exceptions
    |     ) from None
    | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
    +-+---------------- 1 ----------------
      | Traceback (most recent call last):
      |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/shared/session.py", line 324, in _receive_loop
      |     await self._received_request(responder)
      |   File "/home/lewis/mcp/mcp_k8s_server/.venv/lib/python3.13/site-packages/mcp/server/session.py", line 163, in _received_request
      |     raise RuntimeError(
      |         "Received request before initialization was complete"
      |     )
      | RuntimeError: Received request before initialization was complete
      +------------------------------------

I also add a parameter when create object of FastMCP

# Create the MCP server with initialization timeout
# This helps prevent "Received request before initialization was complete" errors
# by automatically closing connections that don't initialize properly
mcp = FastMCP(
    config.server.name,
    host=config.server.host,
    port=config.server.port,
    settings={"initialization_timeout": 10.0}  # 10 second timeout
)

mcp server run code:

 mcp.run(transport="sse")

BTW: my mcp client is CherryStdio

How to resolve this?

like image 538
Dangerman Avatar asked May 31 '26 03:05

Dangerman


1 Answers

It looks like it needs two initial messages to properly get into an "initialized" state:

  1. Call "initialize" first:

    {
      "jsonrpc": "2.0",
      "id": 0,
      "method": "initialize",
      "params": {
        "protocolVersion": "2024-11-05",
        "capabilities": {},
        "clientInfo": {
            "name": "whatever",
            "version": "0.0.0"
        }
      }
    }
    
  2. Then send an "initialized" notification:

    {"jsonrpc":"2.0","method":"notifications/initialized"}
    
  3. Finally, you can do a regular method call:

    {"jsonrpc":"2.0","method":"tools/list","id":1}
    

I found this with some experimenting and after looking through the source. No idea if this is how the protocol is expected to work, or if this is specific to FastMCP. (I couldn't find anything about this in the documentation first, but then now finally spotted that the above sequence is described here: https://modelcontextprotocol.io/specification/draft/basic/lifecycle)

like image 120
pdenes Avatar answered Jun 01 '26 21:06

pdenes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!