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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(mcp): give the key/team grant question one predicate
Tinb2d4dde46468
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.pyindex 5d8ac8d678f..dc796837277 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@@ -1963,6 +1963,18 @@ class MCPRequestHandler:return allowed_tools+ @staticmethod+ def tool_is_granted(bare_tool_name: str, allowed_tool_names: list[str] | None) -> bool:+ """Whether key/team tool permissions reach ``bare_tool_name`` on one server.++ ``None`` means no tool-level restriction; an empty list grants nothing. Entries+ name a tool on a single server and every writer stores them bare, so the+ comparison is exact against the bare name rather than against the spellings+ routing accepts. Both the listing path and the call path answer through here, so+ discovery cannot advertise a tool that ``tools/call`` then refuses.+ """+ return allowed_tool_names is None or bare_tool_name in allowed_tool_names+@staticmethodasync def is_tool_allowed_for_server(tool_name: str,@@ -1973,7 +1985,7 @@ class MCPRequestHandler:Check if a specific tool is allowed for a server based on key/team permissions.Args:- tool_name: Name of the tool to check+ tool_name: Bare tool name, already resolved against the server's prefixesserver_id: Server IDuser_api_key_auth: User auth@@ -1984,17 +1996,7 @@ class MCPRequestHandler:server_id=server_id,user_api_key_auth=user_api_key_auth,)-- # None means no restrictions (allow all)- if allowed_tools is None:- return True-- # Empty list means no tools allowed- if not allowed_tools:- return False-- # Check if tool is in allowed list- return tool_name in allowed_tools+ return MCPRequestHandler.tool_is_granted(tool_name, allowed_tools)@staticmethoddef is_tool_allowed(diff --git a/litellm/proxy/_experimental/mcp_server/server.py b/litellm/proxy/_experimental/mcp_server/server.pyindex 2c774224098..48effdb0f6e 100644--- a/litellm/proxy/_experimental/mcp_server/server.py+++ b/litellm/proxy/_experimental/mcp_server/server.py@@ -2305,14 +2305,16 @@ if MCP_AVAILABLE:server_id=server_id,user_api_key_auth=user_api_key_auth,)- if allowed_tool_names is None:- return tools# Tools arrive prefixed with the server's own prefix; strip exactly that# prefix (resolved from the server) rather than the first separator, so a# prefix containing the separator still reduces to the stored bare name.server = global_mcp_server_manager.get_mcp_server_by_id(server_id)- return [t for t in tools if strip_known_server_prefix(t.name, server) in allowed_tool_names]+ return [+ t+ for t in tools+ if MCPRequestHandler.tool_is_granted(strip_known_server_prefix(t.name, server), allowed_tool_names)+ ]async def _list_mcp_tools(user_api_key_auth: UserAPIKeyAuth | None = None,diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.pyindex 5115fde2687..b2cdcd51294 100644--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py@@ -8154,3 +8154,61 @@ class TestListFiltersHonorThePrefixBoundary:assert refused, entryassert hidden, entry++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "grants,expected",+ [+ (None, True),+ ([], False),+ (["read_wiki_contents"], True),+ (["read_wiki_structure"], False),+ ([f"{SERVER_ID}-read_wiki_contents"], False),+ (["READ_WIKI_CONTENTS"], False),+ ],+ )+ async def test_key_team_listing_and_dispatch_agree(self, grants, expected):+ """The key/team grant question, driven through both production paths.++ Listing and dispatch read one predicate, so a row where the tool is advertised+ and then refused (or hidden while callable) cannot exist. Asserting the expected+ verdict as well as the agreement matters: both paths reading one predicate makes+ equality alone tautological, so a wrong predicate would keep them consistent.+ Grants are stored bare, so the wire-form and case-variant rows deny; that is+ deliberately unlike the server-level lists, which honor every spelling.+ """+ from mcp.types import Tool as MCPTool++ from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import (+ MCPRequestHandler,+ )+ from litellm.proxy._experimental.mcp_server.server import (+ filter_tools_by_key_team_permissions,+ )+ from litellm.proxy._types import UserAPIKeyAuth++ server = MCPServer(+ server_id=self.SERVER_ID,+ name=self.SERVER_ID,+ url="http://127.0.0.1:5115/mcp",+ transport=MCPTransport.http,+ )+ published = MCPTool(+ name=f"{self.SERVER_ID}-read_wiki_contents", description="", inputSchema={"type": "object"}+ )+ auth = UserAPIKeyAuth(api_key="sk-test")++ with patch.object(+ MCPRequestHandler, "get_allowed_tools_for_server", AsyncMock(return_value=grants)+ ), patch(+ "litellm.proxy._experimental.mcp_server.server.global_mcp_server_manager"+ ) as mock_manager:+ mock_manager.get_mcp_server_by_id.return_value = server++ listed = await filter_tools_by_key_team_permissions([published], self.SERVER_ID, auth) != []+ callable_ = await MCPRequestHandler.is_tool_allowed_for_server(+ tool_name="read_wiki_contents", server_id=self.SERVER_ID, user_api_key_auth=auth+ )++ assert listed == callable_, f"grants={grants!r} listed={listed} callable={callable_}"+ assert listed is expected, f"grants={grants!r} expected={expected} got={listed}"