[BUG] MCPToolResolver cleanup, failure disconnect, and external schema discovery fail inside running asyncio event loops
Maintainer thường phản hồi trong vòng 1 ngày
@iridite đang làm issue này rồi.
Từ ngày 27/11/2026.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Start with lib/crewai/src/crewai/mcp/tool_resolver.py and trace _resolve_native(), cleanup(), and _get_mcp_tool_schemas() alongside the existing event loop bridge. Reproduce the failures inside a running asyncio loop, including a client whose disconnect raises. Done when all three paths work in that context and cleanup attempts each client; the issue links open pull request #9847.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
MCPToolResolver._resolve_native() checks asyncio.get_running_loop() and bridges execution through a ThreadPoolExecutor + contextvars.copy_context() when called from an active asyncio event loop. However, three other call sites in MCPToolResolver (lib/crewai/src/crewai/mcp/tool_resolver.py) still invoke asyncio.run(...) directly:
MCPToolResolver.cleanup()callsasyncio.run(_disconnect_all())directly. Inside an active event loop (e.g.,akickoff(), async Flows, async web services, orpytest-asyncio),asyncio.run()raisesRuntimeError: asyncio.run() cannot be called from a running event loop.cleanup()catches theRuntimeError, logs it, clearsself._clients, and leaves connected MCP clients undisconnected. Additionally,_disconnect_all()iteratesself._clientswithout per-client error isolation, so an exception on an earlier client skips disconnecting remaining clients.MCPToolResolver._get_mcp_tool_schemas()callsasyncio.run(self._get_mcp_tool_schemas_async(server_params))directly. When resolving HTTPS MCP server URLs (resolve(["https://..."])) inside a running loop,asyncio.run()hitsRuntimeError, is caught byexcept Exception, logs a warning, and silently returns{}(zero tools discovered).MCPToolResolver._resolve_native()exception unwinding (except Exception as e: if discovery_client.connected:) callsasyncio.run(discovery_client.disconnect())directly when a failure occurs after discovery whilediscovery_client.connectedis stillTrue.
Steps to Reproduce
import asyncio
from unittest.mock import AsyncMock, MagicMock, patch
from crewai.agent.core import Agent
from crewai.mcp.tool_resolver import MCPToolResolver
async def main() -> None:
agent = Agent(role="Test", goal="Test", backstory="Test")
resolver = MCPToolResolver(agent=agent, logger=agent._logger)
client = MagicMock()
client.connected = True
client.disconnect = AsyncMock()
resolver._clients.append(client)
resolver.cleanup()
print("disconnected calls:", client.disconnect.await_count) # Prints 0 instead of 1
with patch.object(
resolver,
"_get_mcp_tool_schemas_async",
new=AsyncMock(return_value={"search": {"description": "Search", "args_schema": None}}),
):
print("schemas:", resolver._get_mcp_tool_schemas({"url": "https://mcp.example.com/api"}))
# Prints {} instead of {"search": ...}
asyncio.run(main())
Expected behavior
cleanup(), _get_mcp_tool_schemas(), and _resolve_native() failure cleanup should work safely when called inside an already-running asyncio event loop (via the same ThreadPoolExecutor + contextvars.copy_context() bridge already used for native discovery), and cleanup() should attempt disconnecting every client even if one client raises.
Screenshots/Code snippets
See minimal reproduction script above.
Operating System
macOS Sonoma
Python Version
3.12
crewAI Version
1.15.45
crewAI Tools Version
N/A
Virtual Environment
Venv
Evidence
Reproduced locally on main (77237ae):
cleanup()inside an active loop logsRuntimeError: asyncio.run() cannot be called from a running event loopand awaitsdisconnect()0 times._get_mcp_tool_schemas()inside an active loop logs theRuntimeErrorwarning and returns{}.
Possible Solution
Extract a shared _run_async helper on MCPToolResolver that delegates to asyncio.run() inside a ThreadPoolExecutor with copied contextvars whenever an event loop is already running, use it across cleanup(), _resolve_native(), and _get_mcp_tool_schemas(), and wrap per-client disconnect() inside cleanup() with individual try/except.
Additional context
Note: AI-assisted diagnosis and regression test verification (labeled llm-generated per CONTRIBUTING.md).
- Ngôn ngữ chính
- Python
- Star
- 61.2k
- Fork
- 9.2k
- Merge trung bình
- 1 ngày 14 giờ
- Pull request đã merge (30 ngày)
- 180
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của crewAIInc/crewAI
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[FEATURE] Add Straitly as a native OpenAI-compatible LLM providerCó thể đã có người làm @HashimSyedUBC đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG] Native OpenAI sync streaming with response_model returns an empty string and emits no completed event when the parse is missingCó thể đã có người làm @basil-k-aji-dev đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG] Native Azure provider ignores the seed settingCó thể đã có người làm @basil-k-aji-dev đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
crewAIInc/crewAI#9787 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG] LiteLLM path reports 0 cached and reasoning tokens in the usage summaryCó thể đã có người làm @basil-k-aji-dev đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của crewAIInc/crewAI
Issue tương tự
-
[BUG] SMTP listener now binds to EMAIL_PORT instead of 2525 since the gunicorn switch 5.0.3-ls373Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
linuxserver/docker-healthchecks#137 · 1 bình luận ·
-
RequirementAgent treats list -> add -> list as a tool call cycle and drops the second list call (Python)Có thể đã có người làm @chaitanya0305 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
i-am-bee/beeai-framework#2049 · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
fedora-infra/anitya#2272 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
unitaryfoundation/mitiq#3205 ·
Maintainer thường phản hồi trong vòng 6 ngày
-
[Bug]: Index worker test drops empty Git config values on WindowsCó thể đã có người làm @xiaoyesoso đã nhận hôm nay. Đang mởtriage/confirmed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
agentscope-ai/agentscope#4377 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày