From 6f1195ef746b8ee1e17b7daba9ffc2e5fd70a9df Mon Sep 17 00:00:00 2001 From: Janardhan Date: Wed, 25 Mar 2026 12:58:42 +0530 Subject: [PATCH] Fix deadlock in MCPTool.message_handler when server sends tool/prompt list_changed notifications MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit message_handler was awaiting load_tools()/load_prompts() inline on notifications/tools/list_changed. load_tools() calls _ensure_connected() which sends a ping, but the session cannot process it while blocked waiting for an in-flight call_tool response — causing a deadlock. Replace inline reloads with debug logging. Tools/prompts will be refreshed on the next connect(reset=True) or explicit load_tools() call. Fixes #4896 --- python/packages/core/agent_framework/_mcp.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/python/packages/core/agent_framework/_mcp.py b/python/packages/core/agent_framework/_mcp.py index 0c241cb89a..378e63ba96 100644 --- a/python/packages/core/agent_framework/_mcp.py +++ b/python/packages/core/agent_framework/_mcp.py @@ -707,9 +707,14 @@ async def message_handler( if isinstance(message, types.ServerNotification): match message.root.method: case "notifications/tools/list_changed": - await self.load_tools() + # Only log here. Calling load_tools() — even as a background task — + # causes a deadlock: load_tools() → _ensure_connected() → send_ping(), + # but the session cannot process the ping while an in-flight call_tool + # request is waiting for its response. Tools will be up-to-date on the + # next explicit load_tools() or connect(reset=True) call. + logger.debug("Tools list changed notification received; reload on next connect.") case "notifications/prompts/list_changed": - await self.load_prompts() + logger.debug("Prompts list changed notification received; reload on next connect.") case _: logger.debug("Unhandled notification: %s", message.root.method)