Skip to content

[BUG] Exception occurs during client session disconnection #171

Open
@cuspymd

Description

@cuspymd

Describe the bug

When adding a middleware, the following exception always occurs during client session disconnection. While there is no issue with calling the tool, I'm curious if there is a way to prevent this exception from occurring.

[2025-06-13 12:15:28,591][DEBUG][sse_starlette.sse][line=230][49972] - Got event: http.disconnect. Stop streaming.
[2025-06-13 12:15:28,591][DEBUG][root][line=159][49972] - Client session disconnected 04bbc672-f124-4291-b1fa-3b8a923ffe35
     ERROR   Exception in ASGI application
  + Exception Group Traceback (most recent call last):
  |   File "C:\...\Lib\site-packages\starlette\_utils.py", line 76, in collapse_excgroups
  |     yield
  |   File "C:\...\Lib\site-packages\starlette\middleware\base.py", line 177, in __call__
  |     async with anyio.create_task_group() as task_group:
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "C:\...\Lib\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 "C:\...\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 409, in run_asgi
    |     result = await app(  # type: ignore[func-returns-value]
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\...\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 60, in __call__
    |     return await self.app(scope, receive, send)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\...\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    |     await super().__call__(scope, receive, send)
    |   File "C:\...\Lib\site-packages\starlette\applications.py", line 112, in __call__
    |     await self.middleware_stack(scope, receive, send)
    |   File "C:\...\Lib\site-packages\starlette\middleware\errors.py", line 187, in __call__
    |     raise exc
    |   File "C:\...\Lib\site-packages\starlette\middleware\errors.py", line 165, in __call__
    |     await self.app(scope, receive, _send)
    |   File "C:\...\Lib\site-packages\starlette\middleware\base.py", line 176, in __call__
    |     with recv_stream, send_stream, collapse_excgroups():
    |                                    ^^^^^^^^^^^^^^^^^^^^
    |   File "C:\...\Lib\contextlib.py", line 158, in __exit__
    |     self.gen.throw(value)
    |   File "C:\...\Lib\site-packages\starlette\_utils.py", line 82, in collapse_excgroups
    |     raise exc
    |   File "C:\...\Lib\site-packages\starlette\middleware\base.py", line 179, in __call__
    |     await response(scope, wrapped_receive, send)
    |   File "C:\...\Lib\site-packages\starlette\middleware\base.py", line 216, in __call__
    |     async for chunk in self.body_iterator:
    |   File "C:\...\Lib\site-packages\starlette\middleware\base.py", line 163, in body_stream
    |     assert message["type"] == "http.response.body"
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    | AssertionError
    +------------------------------------

To Reproduce

  1. After adding the middleware below to the code, start the server
  2. Run Cline and connect to the MCP server
  3. Terminate the Cline (VS Code)
@app.middleware("http")
async def some_middleware(request: Request, call_next):
    return await call_next(request)

System Info

fastapi : 0.115.12
fastapi-mcp : 0.3.4
MCP host : Cline (VS Code)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions