Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(app): reject an MCP name the library cannot load back (#3025)
lihongyuan9972f3f6fa0b2f
diff --git a/src/agentscope/app/_router/_mcp.py b/src/agentscope/app/_router/_mcp.pyindex 697db23f2..71b4fcdfd 100644--- a/src/agentscope/app/_router/_mcp.py+++ b/src/agentscope/app/_router/_mcp.py@@ -7,11 +7,13 @@ Installing from a hub only writes here; putting an MCP into a workspacestays a separate, explicit act."""from fastapi import APIRouter, Depends, HTTPException, status+from pydantic import ValidationErrorfrom ..deps import get_current_user_id, get_mcp_hubs, get_storagefrom ..hub import MCPHubBasefrom .._service import MCPRenderError, render_mcpfrom ..storage import StorageBase+from ...mcp import MCPClientfrom ._schema import MCPView, UpdateMCPRequestmcp_router = APIRouter(prefix="/mcp", tags=["mcp"])@@ -44,6 +46,12 @@ async def update_mcp(sit anywhere in the config — inside a URL, a header, an env var — sothe card's template plus the merged answers is the only thing thatknows where to put it.++ Raises:+ `HTTPException`: 404 if the MCP is not in the caller's library,+ 400 if the MCP has no card to re-render from, 409 if the new+ name is taken, or 422 if the name is not made of+ ``[a-zA-Z0-9_-]``."""record = await storage.get_mcp(user_id, mcp_id)if record is None:@@ -90,7 +98,20 @@ async def update_mcp(record.values = mergedrecord.version = card.versionelif body.name is not None:- record.client.name = body.name+ # ``MCPClient`` checks its name in ``model_post_init``, which a+ # plain attribute assignment skips; rebuild the client through+ # ``model_validate`` so an illegal name is refused here. Stored+ # as-is it would fail the same check on the next read, turning+ # every route that loads the library into a 500.+ try:+ record.client = MCPClient.model_validate(+ {**record.client.model_dump(), "name": body.name},+ )+ except ValidationError as exc:+ raise HTTPException(+ status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,+ detail=str(exc),+ ) from excif body.enabled is not None:record.enabled = body.enableddiff --git a/tests/mcp_rename_test.py b/tests/mcp_rename_test.pynew file mode 100644index 000000000..b69db8d41--- /dev/null+++ b/tests/mcp_rename_test.py@@ -0,0 +1,128 @@+# -*- coding: utf-8 -*-+"""MCP library rename test cases."""+import tempfile+from typing import Any+from unittest import IsolatedAsyncioTestCase++import fakeredis.aioredis+from fastapi.testclient import TestClient++from agentscope.app import create_app+from agentscope.app.message_bus import RedisMessageBus+from agentscope.app.storage import MCPRecord, RedisStorage+from agentscope.app.workspace_manager import LocalWorkspaceManager+from agentscope.mcp import HttpMCPConfig, MCPClient++HEADERS = {"X-User-ID": "alice"}+++def _fake_storage() -> Any:+ """Build a fakeredis-backed storage and message bus."""+ redis = fakeredis.aioredis.FakeRedis(decode_responses=True)++ class _Storage(RedisStorage):+ """Storage sharing the in-memory client."""++ async def __aenter__(self) -> Any:+ self._client = redis+ return self++ async def aclose(self) -> None:+ self._client = None++ class _Bus(RedisMessageBus):+ """Message bus sharing the in-memory client."""++ async def __aenter__(self) -> Any:+ self._client = redis+ return self++ async def aclose(self) -> None:+ self._client = None++ return _Storage(), _Bus()+++class MCPLibraryRenameTest(IsolatedAsyncioTestCase):+ """``PATCH /mcp/{mcp_id}`` must not store a name the library cannot+ load back."""++ def setUp(self) -> None:+ """Start an app holding one installed MCP named ``echo``."""+ # pylint: disable=consider-using-with+ workdir = self.enterContext(tempfile.TemporaryDirectory())+ self._storage, bus = _fake_storage()+ app = create_app(+ storage=self._storage,+ message_bus=bus,+ workspace_manager=LocalWorkspaceManager(workdir),+ enable_index_worker=False,+ )+ self._client = self.enterContext(TestClient(app))++ async def asyncSetUp(self) -> None:+ """Install the fixture MCP straight into storage."""+ record = MCPRecord(+ user_id="alice",+ client=MCPClient(+ name="echo",+ is_stateful=False,+ mcp_config=HttpMCPConfig(url="https://example.com/mcp"),+ ),+ )+ await self._storage.upsert_mcp("alice", record)+ self._mcp_id = record.id++ def _view(self, name: str) -> dict:+ """The expected ``MCPView`` of the fixture MCP under *name*."""+ return {+ "id": self._mcp_id,+ "name": name,+ "is_stateful": False,+ "enabled": True,+ "display_name": None,+ "description": "",+ "tags": [],+ "author": None,+ "icon_url": None,+ "url": None,+ "hub_id": None,+ "card_id": None,+ "version": None,+ }++ async def test_rename_to_unloadable_name_is_rejected(self) -> None:+ """A name ``MCPClient`` would refuse on load answers 422 and leaves+ the library readable."""+ response = self._client.patch(+ f"/mcp/{self._mcp_id}",+ json={"name": "io.github.upstash/context7"},+ headers=HEADERS,+ )+ self.assertEqual(response.status_code, 422)++ listing = self._client.get("/mcp", headers=HEADERS)+ self.assertEqual(listing.status_code, 200)+ self.assertEqual(listing.json(), [self._view("echo")])++ async def test_rename_to_empty_name_is_rejected(self) -> None:+ """An empty name is rejected rather than stored."""+ response = self._client.patch(+ f"/mcp/{self._mcp_id}",+ json={"name": ""},+ headers=HEADERS,+ )+ self.assertEqual(response.status_code, 422)++ async def test_legal_rename_persists(self) -> None:+ """A name within ``[a-zA-Z0-9_-]+`` still renames the MCP."""+ response = self._client.patch(+ f"/mcp/{self._mcp_id}",+ json={"name": "echo_2"},+ headers=HEADERS,+ )+ self.assertEqual(response.status_code, 200)+ self.assertEqual(response.json(), self._view("echo_2"))++ listing = self._client.get("/mcp", headers=HEADERS)+ self.assertEqual(listing.json(), [self._view("echo_2")])