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
feat(mcp_server_manager.py): ensure list tools for openapi servers works as expected
Krrish Dholakia28698933fc43
diff --git a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.pyindex f7cd7a0a02a..334e984ba96 100644--- a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py+++ b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py@@ -324,13 +324,6 @@ class MCPServerManager:)base_tool_name = operation_id.replace(" ", "_").lower()- # Check if tool is allowed for this server- if not self.check_allowed_or_banned_tools(base_tool_name, server):- verbose_logger.debug(- f"Skipping tool {base_tool_name} - not in allowed_tools for server {server.name}"- )- continue-# Add server prefix to tool nameprefixed_tool_name = add_server_prefix_to_tool_name(base_tool_name, server_prefixdiff --git a/litellm/proxy/_experimental/mcp_server/server.py b/litellm/proxy/_experimental/mcp_server/server.pyindex f38e44e99d9..37157b4e0ac 100644--- a/litellm/proxy/_experimental/mcp_server/server.py+++ b/litellm/proxy/_experimental/mcp_server/server.py@@ -501,13 +501,13 @@ if MCP_AVAILABLE:)filtered_tools = filter_tools_by_allowed_tools(tools, server)-+filtered_tools = await filter_tools_by_key_team_permissions(tools=filtered_tools,server_id=server_id,user_api_key_auth=user_api_key_auth,)-+all_tools.extend(filtered_tools)verbose_logger.debug(@@ -522,6 +522,7 @@ if MCP_AVAILABLE:verbose_logger.info(f"Successfully fetched {len(all_tools)} tools total from all MCP servers")+return all_toolsasync def filter_tools_by_key_team_permissions(@@ -531,7 +532,7 @@ if MCP_AVAILABLE:) -> List[MCPTool]:"""Filter tools based on key/team mcp_tool_permissions.-+Note: Tool names in the DB are stored without server prefixes,but tool names from MCP servers are prefixed. We need to stripthe prefix before comparing.@@ -553,7 +554,7 @@ if MCP_AVAILABLE:else:# No restrictions, return all toolsfiltered_tools = tools-+return filtered_toolsasync def _list_mcp_tools(@@ -598,30 +599,7 @@ if MCP_AVAILABLE:)# Continue with empty managed tools list instead of failing completely- # Get tools from local registry- local_tools = []- try:- local_tools_raw = global_mcp_tool_registry.list_tools()-- # Convert local tools to MCPTool format- for tool in local_tools_raw:- # Convert from litellm.types.mcp_server.tool_registry.MCPTool to mcp.types.Tool- mcp_tool = MCPTool(- name=tool.name,- description=tool.description,- inputSchema=tool.input_schema,- )- local_tools.append(mcp_tool)- except Exception as e:- verbose_logger.exception(- f"Error getting tools from local registry: {str(e)}"- )- # Continue with empty local tools list instead of failing completely-- # Combine all tools- all_tools = managed_tools + local_tools-- return all_tools+ return managed_tools@clientasync def call_mcp_tool(diff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yamlindex 462d329420c..95c6869b95b 100644--- a/litellm/proxy/_new_secret_config.yaml+++ b/litellm/proxy/_new_secret_config.yaml@@ -21,6 +21,7 @@ mcp_servers:url: "http://0.0.0.0:8090"spec_path: "/Users/krrishdholakia/Documents/temp_py_folder/example_openapi.json"auth_type: none+ allowed_tools: ["getpetbyid", "my_api_mcp-findpetsbystatus"]litellm_settings:diff --git a/litellm/types/mcp_server/mcp_server_manager.py b/litellm/types/mcp_server/mcp_server_manager.pyindex 254c5355c63..a247dc3f614 100644--- a/litellm/types/mcp_server/mcp_server_manager.py+++ b/litellm/types/mcp_server/mcp_server_manager.py@@ -16,7 +16,6 @@ class MCPServer(BaseModel):alias: Optional[str] = Noneserver_name: Optional[str] = Noneurl: Optional[str] = None- spec_path: Optional[str] = Nonetransport: MCPTransportTypespec_path: Optional[str] = Noneauth_type: Optional[MCPAuthType] = Nonediff --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 66871aefc1c..769785b9214 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@@ -1001,7 +1001,7 @@ async def test_list_tools_with_no_tool_permissions_shows_all():async def test_list_tools_strips_prefix_when_matching_permissions():"""Test that tool permission filtering correctly strips prefixes from tool names.-+Tools from MCP servers are prefixed (e.g., "GITMCP-fetch_litellm_documentation"),but allowed tools in DB are stored without prefix (e.g., "fetch_litellm_documentation").The filtering should strip the prefix before comparing.@@ -1056,7 +1056,9 @@ async def test_list_tools_strips_prefix_when_matching_permissions():tool1.inputSchema = {}tool2 = MagicMock()- tool2.name = "GITMCP-search_litellm_documentation" # Prefixed, not in allowed list+ tool2.name = (+ "GITMCP-search_litellm_documentation" # Prefixed, not in allowed list+ )tool2.description = "Search docs"tool2.inputSchema = {}@@ -1093,3 +1095,76 @@ async def test_list_tools_strips_prefix_when_matching_permissions():"GITMCP-fetch_litellm_documentation","GITMCP-search_litellm_code",]+++def test_filter_tools_by_allowed_tools():+ """Test that filter_tools_by_allowed_tools filters tools correctly"""+ from mcp.types import Tool++ from litellm.proxy._experimental.mcp_server.server import (+ filter_tools_by_allowed_tools,+ )+ from litellm.types.mcp import MCPTransport+ from litellm.types.mcp_server.mcp_server_manager import MCPServer++ mcp_server = MCPServer(+ server_id="my_api_mcp",+ name="my_api_mcp",+ alias="my_api_mcp",+ transport=MCPTransport.http,+ allowed_tools=["getpetbyid", "my_api_mcp-findpetsbystatus"],+ disallowed_tools=None,+ )+ tools_to_return = [+ Tool(+ name="my_api_mcp-getpetbyid",+ title=None,+ description="Find pet by ID",+ inputSchema={+ "type": "object",+ "properties": {"petId": {"type": "integer", "description": ""}},+ "required": ["petId"],+ },+ outputSchema=None,+ annotations=None,+ ),+ Tool(+ name="my_api_mcp-findpetsbystatus",+ title=None,+ description="Finds Pets by status",+ inputSchema={+ "type": "object",+ "properties": {"status": {"type": "string", "description": ""}},+ "required": ["status"],+ },+ outputSchema=None,+ annotations=None,+ ),+ Tool(+ name="my_api_mcp-addpet",+ title=None,+ description="Add a new pet to the store",+ inputSchema={+ "type": "object",+ "properties": {+ "body": {+ "type": "object",+ "description": "Request body",+ "properties": {+ "name": {"type": "string"},+ "status": {"type": "string"},+ },+ }+ },+ "required": ["body"],+ },+ outputSchema=None,+ annotations=None,+ ),+ ]++ filtered_tools = filter_tools_by_allowed_tools(tools_to_return, mcp_server)++ assert len(filtered_tools) == 2+ assert filtered_tools[0].name == "my_api_mcp-getpetbyid"+ assert filtered_tools[1].name == "my_api_mcp-findpetsbystatus"