{"record":{"id":"76863ae654c944ca","repo":"agentscope-ai/agentscope","slug":"name-r-already-exists-for-agent-agent-id-r-ses","errorCode":null,"errorMessage":"{name!r} already exists for agent={agent_id!r} session={session_id!r}","messagePattern":"(.+?) already exists for agent=(.+?) session=(.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":409,"severity":"warning","filePath":"src/agentscope/workspace/_mcp_gateway/_mcp_gateway_app.py","lineNumber":129,"sourceCode":"        return [\n            c.model_dump(mode=\"json\")\n            for c in state.clients.get((agent_id, session_id), {}).values()\n        ]\n\n    @app.post(\"/mcps\")\n    async def _add_mcp(\n        request: Request,\n        agent_id: str = \"\",\n        session_id: str = \"\",\n    ) -> dict[str, Any]:\n        body = await request.json()\n        name = body.get(\"name\", \"\")\n        if not name:\n            raise HTTPException(400, \"name required\")\n        async with state.lock:\n            by_name = state.clients.setdefault((agent_id, session_id), {})\n            if name in by_name:\n                raise HTTPException(\n                    409,\n                    f\"{name!r} already exists for agent={agent_id!r} \"\n                    f\"session={session_id!r}\",\n                )\n            try:\n                by_name[name] = await _build_client(body)\n            except HTTPException:\n                raise\n            except Exception as e:  # noqa: BLE001\n                raise HTTPException(500, f\"connect failed: {e}\") from e\n        return {\"ok\": True}\n\n    @app.delete(\"/mcps/{name}\")\n    async def _remove_mcp(\n        name: str,\n        agent_id: str = \"\",\n        session_id: str = \"\",\n    ) -> dict[str, Any]:","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/workspace/_mcp_gateway/_mcp_gateway_app.py#L111-L147","documentation":"Raised as HTTP 409 by _add_mcp when the requested name already exists in the registry for the same (agent_id, session_id). The gateway disallows duplicate names within one session so /mcps/{name} stays unambiguous.","triggerScenarios":"POST /mcps with a name already added for the same agent_id/session_id, e.g. re-running registration code without removing the previous entry or without checking existence first.","commonSituations":"Retried registration after a network hiccup, hot-reload of client code that re-registers on every startup, or copy-pasted setup blocks registering the same server twice.","solutions":["DELETE /mcps/{name} before re-adding, or pick a unique name per registration.","Make registration idempotent: catch 409 and treat it as success if the config is unchanged.","List existing MCPs first and skip names already registered."],"exampleFix":"# before\nawait client.post(\"/mcps\", json={\"name\": \"my-server\", ...})  # 2nd time -> 409\n\n# after\nr = await client.post(\"/mcps\", json={\"name\": \"my-server\", ...})\nif r.status_code == 409:\n    await client.delete(\"/mcps/my-server\")\n    r = await client.post(\"/mcps\", json={\"name\": \"my-server\", ...})","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"r = await client.post(\"/mcps\", json=config)\nif r.status_code == 409:\n    await client.delete(f\"/mcps/{config['name']}\")\n    r = await client.post(\"/mcps\", json=config)","preventionTips":["Make registration idempotent: treat 409 as already-registered.","Use unique names per session or delete before re-add during dev hot reload.","List existing registrations before adding duplicates."],"tags":["agentscope","mcp","gateway","http-409","duplicate"],"backgroundTag":"duplicate-resource-conflict","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}