Skip to content

Commit f171760

Browse files
committed
fix: Handle stale MCP connections in DevUI executor
MCP tools can become stale when HTTP streaming responses end - the underlying stdio streams close but `is_connected` remains True. This causes subsequent requests to fail with `ClosedResourceError`. Add `_ensure_mcp_connections()` to detect and reconnect stale MCP tools before agent execution. This is a workaround for an upstream Agent Framework issue where connection state isn't properly tracked. Fixes MCP tools failing on second HTTP request in DevUI. fixes #1476 #1515 #2865
1 parent 06c7209 commit f171760

10 files changed

Lines changed: 4827 additions & 711 deletions

File tree

‎python/packages/devui/agent_framework_devui/_executor.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,65 @@ def _setup_agent_framework_tracing(self) -> None:
116116
else:
117117
logger.debug("ENABLE_OTEL not set, skipping observability setup")
118118

119+
async def _ensure_mcp_connections(self, agent: Any) -> None:
120+
"""Ensure MCP tool connections are healthy before agent execution.
121+
122+
This is a workaround for an Agent Framework bug where MCP tool connections
123+
can become stale (underlying streams closed) but is_connected remains True.
124+
This happens when HTTP streaming responses end and GeneratorExit propagates.
125+
126+
This method detects stale connections and reconnects them. It's designed to
127+
be a no-op once the Agent Framework fixes this issue upstream.
128+
129+
Args:
130+
agent: Agent object that may have MCP tools
131+
"""
132+
if not hasattr(agent, "_local_mcp_tools"):
133+
return
134+
135+
for mcp_tool in agent._local_mcp_tools:
136+
if not getattr(mcp_tool, "is_connected", False):
137+
continue
138+
139+
tool_name = getattr(mcp_tool, "name", "unknown")
140+
141+
try:
142+
# Check if underlying write stream is closed
143+
session = getattr(mcp_tool, "session", None)
144+
if session is None:
145+
continue
146+
147+
write_stream = getattr(session, "_write_stream", None)
148+
if write_stream is None:
149+
continue
150+
151+
# Detect stale connection: is_connected=True but stream is closed
152+
is_closed = getattr(write_stream, "_closed", False)
153+
if not is_closed:
154+
continue # Connection is healthy
155+
156+
# Stale connection detected - reconnect
157+
logger.warning(f"MCP tool '{tool_name}' has stale connection (stream closed), reconnecting...")
158+
159+
# Clean up old connection
160+
try:
161+
if hasattr(mcp_tool, "close"):
162+
await mcp_tool.close()
163+
except Exception as close_err:
164+
logger.debug(f"Error closing stale MCP tool '{tool_name}': {close_err}")
165+
# Force reset state
166+
mcp_tool.is_connected = False
167+
mcp_tool.session = None
168+
169+
# Reconnect
170+
if hasattr(mcp_tool, "connect"):
171+
await mcp_tool.connect()
172+
logger.info(f"MCP tool '{tool_name}' reconnected successfully")
173+
174+
except Exception as e:
175+
# If detection fails, log and continue - let it fail naturally during execution
176+
logger.debug(f"Error checking MCP tool '{tool_name}' connection: {e}")
177+
119178
async def discover_entities(self) -> list[EntityInfo]:
120179
"""Discover all available entities.
121180
@@ -276,6 +335,12 @@ async def _execute_agent(
276335
logger.debug(f"Executing agent with text input: {user_message[:100]}...")
277336
else:
278337
logger.debug(f"Executing agent with multimodal ChatMessage: {type(user_message)}")
338+
339+
# Workaround for MCP tool stale connection bug (GitHub issue pending)
340+
# When HTTP streaming ends, GeneratorExit can close MCP stdio streams
341+
# but is_connected stays True. Detect and reconnect before execution.
342+
await self._ensure_mcp_connections(agent)
343+
279344
# Check if agent supports streaming
280345
if hasattr(agent, "run_stream") and callable(agent.run_stream):
281346
# Use Agent Framework's native streaming with optional thread

‎python/packages/devui/agent_framework_devui/ui/assets/index.css‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎python/packages/devui/agent_framework_devui/ui/assets/index.js‎

Lines changed: 118 additions & 118 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)