Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

feat(user_api_key_auth_mcp.py): correctly dereference mcp server name from ids

Krrish Dholakiadf828718c5ac
diff --git a/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py b/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py
index 61600c25d37..31032b27b22 100644
--- a/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py
+++ b/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py
@@ -294,6 +294,9 @@ class MCPRequestHandler:
) -> List[str]:
"""
Get list of allowed MCP servers for the given user/key based on permissions
+
+ Returns:
+ List[str]: List of allowed MCP servers by server id
"""
from typing import List
@@ -331,7 +334,7 @@ class MCPRequestHandler:
return []
@staticmethod
- async def is_tool_allowed(
+ def is_tool_allowed(
allowed_mcp_servers: List[str],
server_name: str,
) -> bool:
diff --git a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
index 4c866561f70..a88f94be06f 100644
--- a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
+++ b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
@@ -919,6 +919,14 @@ class MCPServerManager:
return server
return None
+ def get_mcp_server_names_from_ids(self, server_ids: List[str]) -> List[str]:
+ server_names = []
+ registry = self.get_registry()
+ for server in registry.values():
+ if server.server_id in server_ids:
+ server_names.append(server.name)
+ return server_names
+
def get_mcp_server_by_name(self, server_name: str) -> Optional[MCPServer]:
"""
Get the MCP Server from the server name
diff --git a/litellm/proxy/_experimental/mcp_server/server.py b/litellm/proxy/_experimental/mcp_server/server.py
index 8074d25db7f..96e4d47a914 100644
--- a/litellm/proxy/_experimental/mcp_server/server.py
+++ b/litellm/proxy/_experimental/mcp_server/server.py
@@ -561,13 +561,19 @@ if MCP_AVAILABLE:
)
## CHECK IF USER IS ALLOWED TO CALL THIS TOOL
- allowed_mcp_servers = await MCPRequestHandler.get_allowed_mcp_servers(
+ allowed_mcp_server_ids = await MCPRequestHandler.get_allowed_mcp_servers(
user_api_key_auth=user_api_key_auth,
)
+
+ allowed_mcp_servers = global_mcp_server_manager.get_mcp_server_names_from_ids(
+ allowed_mcp_server_ids
+ )
+
if not MCPRequestHandler.is_tool_allowed(
allowed_mcp_servers=allowed_mcp_servers,
server_name=server_name_from_prefix,
):
+
raise HTTPException(
status_code=403,
detail=f"User not allowed to call this tool. Allowed MCP servers: {allowed_mcp_servers}",