Repository navigation
feat(mcp): let a key scope to zero MCP servers - #31028
ryan-crabbe-berri wants to merge 1 commit into
Conversation
A key's object_permission.mcp_servers was a non-nullable String[], so an empty list and an unset value both collapsed to [], and the resolver read either one as "inherit the team's full MCP server list". That made it impossible to give a key zero servers under a team that has servers, which pushed keys past provider tool caps. The column is now nullable JSON where null inherits the team, [] means no servers, and a list means exactly those, and the resolver honors the distinction. A data-preserving migration converts the column in place and backfills existing [] to null, so every current key and team keeps inheriting and no live row changes behavior. The dashboard key create and edit forms gain an explicit Inherit / No access / Specific control so the new zero state is reachable without the API
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
@greptileai review |
Greptile SummaryThis PR introduces a nullable-JSONB tri-state for
Confidence Score: 3/5The resolver, cache, and UI changes are correct and well-tested, but two write-path issues should be resolved before merging. get_objectpermissions_for_mcp_server now pulls every object-permission row into memory with joined relations to filter in Python, and the update path passes Python None directly for mcp_servers while the sibling mcp_tool_permissions always goes through safe_dumps — if Prisma treats bare None as omit-field, switching a key back to inherit mode will silently leave the old restriction in the DB. litellm/proxy/_experimental/mcp_server/db.py (full table scan) and litellm/proxy/management_helpers/object_permission_utils.py (None vs safe_dumps inconsistency in the update path).
|
| Filename | Overview |
|---|---|
| litellm-proxy-extras/litellm_proxy_extras/migrations/20260622120000_make_mcp_servers_nullable_jsonb/migration.sql | Hand-authored in-place ALTER using a USING cast that preserves data and backfills empty arrays to NULL; avoids Prisma's destructive DROP/ADD approach. Logic is correct and well-commented. |
| litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py | Tri-state resolver logic (None=inherit, []=zero, list=intersect) is correctly implemented; guard-clause returns and exception handler now return None instead of [], matching the new semantics. Test coverage is thorough. |
| litellm/proxy/_experimental/mcp_server/db.py | get_objectpermissions_for_mcp_server replaced a targeted DB-level has-filter with a full Python-side scan of all object-permission rows (with eagerly loaded relations), introducing a potentially severe performance regression. |
| litellm/proxy/management_helpers/object_permission_utils.py | mcp_servers serialization added for both create and update paths, but None is passed directly to the Prisma upsert (unlike sibling field mcp_tool_permissions which always goes through safe_dumps), creating a potential null-reset gap. |
| litellm/models/object_permission.py | Default changed from [] to None so cache round-trips (exclude_none serialization) correctly preserve the inherit/zero distinction. Cache round-trip tests confirm this. |
| ui/litellm-dashboard/src/components/templates/key_edit_view.tsx | Radio group (Inherit/No access/Specific) correctly drives conditional rendering of MCPServerSelector; getMcpScopeMode initialises the form field from existing key data using the correct null/empty/list checks. |
| ui/litellm-dashboard/src/components/templates/key_info_view.tsx | handleKeyUpdate correctly translates the mcp_scope_mode tri-state into the object_permission payload, clears access groups and toolsets when mode is not specific, and deletes internal form fields before the API call. |
| ui/litellm-dashboard/src/components/organisms/create_key_button.tsx | Create-key form properly maps mcp_scope_mode to the object_permission payload; inherit leaves mcp_servers absent (DB defaults to NULL), none sends [], specific sends the selected servers. |
| tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py | Existing tests correctly updated from [] to None for inherit guard conditions; new parametrised test covers all three tri-state scenarios end-to-end through get_allowed_mcp_servers. |
Reviews (1): Last reviewed commit: "feat(mcp): let a key scope to zero MCP s..." | Re-trigger Greptile
| object_permission_records = await ObjectPermissionRepository( | ||
| prisma_client | ||
| ).table.find_many( | ||
| where={ | ||
| "mcp_servers": {"has": mcp_server_id}, | ||
| }, | ||
| include={ | ||
| "teams": True, | ||
| "verification_tokens": True, | ||
| }, | ||
| ) | ||
|
|
||
| return object_permission_records | ||
| return [ | ||
| record | ||
| for record in object_permission_records | ||
| if isinstance(record.mcp_servers, list) and mcp_server_id in record.mcp_servers | ||
| ] |
There was a problem hiding this comment.
Full table scan replacing targeted DB query
get_objectpermissions_for_mcp_server now loads every LiteLLM_ObjectPermissionTable row (including eagerly-joined teams and verification_tokens) and filters by mcp_server_id in Python. Before this change, a PostgreSQL GIN-indexed has filter did the work in the engine. With even a few thousand object-permission rows this will be slow and memory-heavy.
The JSONB column can't use Prisma's scalar-list has, but PostgreSQL's JSONB containment operator @> works directly: WHERE "mcp_servers" @> '["<id>"]'::jsonb. A prisma_client.execute_raw call with that predicate would restore the index-backed query without pulling every row into Python.
| # mcp_servers is a JSON column; serialize the list (None stays NULL = inherit). | ||
| if isinstance(existing_object_permissions_dict.get("mcp_servers"), list): | ||
| existing_object_permissions_dict["mcp_servers"] = safe_dumps( | ||
| existing_object_permissions_dict["mcp_servers"] | ||
| ) |
There was a problem hiding this comment.
None passed directly to Prisma may not reset the JSON column to SQL NULL
mcp_tool_permissions (the sibling Json? field) is always routed through safe_dumps(), including when the value is None (safe_dumps(None) → "null"), so Prisma receives an explicit JSON null string and writes JSON null to the column. Here, mcp_servers = None is passed as a raw Python None in the upsert dict, which some Prisma client versions treat as "omit field" rather than "SET NULL". If the column isn't reset to SQL NULL when a user switches back to "inherit" mode, the old [] / specific-list value will persist and the key will remain locked out of team servers silently.
If Prisma's Python client does correctly map None → NULL for Json? fields this is fine, but the inconsistency with the existing mcp_tool_permissions pattern is worth a quick regression test against the real ORM path.
|
Main already lets a key opt out of every MCP server with the no-mcp-servers option from #31029, reopen if something is still missing |
Summary
A virtual key's
object_permission.mcp_serverswas a non-nullableString[], so an empty list and an unset value both collapsed to[], and the runtime resolver read either one as "inherit the team's full MCP server list". There was no way to give a key zero servers under a team that has servers, which pushed keys past provider tool caps (for example OpenAI's 128-tool limit)This makes the column a nullable JSON value with a clear tri-state:
nullinherits the team,[]means no servers, and a list means exactly those servers. The resolver honors the distinction, a data-preserving migration converts the column in place and backfills existing[]tonullso every current key and team keeps inheriting and no live row changes behavior, and the dashboard key forms get an explicit Inherit / No access / Specific control so the zero state is reachable without the APIType
New Feature
Changes
schema.prismaand its two synced copies:mcp_serversis nowJson?instead ofString[] @default([])20260622120000_make_mcp_servers_nullable_jsonbdoes an in-placeALTER ... TYPE jsonb USING (...)that backfills empty arrays tonull. Prisma's own diff drops and recreates the column (data loss), so this is hand-authored per the migration runbookMCPRequestHandler._get_allowed_mcp_servers_for_keynow returnsOptional[List[str]]; the key/team combination treatsnullas inherit and[]as an explicit empty scope that intersects the team down to nothingLiteLLM_ObjectPermissionTable.mcp_serversdefault changed toNoneso the inherit/zero distinction survives cache round-tripsmcp_serverslike its existingmcp_tool_permissionsJSON siblingget_objectpermissions_for_mcp_servermatches in Python because the jsonb column cannot use the prismahasfilterTest plan
[]under a populated team resolves to zero,nullinherits the team, a real subset intersects_get_allowed_mcp_servers_for_keyreturnsNone/[]/ list for the three stored shapesNoneasNoneand[]as[], guarding the model defaultProof of Fix
Live on a proxy at
localhost:4000with three local MCP servers and a team scoped to all three. Three keys created under that team:UI: Virtual Keys -> Create or Edit a key -> the "MCP Servers / Access Groups" control now offers Inherit from team, No MCP access, and Specific servers