fastmcp icon indicating copy to clipboard operation
fastmcp copied to clipboard

issue on sse server and interrupted tool call

Open luogni opened this issue 7 months ago • 2 comments

Description

Hi, I've developed a simple fastmcp server and using it via sse with LibreChat.

I have a simple tool for running commands (async):

    proc = await asyncio.create_subprocess_shell(
        cmd,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
        cwd="/aymcp",
    )
    try:
        stdout, stderr = await proc.communicate()
    except Exception:
        return "Error executing this command"
    if stdout:
        return stdout.decode()
    else:
        return stderr.decode()

The tool works fine but if I:

  • tell LibreChat model to use the tool
  • interrupt (press the stop button on LibreChat) the tool while it's running I get this exception:
aymcp          | ERROR:    Exception in ASGI application
aymcp          | Traceback (most recent call last):
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
aymcp          |     result = await app(  # type: ignore[func-returns-value]
aymcp          |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
aymcp          |     return await self.app(scope, receive, send)
aymcp          |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/applications.py", line 112, in __call__
aymcp          |     await self.middleware_stack(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 187, in __call__
aymcp          |     raise exc
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 165, in __call__
aymcp          |     await self.app(scope, receive, _send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
aymcp          |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
aymcp          |     raise exc
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
aymcp          |     await app(scope, receive, sender)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 714, in __call__
aymcp          |     await self.middleware_stack(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 734, in app
aymcp          |     await route.handle(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 288, in handle
aymcp          |     await self.app(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 76, in app
aymcp          |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
aymcp          |     raise exc
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
aymcp          |     await app(scope, receive, sender)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 74, in app
aymcp          |     await response(scope, receive, send)
aymcp          |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
aymcp          | TypeError: 'NoneType' object is not callable

How can I avoid the exception inside the server? I'm running it with: uvicorn aymcp:app --host 0.0.0.0 --port 8000

tried also: fastmcp run aymcp/init.py:mcp --transport sse --host 0.0.0.0 --port 8000

but same issue.

Example Code


Version Information

[[package]]
name = "fastmcp"
version = "2.2.10"

Additional Context

No response

luogni avatar May 08 '25 08:05 luogni

tried now with updated mcp and fastmcp releases:

aymcp          | ERROR:    Exception in ASGI application
aymcp          | Traceback (most recent call last):
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
aymcp          |     result = await app(  # type: ignore[func-returns-value]
aymcp          |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
aymcp          |     return await self.app(scope, receive, send)
aymcp          |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/applications.py", line 112, in __call__
aymcp          |     await self.middleware_stack(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 187, in __call__
aymcp          |     raise exc
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 165, in __call__
aymcp          |     await self.app(scope, receive, _send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/fastmcp/server/http.py", line 59, in __call__
aymcp          |     await self.app(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
aymcp          |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
aymcp          |     raise exc
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
aymcp          |     await app(scope, receive, sender)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 714, in __call__
aymcp          |     await self.middleware_stack(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 734, in app
aymcp          |     await route.handle(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 288, in handle
aymcp          |     await self.app(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 76, in app
aymcp          |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
aymcp          |     raise exc
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
aymcp          |     await app(scope, receive, sender)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 74, in app
aymcp          |     await response(scope, receive, send)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/responses.py", line 150, in __call__
aymcp          |     await send(
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 39, in sender
aymcp          |     await send(message)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/_exception_handler.py", line 39, in sender
aymcp          |     await send(message)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in _send
aymcp          |     await send(message)
aymcp          |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 513, in send
aymcp          |     raise RuntimeError(msg % message_type)
aymcp          | RuntimeError: Unexpected ASGI message 'http.response.start' sent, after response already completed.

different error.

luogni avatar May 09 '25 05:05 luogni

and now I moved to http streamable and:

aymcp          | INFO:     192.168.112.7:53930 - "POST /mcp/ HTTP/1.1" 200 OK
aymcp          | SSE response error
aymcp          |   + Exception Group Traceback (most recent call last):
aymcp          |   |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/mcp/server/streamable_http.py", line 511, in _handle_post_request
aymcp          |   |     async with anyio.create_task_group() as tg:
aymcp          |   |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
aymcp          |   |     raise BaseExceptionGroup(
aymcp          |   | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
aymcp          |   +-+---------------- 1 ----------------
aymcp          |     | Traceback (most recent call last):
aymcp          |     |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/mcp/server/streamable_http.py", line 515, in _handle_post_request
aymcp          |     |     await writer.send(session_message)
aymcp          |     |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/anyio/streams/memory.py", line 242, in send
aymcp          |     |     self.send_nowait(item)
aymcp          |     |   File "/root/.cache/pypoetry/virtualenvs/aymcp-aSnAZHCZ-py3.11/lib/python3.11/site-packages/anyio/streams/memory.py", line 211, in send_nowait
aymcp          |     |     raise ClosedResourceError
aymcp          |     | anyio.ClosedResourceError
aymcp          |     +------------------------------------
aymcp          | ERROR:    ASGI callable returned without completing response.

luogni avatar May 16 '25 06:05 luogni

Same here with Streamable Http

IbrahimMCode avatar Jun 23 '25 08:06 IbrahimMCode