Skip to content

Disconnecting an MCP server causes a CPU core to get stuck at 100% #2182

@upa

Description

@upa

Describe the bug
When connecting to an MCP server and then disconnecting it, a thread consumes 100% of a cpu core.

To Reproduce
Steps to reproduce the behavior:

Run the minimal script below:

import chainlit as cl

from mcp import ClientSession

@cl.on_chat_start
async def chat_start():
    await cl.Message(content="hello world").send()

@cl.on_mcp_connect
async def mcp_connect(connection, session: ClientSession):
    tools = await session.list_tools()
    print(f"connect mcp: {tools}")
  1. Access the chat window.
  2. Add an MCP server.
  3. Disconnect from the MCP server.

After disconnecting, one CPU core gets stuck at 100% utilization due to an async thread.

I found that except Exception: pass in disconnect_mcp() hides an exception Attempted to exit cancel scope in a different task than it was entered in, which may causes the issue, I think.

ref: modelcontextprotocol/python-sdk#521

Expected behavior

The MCP server should disconnect properly (without exhausting a CPU core).

Screenshots
If applicable, add screenshots to help explain your problem.

Image

Additional context

I ran chainlit==2.5.5 with mcp==1.9.0 on ubuntu 24.04.

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendPertains to the Python backend.bugSomething isn't workingneeds-triagestaleIssue has not had recent activity or appears to be solved. Stale issues will be automatically closed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions