Repository navigation
fix(mcp): ignore stale ids on key save #29128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
94fef31
ed1d3d7
e9df894
e01e698
da7852e
5393b28
2084a5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -299,9 +299,45 @@ def _extract_requested_mcp_toolsets( | |
| return set() | ||
|
|
||
|
|
||
| async def _get_stale_mcp_server_ids( | ||
| requested_servers: Set[str], | ||
| prisma_client: Optional[PrismaClient], | ||
| ) -> Set[str]: | ||
| """ | ||
| Return the subset of *requested_servers* that no longer exist. | ||
|
|
||
| Queries the DB when *prisma_client* is available (authoritative); falls back | ||
| to the in-memory registry for config-only deployments without a DB. | ||
| """ | ||
| from litellm.proxy._experimental.mcp_server.mcp_server_manager import ( | ||
| global_mcp_server_manager, | ||
| ) | ||
|
|
||
| if prisma_client is not None: | ||
| from litellm.proxy._experimental.mcp_server.db import get_mcp_servers | ||
|
|
||
| existing_in_db = await get_mcp_servers(prisma_client, list(requested_servers)) | ||
| existing_ids = {s.server_id for s in existing_in_db} | ||
| # Config-file servers live only in the in-memory registry and are never | ||
| # written to the DB — include them so they are not misclassified as stale. | ||
| existing_ids |= { | ||
| sid | ||
| for sid in requested_servers | ||
| if global_mcp_server_manager.get_mcp_server_by_id(sid) is not None | ||
| } | ||
| return requested_servers - existing_ids | ||
|
|
||
| return { | ||
| sid | ||
| for sid in requested_servers | ||
| if global_mcp_server_manager.get_mcp_server_by_id(sid) is None | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused helper function never called anywhere in codebaseLow Severity The Reviewed by Cursor Bugbot for commit e01e698. Configure here. |
||
|
|
||
|
|
||
| async def validate_key_mcp_servers_against_team( | ||
| object_permission: Optional[dict], | ||
| team_obj: Optional["LiteLLM_TeamTableCachedObj"], | ||
| prisma_client: Optional[PrismaClient] = None, | ||
| ): | ||
| """ | ||
| Validate that MCP servers requested on a key are within the allowed scope. | ||
|
|
@@ -332,7 +368,17 @@ async def validate_key_mcp_servers_against_team( | |
|
|
||
| # Validate requested server IDs | ||
| if requested_servers: | ||
| disallowed_servers = requested_servers - all_allowed_servers | ||
| # Strip IDs for deleted servers — carrying a stale ID from a form that | ||
| # hasn't refreshed is not an authorization violation. | ||
| stale_ids = await _get_stale_mcp_server_ids(requested_servers, prisma_client) | ||
|
veria-ai[bot] marked this conversation as resolved.
Resolved
|
||
| if stale_ids: | ||
| verbose_proxy_logger.warning( | ||
| "validate_key_mcp_servers_against_team: ignoring stale MCP server " | ||
| f"IDs (no longer in registry): {sorted(stale_ids)}" | ||
| ) | ||
| active_requested_servers = requested_servers - stale_ids | ||
|
|
||
| disallowed_servers = active_requested_servers - all_allowed_servers | ||
| if disallowed_servers: | ||
| if team_obj is not None: | ||
| team_id = team_obj.team_id | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Key generation discards normalized object permission return value
Low Severity
In the key generation path, the return value of
validate_key_mcp_servers_against_teamis discarded (awaitwithout assignment), while the key update path explicitly captures and applies it viaLiteLLM_ObjectPermissionBase(**normalized_object_permission). The generation path relies silently on in-place dict mutation propagating throughdata_json, creating an asymmetric and fragile pattern that would silently break if the function ever stops mutating in-place.Additional Locations (1)
litellm/proxy/management_endpoints/key_management_endpoints.py#L2355-L2366Reviewed by Cursor Bugbot for commit e01e698. Configure here.