Skip to content

Commit a592492

Browse files
MengsDDavdGao
andauthored
fix(mcp): support to raise the exception on MCP disconnection (#1024)
--------- Co-authored-by: DavdGao <[email protected]>
1 parent f6aa138 commit a592492

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/agentscope/mcp/_stateful_client_base.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ async def connect(self) -> None:
6969
self.stack = None
7070
raise
7171

72-
async def close(self) -> None:
72+
async def close(self, ignore_errors: bool = True) -> None:
7373
"""Clean up the MCP client resources. You must call this method when
74-
your application is done."""
74+
your application is done.
75+
76+
Args:
77+
ignore_errors (`bool`):
78+
Whether to ignore errors during cleanup. Defaults to `True`.
79+
"""
7580
if not self.is_connected:
7681
raise RuntimeError(
7782
"The MCP server is not connected. Call connect() before "
@@ -80,8 +85,9 @@ async def close(self) -> None:
8085

8186
try:
8287
await self.stack.aclose()
83-
logger.info("MCP client closed.")
8488
except Exception as e:
89+
if not ignore_errors:
90+
raise e
8591
logger.warning("Error during MCP client cleanup: %s", e)
8692
finally:
8793
self.stack = None

0 commit comments

Comments
 (0)