"Event loop is closed" error when calling MCP Tools

Hi everyone,

I’m still struggling with an “Event loop is closed” error when my CrewAI agent uses a tool. This tool is a tool on my MCP server.

The Core Problem:

When a CrewAI task invokes a tool (e.g., fetch_notes), I get the following error from CrewAI:

╭────────────────────────────────────────────────────────────────────────────────────── Tool Error ──────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                                                                        │
│  Tool Usage Failed                                                                                                                                                                     │
│  Name: fetch_notes                                                                                                                                                                     │
│  Error: Event loop is closed                                                                                                                                                           │
│                                                                                                                                                                                        │
│                                                                                                                                                                                        │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

And in my console logs, I see related messages about an unawaited coroutine, which may be a symptom of the underlying event loop issue:

Exception ignored in: <coroutine object ClientSession.call_tool at 0x14ea2c820>
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/warnings.py", line 555, in _warn_unawaited_coroutine
    warn(msg, category=RuntimeWarning, stacklevel=2, source=coro)
RuntimeWarning: coroutine 'ClientSession.call_tool' was never awaited

My Setup:

  1. FastAPI Server :

    • The server is a FastAPI application with a fastmcp app mounted.
    • The application running on my FastAPI server calls to my FastMCP server.

    Server main.py:

    from fastapi import FastAPI
    from ai.mcp.mcp_server import mcp_app # This is the FastMCP.http_app()
    from ai.routes.routes import router # Other app routes
    
    app = FastAPI(lifespan=mcp_app.lifespan)
    app.mount("/mcp-server", mcp_app) # Mounting the MCP server
    app.include_router(router=router)
    

    Example Synchronous MCP Tool on Server (mcp.py):

    
    @mcp.tool()
    def fetch_notes(patientId: str, ctx: Context) -> NoteResponse: 
        token = get_access_token()
        # ... auth checks ...
        url = f"http://EXAMPLE.url"
        headers = {"Authorization": token.token}
        resp = requests.get(url, headers=headers, timeout=10)
        resp.raise_for_status()
        return resp.json() 
    
    
    
  2. CrewAI MCP Adapter:

        server_params = {
            "url": "http://localhost:8000/mcp-server/mcp",
            "transport": "streamable-http",
            "headers": {"Authorization": raw.get("token")},
        }
        try:
            with MCPServerAdapter(server_params) as mcp_tools:
                print(f"Available tools: {[tool.name for tool in mcp_tools]}")
    
    
    

Available tools prints fine, listing all the tools the agent has at it’s disposal correctly. However, the error occurs when calling the tools.

Thanks for any insights!


I ran into the same issue when using MCP Tool Adapter with STDIO. Any ideas on how to deal with this? They randomly happened, not always.

I encountered an error while trying to use the tool. This was the error: Event loop is closed.
 Tool execute_sql_tool accepts these inputs: Tool Name: execute_sql_tool
Tool Arguments: {'properties': {'sql': {'anyOf': [], 'description': 'The sql to execute.', 'enum': None, 'items': None, 'properties': {}, 'title': '', 'type': 'string'}}, 'required': ['sql'], 'title': 'DynamicModel', 'type': 'object'}
Tool Description: Use this tool to execute sql statement.

I have the same issue -Windows machine and venv setup! Even tried to implement the basic add example using STDIO. Always returns this error.

╭─────────────────────────────────────────────────────────────────────────────── Tool Error ───────────────────────────────────────────────────────────────────────────────╮
│ │
│ Tool Usage Failed │
│ Name: add │
│ Error: Event loop is closed │
│ │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

I encountered an error while trying to use the tool. This was the error: Event loop is closed.
Tool add accepts these inputs: Tool Name: add
Tool Arguments: {‘properties’: {‘a’: {‘anyOf’: , ‘description’: ‘’, ‘enum’: None, ‘items’: None, ‘properties’: {}, ‘title’: ‘A’, ‘type’: ‘number’}, ‘b’: {‘anyOf’: , ‘description’: ‘’, ‘enum’: None, ‘items’: None, ‘properties’: {}, ‘title’: ‘B’, ‘type’: ‘number’}}, ‘required’: [‘a’, ‘b’], ‘title’: ‘addArguments’, ‘type’: ‘object’}
Tool Description: Add two numbers (ints or floats)

.venv\Lib\site-packages\crewai\tools\tool_usage.py:265: RuntimeWarning: coroutine ‘ClientSession.call_tool’ was never awaited
return error # type: ignore # No return value expected
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
.venv\Lib\site-packages\crewai\tools\tool_usage.py:269: RuntimeWarning: coroutine ‘ClientSession.call_tool’ was never awaited
return self.use(calling=calling, tool_string=tool_string) # type: ignore # No return value expected
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

I managed to get it to work using this approach. Define a Manual connection to start and stop it, then before kicking off the crew start the mcp server and after close it.

e.g. in the crew define two functions.
mcp_server_adapter = MCPServerAdapter(server_params)
mcp_tools =

def start_mcp_adapter():
global mcp_tools
try:
mcp_server_adapter.start()
mcp_tools = mcp_server_adapter.tools
print(f"Available tools: {[tool.name for tool in mcp_tools]}“)
except Exception as e:
print(f"Error starting MCP adapter: {e}”)
mcp_tools =

def stop_mcp_adapter():
if mcp_server_adapter and getattr(mcp_server_adapter, ‘is_connected’, False):
print(“Stopping MCP server connection…”)
mcp_server_adapter.stop()
elif mcp_server_adapter:
print(“MCP server adapter was not connected. No stop needed or start failed.”)

then before kicking off the crew start it and when it’s finished stop it.
start_mcp_adapter()
try:
kickoff crew…
finally:
stop_mcp_adapter()

Thanks, this worked for me also.

A few weeks ago I added an official way to use MCP Server with CrewBase, check out here.

Let me know if you still facing issues, pls

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.