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:
-
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()
- The server is a FastAPI application with a
-
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!