Skip to content

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

Refine tutorial for multiple stateful MCP clients (#706)

qbc6ee8fa352bca
diff --git a/docs/tutorial/en/src/task_mcp.py b/docs/tutorial/en/src/task_mcp.py
index 2db57c1fb..58e51e601 100644
--- a/docs/tutorial/en/src/task_mcp.py
+++ b/docs/tutorial/en/src/task_mcp.py
@@ -53,6 +53,7 @@ from agentscope.tool import Toolkit
#
# .. note:: - The StdIO MCP server only has stateful client, when ``connect()`` is called, it will start the MCP server locally and then connect to it.
# - For stateful clients, developers must ensure the client is connected when calling the tool functions.
+# - When multiple `HttpStatefulClients` or `StdIOStatefulClients` are connected, they should be closed in Last In First Out (LIFO) order to prevent errors.
#
# Taking Gaode map MCP server as an example, the creation of stateful and stateless clients are very similar:
#
diff --git a/docs/tutorial/zh_CN/src/task_mcp.py b/docs/tutorial/zh_CN/src/task_mcp.py
index 666fe0da9..2a5d3c3d5 100644
--- a/docs/tutorial/zh_CN/src/task_mcp.py
+++ b/docs/tutorial/zh_CN/src/task_mcp.py
@@ -55,6 +55,7 @@ from agentscope.tool import Toolkit
#
# .. note:: - StdIO MCP 服务器只有有状态客户端,当调用 ``connect()`` 时,它将在本地启动 MCP 服务器然后连接到它。
# - 对于有状态客户端,开发者必须确保在调用工具函数时客户端已连接。
+# - 当有多个 `HttpStatefulClient` 或 `StdIOStatefulClient` 建立连接时,应按照后进先出 (LIFO) 的顺序关闭它们以避免引发错误。
#
# 以高德地图 MCP 服务器为例,有状态和无状态客户端的创建非常相似:
#
diff --git a/examples/agent_deep_research/deep_research_agent.py b/examples/agent_deep_research/deep_research_agent.py
index c86766147..a3fc880f1 100644
--- a/examples/agent_deep_research/deep_research_agent.py
+++ b/examples/agent_deep_research/deep_research_agent.py
@@ -1067,7 +1067,7 @@ class DeepResearchAgent(ReActAgent):
you have done so far.
Args:
- _response (`str`):
+ response (`str`):
Your response to the user.
"""
checklist = self.current_subtask[0].knowledge_gaps
diff --git a/src/agentscope/mcp/_http_stateful_client.py b/src/agentscope/mcp/_http_stateful_client.py
index 05f37438d..9c13d8c84 100644
--- a/src/agentscope/mcp/_http_stateful_client.py
+++ b/src/agentscope/mcp/_http_stateful_client.py
@@ -19,6 +19,13 @@ class HttpStatefulClient(StatefulClientBase):
.. note:: The stateful client will maintain one session across multiple
tool calls, until the client is closed by explicitly calling the
`close()` method.
+
+ .. note:: When multiple HttpStatefulClient instances are connected,
+ they should be closed following the Last In First Out (LIFO) principle
+ to avoid potential errors. Always close the most recently registered
+ client first, then work backwards to the first one.
+ For more details, please refer to this `issue
+ <https://github.com/modelcontextprotocol/python-sdk/issues/577>`_.
"""
def __init__(
diff --git a/src/agentscope/mcp/_stdio_stateful_client.py b/src/agentscope/mcp/_stdio_stateful_client.py
index 71e13e33d..4a134c78a 100644
--- a/src/agentscope/mcp/_stdio_stateful_client.py
+++ b/src/agentscope/mcp/_stdio_stateful_client.py
@@ -19,6 +19,13 @@ class StdIOStatefulClient(StatefulClientBase):
.. note:: The stateful client will maintain one session across multiple
tool calls, until the client is closed by explicitly calling the
`close()` method.
+
+ .. note:: When multiple StdIOStatefulClient instances are connected,
+ they should be closed following the Last In First Out (LIFO) principle
+ to avoid potential errors. Always close the most recently registered
+ client first, then work backwards to the first one.
+ For more details, please refer to this `issue
+ <https://github.com/modelcontextprotocol/python-sdk/issues/577>`_.
"""
def __init__(
@@ -52,7 +59,7 @@ class StdIOStatefulClient(StatefulClientBase):
encoding (`str`, optional):
The text encoding used when sending/receiving messages to the
server. Defaults to "utf-8".
- encoding_error_handler (`Literal["strict", "ignore", "replace"]`,
+ encoding_error_handler (`Literal["strict", "ignore", "replace"]`, \
defaults to "strict"):
The text encoding error handler.
"""