Skip to content

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

fix(workspace): read the persisted .mcp file as utf-8-sig (#3338)

ordinary being402c3664f70c
diff --git a/src/agentscope/workspace/_base.py b/src/agentscope/workspace/_base.py
index c769bd350..ce10d213c 100644
--- a/src/agentscope/workspace/_base.py
+++ b/src/agentscope/workspace/_base.py
@@ -951,7 +951,8 @@ class WorkspaceBase:
if not await backend.file_exists(self._mcp_file):
return {}
raw = await backend.read_file(self._mcp_file)
- data = json.loads(raw.decode("utf-8"))
+ # Accept a leading UTF-8 BOM from external editors.
+ data = json.loads(raw.decode("utf-8-sig"))
except Exception as e:
logger.warning(
"Failed to read MCP file at %s, falling back to "
diff --git a/tests/workspace_local_test.py b/tests/workspace_local_test.py
index ccc4e858f..2b7a55ece 100644
--- a/tests/workspace_local_test.py
+++ b/tests/workspace_local_test.py
@@ -1818,6 +1818,38 @@ class TestLocalWorkspaceMCPScoping(IsolatedAsyncioTestCase):
["seed"],
)
+ async def test_mcp_file_with_utf8_bom_is_restored(self) -> None:
+ """A BOM-prefixed .mcp still restores the persisted declarations.
+
+ The file is ordinary JSON in the workspace; a Windows editor may
+ re-save it with a byte order mark. Plain UTF-8 used to fail to
+ parse, silently dropping every declaration and falling back to
+ ``default_mcps``.
+ """
+ ws = await self._workspace(default_mcps=[self._make_mcp("seed")])
+ await ws.add_mcp(
+ self._make_mcp("extra"),
+ agent_id="agent-A",
+ session_id="sess-1",
+ )
+ with open(self.mcp_file, "rb") as f:
+ payload = f.read()
+ with open(self.mcp_file, "wb") as f:
+ f.write(b"\xef\xbb\xbf" + payload)
+ await ws.close()
+
+ ws2 = await self._workspace(default_mcps=[self._make_mcp("seed")])
+ self.assertEqual(
+ [
+ m.name
+ for m in await ws2.list_mcps(
+ agent_id="agent-A",
+ session_id="sess-1",
+ )
+ ],
+ ["seed", "extra"],
+ )
+
async def test_reset_returns_to_factory_defaults(self) -> None:
"""``reset`` drops ``.mcp``, so defaults are seeded again."""
ws = await self._workspace(default_mcps=[self._make_mcp("seed")])