python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

`RuntimeError: Task group is not initialized` in the simplest possible MCP server with default config

Open Nayjest opened this issue 8 months ago • 3 comments

The bug

RuntimeError: Task group is not initialized. Make sure to use run().
Response code: 500 (Internal Server Error); Time: 9ms (9 ms); Content length: 1677 bytes (1.68 kB)

Reproduces in each subsequent HTTP request (the first one works fine).

It's very counterintuitive if the default configuration doesn't work out of the box.

To Reproduce

from mcp.server.fastmcp import FastMCP
mcp = FastMCP(port=8010, debug=True, host='0.0.0.0')
@mcp.tool()
def ping(): return "pong"
mcp.run(transport="streamable-http")
###
POST http://localhost:8010/mcp/session
Content-Type: application/json
Accept: application/json, text/event-stream

{
    "method": "initialize",
    "jsonrpc": "2.0",
    "id": 1
}

Stack trace

HTTP/1.1 500 Internal Server Error
date: Thu, 29 May 2025 14:19:00 GMT
server: uvicorn
content-length: 1677
content-type: text/plain; charset=utf-8

Traceback (most recent call last):
  File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\middleware\errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\routing.py", line 758, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\routing.py", line 778, in app
    await route.handle(scope, receive, send)
  File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\starlette\routing.py", line 487, in handle
    await self.app(scope, receive, send)
  File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\mcp\server\fastmcp\server.py", line 786, in handle_streamable_http
    await self.session_manager.handle_request(scope, receive, send)
  File "C:\Users\********\AppData\Roaming\Python\Python313\site-packages\mcp\server\streamable_http_manager.py", line 137, in handle_request
    raise RuntimeError("Task group is not initialized. Make sure to use run().")
RuntimeError: Task group is not initialized. Make sure to use run().

Related issues

https://github.com/modelcontextprotocol/python-sdk/issues/713

Nayjest avatar May 29 '25 14:05 Nayjest

IMHO,

  • The FastMCP class should be able to handle its intended functionality without additional setup, or it should fail early with a clear error message if additional configuration is needed.
  • Incorrect client behavior should not produce Error 500 on the MCP server side.

Nayjest avatar May 29 '25 16:05 Nayjest

The behavior where every following request dies after incorrect client behavior is what this PR addresses https://github.com/modelcontextprotocol/python-sdk/pull/841

The server-side session manager is being killed during unhandled exceptions, leaving the whole server unusable.

soby avatar May 29 '25 23:05 soby

@sorby @ihrpr I confirm, for code attached in the description, PR #841 fixes the issue

Nayjest avatar May 30 '25 08:05 Nayjest

#841 just merged, thank you @soby!

bhosmer-ant avatar Jul 09 '25 15:07 bhosmer-ant