Skip to content

Missing SSE Session when using /mcp instead of /mcp/ #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
flobaader opened this issue May 24, 2025 · 0 comments
Open

Missing SSE Session when using /mcp instead of /mcp/ #585

flobaader opened this issue May 24, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@flobaader
Copy link

Description

When the client is using this url /mcp instead of /mcp/ to connect to the server,
I get this error with a minimal example:

INFO:     79.213.152.157:0 - "GET /mcp HTTP/1.1" 307 Temporary Redirect
INFO:     79.213.152.157:0 - "POST /mcp HTTP/1.1" 307 Temporary Redirect
INFO:     79.213.152.157:0 - "POST /mcp/ HTTP/1.1" 200 OK
INFO:     79.213.152.157:0 - "POST /mcp HTTP/1.1" 307 Temporary Redirect
INFO:     79.213.152.157:0 - "POST /mcp HTTP/1.1" 307 Temporary Redirect
INFO:     79.213.152.157:0 - "GET /mcp/ HTTP/1.1" 200 OK
INFO:     79.213.152.157:0 - "POST /mcp/ HTTP/1.1" 200 OK
ERROR:      + Exception Group Traceback (most recent call last):
  |   File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 692, in lifespan
  |     async with self.lifespan_context(app) as maybe_state:
  |   File "/usr/local/lib/python3.10/contextlib.py", line 217, in __aexit__
  |     await self.gen.athrow(typ, value, traceback)
  |   File "/usr/local/lib/python3.10/site-packages/fastmcp/server/http.py", line 370, in lifespan
  |     async with session_manager.run():
  |   File "/usr/local/lib/python3.10/contextlib.py", line 217, in __aexit__
  |     await self.gen.athrow(typ, value, traceback)
  |   File "/usr/local/lib/python3.10/site-packages/mcp/server/streamable_http_manager.py", line 106, in run
  |     async with anyio.create_task_group() as tg:
  |   File "/usr/local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
  |     raise BaseExceptionGroup(
  | exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Exception Group Traceback (most recent call last):
    |   File "/usr/local/lib/python3.10/site-packages/mcp/server/streamable_http_manager.py", line 235, in run_server
    |     async with http_transport.connect() as streams:
    |   File "/usr/local/lib/python3.10/contextlib.py", line 217, in __aexit__
    |     await self.gen.athrow(typ, value, traceback)
    |   File "/usr/local/lib/python3.10/site-packages/mcp/server/streamable_http.py", line 843, in connect
    |     async with anyio.create_task_group() as tg:
    |   File "/usr/local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
    |     raise BaseExceptionGroup(
    | exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
    +-+---------------- 1 ----------------
      | Exception Group Traceback (most recent call last):
      |   File "/usr/local/lib/python3.10/site-packages/mcp/server/streamable_http.py", line 916, in connect
      |     yield read_stream, write_stream
      |   File "/usr/local/lib/python3.10/site-packages/mcp/server/streamable_http_manager.py", line 238, in run_server
      |     await self.app.run(
      |   File "/usr/local/lib/python3.10/site-packages/mcp/server/lowlevel/server.py", line 495, in run
      |     async with AsyncExitStack() as stack:
      |   File "/usr/local/lib/python3.10/contextlib.py", line 714, in __aexit__
      |     raise exc_details[1]
      |   File "/usr/local/lib/python3.10/contextlib.py", line 217, in __aexit__
      |     await self.gen.athrow(typ, value, traceback)
      |   File "/usr/local/lib/python3.10/site-packages/fastmcp/server/server.py", line 101, in wrap
      |     yield context
      |   File "/usr/local/lib/python3.10/contextlib.py", line 697, in __aexit__
      |     cb_suppress = await cb(*exc_details)
      |   File "/usr/local/lib/python3.10/site-packages/mcp/shared/session.py", line 220, in __aexit__
      |     return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
      |   File "/usr/local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
      |     raise BaseExceptionGroup(
      | exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
      +-+---------------- 1 ----------------
        | Traceback (most recent call last):
        |   File "/usr/local/lib/python3.10/site-packages/mcp/server/session.py", line 147, in _receive_loop
        |     await super()._receive_loop()
        |   File "/usr/local/lib/python3.10/site-packages/mcp/shared/session.py", line 370, in _receive_loop
        |     await self._received_request(responder)
        |   File "/usr/local/lib/python3.10/site-packages/mcp/server/session.py", line 175, in _received_request
        |     raise RuntimeError(
        | RuntimeError: Received request before initialization was complete
        +------------------------------------

INFO:     79.213.152.157:0 - "POST /mcp HTTP/1.1" 307 Temporary Redirect
INFO:     79.213.152.157:0 - "POST /mcp HTTP/1.1" 307 Temporary Redirect
[05/24/25 07:59:49] ERROR    Original RuntimeError from mcp library: http.py:313
                             Task group is not initialized. Make                
                             sure to use run().                                 
                             ╭─ Traceback (most recent call last) ─╮            
                             │ /usr/local/lib/python3.10/site-pack │            
                             │ ages/fastmcp/server/http.py:310 in  │            
                             │ handle_streamable_http              │            
                             │                                     │            
                             │   307 │   │   scope: Scope, receive │            
                             │   308 │   ) -> None:                │            
                             │   309 │   │   try:                  │            
                             │ ❱ 310 │   │   │   await session_man │            
                             │   311 │   │   except RuntimeError a │            
                             │   312 │   │   │   if str(e) == "Tas │            
                             │   313 │   │   │   │   logger.error( │            
                             │                                     │            
                             │ /usr/local/lib/python3.10/site-pack │            
                             │ ages/mcp/server/streamable_http_man │            
                             │ ager.py:137 in handle_request       │            
                             │                                     │            
                             │   134 │   │   │   send: ASGI send f │            
                             │   135 │   │   """                   │            
                             │   136 │   │   if self._task_group i │            
                             │ ❱ 137 │   │   │   raise RuntimeErro │            
                             │   138 │   │                         │            
                             │   139 │   │   # Dispatch to the app │            
                             │   140 │   │   if self.stateless:    │            
                             ╰─────────────────────────────────────╯            
                             RuntimeError: Task group is not                    
                             initialized. Make sure to use run().               
INFO:     79.213.152.157:0 - "POST /mcp/ HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/fastmcp/server/http.py", line 310, in handle_streamable_http
    await session_manager.handle_request(scope, receive, send)
  File "/usr/local/lib/python3.10/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().

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 187, in __call__
    raise exc
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.10/site-packages/fastmcp/server/http.py", line 72, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 714, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 734, in app
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 460, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.10/site-packages/fastmcp/server/http.py", line 328, in handle_streamable_http
    raise RuntimeError(f"{new_error_message}\\nOriginal error: {e}") from e
RuntimeError: FastMCP's StreamableHTTPSessionManager task group was not initialized. This commonly occurs when the FastMCP application's lifespan is not passed to the parent ASGI application (e.g., FastAPI or Starlette). Please ensure you are setting `lifespan=mcp_app.lifespan` in your parent app's constructor, where `mcp_app` is the application instance returned by `fastmcp_instance.http_app()`. \nFor more details, see the FastMCP ASGI integration documentation: https://gofastmcp.com/deployment/asgi\nOriginal error: Task group is not initialized. Make sure to use run().
                    ERROR    Original RuntimeError from mcp library: http.py:313
                             Task group is not initialized. Make                
                             sure to use run().                                 
                             ╭─ Traceback (most recent call last) ─╮   

Example Code

Version Information

from fastmcp import FastMCP
from fastapi import FastAPI
from starlette.routing import Mount

# Create your FastMCP server as well as any tools, resources, etc.
mcp = FastMCP("MyServer")

# Create the ASGI app
mcp_app = mcp.http_app(path='/mcp')

# Create a FastAPI app and mount the MCP server
app = FastAPI(lifespan=mcp_app.lifespan)
app.mount("/mcp-server", mcp_app)

Additional Context

No response

@flobaader flobaader added the bug Something isn't working label May 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant