Skip to content

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

fix(workspace): return the registered client from add_mcp (#2781)

qbc28ff9fdd5d1f
diff --git a/src/agentscope/workspace/_base.py b/src/agentscope/workspace/_base.py
index dbff9e778..db64755d1 100644
--- a/src/agentscope/workspace/_base.py
+++ b/src/agentscope/workspace/_base.py
@@ -665,7 +665,7 @@ class WorkspaceBase:
*,
agent_id: str | None = None,
session_id: str | None = None,
- ) -> None:
+ ) -> MCPClient:
"""Register a new MCP server for one agent/session and persist.
Implementations record the config in :attr:`_mcp_specs`, start
@@ -682,6 +682,12 @@ class WorkspaceBase:
session_id (`str | None`, optional):
The owning session. ``None`` means the legacy ``""``.
+ Returns:
+ `MCPClient`:
+ The live client stored and used by the workspace, which may
+ differ from ``mcp_client`` for sandboxed implementations.
+ It can be evicted later, use :meth:`list_mcps` then.
+
Raises:
`ValueError`:
If the name already exists for this agent/session.
diff --git a/src/agentscope/workspace/_local_workspace.py b/src/agentscope/workspace/_local_workspace.py
index 49f2b72f5..5aa8c4716 100644
--- a/src/agentscope/workspace/_local_workspace.py
+++ b/src/agentscope/workspace/_local_workspace.py
@@ -683,7 +683,7 @@ class LocalWorkspace(WorkspaceBase):
*,
agent_id: str | None = None,
session_id: str | None = None,
- ) -> None:
+ ) -> MCPClient:
"""Add an MCP client for one agent/session and persist it.
Args:
@@ -694,6 +694,10 @@ class LocalWorkspace(WorkspaceBase):
session_id (`str | None`, optional):
The owning session. ``None`` means the legacy ``""``.
+ Returns:
+ `MCPClient`:
+ The input client, which is stored and used directly.
+
Raises:
`ValueError`:
If the name already exists for this agent/session.
@@ -720,6 +724,7 @@ class LocalWorkspace(WorkspaceBase):
# persisted copy is self-contained.
self._mcp_specs[(agent_id, session_id)] = [*specs, mcp_client]
await self._save_mcp_file()
+ return mcp_client
async def remove_mcp(
self,
diff --git a/src/agentscope/workspace/_sandboxed_base.py b/src/agentscope/workspace/_sandboxed_base.py
index 19e970c80..b6f77ee93 100644
--- a/src/agentscope/workspace/_sandboxed_base.py
+++ b/src/agentscope/workspace/_sandboxed_base.py
@@ -318,7 +318,7 @@ class SandboxedWorkspaceBase(WorkspaceBase):
*,
agent_id: str | None = None,
session_id: str | None = None,
- ) -> None:
+ ) -> MCPClient:
"""Register a new MCP server through the in-sandbox gateway.
Args:
@@ -329,6 +329,10 @@ class SandboxedWorkspaceBase(WorkspaceBase):
session_id (`str | None`, optional):
The owning session. ``None`` means the legacy ``""``.
+ Returns:
+ `MCPClient`:
+ The connected gateway proxy stored by the workspace.
+
Raises:
`ValueError`:
If the name already exists for this agent/session.
@@ -362,6 +366,7 @@ class SandboxedWorkspaceBase(WorkspaceBase):
# persisted copy is self-contained.
self._mcp_specs[(agent_id, session_id)] = [*specs, mcp_client]
await self._save_mcp_file()
+ return client
async def remove_mcp(
self,
diff --git a/tests/workspace_daytona_test.py b/tests/workspace_daytona_test.py
index 2cbea32f6..a080e1c4e 100644
--- a/tests/workspace_daytona_test.py
+++ b/tests/workspace_daytona_test.py
@@ -1111,18 +1111,23 @@ class TestDaytonaWorkspaceBuiltinToolsMock(IsolatedAsyncioTestCase):
)
self.assertIn("/home/daytona/src/app.py", glob_text)
- async def test_mcp_add_remove_persists_mcp_file(self) -> None:
- """Dynamic MCP changes are reflected in the sandbox ``.mcp`` file."""
+ async def test_mcp_add_returns_proxy_and_persists_mcp_file(self) -> None:
+ """Adding an MCP returns the stored gateway proxy and persists it."""
mcp = MCPClient(
name="demo",
mcp_config=StdioMCPConfig(command="node", args=["server.js"]),
is_stateful=True,
)
- await self.workspace.add_mcp(mcp, agent_id="a", session_id="s")
+ registered = await self.workspace.add_mcp(
+ mcp,
+ agent_id="a",
+ session_id="s",
+ )
live = self.workspace._mcp_instances[("a", "s")]
- self.assertIn("demo", live)
+ self.assertIs(registered, live["demo"])
+ self.assertIsNot(registered, mcp)
raw = await self.workspace._backend.read_file("/home/daytona/.mcp")
data = json.loads(raw.decode("utf-8"))
self.assertEqual(data["mcps"]["a"]["s"][0]["name"], "demo")
diff --git a/tests/workspace_local_test.py b/tests/workspace_local_test.py
index 03f6474cf..9f5ee154c 100644
--- a/tests/workspace_local_test.py
+++ b/tests/workspace_local_test.py
@@ -1624,13 +1624,15 @@ class TestLocalWorkspaceMCPScoping(IsolatedAsyncioTestCase):
async def test_add_and_remove_are_scoped(self) -> None:
"""``add_mcp`` / ``remove_mcp`` touch only the calling scope."""
ws = await self._workspace(default_mcps=[self._make_mcp("seed")])
+ extra = self._make_mcp("extra")
- await ws.add_mcp(
- self._make_mcp("extra"),
+ registered = await ws.add_mcp(
+ extra,
agent_id="agent-A",
session_id="sess-1",
)
+ self.assertIs(registered, extra)
self.assertEqual(
[
m.name