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): keep REST tool listing in step with key/team grant enforcement
tin33a92bd48f4d
diff --git a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py b/litellm/proxy/_experimental/mcp_server/rest_endpoints.pyindex 1736e34d70c..b5d2471336f 100644--- a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py+++ b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py@@ -99,9 +99,9 @@ if MCP_AVAILABLE:MCPServer,_apply_toolset_scope,_fire_mcp_tool_call_logging,- _tool_name_matches,execute_mcp_tool,filter_tools_by_allowed_tools,+ filter_tools_by_key_team_permissions,)########################################################@@ -530,19 +530,17 @@ if MCP_AVAILABLE:tools = filter_tools_by_allowed_tools(tools, server)# Filter by the key's effective tool permissions through the same- # primitive the MCP protocol path uses (direct grants, toolset grants,- # and team/agent/org ceilings), so REST listing cannot drift from it+ # function the MCP protocol path uses (direct grants, toolset grants,+ # and team/agent/org ceilings), so REST listing cannot drift from it.+ # Entries here are tool names on one server, written bare by every+ # writer, and dispatch compares them bare; matching a wider set of+ # spellings would advertise a tool that tools/call then refusesif user_api_key_auth:- from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import (- MCPRequestHandler,- )-- allowed_tools_for_server = await MCPRequestHandler.get_allowed_tools_for_server(+ tools = await filter_tools_by_key_team_permissions(+ tools=tools,server_id=server.server_id,user_api_key_auth=user_api_key_auth,)- if allowed_tools_for_server is not None:- tools = [tool for tool in tools if _tool_name_matches(tool.name, allowed_tools_for_server, server)]return _create_tool_response_objects(tools, server)diff --git a/tests/mcp_tests/test_mcp_server.py b/tests/mcp_tests/test_mcp_server.pyindex 390f6917573..434a9bc3809 100644--- a/tests/mcp_tests/test_mcp_server.py+++ b/tests/mcp_tests/test_mcp_server.py@@ -2039,6 +2039,65 @@ async def test_get_tools_for_single_server_applies_disallowed_tools_without_alloassert [tool.name for tool in result] == ["read_email"]+async def test_rest_listing_hides_key_grants_dispatch_would_refuse():+ """REST listing must answer for exactly the key/team grants dispatch honors.++ ``mcp_tool_permissions`` and toolset rows name a tool on one server, so both+ the MCP list path and ``tools/call`` compare them bare. A wire-form entry+ therefore grants nothing, and REST listing that matched the prefixed+ spelling would advertise a tool the very next call refuses.+ """+ from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import (+ MCPRequestHandler,+ )+ from litellm.proxy._experimental.mcp_server.rest_endpoints import (+ _get_tools_for_single_server,+ )+ from litellm.proxy._types import UserAPIKeyAuth+ from mcp.types import Tool as MCPTool++ server_id = "3c6f6617-d23c-4f48-bfb0-f205e3b27bab"+ mock_server = MagicMock()+ mock_server.mcp_info = {"server_name": server_id}+ mock_server.name = server_id+ mock_server.server_id = server_id+ mock_server.server_name = None+ mock_server.alias = None+ mock_server.short_prefix = None+ mock_server.allowed_tools = None+ mock_server.disallowed_tools = None+ mock_server.tool_name_to_display_name = None++ mock_tools = [+ MCPTool(+ name="read_wiki_contents",+ description="Read a wiki",+ inputSchema={"type": "object"},+ ),+ ]++ with patch(+ "litellm.proxy._experimental.mcp_server.rest_endpoints.global_mcp_server_manager"+ ) as mock_manager, patch(+ "litellm.proxy._experimental.mcp_server.server.global_mcp_server_manager"+ ) as mock_server_manager, patch.object(+ MCPRequestHandler,+ "get_allowed_tools_for_server",+ AsyncMock(return_value=[f"{server_id}-read_wiki_contents"]),+ ):+ mock_manager._get_tools_from_server = AsyncMock(return_value=mock_tools)+ mock_server_manager.get_mcp_server_by_id.return_value = mock_server++ result = await _get_tools_for_single_server(+ mock_server,+ "Bearer test_token",+ user_api_key_auth=UserAPIKeyAuth(api_key="sk-test"),+ )++ assert result == []++@pytest.mark.asyncioasync def test_list_tool_rest_api_with_server_specific_auth():"""Test list_tool_rest_api with server-specific auth headers."""