Skip to content

Build and run agents you can see, understand and trust.

fix(mcp): support to raise the exception on MCP disconnection (#1024)

MengsDa5924924085c
diff --git a/src/agentscope/mcp/_stateful_client_base.py b/src/agentscope/mcp/_stateful_client_base.py
index 88ca44034..d88e5b69b 100644
--- a/src/agentscope/mcp/_stateful_client_base.py
+++ b/src/agentscope/mcp/_stateful_client_base.py
@@ -69,9 +69,14 @@ class StatefulClientBase(MCPClientBase, ABC):
self.stack = None
raise
- async def close(self) -> None:
+ async def close(self, ignore_errors: bool = True) -> None:
"""Clean up the MCP client resources. You must call this method when
- your application is done."""
+ your application is done.
+
+ Args:
+ ignore_errors (`bool`):
+ Whether to ignore errors during cleanup. Defaults to `True`.
+ """
if not self.is_connected:
raise RuntimeError(
"The MCP server is not connected. Call connect() before "
@@ -80,8 +85,9 @@ class StatefulClientBase(MCPClientBase, ABC):
try:
await self.stack.aclose()
- logger.info("MCP client closed.")
except Exception as e:
+ if not ignore_errors:
+ raise e
logger.warning("Error during MCP client cleanup: %s", e)
finally:
self.stack = None