@@ -215,9 +215,9 @@ async def mcp_server_tool_call(
215
215
"""
216
216
from fastapi import Request
217
217
218
+ from litellm .exceptions import BlockedPiiEntityError , GuardrailRaisedException
218
219
from litellm .proxy .litellm_pre_call_utils import add_litellm_data_to_request
219
220
from litellm .proxy .proxy_server import proxy_config
220
- from litellm .exceptions import BlockedPiiEntityError , GuardrailRaisedException
221
221
222
222
# Validate arguments
223
223
user_api_key_auth , mcp_auth_header , _ , mcp_server_auth_headers , mcp_protocol_version = get_auth_context ()
@@ -279,33 +279,15 @@ async def mcp_server_tool_call(
279
279
############ Helper Functions ##########################
280
280
########################################################
281
281
282
- async def _get_tools_from_mcp_servers (
283
- user_api_key_auth : Optional [UserAPIKeyAuth ],
284
- mcp_auth_header : Optional [str ],
282
+ async def _get_allowed_mcp_servers_from_mcp_server_names (
285
283
mcp_servers : Optional [List [str ]],
286
- mcp_server_auth_headers : Optional [Dict [str , str ]] = None ,
287
- mcp_protocol_version : Optional [str ] = None ,
288
- ) -> List [MCPTool ]:
284
+ allowed_mcp_servers : List [str ],
285
+ ) -> List [str ]:
289
286
"""
290
- Helper method to fetch tools from MCP servers based on server filtering criteria.
291
-
292
- Args:
293
- user_api_key_auth: User authentication info for access control
294
- mcp_auth_header: Optional auth header for MCP server (deprecated)
295
- mcp_servers: Optional list of server names/aliases to filter by
296
- mcp_server_auth_headers: Optional dict of server-specific auth headers {server_alias: auth_value}
297
-
298
- Returns:
299
- List[MCPTool]: Combined list of tools from filtered servers
287
+ Get the filtered MCP servers from the MCP server names
300
288
"""
301
- if not MCP_AVAILABLE :
302
- return []
303
-
304
- # Get allowed MCP servers based on user permissions
305
- allowed_mcp_servers = await global_mcp_server_manager .get_allowed_mcp_servers (user_api_key_auth )
306
-
307
- filtered_server_ids = set ()
308
-
289
+ from typing import Set
290
+ filtered_server_ids : Set [str ] = set ()
309
291
# Filter servers based on mcp_servers parameter if provided
310
292
if mcp_servers is not None :
311
293
for server_or_group in mcp_servers :
@@ -336,6 +318,40 @@ async def _get_tools_from_mcp_servers(
336
318
337
319
if filtered_server_ids :
338
320
allowed_mcp_servers = list (filtered_server_ids )
321
+
322
+ return allowed_mcp_servers
323
+
324
+ async def _get_tools_from_mcp_servers (
325
+ user_api_key_auth : Optional [UserAPIKeyAuth ],
326
+ mcp_auth_header : Optional [str ],
327
+ mcp_servers : Optional [List [str ]],
328
+ mcp_server_auth_headers : Optional [Dict [str , str ]] = None ,
329
+ mcp_protocol_version : Optional [str ] = None ,
330
+ ) -> List [MCPTool ]:
331
+ """
332
+ Helper method to fetch tools from MCP servers based on server filtering criteria.
333
+
334
+ Args:
335
+ user_api_key_auth: User authentication info for access control
336
+ mcp_auth_header: Optional auth header for MCP server (deprecated)
337
+ mcp_servers: Optional list of server names/aliases to filter by
338
+ mcp_server_auth_headers: Optional dict of server-specific auth headers {server_alias: auth_value}
339
+
340
+ Returns:
341
+ List[MCPTool]: Combined list of tools from filtered servers
342
+ """
343
+ if not MCP_AVAILABLE :
344
+ return []
345
+
346
+ # Get allowed MCP servers based on user permissions
347
+ allowed_mcp_servers = await global_mcp_server_manager .get_allowed_mcp_servers (user_api_key_auth )
348
+
349
+ if mcp_servers is not None :
350
+ allowed_mcp_servers = await _get_allowed_mcp_servers_from_mcp_server_names (
351
+ mcp_servers = mcp_servers ,
352
+ allowed_mcp_servers = allowed_mcp_servers ,
353
+ )
354
+
339
355
340
356
# Get tools from each allowed server
341
357
all_tools = []
@@ -556,20 +572,25 @@ async def _handle_local_mcp_tool(
556
572
except Exception as e :
557
573
return [TextContent (text = f"Error: { str (e )} " , type = "text" )]
558
574
559
- async def extract_mcp_auth_context ( scope , path ) :
575
+ def _get_mcp_servers_in_path ( path : str ) -> Optional [ List [ str ]] :
560
576
"""
561
- Extracts mcp_servers from the path and processes the MCP request for auth context.
562
- Returns: (user_api_key_auth, mcp_auth_header, mcp_servers, mcp_server_auth_headers)
577
+ Get the MCP servers from the path
563
578
"""
564
579
import re
565
-
566
- mcp_servers_from_path = None
580
+ mcp_servers_from_path : Optional [List [str ]] = None
567
581
mcp_path_match = re .match (r"^/mcp/([^/]+)(/.*)?$" , path )
568
582
if mcp_path_match :
569
583
mcp_servers_str = mcp_path_match .group (1 )
570
584
if mcp_servers_str :
571
585
mcp_servers_from_path = [s .strip () for s in mcp_servers_str .split ("," ) if s .strip ()]
586
+ return mcp_servers_from_path
572
587
588
+ async def extract_mcp_auth_context (scope , path ):
589
+ """
590
+ Extracts mcp_servers from the path and processes the MCP request for auth context.
591
+ Returns: (user_api_key_auth, mcp_auth_header, mcp_servers, mcp_server_auth_headers)
592
+ """
593
+ mcp_servers_from_path = _get_mcp_servers_in_path (path )
573
594
if mcp_servers_from_path is not None :
574
595
(
575
596
user_api_key_auth ,
0 commit comments