From fa1ab7a592fb8dd2bbd17256e42a044dccced7bb Mon Sep 17 00:00:00 2001
From: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
Date: Wed, 28 Oct 2026 14:44:07 -0700
Subject: [PATCH 1/3] fix(agents): keep every agent public when publishes race
across replicas
POST /v1/agents/{agent_id}/make_public appended to this replica's in-memory list and wrote the whole list back, so concurrent publishes on different replicas or workers kept only the last one. The append now happens on the stored list under the per-section advisory lock, and an agent already public on another replica answers 400
---
litellm/proxy/agent_endpoints/endpoints.py | 20 +-
litellm/proxy/proxy_server.py | 78 ++++++-
.../proxy/agent_endpoints/test_endpoints.py | 196 +++++++++++++-----
3 files changed, 221 insertions(+), 73 deletions(-)
diff --git a/litellm/proxy/agent_endpoints/endpoints.py b/litellm/proxy/agent_endpoints/endpoints.py
index 89794fc2480..6bb4c91f6d5 100644
--- a/litellm/proxy/agent_endpoints/endpoints.py
+++ b/litellm/proxy/agent_endpoints/endpoints.py
@@ -1188,22 +1188,18 @@ async def make_agent_public(
if agent is None:
raise HTTPException(status_code=404, detail=f"Agent with ID {agent_id} not found")
- config: Final = await proxy_config.get_config()
-
- current_public_agent_groups: Final = list(litellm.public_agent_groups or [])
- if not AGENT_REGISTRY.ids_for_agent(agent.agent_id).isdisjoint(current_public_agent_groups):
+ stored_public_agent_groups: Final = await proxy_config.append_to_list_setting(
+ section_name="litellm_settings",
+ key="public_agent_groups",
+ item=agent.agent_id,
+ listed_as=AGENT_REGISTRY.ids_for_agent(agent.agent_id),
+ )
+ if stored_public_agent_groups is None:
raise HTTPException(
status_code=400,
detail=f"Agent with name {agent.agent_name} already in public agent groups",
)
- updated_public_agent_groups: Final = [*current_public_agent_groups, agent.agent_id]
-
- if "litellm_settings" not in config or config["litellm_settings"] is None:
- config["litellm_settings"] = {}
-
- config["litellm_settings"]["public_agent_groups"] = updated_public_agent_groups
-
- await proxy_config.save_config(new_config=config)
+ updated_public_agent_groups: Final = list(stored_public_agent_groups)
litellm.public_agent_groups = updated_public_agent_groups
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 64fb2791913..7433ef91eb2 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -5441,6 +5441,17 @@ _CONFIG_PERSISTED_SECTIONS: Final = ("general_settings", "router_settings", "lit
_CONFIG_UNMANAGED_EXCLUSIONS: Final = frozenset(("environment_variables", "model_list"))
_CONFIG_SECTION_VALUES: Final = TypeAdapter(Mapping[str, JsonValue])
_CONFIG_SECTION_LOCK_SQL: Final = "SELECT 1 AS locked FROM pg_advisory_xact_lock(hashtext($1))"
+_STRING_LIST: Final = TypeAdapter(list[str])
+_CONFIG_MAPPING: Final = TypeAdapter(Mapping[str, object])
+
+
+def _with_list_item(
+ section: Mapping[str, JsonValue], *, key: str, item: str, listed_as: frozenset[str]
+) -> Mapping[str, JsonValue]:
+ current: Final = _STRING_LIST.validate_python(section.get(key) or [])
+ if not listed_as.isdisjoint(current):
+ return section
+ return MappingProxyType({**section, key: [*current, item]})
class _ConfigParamWhere(TypedDict):
@@ -5966,6 +5977,63 @@ class ProxyConfig:
changed_keys: Mapping[str, JsonValue],
removed_keys: frozenset[str],
prisma_client: PrismaClient,
+ ) -> Mapping[str, JsonValue] | None:
+ return await self._rewrite_config_section(
+ section_name=section_name,
+ prisma_client=prisma_client,
+ rewrite=lambda existing_section: MappingProxyType(
+ {
+ key: value
+ for key, value in chain(
+ ((key, value) for key, value in existing_section.items() if key not in removed_keys),
+ changed_keys.items(),
+ )
+ }
+ ),
+ )
+
+ async def append_to_list_setting(
+ self, *, section_name: Section, key: str, item: str, listed_as: frozenset[str]
+ ) -> tuple[str, ...] | None:
+ if prisma_client is None or not (
+ general_settings_view().get("store_model_in_db", False) is True or store_model_in_db
+ ):
+ return await self._append_to_file_list_setting(
+ section_name=section_name, key=key, item=item, listed_as=listed_as
+ )
+ store: Final = self._settings_stores[section_name]
+ if store.owned_by_config(key):
+ if not listed_as.isdisjoint(_STRING_LIST.validate_python(store.get(key) or [])):
+ return None
+ self._raise_config_owned(section_name=section_name, rejected=(key,), store=store)
+ written_section: Final = await self._rewrite_config_section(
+ section_name=section_name,
+ prisma_client=prisma_client,
+ rewrite=lambda existing_section: _with_list_item(existing_section, key=key, item=item, listed_as=listed_as),
+ )
+ if written_section is None:
+ return None
+ store.apply_db_row(section_name, written_section)
+ await invalidate_config_param(section_name)
+ return tuple(_STRING_LIST.validate_python(written_section[key]))
+
+ async def _append_to_file_list_setting(
+ self, *, section_name: Section, key: str, item: str, listed_as: frozenset[str]
+ ) -> tuple[str, ...] | None:
+ config: Final = _CONFIG_MAPPING.validate_python(await self.get_config())
+ section: Final = _CONFIG_SECTION_VALUES.validate_python(config.get(section_name) or {})
+ updated_section: Final = _with_list_item(section, key=key, item=item, listed_as=listed_as)
+ if updated_section == section:
+ return None
+ await self.save_config(new_config={**config, section_name: dict(updated_section)})
+ return tuple(_STRING_LIST.validate_python(updated_section[key]))
+
+ async def _rewrite_config_section(
+ self,
+ *,
+ section_name: str,
+ prisma_client: PrismaClient,
+ rewrite: Callable[[Mapping[str, JsonValue]], Mapping[str, JsonValue]],
) -> Mapping[str, JsonValue] | None:
async with prisma_client.tx() as tx:
await tx.query_raw(_CONFIG_SECTION_LOCK_SQL, section_name)
@@ -5980,15 +6048,7 @@ class ProxyConfig:
if isinstance(existing_value, Mapping)
else MappingProxyType({})
)
- merged_section: Final[Mapping[str, JsonValue]] = MappingProxyType(
- {
- key: value
- for key, value in chain(
- ((key, value) for key, value in existing_section.items() if key not in removed_keys),
- changed_keys.items(),
- )
- }
- )
+ merged_section: Final = rewrite(existing_section)
if merged_section == existing_section:
return None
serialized_section: Final = json.dumps(dict(merged_section))
diff --git a/tests/unit/proxy/agent_endpoints/test_endpoints.py b/tests/unit/proxy/agent_endpoints/test_endpoints.py
index 81b6c09ca12..8eecdf02fb8 100644
--- a/tests/unit/proxy/agent_endpoints/test_endpoints.py
+++ b/tests/unit/proxy/agent_endpoints/test_endpoints.py
@@ -1,6 +1,9 @@
+import asyncio
import json
from collections.abc import Mapping
+from dataclasses import dataclass
from datetime import datetime, timezone
+from pathlib import Path
from types import SimpleNamespace
from typing import Final
@@ -8,6 +11,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
import httpx
import pytest
+import yaml
from fastapi import FastAPI, HTTPException
from fastapi.testclient import TestClient
from prisma.models import LiteLLM_AgentsTable
@@ -15,6 +19,7 @@ from prisma.models import LiteLLM_AgentsTable
from litellm.constants import REDACTED_BY_LITELM_STRING
from litellm.proxy._types import LiteLLM_AuditLogs, LitellmTableNames, LitellmUserRoles, UserAPIKeyAuth
from litellm.proxy.agent_endpoints import endpoints as agent_endpoints
+from litellm.proxy.agent_endpoints.agent_registry import AgentRegistry
from litellm.proxy.agent_endpoints.auth.agent_permission_handler import (
RestrictedAgentAccess,
)
@@ -1143,77 +1148,164 @@ def test_merged_agent_card_url_has_no_double_slash_without_proxy_base_url(
assert "//a2a" not in interface_url
-class _DbBackedProxyConfig:
- """Round-trips `litellm_settings` through the DB overlay the proxy applies on every
- `get_config()`, which is what re-assigns the `litellm.public_*` globals in production.
-
- Storage goes through JSON the way the `litellm_config` row does, so every read hands back
- freshly built values instead of the objects the endpoint still holds a reference to."""
-
- def __init__(self, stored_litellm_settings: dict[str, object] | None = None) -> None:
- self.stored_litellm_settings_json: str = json.dumps(stored_litellm_settings or {})
-
- async def get_config(self) -> dict[str, dict[str, object]]:
- from litellm.proxy.proxy_server import ProxyConfig
-
- config: Final[dict[str, dict[str, object]]] = {"litellm_settings": {}}
- db_param_value: Final[dict[str, object]] = json.loads(self.stored_litellm_settings_json)
- if not db_param_value:
- return config
- proxy_config: Final = ProxyConfig()
- db_values: Final = proxy_config._prepared_db_settings_values("litellm_settings", db_param_value)
- proxy_config._apply_litellm_settings_db_values(db_values)
- return {"litellm_settings": dict(proxy_config.litellm_settings.resolved())}
-
- async def save_config(self, new_config: dict[str, dict[str, object]]) -> None:
- self.stored_litellm_settings_json = json.dumps(new_config.get("litellm_settings") or {})
+@dataclass(frozen=True, slots=True)
+class _StoredConfigRow:
+ param_name: str
+ param_value: dict[str, object]
-def test_make_agent_public_twice_keeps_both_agents_public(monkeypatch: pytest.MonkeyPatch) -> None:
- """A second /make_public call must not drop the agent published by the first one."""
+class _ConfigRows:
+ """`LiteLLM_Config` as a section-locked transaction reads it, shared by every replica."""
+
+ def __init__(self, rows: Mapping[str, Mapping[str, object]]) -> None:
+ self.serialized_rows: dict[str, str] = {name: json.dumps(dict(value)) for name, value in rows.items()}
+ self.section_lock: Final = asyncio.Lock()
+
+ async def find_first(self, *, where: Mapping[str, str]) -> _StoredConfigRow | None:
+ name: Final = where["param_name"]
+ serialized: Final = self.serialized_rows.get(name)
+ await asyncio.sleep(0)
+ return None if serialized is None else _StoredConfigRow(param_name=name, param_value=json.loads(serialized))
+
+ async def upsert(self, *, where: Mapping[str, str], data: Mapping[str, Mapping[str, str]]) -> None:
+ self.serialized_rows[where["param_name"]] = data["update"]["param_value"]
+
+ def stored(self, name: str) -> dict[str, object]:
+ return json.loads(self.serialized_rows[name])
+
+
+class _ConfigTransaction:
+ def __init__(self, rows: _ConfigRows) -> None:
+ self.litellm_config: Final = rows
+ self.holds_section_lock = False
+
+ async def __aenter__(self) -> "_ConfigTransaction":
+ return self
+
+ async def __aexit__(self, *_: object) -> None:
+ if self.holds_section_lock:
+ self.litellm_config.section_lock.release()
+
+ async def query_raw(self, query: str, section_name: str) -> None:
+ await self.litellm_config.section_lock.acquire()
+ self.holds_section_lock = True
+
+
+class _StaleReplicaPrisma:
+ """A replica whose config reads still return what it cached before another replica wrote."""
+
+ def __init__(self, rows: _ConfigRows, cached_rows: Mapping[str, Mapping[str, object]]) -> None:
+ self.rows: Final = rows
+ self._cached_rows: Final = cached_rows
+
+ async def get_generic_data(self, key: str, value: str, table_name: str) -> SimpleNamespace | None:
+ cached: Final = self._cached_rows.get(value)
+ return None if cached is None else SimpleNamespace(param_name=value, param_value=dict(cached))
+
+ def tx(self) -> _ConfigTransaction:
+ return _ConfigTransaction(self.rows)
+
+
+_PROXY_ADMIN: Final = UserAPIKeyAuth(user_id="test-user", user_role=LitellmUserRoles.PROXY_ADMIN)
+
+
+def _replica_that_missed_a_publish(
+ monkeypatch: pytest.MonkeyPatch, *, stored_public_agent_groups: list[str], store_model_in_db: bool = True
+) -> tuple[_ConfigRows, AgentRegistry]:
import litellm
from litellm.proxy.agent_endpoints import agent_registry as agent_registry_module
- from litellm.proxy.agent_endpoints.agent_registry import AgentRegistry
+ from litellm.proxy.proxy_server import ProxyConfig
registry: Final = AgentRegistry()
registry.register_agent(_sample_agent_response(agent_id="agent-1", agent_name="Agent One"))
registry.register_agent(_sample_agent_response(agent_id="agent-2", agent_name="Agent Two"))
+ rows: Final = _ConfigRows({"litellm_settings": {"public_agent_groups": stored_public_agent_groups}})
+ cached_rows: Final = {"litellm_settings": {"public_agent_groups": []}}
monkeypatch.setattr(agent_registry_module, "global_agent_registry", registry)
- monkeypatch.setattr(litellm, "public_agent_groups", None)
- monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", MagicMock())
- monkeypatch.setattr("litellm.proxy.proxy_server.proxy_config", _DbBackedProxyConfig())
+ monkeypatch.setattr(litellm, "public_agent_groups", [])
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", _StaleReplicaPrisma(rows, cached_rows))
+ monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", store_model_in_db)
+ monkeypatch.setattr("litellm.proxy.proxy_server.general_settings", {"store_model_in_db": store_model_in_db})
+ monkeypatch.setattr("litellm.proxy.proxy_server.proxy_config", ProxyConfig())
+ return rows, registry
- first: Final = client.post("/v1/agents/agent-1/make_public", headers={"Authorization": "Bearer test-key"})
- second: Final = client.post("/v1/agents/agent-2/make_public", headers={"Authorization": "Bearer test-key"})
- assert first.status_code == 200
- assert second.status_code == 200
- assert second.json()["public_agent_groups"] == ["agent-1", "agent-2"]
+@pytest.mark.asyncio
+async def test_make_agent_public_keeps_an_agent_another_replica_published(monkeypatch: pytest.MonkeyPatch) -> None:
+ rows, registry = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=["agent-1"])
+
+ response: Final = await agent_endpoints.make_agent_public(agent_id="agent-2", user_api_key_dict=_PROXY_ADMIN)
+
+ assert response["public_agent_groups"] == ["agent-1", "agent-2"]
+ assert rows.stored("litellm_settings")["public_agent_groups"] == ["agent-1", "agent-2"]
assert [agent.agent_id for agent in registry.get_public_agent_list()] == ["agent-1", "agent-2"]
-def test_make_agent_public_rejects_an_agent_published_only_in_the_db(monkeypatch: pytest.MonkeyPatch) -> None:
- """The duplicate guard must fire off the stored list, not just what this process published."""
- import litellm
- from litellm.proxy.agent_endpoints import agent_registry as agent_registry_module
- from litellm.proxy.agent_endpoints.agent_registry import AgentRegistry
+@pytest.mark.asyncio
+async def test_make_agent_public_concurrent_publishes_all_stay_public(monkeypatch: pytest.MonkeyPatch) -> None:
+ rows, _ = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[])
- registry: Final = AgentRegistry()
- registry.register_agent(_sample_agent_response(agent_id="agent-1", agent_name="Agent One"))
-
- monkeypatch.setattr(agent_registry_module, "global_agent_registry", registry)
- monkeypatch.setattr(litellm, "public_agent_groups", None)
- monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", MagicMock())
- monkeypatch.setattr(
- "litellm.proxy.proxy_server.proxy_config",
- _DbBackedProxyConfig({"public_agent_groups": ["agent-1"]}),
+ await asyncio.gather(
+ agent_endpoints.make_agent_public(agent_id="agent-1", user_api_key_dict=_PROXY_ADMIN),
+ agent_endpoints.make_agent_public(agent_id="agent-2", user_api_key_dict=_PROXY_ADMIN),
)
- duplicate: Final = client.post("/v1/agents/agent-1/make_public", headers={"Authorization": "Bearer test-key"})
+ assert sorted(rows.stored("litellm_settings")["public_agent_groups"]) == ["agent-1", "agent-2"]
- assert duplicate.status_code == 400
- assert "already in public agent groups" in duplicate.json()["detail"]
+
+@pytest.mark.asyncio
+async def test_make_agent_public_rejects_an_agent_another_replica_published(monkeypatch: pytest.MonkeyPatch) -> None:
+ rows, _ = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=["agent-1"])
+
+ with pytest.raises(HTTPException) as duplicate:
+ await agent_endpoints.make_agent_public(agent_id="agent-1", user_api_key_dict=_PROXY_ADMIN)
+
+ assert duplicate.value.status_code == 400
+ assert "already in public agent groups" in duplicate.value.detail
+ assert rows.stored("litellm_settings")["public_agent_groups"] == ["agent-1"]
+
+
+@pytest.mark.asyncio
+async def test_make_agent_public_refuses_a_list_the_config_file_owns(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ from litellm.proxy import proxy_server
+
+ rows, _ = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[])
+ config_file: Final = tmp_path / "config.yaml"
+ config_file.write_text("litellm_settings:\n public_agent_groups: [agent-1]\n")
+ monkeypatch.setattr("litellm.proxy.proxy_server.user_config_file_path", str(config_file))
+ await proxy_server.proxy_config.get_config()
+
+ with pytest.raises(HTTPException) as already_public:
+ await agent_endpoints.make_agent_public(agent_id="agent-1", user_api_key_dict=_PROXY_ADMIN)
+ with pytest.raises(HTTPException) as refused:
+ await agent_endpoints.make_agent_public(agent_id="agent-2", user_api_key_dict=_PROXY_ADMIN)
+
+ assert already_public.value.status_code == 400
+ assert "already in public agent groups" in already_public.value.detail
+ assert refused.value.status_code == 400
+ assert refused.value.detail["keys"] == ["public_agent_groups"]
+ assert rows.stored("litellm_settings")["public_agent_groups"] == []
+
+
+@pytest.mark.asyncio
+async def test_make_agent_public_appends_to_the_config_file_without_db_settings(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[], store_model_in_db=False)
+ config_file: Final = tmp_path / "config.yaml"
+ config_file.write_text("litellm_settings:\n public_agent_groups: [agent-1]\n")
+ monkeypatch.setattr("litellm.proxy.proxy_server.user_config_file_path", str(config_file))
+
+ response: Final = await agent_endpoints.make_agent_public(agent_id="agent-2", user_api_key_dict=_PROXY_ADMIN)
+ with pytest.raises(HTTPException) as duplicate:
+ await agent_endpoints.make_agent_public(agent_id="agent-1", user_api_key_dict=_PROXY_ADMIN)
+
+ assert response["public_agent_groups"] == ["agent-1", "agent-2"]
+ assert yaml.safe_load(config_file.read_text())["litellm_settings"]["public_agent_groups"] == ["agent-1", "agent-2"]
+ assert duplicate.value.status_code == 400
@pytest.mark.parametrize("enabled, claim_field, expected", [(True, "azp", True), (False, "azp", False), (True, None, False)])
From a133f0ac735e673f3c6b27c01d139c2117b7e6b8 Mon Sep 17 00:00:00 2001
From: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
Date: Sat, 31 Oct 2026 01:31:56 -0700
Subject: [PATCH 2/3] fix(proxy): apply AI Hub publication changes as a delta
under the section lock
The AI Hub Make Public modals now send the list they opened with as previous_agent_ids,
previous_mcp_server_ids and previous_model_groups. When that field is set, the bulk
make_public routes add and remove only what the admin changed, inside the per-section
advisory lock, so an item another admin published meanwhile stays public. Without the
field the routes keep replacing the stored list
POST /config/update now reads and writes each section inside the same section lock, so a
publish that commits during a config save is no longer erased
The worker-local public lists are set from the written row before any further await, so a
slow cache invalidation can no longer leave an older list in place
---
litellm/proxy/_lazy_openapi_snapshot.json | 30 ++
litellm/proxy/_types.py | 10 +
litellm/proxy/agent_endpoints/endpoints.py | 30 +-
.../mcp_management_endpoints.py | 22 +-
.../model_management_endpoints.py | 28 +-
litellm/proxy/proxy_server.py | 357 ++++++++++--------
litellm/types/agents.py | 10 +
.../proxy/agent_endpoints/test_endpoints.py | 267 ++++++++++---
tests/unit/proxy/fake_config_table.py | 73 ++++
.../test_mcp_management_endpoints.py | 29 ++
.../test_model_management_endpoints.py | 28 ++
.../proxy/proxy_server/test_routes_config.py | 3 +
tests/unit/proxy/test_proxy_server.py | 2 +
...test_proxy_server_endpoints_and_startup.py | 54 +++
tests/unit/test_router_retry_policy_update.py | 2 +
.../components/AIHub/ModelHubTable.test.tsx | 2 +-
.../AIHub/forms/MakeAgentPublicForm.test.tsx | 17 +-
.../AIHub/forms/MakeAgentPublicForm.tsx | 5 +-
.../AIHub/forms/MakeMCPPublicForm.test.tsx | 21 +-
.../AIHub/forms/MakeMCPPublicForm.tsx | 16 +-
.../AIHub/forms/MakeModelPublicForm.test.tsx | 23 +-
.../AIHub/forms/MakeModelPublicForm.tsx | 5 +-
.../src/components/networking.test.ts | 30 ++
.../src/components/networking.tsx | 17 +-
ui/litellm-dashboard/src/lib/http/schema.d.ts | 15 +
25 files changed, 857 insertions(+), 239 deletions(-)
create mode 100644 tests/unit/proxy/fake_config_table.py
diff --git a/litellm/proxy/_lazy_openapi_snapshot.json b/litellm/proxy/_lazy_openapi_snapshot.json
index 39adb99a0d0..b1bbab1d605 100644
--- a/litellm/proxy/_lazy_openapi_snapshot.json
+++ b/litellm/proxy/_lazy_openapi_snapshot.json
@@ -4111,6 +4111,21 @@
},
"title": "Agent Ids",
"type": "array"
+ },
+ "previous_agent_ids": {
+ "anyOf": [
+ {
+ "items": {
+ "type": "string"
+ },
+ "type": "array"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "description": "The public agent list the caller started from. When set, only the agents added or removed relative to it change, so agents another admin published or unpublished meanwhile keep that change. When omitted, agent_ids replaces the stored list.",
+ "title": "Previous Agent Ids"
}
},
"required": [
@@ -42945,6 +42960,21 @@
},
"title": "Mcp Server Ids",
"type": "array"
+ },
+ "previous_mcp_server_ids": {
+ "anyOf": [
+ {
+ "items": {
+ "type": "string"
+ },
+ "type": "array"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "description": "The public MCP server list the caller started from. When set, only the servers added or removed relative to it change, so servers another admin published or unpublished meanwhile keep that change. When omitted, mcp_server_ids replaces the stored list.",
+ "title": "Previous Mcp Server Ids"
}
},
"required": [
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py
index 5ce6cd93a22..74f41fc7ab2 100644
--- a/litellm/proxy/_types.py
+++ b/litellm/proxy/_types.py
@@ -1573,7 +1573,17 @@ from litellm.models.mcp_server import ( # noqa: E402
class MakeMCPServersPublicRequest(LiteLLMPydanticObjectBase):
+ model_config = ConfigDict(frozen=True)
+
mcp_server_ids: list[str]
+ previous_mcp_server_ids: tuple[str, ...] | None = Field(
+ default=None,
+ description=(
+ "The public MCP server list the caller started from. When set, only the servers added or removed "
+ "relative to it change, so servers another admin published or unpublished meanwhile keep that change. "
+ "When omitted, mcp_server_ids replaces the stored list."
+ ),
+ )
class MCPUserCredentialRequest(LiteLLMPydanticObjectBase):
diff --git a/litellm/proxy/agent_endpoints/endpoints.py b/litellm/proxy/agent_endpoints/endpoints.py
index 6bb4c91f6d5..093116f2772 100644
--- a/litellm/proxy/agent_endpoints/endpoints.py
+++ b/litellm/proxy/agent_endpoints/endpoints.py
@@ -1120,6 +1120,10 @@ async def _resolve_agent_kill_switch(agent_id: str) -> tuple[str, AgentKillSwitc
return agent.agent_id, agent.kill_switch
+def _set_public_agent_groups(public_agent_groups: tuple[str, ...]) -> None:
+ litellm.public_agent_groups = list(public_agent_groups)
+
+
@router.post(
"/v1/agents/{agent_id}/make_public",
tags=["[beta] A2A Agents"],
@@ -1163,7 +1167,6 @@ async def make_agent_public(
try:
# Update the public model groups
- import litellm
from litellm.proxy.agent_endpoints.agent_registry import (
global_agent_registry as AGENT_REGISTRY,
)
@@ -1193,6 +1196,7 @@ async def make_agent_public(
key="public_agent_groups",
item=agent.agent_id,
listed_as=AGENT_REGISTRY.ids_for_agent(agent.agent_id),
+ on_written=_set_public_agent_groups,
)
if stored_public_agent_groups is None:
raise HTTPException(
@@ -1201,8 +1205,6 @@ async def make_agent_public(
)
updated_public_agent_groups: Final = list(stored_public_agent_groups)
- litellm.public_agent_groups = updated_public_agent_groups
-
verbose_proxy_logger.debug(
"Updated public agent groups to: %s by user: %s", updated_public_agent_groups, user_api_key_dict.user_id
)
@@ -1271,8 +1273,6 @@ async def make_agents_public(
)
from litellm.proxy.proxy_server import proxy_config
- # Load existing config
- config: Final = await proxy_config.get_config()
# Check if user has admin permissions
if user_api_key_dict.user_role != LitellmUserRoles.PROXY_ADMIN:
raise HTTPException(
@@ -1282,9 +1282,6 @@ async def make_agents_public(
},
)
- if litellm.public_agent_groups is None:
- litellm.public_agent_groups = []
-
for agent_id in request.agent_ids:
agent = AGENT_REGISTRY.get_agent_by_id(agent_id=agent_id)
if agent is None:
@@ -1296,6 +1293,23 @@ async def make_agents_public(
if agent is None:
raise HTTPException(status_code=404, detail=f"Agent with ID {agent_id} not found")
+ if request.previous_agent_ids is not None:
+ stored_public_agent_groups: Final = await proxy_config.apply_list_setting_delta(
+ section_name="litellm_settings",
+ key="public_agent_groups",
+ requested=request.agent_ids,
+ previous=request.previous_agent_ids,
+ on_written=_set_public_agent_groups,
+ aliases=AGENT_REGISTRY.ids_for_agent,
+ )
+ return {
+ "message": "Successfully updated public agent groups",
+ "public_agent_groups": list(stored_public_agent_groups),
+ "updated_by": user_api_key_dict.user_id,
+ }
+
+ # Load existing config
+ config: Final = await proxy_config.get_config()
litellm.public_agent_groups = request.agent_ids
# Update config with new settings
diff --git a/litellm/proxy/management_endpoints/mcp_management_endpoints.py b/litellm/proxy/management_endpoints/mcp_management_endpoints.py
index f6b836bc4be..40769df8ffd 100644
--- a/litellm/proxy/management_endpoints/mcp_management_endpoints.py
+++ b/litellm/proxy/management_endpoints/mcp_management_endpoints.py
@@ -129,6 +129,10 @@ def does_mcp_server_exist(mcp_server_records: Iterable[_HasServerId], mcp_server
return False
+def _set_public_mcp_servers(public_mcp_servers: tuple[str, ...]) -> None:
+ litellm.public_mcp_servers = list(public_mcp_servers)
+
+
DEFAULT_MCP_REGISTRY_VERSION: Final = "1.0.0"
if TYPE_CHECKING:
@@ -3374,8 +3378,6 @@ if MCP_AVAILABLE:
)
from litellm.proxy.proxy_server import proxy_config
- # Load existing config
- config: Final = await proxy_config.get_config()
# Check if user has admin permissions
if user_api_key_dict.user_role != LitellmUserRoles.PROXY_ADMIN:
raise HTTPException(
@@ -3393,6 +3395,22 @@ if MCP_AVAILABLE:
detail=f"MCP Server with ID {server_id} not found",
)
+ if request.previous_mcp_server_ids is not None:
+ stored_public_mcp_servers: Final = await proxy_config.apply_list_setting_delta(
+ section_name="litellm_settings",
+ key="public_mcp_servers",
+ requested=request.mcp_server_ids,
+ previous=request.previous_mcp_server_ids,
+ on_written=_set_public_mcp_servers,
+ )
+ return {
+ "message": "Successfully updated public mcp servers",
+ "public_mcp_servers": list(stored_public_mcp_servers),
+ "updated_by": user_api_key_dict.user_id,
+ }
+
+ # Load existing config
+ config: Final = await proxy_config.get_config()
# Update config with new settings
if "litellm_settings" not in config or config["litellm_settings"] is None:
config["litellm_settings"] = {}
diff --git a/litellm/proxy/management_endpoints/model_management_endpoints.py b/litellm/proxy/management_endpoints/model_management_endpoints.py
index 8cd3559ba86..025c99595df 100644
--- a/litellm/proxy/management_endpoints/model_management_endpoints.py
+++ b/litellm/proxy/management_endpoints/model_management_endpoints.py
@@ -221,8 +221,16 @@ class UpdatePublicModelGroupsRequest(LiteLLMBaseModel):
"""Request model for updating public model groups"""
model_groups: list[str] = Field(description="List of model group names to make public")
+ previous_model_groups: tuple[str, ...] | None = Field(
+ default=None,
+ description=(
+ "The public model group list the caller started from. When set, only the model groups added or removed "
+ "relative to it change, so model groups another admin published or unpublished meanwhile keep that "
+ "change. When omitted, model_groups replaces the stored list."
+ ),
+ )
- model_config = ConfigDict(extra="forbid")
+ model_config = ConfigDict(extra="forbid", frozen=True)
class _ProxyModelRow(Protocol):
@@ -3381,6 +3389,10 @@ async def update_model(
)
+def _set_public_model_groups(public_model_groups: tuple[str, ...]) -> None:
+ litellm.public_model_groups = list(public_model_groups)
+
+
@router.post(
"/model_group/make_public",
description="Update which model groups are public",
@@ -3427,6 +3439,20 @@ async def update_public_model_groups(
detail={"error": "Set `'STORE_MODEL_IN_DB='True'` in your env to enable this feature."},
)
+ if request.previous_model_groups is not None:
+ stored_public_model_groups: Final = await proxy_config.apply_list_setting_delta(
+ section_name="litellm_settings",
+ key="public_model_groups",
+ requested=request.model_groups,
+ previous=request.previous_model_groups,
+ on_written=_set_public_model_groups,
+ )
+ return {
+ "message": "Successfully updated public model groups",
+ "public_model_groups": list(stored_public_model_groups),
+ "updated_by": user_api_key_dict.user_id,
+ }
+
# Load existing config first (this may overwrite in-memory litellm settings
# from DB values via _update_config_from_db), so set the in-memory value AFTER
config: Final = await proxy_config.get_config()
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 779d7ae04c5..96db812f768 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -299,7 +299,7 @@ def generate_feedback_box():
import contextlib
from collections import defaultdict
from contextlib import AsyncExitStack, asynccontextmanager
-from functools import lru_cache, partial
+from functools import lru_cache, partial, reduce
import litellm
import litellm._redis
@@ -5449,15 +5449,52 @@ _STRING_LIST: Final = TypeAdapter(list[str])
_CONFIG_MAPPING: Final = TypeAdapter(Mapping[str, object])
+def _listed_items(section: Mapping[str, JsonValue], key: str) -> tuple[str, ...]:
+ return tuple(_STRING_LIST.validate_python(section.get(key) or []))
+
+
def _with_list_item(
section: Mapping[str, JsonValue], *, key: str, item: str, listed_as: frozenset[str]
) -> Mapping[str, JsonValue]:
- current: Final = _STRING_LIST.validate_python(section.get(key) or [])
+ current: Final = _listed_items(section, key)
if not listed_as.isdisjoint(current):
return section
return MappingProxyType({**section, key: [*current, item]})
+def _listed_only_as_itself(item: str) -> frozenset[str]:
+ return frozenset((item,))
+
+
+def _with_list_delta(
+ section: Mapping[str, JsonValue],
+ *,
+ key: str,
+ requested: tuple[str, ...],
+ previous: frozenset[str],
+ aliases: Callable[[str], frozenset[str]],
+) -> Mapping[str, JsonValue]:
+ current: Final = _listed_items(section, key)
+ removed: Final = frozenset(chain.from_iterable(aliases(item) for item in previous.difference(requested)))
+ kept: Final = tuple(item for item in current if item not in removed)
+ added: Final = tuple(
+ item for item in dict.fromkeys(requested) if item not in previous and aliases(item).isdisjoint(kept)
+ )
+ if (*kept, *added) == current:
+ return section
+ return MappingProxyType({**section, key: [*kept, *added]})
+
+
+@dataclass(frozen=True, slots=True)
+class _SectionRewrite:
+ before: Mapping[str, JsonValue]
+ after: Mapping[str, JsonValue]
+
+ @property
+ def changed(self) -> bool:
+ return self.after != self.before
+
+
class _ConfigParamWhere(TypedDict):
param_name: ReadOnly[str]
@@ -5476,6 +5513,37 @@ class _ConfigParamUpsert(TypedDict):
update: ReadOnly[_ConfigParamUpdate]
+async def _rewrite_config_section(
+ prisma_client: PrismaClient,
+ *,
+ section_name: str,
+ rewrite: Callable[[Mapping[str, JsonValue]], Mapping[str, JsonValue]],
+) -> _SectionRewrite:
+ async with prisma_client.tx() as tx:
+ await tx.query_raw(_CONFIG_SECTION_LOCK_SQL, section_name)
+ config_table: Final = cast("TableActions[_ConfigParamRow]", tx.litellm_config)
+ config_where: Final[_ConfigParamWhere] = {"param_name": section_name}
+ existing_row: Final[_ConfigParamRow | None] = await config_table.find_first(where=config_where)
+ existing_value: Final[object] = cast(object, existing_row.param_value) if existing_row is not None else None
+ existing_section: Final[Mapping[str, JsonValue]] = (
+ _CONFIG_SECTION_VALUES.validate_json(existing_value)
+ if isinstance(existing_value, str)
+ else _CONFIG_SECTION_VALUES.validate_python(existing_value)
+ if isinstance(existing_value, Mapping)
+ else MappingProxyType({})
+ )
+ section_rewrite: Final = _SectionRewrite(before=existing_section, after=rewrite(existing_section))
+ if not section_rewrite.changed:
+ return section_rewrite
+ serialized_section: Final = json.dumps(dict(section_rewrite.after))
+ config_data: Final[_ConfigParamUpsert] = {
+ "create": {"param_name": section_name, "param_value": serialized_section},
+ "update": {"param_value": serialized_section},
+ }
+ await config_table.upsert(where=config_where, data=config_data)
+ return section_rewrite
+
+
class _EnvironmentVariablesConfigData(TypedDict):
environment_variables: ReadOnly[object]
@@ -5982,9 +6050,9 @@ class ProxyConfig:
removed_keys: frozenset[str],
prisma_client: PrismaClient,
) -> Mapping[str, JsonValue] | None:
- return await self._rewrite_config_section(
+ section_rewrite: Final = await _rewrite_config_section(
+ prisma_client,
section_name=section_name,
- prisma_client=prisma_client,
rewrite=lambda existing_section: MappingProxyType(
{
key: value
@@ -5995,73 +6063,83 @@ class ProxyConfig:
}
),
)
+ return section_rewrite.after if section_rewrite.changed else None
async def append_to_list_setting(
- self, *, section_name: Section, key: str, item: str, listed_as: frozenset[str]
+ self,
+ *,
+ section_name: Section,
+ key: str,
+ item: str,
+ listed_as: frozenset[str],
+ on_written: Callable[[tuple[str, ...]], None],
) -> tuple[str, ...] | None:
+ section_rewrite: Final = await self._rewrite_list_setting(
+ section_name=section_name,
+ key=key,
+ rewrite=lambda section: _with_list_item(section, key=key, item=item, listed_as=listed_as),
+ on_written=on_written,
+ )
+ return _listed_items(section_rewrite.after, key) if section_rewrite.changed else None
+
+ async def apply_list_setting_delta(
+ self,
+ *,
+ section_name: Section,
+ key: str,
+ requested: Sequence[str],
+ previous: Sequence[str],
+ on_written: Callable[[tuple[str, ...]], None],
+ aliases: Callable[[str], frozenset[str]] = _listed_only_as_itself,
+ ) -> tuple[str, ...]:
+ section_rewrite: Final = await self._rewrite_list_setting(
+ section_name=section_name,
+ key=key,
+ rewrite=lambda section: _with_list_delta(
+ section, key=key, requested=tuple(requested), previous=frozenset(previous), aliases=aliases
+ ),
+ on_written=on_written,
+ )
+ return _listed_items(section_rewrite.after, key)
+
+ async def _rewrite_list_setting(
+ self,
+ *,
+ section_name: Section,
+ key: str,
+ rewrite: Callable[[Mapping[str, JsonValue]], Mapping[str, JsonValue]],
+ on_written: Callable[[tuple[str, ...]], None],
+ ) -> _SectionRewrite:
if prisma_client is None or not (
general_settings_view().get("store_model_in_db", False) is True or store_model_in_db
):
- return await self._append_to_file_list_setting(
- section_name=section_name, key=key, item=item, listed_as=listed_as
- )
+ file_rewrite: Final = await self._rewrite_file_section(section_name=section_name, rewrite=rewrite)
+ on_written(_listed_items(file_rewrite.after, key))
+ return file_rewrite
store: Final = self._settings_stores[section_name]
if store.owned_by_config(key):
- if not listed_as.isdisjoint(_STRING_LIST.validate_python(store.get(key) or [])):
- return None
- self._raise_config_owned(section_name=section_name, rejected=(key,), store=store)
- written_section: Final = await self._rewrite_config_section(
- section_name=section_name,
- prisma_client=prisma_client,
- rewrite=lambda existing_section: _with_list_item(existing_section, key=key, item=item, listed_as=listed_as),
+ config_section: Final[Mapping[str, JsonValue]] = MappingProxyType({key: store.get(key)})
+ if rewrite(config_section) != config_section:
+ self._raise_config_owned(section_name=section_name, rejected=(key,), store=store)
+ return _SectionRewrite(before=config_section, after=config_section)
+ section_rewrite: Final = await _rewrite_config_section(
+ prisma_client, section_name=section_name, rewrite=rewrite
)
- if written_section is None:
- return None
- store.apply_db_row(section_name, written_section)
- await invalidate_config_param(section_name)
- return tuple(_STRING_LIST.validate_python(written_section[key]))
+ store.apply_db_row(section_name, section_rewrite.after)
+ on_written(_listed_items(section_rewrite.after, key))
+ if section_rewrite.changed:
+ await invalidate_config_param(section_name)
+ return section_rewrite
- async def _append_to_file_list_setting(
- self, *, section_name: Section, key: str, item: str, listed_as: frozenset[str]
- ) -> tuple[str, ...] | None:
+ async def _rewrite_file_section(
+ self, *, section_name: Section, rewrite: Callable[[Mapping[str, JsonValue]], Mapping[str, JsonValue]]
+ ) -> _SectionRewrite:
config: Final = _CONFIG_MAPPING.validate_python(await self.get_config())
section: Final = _CONFIG_SECTION_VALUES.validate_python(config.get(section_name) or {})
- updated_section: Final = _with_list_item(section, key=key, item=item, listed_as=listed_as)
- if updated_section == section:
- return None
- await self.save_config(new_config={**config, section_name: dict(updated_section)})
- return tuple(_STRING_LIST.validate_python(updated_section[key]))
-
- async def _rewrite_config_section(
- self,
- *,
- section_name: str,
- prisma_client: PrismaClient,
- rewrite: Callable[[Mapping[str, JsonValue]], Mapping[str, JsonValue]],
- ) -> Mapping[str, JsonValue] | None:
- async with prisma_client.tx() as tx:
- await tx.query_raw(_CONFIG_SECTION_LOCK_SQL, section_name)
- config_table: Final = cast("TableActions[_ConfigParamRow]", tx.litellm_config)
- config_where: Final[_ConfigParamWhere] = {"param_name": section_name}
- existing_row: Final[_ConfigParamRow | None] = await config_table.find_first(where=config_where)
- existing_value: Final[object] = cast(object, existing_row.param_value) if existing_row is not None else None
- existing_section: Final[Mapping[str, JsonValue]] = (
- _CONFIG_SECTION_VALUES.validate_json(existing_value)
- if isinstance(existing_value, str)
- else _CONFIG_SECTION_VALUES.validate_python(existing_value)
- if isinstance(existing_value, Mapping)
- else MappingProxyType({})
- )
- merged_section: Final = rewrite(existing_section)
- if merged_section == existing_section:
- return None
- serialized_section: Final = json.dumps(dict(merged_section))
- config_data: Final[_ConfigParamUpsert] = {
- "create": {"param_name": section_name, "param_value": serialized_section},
- "update": {"param_value": serialized_section},
- }
- await config_table.upsert(where=config_where, data=config_data)
- return merged_section
+ section_rewrite: Final = _SectionRewrite(before=section, after=rewrite(section))
+ if section_rewrite.changed:
+ await self.save_config(new_config={**config, section_name: dict(section_rewrite.after)})
+ return section_rewrite
async def save_environment_variables(self, updates: dict[str, str | None]) -> None:
"""Persist specific environment variables to the DB config row.
@@ -19690,6 +19768,58 @@ async def invitation_delete(
#### CONFIG MANAGEMENT ####
+def _with_slack_alerting(section: Mapping[str, JsonValue]) -> Mapping[str, JsonValue]:
+ if "alerting" not in section:
+ return {"alerting": ["slack"]}
+ alerting: Final = section["alerting"]
+ if isinstance(alerting, list) and "slack" not in alerting:
+ return {"alerting": [*alerting, "slack"]}
+ return {}
+
+
+def _with_general_setting(section: Mapping[str, JsonValue], setting: tuple[str, JsonValue]) -> Mapping[str, JsonValue]:
+ key, value = setting
+ return MappingProxyType(
+ {**section, **(_with_slack_alerting(section) if key == "alert_to_webhook_url" else {}), key: value}
+ )
+
+
+def _merged_litellm_settings(
+ existing: Mapping[str, JsonValue], updates: Mapping[str, JsonValue]
+) -> Mapping[str, JsonValue]:
+ incoming_callbacks: Final = updates.get("success_callback")
+ existing_callbacks: Final = existing.get("success_callback")
+ merged: Final = {**existing, **updates}
+ if not isinstance(incoming_callbacks, list):
+ return MappingProxyType(merged)
+ return _CONFIG_SECTION_VALUES.validate_python(
+ {
+ **merged,
+ "success_callback": list(
+ set(normalize_callback_names(existing_callbacks) + incoming_callbacks)
+ if isinstance(existing_callbacks, list)
+ else set(incoming_callbacks)
+ ),
+ }
+ )
+
+
+async def _update_config_section(
+ prisma_client: PrismaClient,
+ *,
+ section_name: str,
+ merge: Callable[[Mapping[str, JsonValue]], Mapping[str, JsonValue]],
+ user_api_key_dict: UserAPIKeyAuth,
+) -> None:
+ section_rewrite: Final = await _rewrite_config_section(prisma_client, section_name=section_name, rewrite=merge)
+ await invalidate_config_param(section_name)
+ asyncio.create_task(
+ create_config_audit_log(
+ section_name, "updated", dict(section_rewrite.before), dict(section_rewrite.after), user_api_key_dict
+ )
+ )
+
+
@router.post(
"/config/update",
tags=["config.yaml"],
@@ -19785,106 +19915,39 @@ async def update_config(
proxy_config.reject_config_owned_writes(section_name="litellm_settings", changed_keys=raw_litellm_settings)
proxy_config.reject_config_owned_writes(section_name="router_settings", changed_keys=router_settings_updates)
- async def _read_section(param_name: str) -> dict:
- row: Final[_ConfigParamRow | None] = await _config_param_table(prisma_client, use_writer=True).find_first(
- where={"param_name": param_name}
- )
- if row is None or row.param_value is None:
- return {}
- return dict(row.param_value)
-
- async def _upsert_section(param_name: str, value: dict) -> None:
- serialized: Final = json.dumps(value)
- await ConfigRepository(prisma_client).table.upsert(
- where={"param_name": param_name},
- data={
- "create": {"param_name": param_name, "param_value": serialized},
- "update": {"param_value": serialized},
- },
- )
- # invalidate the DualCache entry so the next reader (this process
- # or any other proxy in the cluster) goes to DB.
- await invalidate_config_param(param_name)
-
- # general_settings: merge per-key, with the alert_to_webhook_url side
- # effect of auto-enabling slack alerting.
if config_info.general_settings is not None:
- existing = await _read_section("general_settings")
- before_general_settings: Final = copy.deepcopy(existing)
- for k, v in requested_general_settings.items():
- if k == "alert_to_webhook_url":
- if "alerting" not in existing:
- existing["alerting"] = ["slack"]
- elif isinstance(existing["alerting"], list) and "slack" not in existing["alerting"]:
- existing["alerting"].append("slack")
- existing[k] = v
- await _upsert_section("general_settings", existing)
- asyncio.create_task(
- create_config_audit_log(
- "general_settings", "updated", before_general_settings, existing, user_api_key_dict
- )
+ await _update_config_section(
+ prisma_client,
+ section_name="general_settings",
+ merge=lambda existing: reduce(_with_general_setting, requested_general_settings.items(), existing),
+ user_api_key_dict=user_api_key_dict,
)
- # environment_variables: idempotently encrypt the request values
- # (plaintext on first write, OR ciphertext the UI read back via
- # /get/config/callbacks and re-submitted on save), then merge into
- # existing. Only the sent keys are re-written; untouched keys keep
- # their stored ciphertext byte-for-byte.
if config_info.environment_variables is not None:
- existing = await _read_section("environment_variables")
- before_environment_variables: Final = copy.deepcopy(existing)
- existing.update(
+ encrypted_environment_variables: Final = _CONFIG_SECTION_VALUES.validate_python(
proxy_config.encrypt_env_variables_for_db(environment_variables=config_info.environment_variables)
)
- await _upsert_section("environment_variables", existing)
- asyncio.create_task(
- create_config_audit_log(
- "environment_variables", "updated", before_environment_variables, existing, user_api_key_dict
- )
+ await _update_config_section(
+ prisma_client,
+ section_name="environment_variables",
+ merge=lambda existing: MappingProxyType({**existing, **encrypted_environment_variables}),
+ user_api_key_dict=user_api_key_dict,
)
- # litellm_settings: merge existing + request, request wins (matching
- # router_settings semantics — the caller's value for any given key is
- # what gets persisted). success_callback is special-cased: it is
- # always normalized + deduped, and unioned with any existing list,
- # because callbacks are additive (callers send the new entry, not
- # the full set). Normalizing on every write — not only when an
- # existing entry is present — keeps the DB free of mixed-case
- # entries that delete_callback (lowercase lookup) cannot find.
if config_info.litellm_settings is not None:
- existing = await _read_section("litellm_settings")
- before_litellm_settings: Final = copy.deepcopy(existing)
- merged: Final = {**existing, **updated_litellm_settings}
-
- incoming_cb: Final = updated_litellm_settings.get("success_callback")
- existing_cb: Final = existing.get("success_callback")
- if isinstance(incoming_cb, list):
- if isinstance(existing_cb, list):
- # Normalize the existing list too — a row written by a
- # different code path may still hold mixed-case names,
- # which would otherwise dedup-miss against the lowercase
- # incoming entries.
- merged["success_callback"] = list(set(normalize_callback_names(existing_cb) + incoming_cb))
- else:
- merged["success_callback"] = list(set(incoming_cb))
-
- await _upsert_section("litellm_settings", merged)
- asyncio.create_task(
- create_config_audit_log(
- "litellm_settings", "updated", before_litellm_settings, merged, user_api_key_dict
- )
+ await _update_config_section(
+ prisma_client,
+ section_name="litellm_settings",
+ merge=lambda existing: _merged_litellm_settings(existing, updated_litellm_settings),
+ user_api_key_dict=user_api_key_dict,
)
- # router_settings: merge existing + request, request wins.
if isinstance(raw_router_settings, dict):
- existing = await _read_section("router_settings")
- before_router_settings: Final = copy.deepcopy(existing)
- new_router_settings: Final = {**existing, **router_settings_updates}
- await _upsert_section("router_settings", new_router_settings)
- asyncio.create_task(
- create_config_audit_log(
- "router_settings", "updated", before_router_settings, new_router_settings, user_api_key_dict
- )
+ await _update_config_section(
+ prisma_client,
+ section_name="router_settings",
+ merge=lambda existing: MappingProxyType({**existing, **router_settings_updates}),
+ user_api_key_dict=user_api_key_dict,
)
await proxy_config.add_deployment(prisma_client=prisma_client, proxy_logging_obj=proxy_logging_obj)
diff --git a/litellm/types/agents.py b/litellm/types/agents.py
index 826aa5d9c98..d16dffda71a 100644
--- a/litellm/types/agents.py
+++ b/litellm/types/agents.py
@@ -454,7 +454,17 @@ class AgentMakePublicResponse(LiteLLMBaseModel):
class MakeAgentsPublicRequest(LiteLLMBaseModel):
+ model_config = ConfigDict(frozen=True)
+
agent_ids: list[str]
+ previous_agent_ids: tuple[str, ...] | None = Field(
+ default=None,
+ description=(
+ "The public agent list the caller started from. When set, only the agents added or removed relative to "
+ "it change, so agents another admin published or unpublished meanwhile keep that change. When omitted, "
+ "agent_ids replaces the stored list."
+ ),
+ )
def normalize_a2a_jsonrpc_response(
diff --git a/tests/unit/proxy/agent_endpoints/test_endpoints.py b/tests/unit/proxy/agent_endpoints/test_endpoints.py
index 8eecdf02fb8..f2fdb3426c8 100644
--- a/tests/unit/proxy/agent_endpoints/test_endpoints.py
+++ b/tests/unit/proxy/agent_endpoints/test_endpoints.py
@@ -1,4 +1,5 @@
import asyncio
+import itertools
import json
from collections.abc import Mapping
from dataclasses import dataclass
@@ -30,8 +31,9 @@ from litellm.proxy.agent_endpoints.endpoints import (
router,
user_api_key_auth,
)
-from litellm.types.agents import AgentResponse, PatchAgentRequest
+from litellm.types.agents import AgentResponse, MakeAgentsPublicRequest, PatchAgentRequest
from litellm.types.proxy.agent_identity import AgentIdentityBinding
+from tests.unit.proxy.fake_config_table import ConfigRows, ReplicaPrisma
def _sample_agent_card_params() -> dict:
@@ -1148,70 +1150,12 @@ def test_merged_agent_card_url_has_no_double_slash_without_proxy_base_url(
assert "//a2a" not in interface_url
-@dataclass(frozen=True, slots=True)
-class _StoredConfigRow:
- param_name: str
- param_value: dict[str, object]
-
-
-class _ConfigRows:
- """`LiteLLM_Config` as a section-locked transaction reads it, shared by every replica."""
-
- def __init__(self, rows: Mapping[str, Mapping[str, object]]) -> None:
- self.serialized_rows: dict[str, str] = {name: json.dumps(dict(value)) for name, value in rows.items()}
- self.section_lock: Final = asyncio.Lock()
-
- async def find_first(self, *, where: Mapping[str, str]) -> _StoredConfigRow | None:
- name: Final = where["param_name"]
- serialized: Final = self.serialized_rows.get(name)
- await asyncio.sleep(0)
- return None if serialized is None else _StoredConfigRow(param_name=name, param_value=json.loads(serialized))
-
- async def upsert(self, *, where: Mapping[str, str], data: Mapping[str, Mapping[str, str]]) -> None:
- self.serialized_rows[where["param_name"]] = data["update"]["param_value"]
-
- def stored(self, name: str) -> dict[str, object]:
- return json.loads(self.serialized_rows[name])
-
-
-class _ConfigTransaction:
- def __init__(self, rows: _ConfigRows) -> None:
- self.litellm_config: Final = rows
- self.holds_section_lock = False
-
- async def __aenter__(self) -> "_ConfigTransaction":
- return self
-
- async def __aexit__(self, *_: object) -> None:
- if self.holds_section_lock:
- self.litellm_config.section_lock.release()
-
- async def query_raw(self, query: str, section_name: str) -> None:
- await self.litellm_config.section_lock.acquire()
- self.holds_section_lock = True
-
-
-class _StaleReplicaPrisma:
- """A replica whose config reads still return what it cached before another replica wrote."""
-
- def __init__(self, rows: _ConfigRows, cached_rows: Mapping[str, Mapping[str, object]]) -> None:
- self.rows: Final = rows
- self._cached_rows: Final = cached_rows
-
- async def get_generic_data(self, key: str, value: str, table_name: str) -> SimpleNamespace | None:
- cached: Final = self._cached_rows.get(value)
- return None if cached is None else SimpleNamespace(param_name=value, param_value=dict(cached))
-
- def tx(self) -> _ConfigTransaction:
- return _ConfigTransaction(self.rows)
-
-
_PROXY_ADMIN: Final = UserAPIKeyAuth(user_id="test-user", user_role=LitellmUserRoles.PROXY_ADMIN)
def _replica_that_missed_a_publish(
monkeypatch: pytest.MonkeyPatch, *, stored_public_agent_groups: list[str], store_model_in_db: bool = True
-) -> tuple[_ConfigRows, AgentRegistry]:
+) -> tuple[ConfigRows, AgentRegistry]:
import litellm
from litellm.proxy.agent_endpoints import agent_registry as agent_registry_module
from litellm.proxy.proxy_server import ProxyConfig
@@ -1219,12 +1163,12 @@ def _replica_that_missed_a_publish(
registry: Final = AgentRegistry()
registry.register_agent(_sample_agent_response(agent_id="agent-1", agent_name="Agent One"))
registry.register_agent(_sample_agent_response(agent_id="agent-2", agent_name="Agent Two"))
- rows: Final = _ConfigRows({"litellm_settings": {"public_agent_groups": stored_public_agent_groups}})
+ rows: Final = ConfigRows({"litellm_settings": {"public_agent_groups": stored_public_agent_groups}})
cached_rows: Final = {"litellm_settings": {"public_agent_groups": []}}
monkeypatch.setattr(agent_registry_module, "global_agent_registry", registry)
monkeypatch.setattr(litellm, "public_agent_groups", [])
- monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", _StaleReplicaPrisma(rows, cached_rows))
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", ReplicaPrisma(rows, cached_rows))
monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", store_model_in_db)
monkeypatch.setattr("litellm.proxy.proxy_server.general_settings", {"store_model_in_db": store_model_in_db})
monkeypatch.setattr("litellm.proxy.proxy_server.proxy_config", ProxyConfig())
@@ -1308,6 +1252,205 @@ async def test_make_agent_public_appends_to_the_config_file_without_db_settings(
assert duplicate.value.status_code == 400
+def _hold_the_first_invalidation_until_the_next_one(monkeypatch: pytest.MonkeyPatch) -> None:
+ from litellm.proxy import proxy_server
+
+ real_invalidate: Final = proxy_server.invalidate_config_param
+ next_invalidation: Final = asyncio.Event()
+ invalidations: Final = itertools.count()
+
+ async def invalidate(param_name: str) -> None:
+ if next(invalidations) == 0:
+ await asyncio.wait_for(next_invalidation.wait(), timeout=5)
+ else:
+ next_invalidation.set()
+ await real_invalidate(param_name)
+
+ monkeypatch.setattr(proxy_server, "invalidate_config_param", invalidate)
+
+
+@pytest.mark.asyncio
+async def test_make_agent_public_concurrent_publishes_stay_in_the_worker_list_when_invalidation_is_slow(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ import litellm
+
+ rows, _ = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[])
+ _hold_the_first_invalidation_until_the_next_one(monkeypatch)
+
+ await asyncio.gather(
+ agent_endpoints.make_agent_public(agent_id="agent-1", user_api_key_dict=_PROXY_ADMIN),
+ agent_endpoints.make_agent_public(agent_id="agent-2", user_api_key_dict=_PROXY_ADMIN),
+ )
+
+ assert rows.stored("litellm_settings")["public_agent_groups"] == ["agent-1", "agent-2"]
+ assert litellm.public_agent_groups == ["agent-1", "agent-2"]
+
+
+@pytest.mark.asyncio
+async def test_make_agent_public_is_seen_by_the_next_settings_read_on_this_worker(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ from litellm.caching.dual_cache import DualCache
+ from litellm.proxy import proxy_server
+ from litellm.proxy import utils as proxy_utils
+
+ rows, _ = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=["agent-1"])
+ live_replica: Final = ReplicaPrisma(rows)
+ monkeypatch.setattr(proxy_server, "prisma_client", live_replica)
+ monkeypatch.setattr(proxy_utils, "litellm_config_cache", DualCache())
+ await proxy_utils.get_config_param(live_replica, "litellm_settings")
+
+ await agent_endpoints.make_agent_public(agent_id="agent-2", user_api_key_dict=_PROXY_ADMIN)
+
+ cached_row: Final = await proxy_utils.get_config_param(live_replica, "litellm_settings")
+ assert cached_row.param_value["public_agent_groups"] == ["agent-1", "agent-2"]
+ assert proxy_server.proxy_config.litellm_settings.get("public_agent_groups") == ["agent-1", "agent-2"]
+
+
+def _make_agents_public(agent_ids: list[str], *, previous_agent_ids: list[str] | None):
+ return agent_endpoints.make_agents_public(
+ request=MakeAgentsPublicRequest(agent_ids=agent_ids, previous_agent_ids=previous_agent_ids),
+ user_api_key_dict=_PROXY_ADMIN,
+ )
+
+
+@pytest.mark.asyncio
+@pytest.mark.parametrize(
+ "stored, previous, requested, expected",
+ [
+ (["agent-1"], [], ["agent-2"], ["agent-1", "agent-2"]),
+ (["agent-1", "agent-2"], ["agent-1", "agent-2"], ["agent-2"], ["agent-2"]),
+ (["agent-2"], ["agent-1"], ["agent-1"], ["agent-2"]),
+ (["agent-1"], [], ["agent-1"], ["agent-1"]),
+ (["agent-2", "agent-1"], ["agent-1"], ["agent-1", "agent-2"], ["agent-2", "agent-1"]),
+ ([], [], ["agent-2", "agent-1", "agent-2"], ["agent-2", "agent-1"]),
+ ],
+ ids=[
+ "keeps-an-agent-the-stale-modal-never-showed",
+ "drops-only-the-deselected-agent",
+ "keeps-another-writers-removal",
+ "skips-an-agent-already-stored",
+ "keeps-the-stored-order",
+ "adds-in-request-order-once",
+ ],
+)
+async def test_make_agents_public_applies_only_the_modal_change_to_the_stored_list(
+ monkeypatch: pytest.MonkeyPatch, stored: list[str], previous: list[str], requested: list[str], expected: list[str]
+) -> None:
+ import litellm
+
+ rows, _ = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=stored)
+
+ response: Final = await _make_agents_public(requested, previous_agent_ids=previous)
+
+ assert response["public_agent_groups"] == expected
+ assert rows.stored("litellm_settings")["public_agent_groups"] == expected
+ assert litellm.public_agent_groups == expected
+
+
+@pytest.mark.asyncio
+async def test_make_agents_public_concurrent_modal_saves_keep_both_agents(monkeypatch: pytest.MonkeyPatch) -> None:
+ import litellm
+
+ rows, registry = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[])
+ _hold_the_first_invalidation_until_the_next_one(monkeypatch)
+
+ await asyncio.gather(
+ _make_agents_public(["agent-1"], previous_agent_ids=[]),
+ _make_agents_public(["agent-2"], previous_agent_ids=[]),
+ )
+
+ assert rows.stored("litellm_settings")["public_agent_groups"] == ["agent-1", "agent-2"]
+ assert litellm.public_agent_groups == ["agent-1", "agent-2"]
+ assert [agent.agent_id for agent in registry.get_public_agent_list()] == ["agent-1", "agent-2"]
+
+
+@pytest.mark.asyncio
+async def test_make_agents_public_treats_a_config_agents_legacy_id_as_the_same_agent(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ rows, registry = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[])
+ config_agent: Final = {"agent_name": "Config Agent", "agent_card_params": _sample_agent_card_params()}
+ registry.load_agents_from_config([config_agent])
+ stable_id: Final = registry.stable_agent_id(next(iter(registry.config_agent_legacy_ids)))
+ legacy_id: Final = next(iter(registry.ids_for_agent(stable_id) - {stable_id}))
+ rows.serialized_rows["litellm_settings"] = json.dumps({"public_agent_groups": [legacy_id, "agent-1"]})
+
+ kept: Final = await _make_agents_public([stable_id, "agent-1"], previous_agent_ids=[])
+ unpublished: Final = await _make_agents_public(["agent-1"], previous_agent_ids=[stable_id, "agent-1"])
+
+ assert kept["public_agent_groups"] == [legacy_id, "agent-1"]
+ assert unpublished["public_agent_groups"] == ["agent-1"]
+ assert rows.stored("litellm_settings")["public_agent_groups"] == ["agent-1"]
+
+
+@pytest.mark.asyncio
+async def test_make_agents_public_without_previous_agent_ids_replaces_the_stored_list(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ import litellm
+ from litellm.proxy import proxy_server
+
+ _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=["agent-1"])
+ saved_configs: Final = []
+ monkeypatch.setattr(
+ proxy_server.proxy_config, "get_config", AsyncMock(return_value={"litellm_settings": {"drop_params": True}})
+ )
+ monkeypatch.setattr(
+ proxy_server.proxy_config,
+ "save_config",
+ AsyncMock(side_effect=lambda new_config: saved_configs.append(new_config)),
+ )
+
+ response: Final = await _make_agents_public(["agent-2"], previous_agent_ids=None)
+
+ assert response["public_agent_groups"] == ["agent-2"]
+ assert litellm.public_agent_groups == ["agent-2"]
+ assert saved_configs == [{"litellm_settings": {"drop_params": True, "public_agent_groups": ["agent-2"]}}]
+
+
+@pytest.mark.asyncio
+async def test_make_agents_public_refuses_a_change_to_a_list_the_config_file_owns(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ from litellm.proxy import proxy_server
+
+ rows, _ = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[])
+ config_file: Final = tmp_path / "config.yaml"
+ config_file.write_text("litellm_settings:\n public_agent_groups: [agent-1]\n")
+ monkeypatch.setattr("litellm.proxy.proxy_server.user_config_file_path", str(config_file))
+ await proxy_server.proxy_config.get_config()
+
+ unchanged: Final = await _make_agents_public(["agent-1"], previous_agent_ids=["agent-1"])
+ with pytest.raises(HTTPException) as refused:
+ await _make_agents_public(["agent-1", "agent-2"], previous_agent_ids=["agent-1"])
+
+ assert unchanged["public_agent_groups"] == ["agent-1"]
+ assert refused.value.status_code == 400
+ assert refused.value.detail["keys"] == ["public_agent_groups"]
+ assert rows.stored("litellm_settings")["public_agent_groups"] == []
+
+
+@pytest.mark.asyncio
+async def test_make_agents_public_applies_the_modal_change_to_the_config_file_without_db_settings(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ import litellm
+
+ _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[], store_model_in_db=False)
+ config_file: Final = tmp_path / "config.yaml"
+ config_file.write_text("litellm_settings:\n public_agent_groups: [agent-1]\n")
+ monkeypatch.setattr("litellm.proxy.proxy_server.user_config_file_path", str(config_file))
+
+ added: Final = await _make_agents_public(["agent-2"], previous_agent_ids=[])
+ unchanged: Final = await _make_agents_public(["agent-1", "agent-2"], previous_agent_ids=["agent-1", "agent-2"])
+
+ assert added["public_agent_groups"] == unchanged["public_agent_groups"] == ["agent-1", "agent-2"]
+ assert yaml.safe_load(config_file.read_text())["litellm_settings"]["public_agent_groups"] == ["agent-1", "agent-2"]
+ assert litellm.public_agent_groups == ["agent-1", "agent-2"]
+
+
@pytest.mark.parametrize("enabled, claim_field, expected", [(True, "azp", True), (False, "azp", False), (True, None, False)])
def test_jwt_authentication_status_does_not_require_virtual_keys(
monkeypatch: pytest.MonkeyPatch, enabled: bool, claim_field: str | None, expected: bool
diff --git a/tests/unit/proxy/fake_config_table.py b/tests/unit/proxy/fake_config_table.py
new file mode 100644
index 00000000000..65d13c31dab
--- /dev/null
+++ b/tests/unit/proxy/fake_config_table.py
@@ -0,0 +1,73 @@
+import asyncio
+import json
+from collections.abc import Callable, Mapping
+from dataclasses import dataclass
+from types import SimpleNamespace
+from typing import Final
+
+
+@dataclass(frozen=True, slots=True)
+class StoredConfigRow:
+ param_name: str
+ param_value: dict[str, object]
+
+
+class ConfigRows:
+
+ def __init__(self, rows: Mapping[str, Mapping[str, object]]) -> None:
+ self.serialized_rows: dict[str, str] = {name: json.dumps(dict(value)) for name, value in rows.items()}
+
+ async def find_first(self, *, where: Mapping[str, str]) -> StoredConfigRow | None:
+ name: Final = where["param_name"]
+ serialized: Final = self.serialized_rows.get(name)
+ await asyncio.sleep(0)
+ return None if serialized is None else StoredConfigRow(param_name=name, param_value=json.loads(serialized))
+
+ async def upsert(self, *, where: Mapping[str, str], data: Mapping[str, Mapping[str, str]]) -> None:
+ self.serialized_rows[where["param_name"]] = data["update"]["param_value"]
+
+ def stored(self, name: str) -> dict[str, object]:
+ return json.loads(self.serialized_rows[name])
+
+
+class SectionLockedTransaction:
+
+ def __init__(self, litellm_config: object, section_lock: asyncio.Lock) -> None:
+ self.litellm_config: Final = litellm_config
+ self._section_lock: Final = section_lock
+ self._holds_section_lock = False
+
+ async def __aenter__(self) -> "SectionLockedTransaction":
+ return self
+
+ async def __aexit__(self, *_: object) -> None:
+ if self._holds_section_lock:
+ self._section_lock.release()
+
+ async def query_raw(self, query: str, section_name: str) -> list[dict[str, int]]:
+ await self._section_lock.acquire()
+ self._holds_section_lock = True
+ return [{"locked": 1}]
+
+
+def section_locked_transactions(litellm_config: object) -> Callable[[], SectionLockedTransaction]:
+ section_lock: Final = asyncio.Lock()
+ return lambda: SectionLockedTransaction(litellm_config, section_lock)
+
+
+class ReplicaPrisma:
+
+ def __init__(self, rows: ConfigRows, cached_rows: Mapping[str, Mapping[str, object]] | None = None) -> None:
+ self.rows: Final = rows
+ self._cached_rows: Final = cached_rows
+ self.tx: Final = section_locked_transactions(rows)
+ self.db: Final = SimpleNamespace(litellm_config=rows)
+ self.writer_db: Final = self.db
+
+ async def get_generic_data(self, key: str, value: str, table_name: str) -> SimpleNamespace | None:
+ cached: Final = (
+ self._cached_rows.get(value)
+ if self._cached_rows is not None
+ else (self.rows.stored(value) if value in self.rows.serialized_rows else None)
+ )
+ return None if cached is None else SimpleNamespace(param_name=value, param_value=dict(cached))
diff --git a/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py b/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py
index 4b1415c8879..c2ded683ebb 100644
--- a/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py
+++ b/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py
@@ -46,6 +46,7 @@ from litellm.proxy.mcp_server.mcp_server_manager import MCPServerConfig, MCPServ
from litellm.proxy.utils import ProxyLogging
from litellm.types.mcp import MCPAuth, MCPCredentials
from litellm.types.mcp_server.mcp_server_manager import MCPServer, PinnedMCPTool
+from tests.unit.proxy.fake_config_table import ConfigRows, ReplicaPrisma
def generate_mock_mcp_server_db_record(
@@ -315,6 +316,34 @@ async def test_mcp_publication_updates_runtime_only_after_successful_save(
save.assert_awaited_once_with(new_config=expected_config)
+@pytest.mark.asyncio
+async def test_mcp_publication_from_the_modal_keeps_a_server_another_admin_published(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ from litellm.proxy.proxy_server import ProxyConfig
+
+ manager: Final = MCPServerManager()
+ manager.config_mcp_servers = {
+ server_id: generate_mock_mcp_server_config_record(server_id=server_id)
+ for server_id in ("server-1", "server-2", "server-3")
+ }
+ rows: Final = ConfigRows({"litellm_settings": {"public_mcp_servers": ["server-1", "server-2"]}})
+ monkeypatch.setattr(litellm, "public_mcp_servers", [])
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", ReplicaPrisma(rows))
+ monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True)
+ monkeypatch.setattr("litellm.proxy.proxy_server.proxy_config", ProxyConfig())
+ monkeypatch.setattr("litellm.proxy.mcp_server.mcp_server_manager.global_mcp_server_manager", manager)
+
+ response: Final = await mgmt_endpoints.make_mcp_servers_public(
+ MakeMCPServersPublicRequest(mcp_server_ids=["server-3"], previous_mcp_server_ids=["server-2"]),
+ generate_mock_user_api_key_auth(user_role=LitellmUserRoles.PROXY_ADMIN),
+ )
+
+ assert response["public_mcp_servers"] == ["server-1", "server-3"]
+ assert rows.stored("litellm_settings")["public_mcp_servers"] == ["server-1", "server-3"]
+ assert litellm.public_mcp_servers == ["server-1", "server-3"]
+
+
class TestMCPCredentialsTokenExchangeProfile:
"""token_exchange_profile must be a declared MCPCredentials field so the management API can
persist the entra_obo profile. An undeclared key is silently stripped by pydantic when the
diff --git a/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py b/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py
index 437d948f8f5..038087cab72 100644
--- a/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py
+++ b/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py
@@ -44,6 +44,7 @@ from litellm.proxy.management_helpers.model_quota_policy import REQUIRE_BOTH_LIM
from litellm.proxy.utils import PrismaClient
from litellm.router import Router
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo, updateDeployment, updateLiteLLMParams
+from tests.unit.proxy.fake_config_table import ConfigRows, ReplicaPrisma
async def _passthrough_row(update_data):
@@ -1757,6 +1758,33 @@ class TestUpdateModel:
assert persisted["litellm_credential_name"] == "shared-credential"
+@pytest.mark.asyncio
+async def test_update_public_model_groups_from_the_modal_keeps_a_group_another_admin_published(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ import litellm
+ from litellm.proxy.management_endpoints.model_management_endpoints import (
+ UpdatePublicModelGroupsRequest,
+ update_public_model_groups,
+ )
+ from litellm.proxy.proxy_server import ProxyConfig
+
+ rows: Final = ConfigRows({"litellm_settings": {"public_model_groups": ["gpt-a", "gpt-b"]}})
+ monkeypatch.setattr(litellm, "public_model_groups", [])
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", ReplicaPrisma(rows))
+ monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True)
+ monkeypatch.setattr("litellm.proxy.proxy_server.proxy_config", ProxyConfig())
+
+ response: Final = await update_public_model_groups(
+ request=UpdatePublicModelGroupsRequest(model_groups=["gpt-c"], previous_model_groups=["gpt-b"]),
+ user_api_key_dict=UserAPIKeyAuth(user_id="admin", user_role=LitellmUserRoles.PROXY_ADMIN),
+ )
+
+ assert response["public_model_groups"] == ["gpt-a", "gpt-c"]
+ assert rows.stored("litellm_settings")["public_model_groups"] == ["gpt-a", "gpt-c"]
+ assert litellm.public_model_groups == ["gpt-a", "gpt-c"]
+
+
class TestUpdatePublicModelGroups:
"""Test that update_public_model_groups correctly sets litellm.public_model_groups
even when get_config() overwrites it with stale DB values."""
diff --git a/tests/unit/proxy/proxy_server/test_routes_config.py b/tests/unit/proxy/proxy_server/test_routes_config.py
index e3b8aff8fcb..e4ce602e630 100644
--- a/tests/unit/proxy/proxy_server/test_routes_config.py
+++ b/tests/unit/proxy/proxy_server/test_routes_config.py
@@ -24,6 +24,8 @@ import httpx
import pytest
from fastapi.testclient import TestClient
+from tests.unit.proxy.fake_config_table import section_locked_transactions
+
from .conftest import VOLATILE_KEYS, normalize
@@ -52,6 +54,7 @@ def _install_litellm_config(mock_prisma: MagicMock) -> MagicMock:
table.delete = AsyncMock()
mock_prisma.db.litellm_config = table
mock_prisma.writer_db.litellm_config = table
+ mock_prisma.tx = section_locked_transactions(table)
return table
diff --git a/tests/unit/proxy/test_proxy_server.py b/tests/unit/proxy/test_proxy_server.py
index 976ed4f28a5..16f32355ab0 100644
--- a/tests/unit/proxy/test_proxy_server.py
+++ b/tests/unit/proxy/test_proxy_server.py
@@ -9,6 +9,7 @@ from unittest import mock
from dotenv import load_dotenv
from tests._master_key import MASTER_KEY
+from tests.unit.proxy.fake_config_table import section_locked_transactions
import litellm.proxy
import litellm.proxy.proxy_server
@@ -3231,6 +3232,7 @@ async def test_update_config_success_callback_normalization(monkeypatch):
self.db.litellm_config.find_first = AsyncMock(side_effect=fake_find_first)
self.db.litellm_config.upsert = AsyncMock(side_effect=fake_upsert)
self.writer_db = self.db
+ self.tx = section_locked_transactions(self.db.litellm_config)
monkeypatch.setattr(proxy_server, "prisma_client", MockPrisma())
diff --git a/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py b/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py
index 55d1587669a..f1664b673d7 100644
--- a/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py
+++ b/tests/unit/proxy/test_proxy_server_endpoints_and_startup.py
@@ -50,6 +50,7 @@ from litellm.proxy.proxy_server import app, aws_error_type_exception_handler, in
from litellm.utils import _invalidate_model_cost_lowercase_map
from litellm.proxy.common_utils.banner import LITELLM_BANNER
from tests._master_key import MASTER_KEY
+from tests.unit.proxy.fake_config_table import ConfigRows, ReplicaPrisma, section_locked_transactions
PUBLICLY_KNOWN_KEY: Final = "sk-" + "1234"
@@ -9838,6 +9839,7 @@ class _FakePrismaClient:
if replica_rows is None
else _LaggingReplicaLitellmConfig(replica_rows, writer=self.writer_db.litellm_config)
)
+ self.tx = section_locked_transactions(self.writer_db.litellm_config)
self.jsonify_object = lambda obj: obj
@@ -9902,6 +9904,58 @@ def test_update_config_writes_only_sent_section(_update_config_setup):
restore()
+@pytest.mark.parametrize(
+ "stored_general_settings, expected_alerting",
+ [({}, ["slack"]), ({"alerting": ["email"]}, ["email", "slack"]), ({"alerting": ["slack", "email"]}, ["slack", "email"])],
+)
+def test_update_config_alert_webhook_turns_on_slack_alerting_once(
+ _update_config_setup, stored_general_settings, expected_alerting
+):
+ webhooks = {"budget_alerts": "https://hooks.example.com/budget"}
+ client, prisma, restore = _update_config_setup(initial_rows={"general_settings": stored_general_settings})
+ try:
+ resp = client.post("/config/update", json={"general_settings": {"alert_to_webhook_url": webhooks}})
+ assert resp.status_code == 200
+ assert prisma.writer_db.litellm_config.rows["general_settings"] == {
+ "alerting": expected_alerting,
+ "alert_to_webhook_url": webhooks,
+ }
+ finally:
+ restore()
+
+
+@pytest.mark.asyncio
+async def test_update_config_and_a_concurrent_publish_both_stay_in_litellm_settings(monkeypatch):
+ from litellm.proxy import proxy_server
+ from litellm.proxy._types import ConfigYAML, LitellmUserRoles, UserAPIKeyAuth
+
+ rows = ConfigRows({"litellm_settings": {"drop_params": True}})
+ proxy_config = proxy_server.ProxyConfig()
+ monkeypatch.setattr(proxy_server, "prisma_client", ReplicaPrisma(rows))
+ monkeypatch.setattr(proxy_server, "store_model_in_db", True)
+ monkeypatch.setattr(proxy_server, "proxy_config", proxy_config)
+ monkeypatch.setattr(proxy_config, "add_deployment", AsyncMock(return_value=None))
+ request = MagicMock()
+ request.json = AsyncMock(return_value={"litellm_settings": {"num_retries": 3}})
+
+ await asyncio.gather(
+ proxy_server.update_config(
+ config_info=ConfigYAML(litellm_settings={"num_retries": 3}),
+ request=request,
+ user_api_key_dict=UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, api_key=MASTER_KEY),
+ ),
+ proxy_config.append_to_list_setting(
+ section_name="litellm_settings",
+ key="public_agent_groups",
+ item="agent-1",
+ listed_as=frozenset({"agent-1"}),
+ on_written=lambda _: None,
+ ),
+ )
+
+ assert rows.stored("litellm_settings") == {"drop_params": True, "num_retries": 3, "public_agent_groups": ["agent-1"]}
+
+
def test_update_config_keeps_an_environment_variable_the_read_replica_has_not_caught_up_on(_update_config_setup):
client, prisma, restore = _update_config_setup(
initial_rows={"environment_variables": {"SAVED_A_MOMENT_AGO": "enc:new"}},
diff --git a/tests/unit/test_router_retry_policy_update.py b/tests/unit/test_router_retry_policy_update.py
index 6ab672d7b04..6a5d46b6a02 100644
--- a/tests/unit/test_router_retry_policy_update.py
+++ b/tests/unit/test_router_retry_policy_update.py
@@ -34,6 +34,7 @@ from litellm.router_utils.pre_call_checks.encrypted_content_affinity_check impor
from litellm.router_utils.pre_call_checks.model_rate_limit_check import ModelRateLimitingCheck
from litellm.router_utils.pre_call_checks.prompt_caching_deployment_check import PromptCachingDeploymentCheck
from litellm.types.router import RetryPolicy, UpdateRouterConfig
+from tests.unit.proxy.fake_config_table import section_locked_transactions
@pytest.fixture(autouse=True)
@@ -431,6 +432,7 @@ async def test_config_update_persists_and_reads_back_retry_policy(monkeypatch):
prisma_client = MagicMock()
prisma_client.db.litellm_config = fake_table
prisma_client.writer_db = prisma_client.db
+ prisma_client.tx = section_locked_transactions(fake_table)
async def _apply_router_settings(*args, **kwargs):
await proxy_server.proxy_config._add_router_settings_from_db_config(
diff --git a/ui/litellm-dashboard/src/components/AIHub/ModelHubTable.test.tsx b/ui/litellm-dashboard/src/components/AIHub/ModelHubTable.test.tsx
index 123c7431732..70b455c84f6 100644
--- a/ui/litellm-dashboard/src/components/AIHub/ModelHubTable.test.tsx
+++ b/ui/litellm-dashboard/src/components/AIHub/ModelHubTable.test.tsx
@@ -741,7 +741,7 @@ describe("ModelHubTable", () => {
await user.click(screen.getByRole("button", { name: "Next" }));
await user.click(screen.getByRole("button", { name: "Save Publication List" }));
- expect(networking.makeMCPPublicCall).toHaveBeenCalledWith("test-token", []);
+ expect(networking.makeMCPPublicCall).toHaveBeenCalledWith("test-token", [], []);
expect(manageVisibility).toBeDisabled();
await act(async () => refresh.reject(new Error("Unable to reload the publication list")));
expect(manageVisibility).toBeDisabled();
diff --git a/ui/litellm-dashboard/src/components/AIHub/forms/MakeAgentPublicForm.test.tsx b/ui/litellm-dashboard/src/components/AIHub/forms/MakeAgentPublicForm.test.tsx
index 882e059e327..029c8f1db8e 100644
--- a/ui/litellm-dashboard/src/components/AIHub/forms/MakeAgentPublicForm.test.tsx
+++ b/ui/litellm-dashboard/src/components/AIHub/forms/MakeAgentPublicForm.test.tsx
@@ -131,7 +131,7 @@ describe("MakeAgentPublicForm", () => {
});
await waitFor(() => {
- expect(mockMakeAgentsPublicCall).toHaveBeenCalledWith("test-token", ["agent-1", "agent-2"]);
+ expect(mockMakeAgentsPublicCall).toHaveBeenCalledWith("test-token", ["agent-1", "agent-2"], ["agent-2"]);
expect(mockProps.onSuccess).toHaveBeenCalled();
expect(mockProps.onClose).toHaveBeenCalled();
});
@@ -276,6 +276,19 @@ describe("MakeAgentPublicForm", () => {
expect(selectAllCheckbox).toBePartiallyChecked();
});
+ it("sends the agents public when the modal opened next to the edited selection", async () => {
+ mockMakeAgentsPublicCall.mockResolvedValueOnce({});
+ render();
+
+ const [, agent1Checkbox, agent2Checkbox] = screen.getAllByRole("checkbox");
+ fireEvent.click(agent1Checkbox);
+ fireEvent.click(agent2Checkbox);
+ fireEvent.click(screen.getByRole("button", { name: "Next" }));
+ fireEvent.click(screen.getByRole("button", { name: "Make Public" }));
+
+ await waitFor(() => expect(mockMakeAgentsPublicCall).toHaveBeenCalledWith("test-token", ["agent-1"], ["agent-2"]));
+ });
+
it("should display skills overflow text when agent has more than 3 skills", () => {
const agentWithManySkills = {
...mockProps.agentHubData[0],
@@ -326,7 +339,7 @@ describe("MakeAgentPublicForm", () => {
// Should handle error and show error notification
await waitFor(() => {
- expect(mockMakeAgentsPublicCall).toHaveBeenCalledWith("test-token", ["agent-2"]);
+ expect(mockMakeAgentsPublicCall).toHaveBeenCalledWith("test-token", ["agent-2"], ["agent-2"]);
});
// Should not call onSuccess or onClose on error
diff --git a/ui/litellm-dashboard/src/components/AIHub/forms/MakeAgentPublicForm.tsx b/ui/litellm-dashboard/src/components/AIHub/forms/MakeAgentPublicForm.tsx
index ef6d72f9c41..2d458ae05ed 100644
--- a/ui/litellm-dashboard/src/components/AIHub/forms/MakeAgentPublicForm.tsx
+++ b/ui/litellm-dashboard/src/components/AIHub/forms/MakeAgentPublicForm.tsx
@@ -28,11 +28,13 @@ const MakeAgentPublicForm: React.FC = ({
}) => {
const [currentStep, setCurrentStep] = useState(0);
const [selectedAgents, setSelectedAgents] = useState>(new Set());
+ const [preselectedAgents, setPreselectedAgents] = useState>(new Set());
const [loading, setLoading] = useState(false);
const handleClose = () => {
setCurrentStep(0);
setSelectedAgents(new Set());
+ setPreselectedAgents(new Set());
onClose();
};
@@ -80,6 +82,7 @@ const MakeAgentPublicForm: React.FC = ({
.map((agent) => agent.agent_id || agent.name);
setSelectedAgents(new Set(alreadyPublicAgents));
+ setPreselectedAgents(new Set(alreadyPublicAgents));
}
}, [visible, agentHubData]);
@@ -94,7 +97,7 @@ const MakeAgentPublicForm: React.FC = ({
const agentIdsToMakePublic = Array.from(selectedAgents);
// Make batch API call for all agents
- await makeAgentsPublicCall(accessToken, agentIdsToMakePublic);
+ await makeAgentsPublicCall(accessToken, agentIdsToMakePublic, Array.from(preselectedAgents));
toast.success(`Successfully made ${agentIdsToMakePublic.length} agent(s) public!`);
handleClose();
diff --git a/ui/litellm-dashboard/src/components/AIHub/forms/MakeMCPPublicForm.test.tsx b/ui/litellm-dashboard/src/components/AIHub/forms/MakeMCPPublicForm.test.tsx
index 881711c1668..c4c85b2829c 100644
--- a/ui/litellm-dashboard/src/components/AIHub/forms/MakeMCPPublicForm.test.tsx
+++ b/ui/litellm-dashboard/src/components/AIHub/forms/MakeMCPPublicForm.test.tsx
@@ -160,7 +160,7 @@ describe("MakeMCPPublicForm", () => {
});
await waitFor(() => {
- expect(mockMakeMCPPublicCall).toHaveBeenCalledWith("test-token", ["server-1", "server-2"]);
+ expect(mockMakeMCPPublicCall).toHaveBeenCalledWith("test-token", ["server-1", "server-2"], ["server-2"]);
expect(mockProps.onSuccess).toHaveBeenCalled();
expect(mockProps.onClose).toHaveBeenCalled();
});
@@ -202,7 +202,7 @@ describe("MakeMCPPublicForm", () => {
fireEvent.click(screen.getByRole("button", { name: "Next" }));
fireEvent.click(screen.getByRole("button", { name: "Save Publication List" }));
- await waitFor(() => expect(mockMakeMCPPublicCall).toHaveBeenCalledWith("test-token", []));
+ await waitFor(() => expect(mockMakeMCPPublicCall).toHaveBeenCalledWith("test-token", [], ["server-2"]));
expect(mockProps.onSuccess).toHaveBeenCalled();
});
@@ -310,7 +310,7 @@ describe("MakeMCPPublicForm", () => {
fireEvent.click(nextButton);
fireEvent.click(screen.getByRole("button", { name: "Save Publication List" }));
- await waitFor(() => expect(mockMakeMCPPublicCall).toHaveBeenCalledWith("test-token", []));
+ await waitFor(() => expect(mockMakeMCPPublicCall).toHaveBeenCalledWith("test-token", [], []));
expect(mockProps.onSuccess).toHaveBeenCalled();
});
@@ -385,6 +385,19 @@ describe("MakeMCPPublicForm", () => {
expect(selectAllCheckbox).toBePartiallyChecked();
});
+ it("sends the servers public when the modal opened next to the edited selection", async () => {
+ mockMakeMCPPublicCall.mockResolvedValueOnce({});
+ render();
+
+ const [, server1Checkbox, server2Checkbox] = screen.getAllByRole("checkbox");
+ fireEvent.click(server1Checkbox);
+ fireEvent.click(server2Checkbox);
+ fireEvent.click(screen.getByRole("button", { name: "Next" }));
+ fireEvent.click(screen.getByRole("button", { name: "Save Publication List" }));
+
+ await waitFor(() => expect(mockMakeMCPPublicCall).toHaveBeenCalledWith("test-token", ["server-1"], ["server-2"]));
+ });
+
it("should display tools overflow text when server has more than 3 tools", () => {
const serverWithManyTools = {
...mockProps.mcpHubData[0],
@@ -429,7 +442,7 @@ describe("MakeMCPPublicForm", () => {
// Should handle error and show error notification
await waitFor(() => {
- expect(mockMakeMCPPublicCall).toHaveBeenCalledWith("test-token", ["server-2"]);
+ expect(mockMakeMCPPublicCall).toHaveBeenCalledWith("test-token", ["server-2"], ["server-2"]);
});
expect(toast.fromError).toHaveBeenCalledWith(error);
diff --git a/ui/litellm-dashboard/src/components/AIHub/forms/MakeMCPPublicForm.tsx b/ui/litellm-dashboard/src/components/AIHub/forms/MakeMCPPublicForm.tsx
index 2448732a236..a7229e79197 100644
--- a/ui/litellm-dashboard/src/components/AIHub/forms/MakeMCPPublicForm.tsx
+++ b/ui/litellm-dashboard/src/components/AIHub/forms/MakeMCPPublicForm.tsx
@@ -32,6 +32,7 @@ interface MakeMCPPublicFormProps {
interface PublicationSelection {
readonly catalog: MCPServerData[];
+ readonly previousServerIds: ReadonlySet;
readonly serverIds: Set;
}
@@ -46,6 +47,7 @@ const MakeMCPPublicForm: React.FC = ({
const [selection, setSelection] = useState(null);
const [loading, setLoading] = useState(false);
const selectedServers = selection?.serverIds ?? new Set();
+ const previousServers: ReadonlySet = selection?.previousServerIds ?? new Set();
const hasPublicationMetadata = mcpHubData.every((server) => typeof server.mcp_info?.is_public_explicit === "boolean");
const canManagePublication = hasPublicationMetadata && selection?.catalog === mcpHubData;
const publicationYaml = [
@@ -82,15 +84,15 @@ const MakeMCPPublicForm: React.FC = ({
} else {
newSelection.delete(serverId);
}
- setSelection({ catalog: mcpHubData, serverIds: newSelection });
+ setSelection({ catalog: mcpHubData, previousServerIds: previousServers, serverIds: newSelection });
};
const handleSelectAll = (checked: boolean) => {
if (checked) {
const allServerIds = mcpHubData.map((server) => server.server_id);
- setSelection({ catalog: mcpHubData, serverIds: new Set(allServerIds) });
+ setSelection({ catalog: mcpHubData, previousServerIds: previousServers, serverIds: new Set(allServerIds) });
} else {
- setSelection({ catalog: mcpHubData, serverIds: new Set() });
+ setSelection({ catalog: mcpHubData, previousServerIds: previousServers, serverIds: new Set() });
}
};
@@ -102,7 +104,11 @@ const MakeMCPPublicForm: React.FC = ({
const publicServerIds = mcpHubData
.filter((server) => server.mcp_info.is_public_explicit === true)
.map((server) => server.server_id);
- setSelection({ catalog: mcpHubData, serverIds: new Set(publicServerIds) });
+ setSelection({
+ catalog: mcpHubData,
+ previousServerIds: new Set(publicServerIds),
+ serverIds: new Set(publicServerIds),
+ });
setCurrentStep(0);
}, [visible, mcpHubData, hasPublicationMetadata]);
@@ -113,7 +119,7 @@ const MakeMCPPublicForm: React.FC = ({
const serverIdsToMakePublic = Array.from(selectedServers);
// Make batch API call for all servers
- await makeMCPPublicCall(accessToken, serverIdsToMakePublic);
+ await makeMCPPublicCall(accessToken, serverIdsToMakePublic, Array.from(previousServers));
toast.success("MCP Hub publication list updated");
handleClose();
diff --git a/ui/litellm-dashboard/src/components/AIHub/forms/MakeModelPublicForm.test.tsx b/ui/litellm-dashboard/src/components/AIHub/forms/MakeModelPublicForm.test.tsx
index f99facad6fb..27534215935 100644
--- a/ui/litellm-dashboard/src/components/AIHub/forms/MakeModelPublicForm.test.tsx
+++ b/ui/litellm-dashboard/src/components/AIHub/forms/MakeModelPublicForm.test.tsx
@@ -170,7 +170,11 @@ describe("MakeModelPublicForm", () => {
});
await waitFor(() => {
- expect(mockMakeModelGroupPublic).toHaveBeenCalledWith("test-token", ["gpt-4", "gpt-3.5-turbo"]);
+ expect(mockMakeModelGroupPublic).toHaveBeenCalledWith(
+ "test-token",
+ ["gpt-4", "gpt-3.5-turbo"],
+ ["gpt-3.5-turbo"],
+ );
expect(mockProps.onSuccess).toHaveBeenCalled();
expect(mockProps.onClose).toHaveBeenCalled();
});
@@ -315,6 +319,21 @@ describe("MakeModelPublicForm", () => {
expect(selectAllCheckbox).toBePartiallyChecked();
});
+ it("sends the model groups public when the modal opened next to the edited selection", async () => {
+ mockMakeModelGroupPublic.mockResolvedValueOnce({});
+ render();
+
+ const [, gpt4Checkbox, gpt35Checkbox] = screen.getAllByRole("checkbox");
+ fireEvent.click(gpt4Checkbox);
+ fireEvent.click(gpt35Checkbox);
+ fireEvent.click(screen.getByRole("button", { name: "Next" }));
+ fireEvent.click(screen.getByRole("button", { name: "Make Public" }));
+
+ await waitFor(() =>
+ expect(mockMakeModelGroupPublic).toHaveBeenCalledWith("test-token", ["gpt-4"], ["gpt-3.5-turbo"]),
+ );
+ });
+
it("should display model badges and information", () => {
render();
@@ -364,7 +383,7 @@ describe("MakeModelPublicForm", () => {
// Should handle error and show error notification
await waitFor(() => {
- expect(mockMakeModelGroupPublic).toHaveBeenCalledWith("test-token", ["gpt-3.5-turbo"]);
+ expect(mockMakeModelGroupPublic).toHaveBeenCalledWith("test-token", ["gpt-3.5-turbo"], ["gpt-3.5-turbo"]);
});
// Should not call onSuccess or onClose on error
diff --git a/ui/litellm-dashboard/src/components/AIHub/forms/MakeModelPublicForm.tsx b/ui/litellm-dashboard/src/components/AIHub/forms/MakeModelPublicForm.tsx
index 20fa0295fed..4247bd8a622 100644
--- a/ui/litellm-dashboard/src/components/AIHub/forms/MakeModelPublicForm.tsx
+++ b/ui/litellm-dashboard/src/components/AIHub/forms/MakeModelPublicForm.tsx
@@ -47,12 +47,14 @@ const MakeModelPublicForm: React.FC = ({
}) => {
const [currentStep, setCurrentStep] = useState(0);
const [selectedModels, setSelectedModels] = useState>(new Set());
+ const [preselectedModels, setPreselectedModels] = useState>(new Set());
const [filteredData, setFilteredData] = useState([]);
const [loading, setLoading] = useState(false);
const handleClose = () => {
setCurrentStep(0);
setSelectedModels(new Set());
+ setPreselectedModels(new Set());
setFilteredData([]);
onClose();
};
@@ -108,6 +110,7 @@ const MakeModelPublicForm: React.FC = ({
.map((model) => model.model_group);
setSelectedModels(new Set(alreadyPublicModels));
+ setPreselectedModels(new Set(alreadyPublicModels));
}
}, [visible, modelHubData]);
@@ -120,7 +123,7 @@ const MakeModelPublicForm: React.FC = ({
setLoading(true);
try {
const modelGroupsToMakePublic = Array.from(selectedModels);
- await makeModelGroupPublic(accessToken, modelGroupsToMakePublic);
+ await makeModelGroupPublic(accessToken, modelGroupsToMakePublic, Array.from(preselectedModels));
toast.success(`Successfully made ${modelGroupsToMakePublic.length} model group(s) public!`);
handleClose();
diff --git a/ui/litellm-dashboard/src/components/networking.test.ts b/ui/litellm-dashboard/src/components/networking.test.ts
index 874cdb52fad..e31384d39d6 100644
--- a/ui/litellm-dashboard/src/components/networking.test.ts
+++ b/ui/litellm-dashboard/src/components/networking.test.ts
@@ -1225,3 +1225,33 @@ describe("searchToolQueryCall", () => {
await expect(Networking.searchToolQueryCall("token", "exa-search-2", "litellm")).rejects.toThrow(errorMessage);
});
});
+
+describe("AI Hub publication calls", () => {
+ const originalFetch = global.fetch;
+
+ afterEach(() => {
+ global.fetch = originalFetch;
+ });
+
+ it.each([
+ {
+ send: () => Networking.makeAgentsPublicCall("token", ["agent-1"], ["agent-2"]),
+ body: { agent_ids: ["agent-1"], previous_agent_ids: ["agent-2"] },
+ },
+ {
+ send: () => Networking.makeMCPPublicCall("token", ["server-1"], ["server-2"]),
+ body: { mcp_server_ids: ["server-1"], previous_mcp_server_ids: ["server-2"] },
+ },
+ {
+ send: () => Networking.makeModelGroupPublic("token", ["gpt-4"], ["gpt-3.5-turbo"]),
+ body: { model_groups: ["gpt-4"], previous_model_groups: ["gpt-3.5-turbo"] },
+ },
+ ])("sends the selected list with the list it started from: $body", async ({ send, body }) => {
+ const mockFetch = vi.fn().mockResolvedValue(new Response("{}", { status: 200 }));
+ global.fetch = mockFetch;
+
+ await send();
+
+ expect(JSON.parse(String(mockFetch.mock.calls.at(-1)?.[1]?.body))).toEqual(body);
+ });
+});
diff --git a/ui/litellm-dashboard/src/components/networking.tsx b/ui/litellm-dashboard/src/components/networking.tsx
index 99a9dc7a707..e975e968065 100644
--- a/ui/litellm-dashboard/src/components/networking.tsx
+++ b/ui/litellm-dashboard/src/components/networking.tsx
@@ -545,7 +545,11 @@ registerAuthHeaderNameGetter(getGlobalLitellmHeaderName);
registerAuthTokenGetter(() => decodeToken(getCookie("token"))?.key ?? null);
registerErrorHandler(handleError);
-export const makeModelGroupPublic = async (accessToken: string, modelGroups: string[]) => {
+export const makeModelGroupPublic = async (
+ accessToken: string,
+ modelGroups: string[],
+ previousModelGroups: string[],
+) => {
const url = proxyBaseUrl ? `${proxyBaseUrl}/model_group/make_public` : `/model_group/make_public`;
const response = await fetch(url, {
method: "POST",
@@ -555,6 +559,7 @@ export const makeModelGroupPublic = async (accessToken: string, modelGroups: str
},
body: JSON.stringify({
model_groups: modelGroups,
+ previous_model_groups: previousModelGroups,
}),
});
return response.json();
@@ -5833,7 +5838,7 @@ export const deleteAgentCall = async (accessToken: string, agentId: string) => {
}
};
-export const makeAgentsPublicCall = async (accessToken: string, agentIds: string[]) => {
+export const makeAgentsPublicCall = async (accessToken: string, agentIds: string[], previousAgentIds: string[]) => {
try {
const url = proxyBaseUrl ? `${proxyBaseUrl}/v1/agents/make_public` : `/v1/agents/make_public`;
@@ -5845,6 +5850,7 @@ export const makeAgentsPublicCall = async (accessToken: string, agentIds: string
},
body: JSON.stringify({
agent_ids: agentIds,
+ previous_agent_ids: previousAgentIds,
}),
});
@@ -5862,7 +5868,11 @@ export const makeAgentsPublicCall = async (accessToken: string, agentIds: string
}
};
-export const makeMCPPublicCall = async (accessToken: string, mcpServerIds: string[]) => {
+export const makeMCPPublicCall = async (
+ accessToken: string,
+ mcpServerIds: string[],
+ previousMcpServerIds: string[],
+) => {
try {
const url = proxyBaseUrl ? `${proxyBaseUrl}/v1/mcp/make_public` : `/v1/mcp/make_public`;
@@ -5874,6 +5884,7 @@ export const makeMCPPublicCall = async (accessToken: string, mcpServerIds: strin
},
body: JSON.stringify({
mcp_server_ids: mcpServerIds,
+ previous_mcp_server_ids: previousMcpServerIds,
}),
});
diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts
index 51348fa9c75..d198f703e50 100644
--- a/ui/litellm-dashboard/src/lib/http/schema.d.ts
+++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts
@@ -38033,11 +38033,21 @@ export interface components {
MakeAgentsPublicRequest: {
/** Agent Ids */
agent_ids: string[];
+ /**
+ * Previous Agent Ids
+ * @description The public agent list the caller started from. When set, only the agents added or removed relative to it change, so agents another admin published or unpublished meanwhile keep that change. When omitted, agent_ids replaces the stored list.
+ */
+ previous_agent_ids?: string[] | null;
};
/** MakeMCPServersPublicRequest */
MakeMCPServersPublicRequest: {
/** Mcp Server Ids */
mcp_server_ids: string[];
+ /**
+ * Previous Mcp Server Ids
+ * @description The public MCP server list the caller started from. When set, only the servers added or removed relative to it change, so servers another admin published or unpublished meanwhile keep that change. When omitted, mcp_server_ids replaces the stored list.
+ */
+ previous_mcp_server_ids?: string[] | null;
};
/** ManagedAgentIdentityStatus */
ManagedAgentIdentityStatus: {
@@ -48720,6 +48730,11 @@ export interface components {
* @description List of model group names to make public
*/
model_groups: string[];
+ /**
+ * Previous Model Groups
+ * @description The public model group list the caller started from. When set, only the model groups added or removed relative to it change, so model groups another admin published or unpublished meanwhile keep that change. When omitted, model_groups replaces the stored list.
+ */
+ previous_model_groups?: string[] | null;
};
/**
* UpdateRouterConfig
From 2f9db93c3a457555618cc2a053ff7a34f6d97a89 Mon Sep 17 00:00:00 2001
From: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
Date: Sat, 31 Oct 2026 03:20:52 -0700
Subject: [PATCH 3/3] fix(ai hub): unpublish deleted agents and MCP servers,
merge useful link edits, match legacy agent ids
---
.../proxy/agent_endpoints/agent_registry.py | 3 +-
litellm/proxy/agent_endpoints/endpoints.py | 9 +-
.../mcp_management_endpoints.py | 8 ++
.../model_management_endpoints.py | 24 ++++-
litellm/proxy/proxy_server.py | 97 ++++++++++++++++---
.../model_management_endpoints.py | 8 ++
.../management/test_ai_hub_publish.py | 69 +++++++++++++
.../proxy/agent_endpoints/test_endpoints.py | 60 ++++++++++++
tests/unit/proxy/fake_config_table.py | 9 +-
.../test_mcp_management_endpoints.py | 59 +++++++++++
.../test_model_management_endpoints.py | 65 +++++++++++++
.../AIHub/UsefulLinksManagement.test.tsx | 61 +++++++++---
.../AIHub/UsefulLinksManagement.tsx | 5 +-
.../src/components/networking.tsx | 3 +-
ui/litellm-dashboard/src/lib/http/schema.d.ts | 9 ++
15 files changed, 455 insertions(+), 34 deletions(-)
create mode 100644 tests/integration/management/test_ai_hub_publish.py
diff --git a/litellm/proxy/agent_endpoints/agent_registry.py b/litellm/proxy/agent_endpoints/agent_registry.py
index 7929f67720d..edebfcd683c 100644
--- a/litellm/proxy/agent_endpoints/agent_registry.py
+++ b/litellm/proxy/agent_endpoints/agent_registry.py
@@ -456,8 +456,9 @@ class AgentRegistry:
return hashlib.sha256(json.dumps(agent_config, sort_keys=True).encode()).hexdigest()
def ids_for_agent(self, agent_id: str) -> frozenset[str]:
+ stable_id: Final = self.stable_agent_id(agent_id)
return frozenset(
- {agent_id, *(legacy for legacy, stable in self.config_agent_legacy_ids.items() if stable == agent_id)}
+ {stable_id, *(legacy for legacy, stable in self.config_agent_legacy_ids.items() if stable == stable_id)}
)
def stable_agent_id(self, agent_id: str) -> str:
diff --git a/litellm/proxy/agent_endpoints/endpoints.py b/litellm/proxy/agent_endpoints/endpoints.py
index 093116f2772..3867528b34f 100644
--- a/litellm/proxy/agent_endpoints/endpoints.py
+++ b/litellm/proxy/agent_endpoints/endpoints.py
@@ -1022,7 +1022,7 @@ async def delete_agent(
"""
await check_feature_access_for_user(user_api_key_dict, "agents")
- from litellm.proxy.proxy_server import prisma_client
+ from litellm.proxy.proxy_server import prisma_client, proxy_config
_check_agent_management_permission(user_api_key_dict)
@@ -1040,6 +1040,13 @@ async def delete_agent(
if existing_agent is None:
raise HTTPException(status_code=404, detail=f"Agent with ID {agent_id} not found in DB.")
+ await proxy_config.remove_from_list_setting(
+ section_name="litellm_settings",
+ key="public_agent_groups",
+ item=agent_id,
+ on_written=_set_public_agent_groups,
+ aliases=AGENT_REGISTRY.ids_for_agent,
+ )
await AGENT_REGISTRY.delete_agent_from_db(agent_id=agent_id, prisma_client=prisma_client)
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 40769df8ffd..968591a5f10 100644
--- a/litellm/proxy/management_endpoints/mcp_management_endpoints.py
+++ b/litellm/proxy/management_endpoints/mcp_management_endpoints.py
@@ -2628,6 +2628,14 @@ if MCP_AVAILABLE:
},
)
+ from litellm.proxy.proxy_server import proxy_config
+
+ await proxy_config.remove_from_list_setting(
+ section_name="litellm_settings",
+ key="public_mcp_servers",
+ item=server_id,
+ on_written=_set_public_mcp_servers,
+ )
# try to delete the mcp server
mcp_server_record_deleted: Final = await delete_mcp_server(prisma_client, server_id)
diff --git a/litellm/proxy/management_endpoints/model_management_endpoints.py b/litellm/proxy/management_endpoints/model_management_endpoints.py
index 77664ae5d7f..fc2066f871a 100644
--- a/litellm/proxy/management_endpoints/model_management_endpoints.py
+++ b/litellm/proxy/management_endpoints/model_management_endpoints.py
@@ -24,7 +24,7 @@ from types import MappingProxyType
from typing import TYPE_CHECKING, Annotated, Final, Literal, Protocol, TypeAlias, TypeVar, cast, runtime_checkable
from fastapi import APIRouter, Depends, Header, HTTPException, Request, status
-from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError, field_validator
+from pydantic import BaseModel, ConfigDict, Field, JsonValue, TypeAdapter, ValidationError, field_validator
import litellm
from litellm._logging import verbose_proxy_logger
@@ -3597,6 +3597,14 @@ async def update_public_model_groups(
)
+_USEFUL_LINKS: Final = TypeAdapter(Mapping[str, JsonValue])
+_PUBLIC_MODEL_GROUPS_LINKS: Final = TypeAdapter(dict[str, str | dict[str, object]])
+
+
+def _set_public_model_groups_links(public_model_groups_links: Mapping[str, JsonValue]) -> None:
+ litellm.public_model_groups_links = _PUBLIC_MODEL_GROUPS_LINKS.validate_python(public_model_groups_links)
+
+
@router.post(
"/model_hub/update_useful_links",
description="Update useful links",
@@ -3623,6 +3631,20 @@ async def update_useful_links(
},
)
+ if request.previous_useful_links is not None:
+ stored_useful_links: Final = await proxy_config.apply_mapping_setting_delta(
+ section_name="litellm_settings",
+ key="public_model_groups_links",
+ requested=_USEFUL_LINKS.validate_python(request.useful_links),
+ previous=_USEFUL_LINKS.validate_python(request.previous_useful_links),
+ on_written=_set_public_model_groups_links,
+ )
+ return {
+ "message": "Successfully updated useful links",
+ "useful_links": dict(stored_useful_links),
+ "updated_by": user_api_key_dict.user_id,
+ }
+
# Load existing config first (this may overwrite in-memory litellm settings
# from DB values via _update_config_from_db), so set the in-memory value AFTER
config: Final = await proxy_config.get_config()
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index bae76709962..78a15dcac21 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -5516,16 +5516,52 @@ def _with_list_delta(
aliases: Callable[[str], frozenset[str]],
) -> Mapping[str, JsonValue]:
current: Final = _listed_items(section, key)
- removed: Final = frozenset(chain.from_iterable(aliases(item) for item in previous.difference(requested)))
+ requested_ids: Final = frozenset(chain.from_iterable(aliases(item) for item in requested))
+ previous_ids: Final = frozenset(chain.from_iterable(aliases(item) for item in previous))
+ removed: Final = previous_ids.difference(requested_ids)
kept: Final = tuple(item for item in current if item not in removed)
added: Final = tuple(
- item for item in dict.fromkeys(requested) if item not in previous and aliases(item).isdisjoint(kept)
+ item
+ for item in _first_of_each_alias_set(requested, aliases)
+ if aliases(item).isdisjoint(previous_ids) and aliases(item).isdisjoint(kept)
)
if (*kept, *added) == current:
return section
return MappingProxyType({**section, key: [*kept, *added]})
+def _first_of_each_alias_set(items: tuple[str, ...], aliases: Callable[[str], frozenset[str]]) -> tuple[str, ...]:
+ return tuple(
+ item
+ for index, item in enumerate(items)
+ if all(aliases(item).isdisjoint(aliases(earlier)) for earlier in items[:index])
+ )
+
+
+def _with_mapping_delta(
+ section: Mapping[str, JsonValue],
+ *,
+ key: str,
+ requested: Mapping[str, JsonValue],
+ previous: Mapping[str, JsonValue],
+) -> Mapping[str, JsonValue]:
+ current: Final = _CONFIG_SECTION_VALUES.validate_python(section.get(key) or {})
+ changed: Final = {
+ name: value for name, value in requested.items() if name not in previous or previous[name] != value
+ }
+ removed: Final = frozenset(previous).difference(requested)
+ kept: Final = {name: value for name, value in current.items() if name not in removed and name not in changed}
+ merged: Final = {**kept, **changed}
+ if merged == dict(current):
+ return section
+ return MappingProxyType({**section, key: merged})
+
+
+def _settings_db_client() -> PrismaClient | None:
+ stores_settings_in_db: Final = general_settings_view().get("store_model_in_db", False) is True or store_model_in_db
+ return prisma_client if stores_settings_in_db else None
+
+
@dataclass(frozen=True, slots=True)
class _SectionRewrite:
before: Mapping[str, JsonValue]
@@ -6121,11 +6157,11 @@ class ProxyConfig:
listed_as: frozenset[str],
on_written: Callable[[tuple[str, ...]], None],
) -> tuple[str, ...] | None:
- section_rewrite: Final = await self._rewrite_list_setting(
+ section_rewrite: Final = await self._rewrite_setting(
section_name=section_name,
key=key,
rewrite=lambda section: _with_list_item(section, key=key, item=item, listed_as=listed_as),
- on_written=on_written,
+ on_written=lambda section: on_written(_listed_items(section, key)),
)
return _listed_items(section_rewrite.after, key) if section_rewrite.changed else None
@@ -6139,29 +6175,60 @@ class ProxyConfig:
on_written: Callable[[tuple[str, ...]], None],
aliases: Callable[[str], frozenset[str]] = _listed_only_as_itself,
) -> tuple[str, ...]:
- section_rewrite: Final = await self._rewrite_list_setting(
+ section_rewrite: Final = await self._rewrite_setting(
section_name=section_name,
key=key,
rewrite=lambda section: _with_list_delta(
section, key=key, requested=tuple(requested), previous=frozenset(previous), aliases=aliases
),
- on_written=on_written,
+ on_written=lambda section: on_written(_listed_items(section, key)),
)
return _listed_items(section_rewrite.after, key)
- async def _rewrite_list_setting(
+ async def remove_from_list_setting(
+ self,
+ *,
+ section_name: Section,
+ key: str,
+ item: str,
+ on_written: Callable[[tuple[str, ...]], None],
+ aliases: Callable[[str], frozenset[str]] = _listed_only_as_itself,
+ ) -> None:
+ if _settings_db_client() is not None and self._settings_stores[section_name].owned_by_config(key):
+ return
+ await self.apply_list_setting_delta(
+ section_name=section_name, key=key, requested=(), previous=(item,), on_written=on_written, aliases=aliases
+ )
+
+ async def apply_mapping_setting_delta(
+ self,
+ *,
+ section_name: Section,
+ key: str,
+ requested: Mapping[str, JsonValue],
+ previous: Mapping[str, JsonValue],
+ on_written: Callable[[Mapping[str, JsonValue]], None],
+ ) -> Mapping[str, JsonValue]:
+ section_rewrite: Final = await self._rewrite_setting(
+ section_name=section_name,
+ key=key,
+ rewrite=lambda section: _with_mapping_delta(section, key=key, requested=requested, previous=previous),
+ on_written=lambda section: on_written(_CONFIG_SECTION_VALUES.validate_python(section.get(key) or {})),
+ )
+ return _CONFIG_SECTION_VALUES.validate_python(section_rewrite.after.get(key) or {})
+
+ async def _rewrite_setting(
self,
*,
section_name: Section,
key: str,
rewrite: Callable[[Mapping[str, JsonValue]], Mapping[str, JsonValue]],
- on_written: Callable[[tuple[str, ...]], None],
+ on_written: Callable[[Mapping[str, JsonValue]], None],
) -> _SectionRewrite:
- if prisma_client is None or not (
- general_settings_view().get("store_model_in_db", False) is True or store_model_in_db
- ):
+ db_client: Final = _settings_db_client()
+ if db_client is None:
file_rewrite: Final = await self._rewrite_file_section(section_name=section_name, rewrite=rewrite)
- on_written(_listed_items(file_rewrite.after, key))
+ on_written(file_rewrite.after)
return file_rewrite
store: Final = self._settings_stores[section_name]
if store.owned_by_config(key):
@@ -6169,11 +6236,9 @@ class ProxyConfig:
if rewrite(config_section) != config_section:
self._raise_config_owned(section_name=section_name, rejected=(key,), store=store)
return _SectionRewrite(before=config_section, after=config_section)
- section_rewrite: Final = await _rewrite_config_section(
- prisma_client, section_name=section_name, rewrite=rewrite
- )
+ section_rewrite: Final = await _rewrite_config_section(db_client, section_name=section_name, rewrite=rewrite)
store.apply_db_row(section_name, section_rewrite.after)
- on_written(_listed_items(section_rewrite.after, key))
+ on_written(section_rewrite.after)
if section_rewrite.changed:
await invalidate_config_param(section_name)
return section_rewrite
diff --git a/litellm/types/proxy/management_endpoints/model_management_endpoints.py b/litellm/types/proxy/management_endpoints/model_management_endpoints.py
index f032bb20c2a..d4cffb4a21d 100644
--- a/litellm/types/proxy/management_endpoints/model_management_endpoints.py
+++ b/litellm/types/proxy/management_endpoints/model_management_endpoints.py
@@ -48,6 +48,14 @@ class UpdateUsefulLinksRequest(LiteLLMBaseModel):
# New format: { "displayName": { "url": "...", "index": 0 } }
# Old format: { "displayName": "url" } (for backward compatibility)
useful_links: dict[str, str | dict[str, object]]
+ previous_useful_links: dict[str, str | dict[str, object]] | None = Field(
+ default=None,
+ description=(
+ "The useful links the caller started from. When set, only the links added, changed or removed relative "
+ "to it change, so links another admin edited meanwhile keep that edit. When omitted, useful_links "
+ "replaces the stored links."
+ ),
+ )
class AutoRouterClassifierDefaultPromptResponse(LiteLLMBaseModel):
diff --git a/tests/integration/management/test_ai_hub_publish.py b/tests/integration/management/test_ai_hub_publish.py
new file mode 100644
index 00000000000..caeb02fb71e
--- /dev/null
+++ b/tests/integration/management/test_ai_hub_publish.py
@@ -0,0 +1,69 @@
+import uuid
+from collections.abc import Callable
+from concurrent.futures import ThreadPoolExecutor
+from types import MappingProxyType
+from typing import Final
+
+import httpx
+import pytest
+from integration._support.client import Gateway, Scenario, list_value, object_value
+
+AGENTS_PER_RACE: Final = 8
+PUBLISHES: Final[MappingProxyType[str, Callable[[Gateway, str], httpx.Response]]] = MappingProxyType({
+ "single-agent-route": lambda proxy, agent_id: proxy.request("POST", f"/v1/agents/{agent_id}/make_public"),
+ "dashboard-modal": lambda proxy, agent_id: proxy.request(
+ "POST", "/v1/agents/make_public", {"agent_ids": [agent_id], "previous_agent_ids": []}
+ ),
+})
+
+
+def _agent(gateway: Gateway, scenario: Scenario) -> str:
+ name: Final = f"hub-publish-{uuid.uuid4().hex}"
+ card: Final = {
+ "protocolVersion": "0.3",
+ "name": name,
+ "description": "Synthetic hub agent",
+ "version": "1.0.0",
+ "url": "http://127.0.0.1:9/",
+ "capabilities": {"streaming": False},
+ "defaultInputModes": ["text"],
+ "defaultOutputModes": ["text"],
+ "skills": [],
+ }
+ created: Final = gateway.request("POST", "/v1/agents", {"agent_name": name, "agent_card_params": card})
+ assert created.status_code == 200, created.text
+ agent_id: Final = str(created.json()["agent_id"])
+ scenario.cleanups.callback(lambda: gateway.request("DELETE", f"/v1/agents/{agent_id}"))
+ return agent_id
+
+
+def _stored_public_agents(gateway: Gateway) -> frozenset[str]:
+ rows: Final = gateway.rows(
+ 'SELECT param_value FROM "LiteLLM_Config" WHERE param_name=%s', ("litellm_settings",)
+ )
+ return frozenset(str(item) for item in list_value(object_value(rows[0]["param_value"])["public_agent_groups"]))
+
+
+@pytest.mark.parametrize("publish", tuple(PUBLISHES), ids=tuple(PUBLISHES))
+def test_concurrent_publishes_through_two_proxies_keep_every_agent_and_a_delete_unpublishes(
+ gateway: Gateway, peer: Gateway, scenario: Scenario, publish: str
+) -> None:
+ agent_ids: Final = tuple(_agent(gateway, scenario) for _ in range(AGENTS_PER_RACE))
+ proxies: Final = (gateway, peer)
+
+ with ThreadPoolExecutor(max_workers=AGENTS_PER_RACE) as pool:
+ replies: Final = tuple(
+ pool.map(
+ lambda indexed: PUBLISHES[publish](proxies[indexed[0] % 2], indexed[1]),
+ enumerate(agent_ids),
+ )
+ )
+
+ assert [reply.status_code for reply in replies] == [200] * AGENTS_PER_RACE, [reply.text for reply in replies]
+ assert frozenset(agent_ids) <= _stored_public_agents(gateway)
+
+ deleted: Final = peer.request("DELETE", f"/v1/agents/{agent_ids[0]}")
+
+ assert deleted.status_code == 200, deleted.text
+ assert agent_ids[0] not in _stored_public_agents(gateway)
+ assert frozenset(agent_ids[1:]) <= _stored_public_agents(gateway)
diff --git a/tests/unit/proxy/agent_endpoints/test_endpoints.py b/tests/unit/proxy/agent_endpoints/test_endpoints.py
index f2fdb3426c8..3bb85b12633 100644
--- a/tests/unit/proxy/agent_endpoints/test_endpoints.py
+++ b/tests/unit/proxy/agent_endpoints/test_endpoints.py
@@ -1385,6 +1385,66 @@ async def test_make_agents_public_treats_a_config_agents_legacy_id_as_the_same_a
assert rows.stored("litellm_settings")["public_agent_groups"] == ["agent-1"]
+@pytest.mark.asyncio
+async def test_make_agents_public_matches_a_legacy_id_in_previous_agent_ids_to_the_stored_agent(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ rows, registry = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[])
+ config_agent: Final = {"agent_name": "Config Agent", "agent_card_params": _sample_agent_card_params()}
+ registry.load_agents_from_config([config_agent])
+ stable_id: Final = registry.stable_agent_id(next(iter(registry.config_agent_legacy_ids)))
+ legacy_id: Final = next(iter(registry.ids_for_agent(stable_id) - {stable_id}))
+ rows.serialized_rows["litellm_settings"] = json.dumps({"public_agent_groups": [stable_id, "agent-1"]})
+
+ unchanged: Final = await _make_agents_public([stable_id, "agent-1"], previous_agent_ids=[legacy_id, "agent-1"])
+ unpublished: Final = await _make_agents_public(["agent-1"], previous_agent_ids=[legacy_id, "agent-1"])
+
+ assert unchanged["public_agent_groups"] == [stable_id, "agent-1"]
+ assert unpublished["public_agent_groups"] == ["agent-1"]
+ assert rows.stored("litellm_settings")["public_agent_groups"] == ["agent-1"]
+
+
+@pytest.mark.asyncio
+async def test_make_agents_public_stores_one_id_when_both_ids_of_a_config_agent_are_ticked(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ rows, registry = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=[])
+ config_agent: Final = {"agent_name": "Config Agent", "agent_card_params": _sample_agent_card_params()}
+ registry.load_agents_from_config([config_agent])
+ legacy_id: Final = next(iter(registry.config_agent_legacy_ids))
+ stable_id: Final = registry.stable_agent_id(legacy_id)
+
+ response: Final = await _make_agents_public([legacy_id, stable_id, "agent-1"], previous_agent_ids=[])
+
+ assert response["public_agent_groups"] == [legacy_id, "agent-1"]
+ assert rows.stored("litellm_settings")["public_agent_groups"] == [legacy_id, "agent-1"]
+
+
+@pytest.mark.asyncio
+async def test_delete_agent_unpublishes_it_so_a_new_agent_reusing_its_id_starts_private(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ import litellm
+
+ rows, registry = _replica_that_missed_a_publish(monkeypatch, stored_public_agent_groups=["agent-1", "agent-2"])
+ agents_table: Final = SimpleNamespace(
+ find_unique=AsyncMock(return_value={"agent_id": "agent-1", "agent_name": "Agent One"})
+ )
+ monkeypatch.setattr(
+ "litellm.proxy.proxy_server.prisma_client",
+ ReplicaPrisma(rows, tables={"litellm_agentstable": agents_table}),
+ )
+ monkeypatch.setattr(
+ agent_endpoints, "AGENT_REGISTRY", MagicMock(wraps=registry, delete_agent_from_db=AsyncMock())
+ )
+
+ response: Final = await agent_endpoints.delete_agent(agent_id="agent-1", user_api_key_dict=_PROXY_ADMIN)
+
+ assert response == {"message": "Agent agent-1 deleted successfully"}
+ assert rows.stored("litellm_settings")["public_agent_groups"] == ["agent-2"]
+ assert litellm.public_agent_groups == ["agent-2"]
+
+
@pytest.mark.asyncio
async def test_make_agents_public_without_previous_agent_ids_replaces_the_stored_list(
monkeypatch: pytest.MonkeyPatch,
diff --git a/tests/unit/proxy/fake_config_table.py b/tests/unit/proxy/fake_config_table.py
index 65d13c31dab..8356f7cf310 100644
--- a/tests/unit/proxy/fake_config_table.py
+++ b/tests/unit/proxy/fake_config_table.py
@@ -57,11 +57,16 @@ def section_locked_transactions(litellm_config: object) -> Callable[[], SectionL
class ReplicaPrisma:
- def __init__(self, rows: ConfigRows, cached_rows: Mapping[str, Mapping[str, object]] | None = None) -> None:
+ def __init__(
+ self,
+ rows: ConfigRows,
+ cached_rows: Mapping[str, Mapping[str, object]] | None = None,
+ tables: Mapping[str, object] | None = None,
+ ) -> None:
self.rows: Final = rows
self._cached_rows: Final = cached_rows
self.tx: Final = section_locked_transactions(rows)
- self.db: Final = SimpleNamespace(litellm_config=rows)
+ self.db: Final = SimpleNamespace(litellm_config=rows, **(tables or {}))
self.writer_db: Final = self.db
async def get_generic_data(self, key: str, value: str, table_name: str) -> SimpleNamespace | None:
diff --git a/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py b/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py
index c2ded683ebb..3acc3268696 100644
--- a/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py
+++ b/tests/unit/proxy/management_endpoints/test_mcp_management_endpoints.py
@@ -344,6 +344,65 @@ async def test_mcp_publication_from_the_modal_keeps_a_server_another_admin_publi
assert litellm.public_mcp_servers == ["server-1", "server-3"]
+def _replica_publishing_servers(
+ monkeypatch: pytest.MonkeyPatch, *, config_file_public_mcp_servers: list[str] | None = None
+) -> ConfigRows:
+ from litellm.proxy.proxy_server import ProxyConfig
+
+ rows: Final = ConfigRows({"litellm_settings": {"public_mcp_servers": ["server-1", "server-2"]}})
+ proxy_config: Final = ProxyConfig()
+ if config_file_public_mcp_servers is not None:
+ proxy_config._load_yaml_settings_stores(
+ {"litellm_settings": {"public_mcp_servers": config_file_public_mcp_servers}}
+ )
+ monkeypatch.setattr(litellm, "public_mcp_servers", ["server-1", "server-2"])
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", ReplicaPrisma(rows))
+ monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True)
+ monkeypatch.setattr("litellm.proxy.proxy_server.proxy_config", proxy_config)
+ monkeypatch.setattr(
+ mgmt_endpoints,
+ "delete_mcp_server",
+ AsyncMock(return_value=generate_mock_mcp_server_db_record(server_id="server-1")),
+ )
+ monkeypatch.setattr(
+ mgmt_endpoints, "global_mcp_server_manager", MagicMock(reload_servers_from_database=AsyncMock())
+ )
+ return rows
+
+
+@pytest.mark.asyncio
+async def test_remove_mcp_server_unpublishes_it_so_a_new_server_reusing_its_id_starts_private(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ rows: Final = _replica_publishing_servers(monkeypatch)
+
+ response: Final = await mgmt_endpoints.remove_mcp_server(
+ server_id="server-1",
+ user_api_key_dict=generate_mock_user_api_key_auth(user_role=LitellmUserRoles.PROXY_ADMIN),
+ litellm_changed_by=None,
+ )
+
+ assert response.status_code == 202
+ assert rows.stored("litellm_settings")["public_mcp_servers"] == ["server-2"]
+ assert litellm.public_mcp_servers == ["server-2"]
+
+
+@pytest.mark.asyncio
+async def test_remove_mcp_server_still_deletes_when_the_config_file_owns_the_public_list(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ rows: Final = _replica_publishing_servers(monkeypatch, config_file_public_mcp_servers=["server-1"])
+
+ response: Final = await mgmt_endpoints.remove_mcp_server(
+ server_id="server-1",
+ user_api_key_dict=generate_mock_user_api_key_auth(user_role=LitellmUserRoles.PROXY_ADMIN),
+ litellm_changed_by=None,
+ )
+
+ assert response.status_code == 202
+ assert rows.stored("litellm_settings")["public_mcp_servers"] == ["server-1", "server-2"]
+
+
class TestMCPCredentialsTokenExchangeProfile:
"""token_exchange_profile must be a declared MCPCredentials field so the management API can
persist the entra_obo profile. An undeclared key is silently stripped by pydantic when the
diff --git a/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py b/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py
index d9f7862bb10..800db944e17 100644
--- a/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py
+++ b/tests/unit/proxy/management_endpoints/test_model_management_endpoints.py
@@ -2120,6 +2120,71 @@ class TestUpdateModel:
assert persisted["litellm_credential_name"] == "shared-credential"
+@pytest.mark.asyncio
+async def test_update_useful_links_from_the_dashboard_keeps_a_link_another_admin_edited(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ import litellm
+ from litellm.proxy.management_endpoints.model_management_endpoints import update_useful_links
+ from litellm.proxy.proxy_server import ProxyConfig
+ from litellm.types.proxy.management_endpoints.model_management_endpoints import UpdateUsefulLinksRequest
+
+ docs: Final = {"url": "https://docs.example.com", "index": 0}
+ status_before: Final = {"url": "https://status.example.com", "index": 1}
+ status_edited_by_other_admin: Final = {"url": "https://status.example.com/v2", "index": 1}
+ retired: Final = {"url": "https://retired.example.com", "index": 2}
+ docs_edited: Final = {"url": "https://docs.example.com/v2", "index": 0}
+ blog: Final = {"url": "https://blog.example.com", "index": 2}
+ rows: Final = ConfigRows(
+ {
+ "litellm_settings": {
+ "public_model_groups_links": {"Docs": docs, "Status": status_edited_by_other_admin, "Retired": retired}
+ }
+ }
+ )
+ monkeypatch.setattr(litellm, "public_model_groups_links", {})
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", ReplicaPrisma(rows))
+ monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True)
+ monkeypatch.setattr("litellm.proxy.proxy_server.proxy_config", ProxyConfig())
+
+ response: Final = await update_useful_links(
+ request=UpdateUsefulLinksRequest(
+ useful_links={"Docs": docs_edited, "Status": status_before, "Blog": blog},
+ previous_useful_links={"Docs": docs, "Status": status_before, "Retired": retired},
+ ),
+ user_api_key_dict=UserAPIKeyAuth(user_id="admin", user_role=LitellmUserRoles.PROXY_ADMIN),
+ )
+
+ expected: Final = {"Docs": docs_edited, "Status": status_edited_by_other_admin, "Blog": blog}
+ assert response["useful_links"] == expected
+ assert rows.stored("litellm_settings")["public_model_groups_links"] == expected
+ assert litellm.public_model_groups_links == expected
+
+
+@pytest.mark.asyncio
+async def test_update_useful_links_that_change_nothing_create_no_settings_row(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ import litellm
+ from litellm.proxy.management_endpoints.model_management_endpoints import update_useful_links
+ from litellm.proxy.proxy_server import ProxyConfig
+ from litellm.types.proxy.management_endpoints.model_management_endpoints import UpdateUsefulLinksRequest
+
+ rows: Final = ConfigRows({})
+ monkeypatch.setattr(litellm, "public_model_groups_links", {})
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", ReplicaPrisma(rows))
+ monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True)
+ monkeypatch.setattr("litellm.proxy.proxy_server.proxy_config", ProxyConfig())
+
+ response: Final = await update_useful_links(
+ request=UpdateUsefulLinksRequest(useful_links={}, previous_useful_links={}),
+ user_api_key_dict=UserAPIKeyAuth(user_id="admin", user_role=LitellmUserRoles.PROXY_ADMIN),
+ )
+
+ assert response["useful_links"] == {}
+ assert "litellm_settings" not in rows.serialized_rows
+
+
@pytest.mark.asyncio
async def test_update_public_model_groups_from_the_modal_keeps_a_group_another_admin_published(
monkeypatch: pytest.MonkeyPatch,
diff --git a/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.test.tsx b/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.test.tsx
index 310201ab243..dc0fabdcb45 100644
--- a/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.test.tsx
+++ b/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.test.tsx
@@ -52,9 +52,11 @@ describe("UsefulLinksManagement", () => {
await user.click(screen.getByRole("button", { name: /add link/i }));
await waitFor(() =>
- expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith("token", {
- Docs: { url: "https://docs.example.com", index: 0 },
- }),
+ expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith(
+ "token",
+ { Docs: { url: "https://docs.example.com", index: 0 } },
+ {},
+ ),
);
expect(await screen.findByText("Docs")).toBeInTheDocument();
@@ -62,6 +64,33 @@ describe("UsefulLinksManagement", () => {
expect(mockedNotifications.success).toHaveBeenCalledWith("Link added successfully");
});
+ it("should send the links it last saved as the starting point of the next save", async () => {
+ const user = userEvent.setup();
+ render();
+
+ const displayNameInput = await screen.findByPlaceholderText("Friendly name");
+ const urlInput = screen.getByPlaceholderText("https://example.com");
+ fireEvent.change(displayNameInput, { target: { value: "Docs" } });
+ fireEvent.change(urlInput, { target: { value: "https://docs.example.com" } });
+ await user.click(screen.getByRole("button", { name: /add link/i }));
+ expect(await screen.findByText("Docs")).toBeInTheDocument();
+
+ fireEvent.change(displayNameInput, { target: { value: "Status" } });
+ fireEvent.change(urlInput, { target: { value: "https://status.example.com" } });
+ await user.click(screen.getByRole("button", { name: /add link/i }));
+
+ await waitFor(() =>
+ expect(mockedUpdateUsefulLinksCall).toHaveBeenLastCalledWith(
+ "token",
+ {
+ Docs: { url: "https://docs.example.com", index: 0 },
+ Status: { url: "https://status.example.com", index: 1 },
+ },
+ { Docs: { url: "https://docs.example.com", index: 0 } },
+ ),
+ );
+ });
+
it("should rearrange links and save the new order", async () => {
const user = userEvent.setup();
mockedGetPublicModelHubInfo.mockResolvedValue({
@@ -87,11 +116,19 @@ describe("UsefulLinksManagement", () => {
await user.click(screen.getByRole("button", { name: /save order/i }));
await waitFor(() =>
- expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith("token", {
- "Second Link": { url: "https://second.example.com", index: 0 },
- "First Link": { url: "https://first.example.com", index: 1 },
- "Third Link": { url: "https://third.example.com", index: 2 },
- }),
+ expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith(
+ "token",
+ {
+ "Second Link": { url: "https://second.example.com", index: 0 },
+ "First Link": { url: "https://first.example.com", index: 1 },
+ "Third Link": { url: "https://third.example.com", index: 2 },
+ },
+ {
+ "First Link": "https://first.example.com",
+ "Second Link": "https://second.example.com",
+ "Third Link": "https://third.example.com",
+ },
+ ),
);
expect(mockedNotifications.success).toHaveBeenCalledWith("Link order saved successfully");
@@ -155,9 +192,11 @@ describe("UsefulLinksManagement", () => {
await user.click(screen.getByRole("button", { name: /save/i }));
await waitFor(() =>
- expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith("token", {
- "Updated Link": { url: "https://test.example.com", index: 0 },
- }),
+ expect(mockedUpdateUsefulLinksCall).toHaveBeenCalledWith(
+ "token",
+ { "Updated Link": { url: "https://test.example.com", index: 0 } },
+ { "Test Link": "https://test.example.com" },
+ ),
);
expect(mockedNotifications.success).toHaveBeenCalledWith("Link updated successfully");
diff --git a/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.tsx b/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.tsx
index 60e97935522..dd9647a826f 100644
--- a/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.tsx
+++ b/ui/litellm-dashboard/src/components/AIHub/UsefulLinksManagement.tsx
@@ -27,6 +27,7 @@ const UsefulLinksManagement: React.FC = ({ accessTok
const [isExpanded, setIsExpanded] = useState(true);
const [isRearranging, setIsRearranging] = useState(false);
const [originalLinksOrder, setOriginalLinksOrder] = useState([]);
+ const [savedLinks, setSavedLinks] = useState>({});
const fetchUsefulLinks = async () => {
if (!accessToken) return;
@@ -36,6 +37,7 @@ const UsefulLinksManagement: React.FC = ({ accessTok
if (response && response.useful_links) {
const usefulLinks = response.useful_links || {};
+ setSavedLinks(usefulLinks);
// Convert object to array of links with ids
// Handle both old format (Dict[str, str]) and new format (Dict[str, {url, index}])
@@ -98,7 +100,8 @@ const UsefulLinksManagement: React.FC = ({ accessTok
};
});
- await updateUsefulLinksCall(accessToken, linksObject);
+ await updateUsefulLinksCall(accessToken, linksObject, savedLinks);
+ setSavedLinks(linksObject);
return true;
} catch (error) {
diff --git a/ui/litellm-dashboard/src/components/networking.tsx b/ui/litellm-dashboard/src/components/networking.tsx
index d662568e1b2..d3f8e4cbc3e 100644
--- a/ui/litellm-dashboard/src/components/networking.tsx
+++ b/ui/litellm-dashboard/src/components/networking.tsx
@@ -1869,11 +1869,12 @@ export const deleteAllowedIP = async (accessToken: string, ip: string) => {
export const updateUsefulLinksCall = async (
accessToken: string,
useful_links: Record,
+ previous_useful_links: Record,
) => {
try {
return await apiClient.post(`/model_hub/update_useful_links`, {
accessToken,
- body: { useful_links: useful_links },
+ body: { useful_links, previous_useful_links },
});
} catch (error) {
console.error("Failed to create key:", error);
diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts
index 9133da6bbdc..bb842a22b6f 100644
--- a/ui/litellm-dashboard/src/lib/http/schema.d.ts
+++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts
@@ -49293,6 +49293,15 @@ export interface components {
};
/** UpdateUsefulLinksRequest */
UpdateUsefulLinksRequest: {
+ /**
+ * Previous Useful Links
+ * @description The useful links the caller started from. When set, only the links added, changed or removed relative to it change, so links another admin edited meanwhile keep that edit. When omitted, useful_links replaces the stored links.
+ */
+ previous_useful_links?: {
+ [key: string]: string | {
+ [key: string]: unknown;
+ };
+ } | null;
/** Useful Links */
useful_links: {
[key: string]: string | {