diff --git a/litellm/proxy/agent_endpoints/endpoints.py b/litellm/proxy/agent_endpoints/endpoints.py index 89794fc2480..c73b60793d3 100644 --- a/litellm/proxy/agent_endpoints/endpoints.py +++ b/litellm/proxy/agent_endpoints/endpoints.py @@ -68,6 +68,7 @@ from litellm.proxy.agent_endpoints.managed_identity import raise_identity_failur from litellm.proxy.auth.user_api_key_auth import user_api_key_auth from litellm.proxy.common_utils.rbac_utils import check_feature_access_for_user from litellm.proxy.management_endpoints.common_daily_activity import get_daily_activity +from litellm.proxy.management_helpers.access_group_member_prune import remove_deleted_agent_from_access_groups from litellm.proxy.utils import PrismaClient, get_custom_url from litellm.repositories.chunked_in import find_many_in from litellm.types.agents import ( @@ -1041,6 +1042,7 @@ async def delete_agent( raise HTTPException(status_code=404, detail=f"Agent with ID {agent_id} not found in DB.") await AGENT_REGISTRY.delete_agent_from_db(agent_id=agent_id, prisma_client=prisma_client) + await remove_deleted_agent_from_access_groups(prisma_client, agent_id) AGENT_REGISTRY.deregister_agent(agent_name=existing_agent.get("agent_name")) diff --git a/litellm/proxy/management_endpoints/mcp_management_endpoints.py b/litellm/proxy/management_endpoints/mcp_management_endpoints.py index 776072c69be..3c324ca83ea 100644 --- a/litellm/proxy/management_endpoints/mcp_management_endpoints.py +++ b/litellm/proxy/management_endpoints/mcp_management_endpoints.py @@ -83,6 +83,7 @@ from litellm.proxy.common_utils.encrypt_decrypt_utils import ( from litellm.proxy.management_endpoints.sso.id_jag_assertion_capture import ( id_jag_assertion_capture_gap, ) +from litellm.proxy.management_helpers.access_group_member_prune import remove_deleted_mcp_server_from_access_groups from litellm.proxy.management_helpers.audit_logs import ( get_audit_log_changed_by, is_audit_logging_enabled, @@ -2632,6 +2633,7 @@ if MCP_AVAILABLE: status_code=status.HTTP_404_NOT_FOUND, detail={"error": f"MCP Server not found, passed server_id={server_id}"}, ) + await remove_deleted_mcp_server_from_access_groups(prisma_client, server_id) global_mcp_server_manager.remove_server(mcp_server_record_deleted) # Ensure registry is up to date by reloading from database diff --git a/litellm/proxy/management_helpers/access_group_member_prune.py b/litellm/proxy/management_helpers/access_group_member_prune.py new file mode 100644 index 00000000000..9763778cfeb --- /dev/null +++ b/litellm/proxy/management_helpers/access_group_member_prune.py @@ -0,0 +1,47 @@ +""" +Drop a deleted agent or MCP server from every unified access group that still lists it. + +Access groups store agent and MCP server ids in plain arrays with no foreign key, so a delete that +leaves them alone keeps the stale id in every group forever. +""" + +from typing import Final + +from litellm.proxy.management_helpers.access_group_model_sync import raw_executor +from litellm.proxy.management_helpers.access_group_team_sync import invalidate_access_group_caches +from litellm.types.llms.base import LiteLLMBaseModel + + +class _TouchedGroupRow(LiteLLMBaseModel): + access_group_id: str + + +_REMOVE_AGENT_ID_SQL: Final = ( + 'UPDATE "LiteLLM_AccessGroupTable" ' + 'SET "access_agent_ids" = array_remove("access_agent_ids", $1) ' + 'WHERE $1 = ANY("access_agent_ids") ' + 'RETURNING "access_group_id"' +) + +_REMOVE_MCP_SERVER_ID_SQL: Final = ( + 'UPDATE "LiteLLM_AccessGroupTable" ' + 'SET "access_mcp_server_ids" = array_remove("access_mcp_server_ids", $1), ' + '"mcp_tool_permissions" = "mcp_tool_permissions" - $1 ' + 'WHERE $1 = ANY("access_mcp_server_ids") OR jsonb_exists("mcp_tool_permissions", $1) ' + 'RETURNING "access_group_id"' +) + + +async def _remove_from_groups(prisma_client: object, sql: str, resource_id: str) -> None: + touched_rows: Final = await raw_executor(prisma_client).query_raw(sql, resource_id) + await invalidate_access_group_caches( + tuple(_TouchedGroupRow.model_validate(row).access_group_id for row in touched_rows) + ) + + +async def remove_deleted_agent_from_access_groups(prisma_client: object, agent_id: str) -> None: + await _remove_from_groups(prisma_client, _REMOVE_AGENT_ID_SQL, agent_id) + + +async def remove_deleted_mcp_server_from_access_groups(prisma_client: object, server_id: str) -> None: + await _remove_from_groups(prisma_client, _REMOVE_MCP_SERVER_ID_SQL, server_id) diff --git a/tests/unit/proxy/agent_endpoints/test_endpoints.py b/tests/unit/proxy/agent_endpoints/test_endpoints.py index 81b6c09ca12..4ae9834c564 100644 --- a/tests/unit/proxy/agent_endpoints/test_endpoints.py +++ b/tests/unit/proxy/agent_endpoints/test_endpoints.py @@ -813,10 +813,12 @@ class TestAgentRBACProxyAdmin: ) self.mock_registry.delete_agent_from_db = AsyncMock() self.mock_registry.deregister_agent = MagicMock() - resp = self.admin_client.delete( - "/v1/agents/agent-123", headers={"Authorization": "Bearer k"} - ) + with patch.object(agent_endpoints, "remove_deleted_agent_from_access_groups", AsyncMock()) as prune: + resp = self.admin_client.delete( + "/v1/agents/agent-123", headers={"Authorization": "Bearer k"} + ) assert resp.status_code == 200 + prune.assert_awaited_once_with(mock_prisma, "agent-123") class TestAgentProtocolVersionValidation: diff --git a/tests/unit/proxy/management_helpers/test_access_group_member_prune.py b/tests/unit/proxy/management_helpers/test_access_group_member_prune.py new file mode 100644 index 00000000000..e082ad9109d --- /dev/null +++ b/tests/unit/proxy/management_helpers/test_access_group_member_prune.py @@ -0,0 +1,68 @@ +from types import SimpleNamespace +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest + +from litellm.proxy.db.prisma_client import PrismaWrapper +from litellm.proxy.db.routing_prisma_wrapper import RoutingPrismaWrapper +from litellm.proxy.management_helpers.access_group_member_prune import ( + remove_deleted_agent_from_access_groups, + remove_deleted_mcp_server_from_access_groups, +) + +_INVALIDATE = "litellm.proxy.management_helpers.access_group_member_prune.invalidate_access_group_caches" + + +def _routed_prisma_client(touched_group_ids: tuple[str, ...], writer_unavailable: bool = False): + writer_inner = MagicMock(name="writer_prisma") + reader_inner = MagicMock(name="reader_prisma") + rows = [{"access_group_id": group_id} for group_id in touched_group_ids] + writer_inner.query_raw = AsyncMock(return_value=rows) + reader_inner.query_raw = AsyncMock(return_value=rows) + writer = PrismaWrapper(original_prisma=writer_inner, iam_token_db_auth=False) + reader = PrismaWrapper(original_prisma=reader_inner, iam_token_db_auth=False) + routing = RoutingPrismaWrapper(writer=writer, reader=reader) + routing._writer_unavailable = writer_unavailable + return SimpleNamespace(db=routing), writer_inner, reader_inner + + +@pytest.mark.asyncio +async def test_deleted_agent_is_removed_from_every_group_listing_it_and_those_caches_drop(): + prisma_client, writer_inner, reader_inner = _routed_prisma_client(("ag-1", "ag-2")) + + with patch(_INVALIDATE, new=AsyncMock()) as invalidate: + await remove_deleted_agent_from_access_groups(prisma_client, "agent-1") + + (update_call,) = writer_inner.query_raw.await_args_list + assert 'array_remove("access_agent_ids", $1)' in update_call.args[0] + assert update_call.args[1:] == ("agent-1",) + invalidate.assert_awaited_once_with(("ag-1", "ag-2")) + reader_inner.query_raw.assert_not_awaited() + + +@pytest.mark.asyncio +async def test_deleted_mcp_server_is_removed_from_server_ids_and_tool_permissions(): + prisma_client, writer_inner, _ = _routed_prisma_client(("ag-1",)) + + with patch(_INVALIDATE, new=AsyncMock()) as invalidate: + await remove_deleted_mcp_server_from_access_groups(prisma_client, "srv-1") + + (update_call,) = writer_inner.query_raw.await_args_list + sql = update_call.args[0] + assert 'array_remove("access_mcp_server_ids", $1)' in sql + assert '"mcp_tool_permissions" = "mcp_tool_permissions" - $1' in sql + assert 'jsonb_exists("mcp_tool_permissions", $1)' in sql + assert update_call.args[1:] == ("srv-1",) + invalidate.assert_awaited_once_with(("ag-1",)) + + +@pytest.mark.asyncio +async def test_prune_stays_on_the_writer_while_writer_flagged_unavailable(): + prisma_client, writer_inner, reader_inner = _routed_prisma_client((), writer_unavailable=True) + + with patch(_INVALIDATE, new=AsyncMock()) as invalidate: + await remove_deleted_agent_from_access_groups(prisma_client, "agent-1") + + writer_inner.query_raw.assert_awaited_once() + reader_inner.query_raw.assert_not_awaited() + invalidate.assert_awaited_once_with(())