From fe3e6bbb016a872525045cb63fd04c6d19203677 Mon Sep 17 00:00:00 2001 From: Kerry Lu Date: Mon, 16 Nov 2026 17:44:44 -0800 Subject: [PATCH] fix(mcp): let zero-budget users call MCP tools that cost nothing (#52130) * fix(mcp): let zero-budget users call MCP tools that cost nothing Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(mcp): price a tool call made by its display name as the tool it runs Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(mcp): keep budget checks for bare display names a paid server can run and for cut non-tool-call bodies Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(mcp): budget check reserved tool names and display names that resolve on to a paid tool Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(mcp): follow chained display names without recursion Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(mcp): price a scoped tool call on the servers it is scoped to and follow display names as execution does Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(mcp): keep the budget check for MCP servers that use display names Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/proxy/auth/auth_checks.py | 114 ++++- litellm/proxy/mcp_server/cost_calculator.py | 42 +- litellm/proxy/mcp_server/server.py | 28 +- litellm/proxy/mcp_server/utils.py | 11 + tests/unit/proxy/auth/test_auth_checks.py | 436 +++++++++++++++--- ...st_auth_checks_object_access_and_lookup.py | 4 +- .../unit/proxy/auth/test_user_api_key_auth.py | 18 + .../test_user_api_key_auth_request_flow.py | 29 +- .../test_mcp_server_tool_calls_and_headers.py | 69 ++- 9 files changed, 645 insertions(+), 106 deletions(-) diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index 00ea7ca14b0..88af8bfdc0c 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -209,6 +209,7 @@ if TYPE_CHECKING: from opentelemetry.trace import Span as _Span from litellm.proxy.spend_tracking.native_budgets import BudgetDecision + from litellm.types.mcp_server.mcp_server_manager import MCPServer Span = _Span else: @@ -1104,6 +1105,8 @@ MCP_DISCOVERY_ROUTES: Final = frozenset({"/mcp-rest/tools/list", "/v1/mcp/tools" MCP_ZERO_SPEND_JSONRPC_METHODS: Final = frozenset({"initialize", "notifications/initialized", "ping", "tools/list"}) +MCP_ADMISSION_SCOPE_KEY: Final = "mcp_servers" +_SCOPE_NAMES: Final = TypeAdapter(tuple[str, ...]) MCP_TOOL_CALL_ROUTES: Final = frozenset({"/mcp/tools/call", "/mcp-rest/tools/call"}) @@ -1116,6 +1119,112 @@ def is_mcp_discovery_request(route: str, request_body: Mapping[str, object]) -> return isinstance(method, str) and method in MCP_ZERO_SPEND_JSONRPC_METHODS +def _is_mcp_tool_call(route: str, request_body: Mapping[str, object]) -> bool: + if route in MCP_TOOL_CALL_ROUTES: + return True + return (route == "/mcp" or route.startswith("/mcp/")) and request_body.get("method") == "tools/call" + + +def _mcp_tool_call_name(route: str, request_body: Mapping[str, object]) -> str | None: + from litellm.proxy.mcp_server.utils import string_field + + if route in MCP_TOOL_CALL_ROUTES: + name: Final = request_body.get("name") + return name if isinstance(name, str) else None + return string_field(request_body.get("params"), "name") + + +def _server_free_price_for(tool_name: str, server: "MCPServer") -> bool: + """A server with display names can run a different tool than the one named, so it is never taken as free.""" + from litellm.proxy.mcp_server.cost_calculator import ( # noqa: PLC0415 # the MCP package imports the auth stack + mcp_tool_cost_per_query, + ) + from litellm.proxy.mcp_server.utils import ( # noqa: PLC0415 # the MCP package imports the auth stack + strip_known_server_prefix, + ) + + if server.tool_name_to_display_name: + return False + return ( + mcp_tool_cost_per_query( + cost_info=(server.mcp_info or {}).get("mcp_server_cost_info"), + tool_name=strip_known_server_prefix(tool_name, server), + ) + <= 0 + ) + + +def _is_shown_as_a_display_name(tool_name: str, server: "MCPServer") -> bool: + return tool_name in (server.tool_name_to_display_name or {}).values() + + +def _is_reserved_mcp_tool_name(tool_name: str) -> bool: + from litellm.proxy.mcp_server.tool_search import ( # noqa: PLC0415 # the MCP package imports the auth stack + MCP_PROXY_TOOL_NAMES, + VIRTUAL_TOOL_NAMES, + ) + + return tool_name in VIRTUAL_TOOL_NAMES or tool_name in MCP_PROXY_TOOL_NAMES + + +def _mcp_scope_names(request_body: Mapping[str, object]) -> tuple[str, ...]: + """The path or header scope of a streamable HTTP call, which can run a prefixed name on another server.""" + try: + return _SCOPE_NAMES.validate_python(request_body.get(MCP_ADMISSION_SCOPE_KEY) or ()) + except ValueError: + return ("",) + + +def is_zero_cost_mcp_tool_call(route: str, request_body: Mapping[str, object]) -> bool: + """A tool call is free when no MCP server charges, or when every server that could run it is free for it. + + Only a name with a known server prefix, or a REST ``server_id``, says which server runs it. Display names can + send a name to another tool, so a call stays budget checked when a server that could run it has any, or when + any server shows a tool under the name.""" + if not _is_mcp_tool_call(route=route, request_body=request_body): + return False + from litellm.proxy.mcp_server.cost_calculator import ( # noqa: PLC0415 # the MCP package imports the auth stack + mcp_server_charges_for_tool_calls, + ) + from litellm.proxy.mcp_server.mcp_server_manager import ( # noqa: PLC0415 # the MCP package imports the auth stack + global_mcp_server_manager, + ) + + servers: Final = tuple(global_mcp_server_manager.get_registry().values()) + if not any( + mcp_server_charges_for_tool_calls((server.mcp_info or {}).get("mcp_server_cost_info")) for server in servers + ): + return True + tool_name: Final = _mcp_tool_call_name(route=route, request_body=request_body) + if tool_name is None: + return False + requested_server_id: Final = request_body.get("server_id") if route in MCP_TOOL_CALL_ROUTES else None + named_servers: Final = tuple( + server + for server in ( + global_mcp_server_manager.server_owning_tool_name_prefix(tool_name), + ( + global_mcp_server_manager.get_mcp_server_by_id(requested_server_id) + or global_mcp_server_manager.get_mcp_server_by_identifier(requested_server_id) + ) + if isinstance(requested_server_id, str) + else None, + ) + if server is not None + ) + servers_in_scope: Final = tuple( + global_mcp_server_manager.get_mcp_server_by_identifier(name) for name in _mcp_scope_names(request_body) + ) + servers_that_could_run_it: Final = tuple( + server for server in (*named_servers, *servers_in_scope) if server is not None + ) + if _is_reserved_mcp_tool_name(tool_name) or not named_servers or None in servers_in_scope: + return False + if any(_is_shown_as_a_display_name(tool_name, server) for server in servers): + return False + return all(_server_free_price_for(tool_name, server) for server in servers_that_could_run_it) + + def is_video_retrieval_request(route: str, http_method: str | None) -> bool: return http_method == "GET" and RouteChecks.check_route_access(route=route, allowed_routes=VIDEO_RETRIEVAL_ROUTES) @@ -1153,7 +1262,10 @@ def request_skips_budget_checks( ) -> bool: if route_skips_budget_checks(route=route) or is_video_retrieval_request(route=route, http_method=http_method): return True - if request_body is not None and is_mcp_discovery_request(route=route, request_body=request_body): + if request_body is not None and ( + is_mcp_discovery_request(route=route, request_body=request_body) + or is_zero_cost_mcp_tool_call(route=route, request_body=request_body) + ): return True return is_model_cost_zero(model=model, llm_router=llm_router) diff --git a/litellm/proxy/mcp_server/cost_calculator.py b/litellm/proxy/mcp_server/cost_calculator.py index e16c9cf62df..23d6938ea8b 100644 --- a/litellm/proxy/mcp_server/cost_calculator.py +++ b/litellm/proxy/mcp_server/cost_calculator.py @@ -15,6 +15,25 @@ else: LitellmLoggingObject = Any +def mcp_tool_cost_per_query(cost_info: MCPServerCostInfo | None, tool_name: str) -> float: + resolved_cost_info: Final = cost_info or MCPServerCostInfo() + tool_name_to_cost_per_query: Final = resolved_cost_info.get("tool_name_to_cost_per_query") or {} + if tool_name in tool_name_to_cost_per_query: + return tool_name_to_cost_per_query[tool_name] + return resolved_cost_info.get("default_cost_per_query") or 0.0 + + +def mcp_server_charges_for_tool_calls(cost_info: MCPServerCostInfo | None) -> bool: + resolved_cost_info: Final = cost_info or MCPServerCostInfo() + return any( + (cost or 0.0) > 0 + for cost in ( + resolved_cost_info.get("default_cost_per_query"), + *(resolved_cost_info.get("tool_name_to_cost_per_query") or {}).values(), + ) + ) + + class MCPCostCalculator: @staticmethod def calculate_mcp_tool_call_cost( @@ -46,24 +65,7 @@ class MCPCostCalculator: ) or {} ) - mcp_server_cost_info: Final[MCPServerCostInfo] = ( - mcp_tool_call_metadata.get("mcp_server_cost_info") or MCPServerCostInfo() + return mcp_tool_cost_per_query( + cost_info=mcp_tool_call_metadata.get("mcp_server_cost_info"), + tool_name=mcp_tool_call_metadata.get("name", ""), ) - ######################################################### - # User defined cost per query - ######################################################### - default_cost_per_query: Final = mcp_server_cost_info.get("default_cost_per_query", None) - tool_name_to_cost_per_query: Final[dict] = mcp_server_cost_info.get("tool_name_to_cost_per_query", {}) or {} - tool_name: Final = mcp_tool_call_metadata.get("name", "") - - ######################################################### - # 1. If tool_name is in tool_name_to_cost_per_query, use the cost per query - # 2. If tool_name is not in tool_name_to_cost_per_query, use the default cost per query - # 3. Default to 0.0 if no cost per query is found - ######################################################### - cost_per_query: float = 0.0 - if tool_name in tool_name_to_cost_per_query: - cost_per_query = tool_name_to_cost_per_query[tool_name] - elif default_cost_per_query is not None: - cost_per_query = default_cost_per_query - return cost_per_query diff --git a/litellm/proxy/mcp_server/server.py b/litellm/proxy/mcp_server/server.py index c255eddb214..db91ce34751 100644 --- a/litellm/proxy/mcp_server/server.py +++ b/litellm/proxy/mcp_server/server.py @@ -102,6 +102,7 @@ from litellm.proxy.mcp_server.utils import ( LITELLM_MCP_SERVER_VERSION, asgi_scope_fields, merge_mcp_headers, + string_field, ) from litellm.types.mcp import ( MCPAuth, @@ -1473,16 +1474,23 @@ if MCP_AVAILABLE: return f"ip:{hashlib.sha256(client_ip.encode('utf-8')).hexdigest()}" return "anonymous" - def _admission_auth_body(body: bytes) -> bytes: - """Return only the JSON-RPC method for admission auth.""" + def _admission_auth_body(body: bytes, scoped_to: Sequence[str] = ()) -> bytes: + """Return only the JSON-RPC method, and the tool name and server scope of a tools/call, for admission auth.""" try: data: Final = TypeAdapter(dict[str, object]).validate_json(body) except (ValueError, TypeError): - return b"{}" + is_cut_tool_call: Final = ( + len(body) >= _MCP_ROUTING_PEEK_MAX_BYTES + and _jsonrpc_text_method(body.decode("utf-8", errors="replace")) == "tools/call" + ) + return json.dumps({"method": "tools/call"}).encode() if is_cut_tool_call else b"{}" method: Final = data.get("method") if not isinstance(method, str): return b"{}" - return json.dumps({"method": method}).encode() + tool_name: Final = string_field(data.get("params"), "name") if method == "tools/call" else None + if tool_name is None: + return json.dumps({"method": method}).encode() + return json.dumps({"method": method, "params": {"name": tool_name}, "mcp_servers": list(scoped_to)}).encode() def _is_initialize_request(body: bytes) -> bool: """ @@ -2544,7 +2552,17 @@ if MCP_AVAILABLE: receive = peek.receive # rebind-ok: replay peeked ASGI messages to the downstream handler async def admission_auth_body() -> bytes: - return _admission_auth_body(await peek.admission_body()) + return _admission_auth_body( + await peek.admission_body(), + scoped_to=MCPRequestHandler.extract_target_server_names_from_path(path) + or tuple( + name.strip() + for name in StarletteRequest(scope) + .headers.get(MCPRequestHandler.LITELLM_MCP_SERVERS_HEADER_NAME, "") + .split(",") + if name.strip() + ), + ) scope[MCP_PEEKED_BODY_SCOPE_KEY] = admission_auth_body ( diff --git a/litellm/proxy/mcp_server/utils.py b/litellm/proxy/mcp_server/utils.py index 024bf2aa3e1..2b7afbc97f5 100644 --- a/litellm/proxy/mcp_server/utils.py +++ b/litellm/proxy/mcp_server/utils.py @@ -478,6 +478,17 @@ def match_known_server_prefix(name: str, known_prefixes: Iterable[str]) -> tuple return None +_STRING_KEYED_OBJECT: Final = TypeAdapter(dict[str, object]) + + +def string_field(value: object, key: str) -> str | None: + try: + field: Final = _STRING_KEYED_OBJECT.validate_python(value).get(key) + except ValueError: + return None + return field if isinstance(field, str) else None + + def strip_known_server_prefix(name: str, server: _McpServerLike | None) -> str: """Strip ``server``'s registered prefix from a prefixed tool/resource name. diff --git a/tests/unit/proxy/auth/test_auth_checks.py b/tests/unit/proxy/auth/test_auth_checks.py index dacbab41e8a..fe4553be9df 100644 --- a/tests/unit/proxy/auth/test_auth_checks.py +++ b/tests/unit/proxy/auth/test_auth_checks.py @@ -27,6 +27,8 @@ from litellm.proxy.auth.auth_checks import get_org_object, get_user_object from litellm.proxy.db.prisma_client import PrismaWrapper from litellm.proxy.auth.auth_checks import get_end_user_object from litellm.caching.caching import DualCache +from litellm.types.mcp import MCPTransport +from litellm.types.mcp_server.mcp_server_manager import MCPServer from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache from litellm.proxy._types import ( LiteLLM_EndUserTable, @@ -162,12 +164,15 @@ async def test_team_require_trace_id_accepts_supported_trace_carriers( @pytest.mark.parametrize("route", ["/v1/traces", "/v1/logs", "/v1/traces/query"]) @pytest.mark.asyncio async def test_team_require_trace_id_accepts_trace_telemetry_routes(route: str) -> None: - assert await _run_team_trace_id_check( - route=route, - method="POST", - request_body={}, - team_metadata={"require_trace_id": True}, - ) is True + assert ( + await _run_team_trace_id_check( + route=route, + method="POST", + request_body={}, + team_metadata={"require_trace_id": True}, + ) + is True + ) @pytest.mark.asyncio @@ -199,11 +204,14 @@ async def test_team_require_trace_id_rejects_overridden_body_metadata_on_chat() async def test_team_require_trace_id_accepts_effective_metadata_on_responses_route( request_body: dict[str, object], ) -> None: - assert await _run_team_trace_id_check( - route="/v1/responses", - request_body=request_body, - team_metadata={"require_trace_id": True}, - ) is True + assert ( + await _run_team_trace_id_check( + route="/v1/responses", + request_body=request_body, + team_metadata={"require_trace_id": True}, + ) + is True + ) @pytest.mark.parametrize( @@ -242,12 +250,15 @@ async def test_team_require_trace_id_rejects_fallback_carriers_when_litellm_trac @pytest.mark.asyncio async def test_team_require_trace_id_accepts_header_on_mcp_with_empty_body() -> None: - assert await _run_team_trace_id_check( - route="/mcp/", - request_body={}, - headers={"x-litellm-trace-id": "trace-mcp-123"}, - team_metadata={"require_trace_id": True}, - ) is True + assert ( + await _run_team_trace_id_check( + route="/mcp/", + request_body={}, + headers={"x-litellm-trace-id": "trace-mcp-123"}, + team_metadata={"require_trace_id": True}, + ) + is True + ) @pytest.mark.parametrize( @@ -280,12 +291,15 @@ async def test_team_require_trace_id_rejects_nonheader_carriers_on_pass_through_ @pytest.mark.asyncio async def test_team_require_trace_id_accepts_header_on_pass_through_route() -> None: - assert await _run_team_trace_id_check( - route="/anthropic/v1/messages", - request_body={}, - headers={"x-litellm-trace-id": "trace-provider-123"}, - team_metadata={"require_trace_id": True}, - ) is True + assert ( + await _run_team_trace_id_check( + route="/anthropic/v1/messages", + request_body={}, + headers={"x-litellm-trace-id": "trace-provider-123"}, + team_metadata={"require_trace_id": True}, + ) + is True + ) @pytest.mark.parametrize( @@ -331,7 +345,9 @@ async def test_team_require_trace_id_rejects_nonheader_carriers_on_registered_pa "jsonrpc": "2.0", "id": "1", "method": "message/send", - "params": {"message": {"messageId": "msg-1", "role": "user", "parts": [{"type": "text", "text": "hi"}]}}, + "params": { + "message": {"messageId": "msg-1", "role": "user", "parts": [{"type": "text", "text": "hi"}]} + }, }, ), ], @@ -372,7 +388,9 @@ async def test_team_require_trace_id_rejects_traceparent_on_mcp_and_agent_routes "jsonrpc": "2.0", "id": "1", "method": "message/send", - "params": {"message": {"messageId": "msg-1", "role": "user", "parts": [{"type": "text", "text": "hi"}]}}, + "params": { + "message": {"messageId": "msg-1", "role": "user", "parts": [{"type": "text", "text": "hi"}]} + }, "metadata": {"trace_id": "trace-body-123"}, }, ), @@ -465,7 +483,7 @@ def test_lens_management_does_not_require_an_inference_user_parameter(route: str async def test_get_end_user_object(customer_spend, customer_budget): """ Scenario 1: normal - get_end_user_object returns the cached user - Scenario 2: user over budget - NOTE: budget enforcement now happens in + Scenario 2: user over budget - NOTE: budget enforcement now happens in common_checks() via _check_end_user_budget(), not in get_end_user_object() This test verifies that get_end_user_object correctly retrieves the end user @@ -593,7 +611,7 @@ async def test_can_key_call_model(model, expect_to_work): if expect_to_work: await can_key_call_model(**args) else: - with pytest.raises(Exception, match='is not available for this API key') as e: + with pytest.raises(Exception, match="is not available for this API key") as e: await can_key_call_model(**args) print(e) @@ -910,16 +928,12 @@ async def test_is_valid_fallback_model(): ) try: - await is_valid_fallback_model( - model="gpt-3.5-turbo", llm_router=router, user_model=None - ) + await is_valid_fallback_model(model="gpt-3.5-turbo", llm_router=router, user_model=None) except Exception as e: pytest.fail(f"Expected is_valid_fallback_model to work, got exception: {e}") with pytest.raises(Exception, match="Invalid") as exc_info: - await is_valid_fallback_model( - model="gpt-4o", llm_router=router, user_model=None - ) + await is_valid_fallback_model(model="gpt-4o", llm_router=router, user_model=None) e = exc_info.value assert "Invalid" in str(e) @@ -933,9 +947,7 @@ async def test_is_valid_fallback_model(): ], ) @pytest.mark.asyncio -async def test_virtual_key_max_budget_check( - token_spend, max_budget, expect_budget_error -): +async def test_virtual_key_max_budget_check(token_spend, max_budget, expect_budget_error): """ Test if virtual key budget checks work as expected: 1. Triggers budget alert for all cases @@ -1050,9 +1062,7 @@ async def test_can_team_access_model(model, team_models, expect_to_work): team_model_aliases=None, ) if not expect_to_work: - pytest.fail( - f"Expected model access check to fail for model={model}, team_models={team_models}" - ) + pytest.fail(f"Expected model access check to fail for model={model}, team_models={team_models}") except Exception as e: if expect_to_work: pytest.fail( @@ -1105,9 +1115,9 @@ async def test_virtual_key_soft_budget_check(spend, soft_budget, expect_alert): await asyncio.sleep(0.1) # Allow time for the alert task to complete - assert ( - alert_triggered == expect_alert - ), f"Expected alert_triggered to be {expect_alert} for spend={spend}, soft_budget={soft_budget}" + assert alert_triggered == expect_alert, ( + f"Expected alert_triggered to be {expect_alert} for spend={spend}, soft_budget={soft_budget}" + ) @pytest.mark.parametrize( @@ -1160,9 +1170,7 @@ async def test_virtual_key_soft_budget_check(spend, soft_budget, expect_alert): ], ) @pytest.mark.asyncio -async def test_team_soft_budget_check( - spend, soft_budget, expect_alert, metadata, expected_alert_emails -): +async def test_team_soft_budget_check(spend, soft_budget, expect_alert, metadata, expected_alert_emails): """ Test cases for _team_soft_budget_check: 1. Spend over soft budget, no alert_emails configured - should NOT trigger alert (alerts only sent when alert_emails configured) @@ -1210,9 +1218,9 @@ async def test_team_soft_budget_check( await asyncio.sleep(0.1) # Allow time for the alert task to complete - assert ( - alert_triggered == expect_alert - ), f"Expected alert_triggered to be {expect_alert} for spend={spend}, soft_budget={soft_budget}" + assert alert_triggered == expect_alert, ( + f"Expected alert_triggered to be {expect_alert} for spend={spend}, soft_budget={soft_budget}" + ) if expect_alert: assert captured_call_info is not None @@ -1224,10 +1232,7 @@ async def test_team_soft_budget_check( if expected_alert_emails is not None: assert captured_call_info.alert_emails == expected_alert_emails else: - assert ( - captured_call_info.alert_emails is None - or captured_call_info.alert_emails == [] - ) + assert captured_call_info.alert_emails is None or captured_call_info.alert_emails == [] @pytest.mark.asyncio @@ -1358,9 +1363,7 @@ async def test_get_fuzzy_user_object() -> None: # Test 5: Only email provided (no SSO ID) mock_prisma.db.litellm_usertable.find_first = AsyncMock(return_value=test_user) - result = await _get_fuzzy_user_object( - prisma_client=mock_prisma, user_email="test@example.com" - ) + result = await _get_fuzzy_user_object(prisma_client=mock_prisma, user_email="test@example.com") assert result == test_user mock_prisma.db.litellm_usertable.find_first.assert_called_with( where={"user_email": {"equals": "test@example.com", "mode": "insensitive"}}, @@ -1369,9 +1372,7 @@ async def test_get_fuzzy_user_object() -> None: # Test 6: Only SSO ID provided (no email) mock_prisma.db.litellm_usertable.find_unique = AsyncMock(return_value=test_user) - result = await _get_fuzzy_user_object( - prisma_client=mock_prisma, sso_user_id="sso_123" - ) + result = await _get_fuzzy_user_object(prisma_client=mock_prisma, sso_user_id="sso_123") assert result == test_user mock_prisma.db.litellm_usertable.find_unique.assert_called_with( where={"sso_user_id": "sso_123"}, include={"organization_memberships": True} @@ -1418,7 +1419,7 @@ async def test_can_key_call_model_with_aliases(model, alias_map, expect_to_work) llm_router=router, ) else: - with pytest.raises(Exception, match='is not available for this API key') as e: + with pytest.raises(Exception, match="is not available for this API key") as e: await can_key_call_model( model=model, llm_model_list=llm_model_list, @@ -1504,9 +1505,7 @@ async def test_delete_cache_access_object(): ], ) @pytest.mark.asyncio -async def test_get_resources_from_access_groups( - resource_field, access_group_data, expected -): +async def test_get_resources_from_access_groups(resource_field, access_group_data, expected): """Test _get_resources_from_access_groups returns correct resource list from access groups.""" from unittest.mock import AsyncMock, MagicMock, patch @@ -2096,7 +2095,9 @@ def test_is_model_cost_zero_judges_an_alias_chain_by_the_deployment_its_entry_ro model_group_alias={"chain-entry": "local-free", "local-free": "paid-gpt"}, ) expected: Final = {"chain-entry": True, "local-free": False, "paid-gpt": False} - order: Final = ("chain-entry", "local-free", "paid-gpt") if entry_first else ("local-free", "paid-gpt", "chain-entry") + order: Final = ( + ("chain-entry", "local-free", "paid-gpt") if entry_first else ("local-free", "paid-gpt", "chain-entry") + ) verdicts: Final = {name: is_model_cost_zero(model=name, llm_router=router) for name in order} @@ -2171,8 +2172,13 @@ async def test_mcp_jsonrpc_zero_spend_method_bypasses_team_budget(route, method) ], ) @pytest.mark.asyncio -async def test_mcp_tool_call_route_still_enforces_team_budget(route, request_body): +async def test_mcp_tool_call_route_still_enforces_team_budget(monkeypatch, route, request_body): from litellm.proxy.auth.auth_checks import common_checks + from litellm.proxy.mcp_server.mcp_server_manager import global_mcp_server_manager + + monkeypatch.setattr( # test-quality-ok: the registry is a module singleton read by the budget check; no injection seam + global_mcp_server_manager, "config_mcp_servers", {_PAID_SERVER[0]: _mcp_server(*_PAID_SERVER)} + ) team_object = LiteLLM_TeamTable(team_id="test-team", spend=150.0, max_budget=100.0) @@ -2223,6 +2229,298 @@ def test_is_mcp_discovery_request(route, request_body, expected): assert is_mcp_discovery_request(route=route, request_body=request_body) is expected +def _mcp_server( + server_id: str, name: str, cost_info: dict[str, object] | None, display_names: dict[str, str] | None = None +) -> MCPServer: + return MCPServer( + server_id=server_id, + name=name, + alias=name, + url="https://example.invalid/mcp", + transport=MCPTransport.http, + tool_name_to_display_name=display_names, + mcp_info=None if cost_info is None else {"server_name": name, "mcp_server_cost_info": cost_info}, + ) + + +_UNPRICED_SERVER = ("unpriced-id", "unpriced_tools", None) +_RENAMED_PAID_TOOL_SERVER = ( + "renamed-id", + "renamed_tools", + {"default_cost_per_query": 0.0, "tool_name_to_cost_per_query": {"premium": 0.5, "other_premium": 0.5}}, + {"premium": "friendly", "basic": "plain", "other_premium": "unpriced_tools-echo"}, +) +_FREE_SERVER_SHOWING_A_TOOL_AS_PREMIUM = ( + "shows-id", + "shows_tools", + None, + {"basic": "premium", "plain": "mcp_tool_call"}, +) +_PAID_DEFAULT_FREE_RENAMED_TOOL_SERVER = ( + "paid-default-id", + "paid_default_tools", + {"default_cost_per_query": 0.5, "tool_name_to_cost_per_query": {"basic": 0.0}}, + {"basic": "plain"}, +) +_SERVER_SHOWING_A_FREE_TOOL_AS_A_PAID_ONE = ( + "swap-id", + "swap_tools", + {"default_cost_per_query": 0.5, "tool_name_to_cost_per_query": {"basic": 0.0}}, + {"basic": "swap_tools-premium", "premium": "swap_tools-basic"}, +) +_SERVER_SHOWING_A_PAID_TOOL_AS_A_FREE_ONES_BARE_NAME = ( + "bare-id", + "bare_tools", + {"default_cost_per_query": 0.5, "tool_name_to_cost_per_query": {"basic": 0.0}}, + {"basic": "friendly", "premium": "basic"}, +) +_PAID_SERVER_SHOWING_A_TOOL_AS_AN_UNPRICED_SERVERS_PREFIXED_NAME = ( + "cross-id", + "cross_tools", + {"default_cost_per_query": 0.5}, + {"premium": "unpriced_tools-echo"}, +) +_ZERO_PRICED_SERVER = ("zero-id", "zero_tools", {"default_cost_per_query": 0.0}) +_PAID_SERVER = ( + "paid-id", + "paid_tools", + {"default_cost_per_query": 0.5, "tool_name_to_cost_per_query": {"gratis": 0.0}}, +) +_FREE_DEFAULT_PAID_TOOL_SERVER = ("mixed-id", "mixed_tools", {"tool_name_to_cost_per_query": {"premium": 0.25}}) + + +@pytest.mark.parametrize( + "servers,route,request_body,expected", + [ + pytest.param( + (_UNPRICED_SERVER, _ZERO_PRICED_SERVER), + "/mcp", + {"method": "tools/call", "params": {"name": "unpriced_tools-echo"}}, + True, + id="jsonrpc-call-when-no-server-charges", + ), + pytest.param( + (_UNPRICED_SERVER, _ZERO_PRICED_SERVER), + "/mcp", + {"method": "tools/call"}, + True, + id="jsonrpc-call-without-a-readable-name-when-no-server-charges", + ), + pytest.param( + (_UNPRICED_SERVER, _ZERO_PRICED_SERVER), + "/mcp-rest/tools/call", + {"name": "echo", "server_id": "zero-id"}, + True, + id="rest-call-when-no-server-charges", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp", + {"method": "tools/call", "params": {"name": "unpriced_tools-echo"}}, + True, + id="jsonrpc-call-prefixed-with-a-free-server", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp", + {"method": "tools/call", "params": {"name": "paid_tools-echo"}}, + False, + id="jsonrpc-call-prefixed-with-a-paid-server", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp", + {"method": "tools/call", "params": {"name": "paid_tools-gratis"}}, + True, + id="jsonrpc-call-to-a-zero-priced-tool-on-a-paid-server", + ), + pytest.param( + (_FREE_DEFAULT_PAID_TOOL_SERVER,), + "/mcp", + {"method": "tools/call", "params": {"name": "mixed_tools-premium"}}, + False, + id="jsonrpc-call-to-a-priced-tool-on-a-server-with-no-default-cost", + ), + pytest.param( + (_FREE_DEFAULT_PAID_TOOL_SERVER,), + "/mcp", + {"method": "tools/call", "params": {"name": "mixed_tools-echo"}}, + True, + id="jsonrpc-call-to-an-unpriced-tool-beside-a-priced-one", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp", + {"method": "tools/call", "params": {"name": "echo"}}, + False, + id="jsonrpc-bare-name-is-not-trusted-when-a-server-charges", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp", + {"method": "tools/call"}, + False, + id="jsonrpc-call-without-a-readable-name-when-a-server-charges", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp-rest/tools/call", + {"name": "echo", "server_id": "unpriced-id"}, + True, + id="rest-bare-name-with-a-free-server-id", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp-rest/tools/call", + {"name": "echo", "server_id": "unpriced_tools"}, + True, + id="rest-bare-name-with-a-free-server-name", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp-rest/tools/call", + {"name": "echo", "server_id": "paid-id"}, + False, + id="rest-bare-name-with-a-paid-server-id", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp-rest/tools/call", + {"name": "unpriced_tools-echo", "server_id": "paid-id"}, + False, + id="rest-free-prefix-with-a-paid-server-id", + ), + pytest.param( + (_RENAMED_PAID_TOOL_SERVER,), + "/mcp-rest/tools/call", + {"name": "friendly", "server_id": "renamed-id"}, + False, + id="rest-call-by-the-display-name-of-a-paid-tool", + ), + pytest.param( + (_RENAMED_PAID_TOOL_SERVER,), + "/mcp-rest/tools/call", + {"name": "plain", "server_id": "renamed-id"}, + False, + id="rest-call-by-a-display-name-stays-checked-even-when-its-tool-is-free", + ), + pytest.param( + (_RENAMED_PAID_TOOL_SERVER,), + "/mcp", + {"method": "tools/call", "params": {"name": "plain"}}, + False, + id="jsonrpc-call-by-a-bare-display-name-stays-checked", + ), + pytest.param( + (_FREE_SERVER_SHOWING_A_TOOL_AS_PREMIUM, _RENAMED_PAID_TOOL_SERVER), + "/mcp", + {"method": "tools/call", "params": {"name": "premium"}}, + False, + id="jsonrpc-call-by-a-free-display-name-that-is-a-paid-native-tool-elsewhere", + ), + pytest.param( + (_FREE_SERVER_SHOWING_A_TOOL_AS_PREMIUM, _RENAMED_PAID_TOOL_SERVER), + "/mcp", + {"method": "tools/call", "params": {"name": "mcp_tool_call"}}, + False, + id="jsonrpc-call-by-a-free-display-name-that-is-a-reserved-tool-name", + ), + pytest.param( + (_PAID_DEFAULT_FREE_RENAMED_TOOL_SERVER,), + "/mcp-rest/tools/call", + {"name": "plain", "server_id": "paid-default-id"}, + False, + id="rest-call-by-a-display-name-on-a-server-with-a-paid-default-stays-checked", + ), + pytest.param( + (_SERVER_SHOWING_A_FREE_TOOL_AS_A_PAID_ONE,), + "/mcp", + {"method": "tools/call", "params": {"name": "swap_tools-premium"}}, + False, + id="jsonrpc-call-by-a-display-name-that-resolves-on-to-a-paid-tool", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp-rest/tools/call", + {"name": "mcp_tool_call", "server_id": "unpriced-id"}, + False, + id="rest-call-of-a-reserved-tool-name-with-a-free-server-id", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp", + {"method": "tools/call", "params": {"name": "unpriced_tools-echo"}, "mcp_servers": ["paid_tools"]}, + False, + id="jsonrpc-call-of-a-free-servers-prefixed-name-scoped-to-a-paid-server-that-would-run-it", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp", + {"method": "tools/call", "params": {"name": "unpriced_tools-echo"}, "mcp_servers": ["unpriced_tools"]}, + True, + id="jsonrpc-call-scoped-to-the-free-server-that-owns-the-name", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER), + "/mcp", + {"method": "tools/call", "params": {"name": "unpriced_tools-echo"}, "mcp_servers": ["some-access-group"]}, + False, + id="jsonrpc-call-scoped-to-a-name-that-is-not-one-server", + ), + pytest.param( + (_UNPRICED_SERVER, _PAID_SERVER_SHOWING_A_TOOL_AS_AN_UNPRICED_SERVERS_PREFIXED_NAME), + "/mcp", + {"method": "tools/call", "params": {"name": "unpriced_tools-echo"}}, + False, + id="prefixed-name-of-a-free-server-that-another-server-shows-a-tool-under-stays-checked", + ), + pytest.param( + (_SERVER_SHOWING_A_PAID_TOOL_AS_A_FREE_ONES_BARE_NAME,), + "/mcp-rest/tools/call", + {"name": "friendly", "server_id": "bare-id"}, + False, + id="rest-call-on-a-server-with-display-names-stays-checked", + ), + pytest.param( + (_UNPRICED_SERVER, _RENAMED_PAID_TOOL_SERVER), + "/mcp", + {"method": "tools/call", "params": {"name": "unpriced_tools-echo"}}, + False, + id="jsonrpc-call-whose-free-looking-name-is-the-display-name-of-a-paid-tool", + ), + pytest.param( + (_UNPRICED_SERVER,), + "/mcp", + {"method": "resources/read", "params": {"name": "unpriced_tools-echo"}}, + False, + id="other-jsonrpc-methods-stay-budget-checked", + ), + pytest.param( + (_UNPRICED_SERVER,), + "/v1/chat/completions", + {"method": "tools/call", "params": {"name": "unpriced_tools-echo"}}, + False, + id="non-mcp-route-with-a-tool-call-shaped-body", + ), + ], +) +def test_request_skips_budget_checks_for_mcp_tool_calls_that_cost_nothing( + monkeypatch, servers, route, request_body, expected +): + from litellm.proxy.auth.auth_checks import request_skips_budget_checks + from litellm.proxy.mcp_server.mcp_server_manager import global_mcp_server_manager + + monkeypatch.setattr( # test-quality-ok: the registry is a module singleton read by the budget check; no injection seam + global_mcp_server_manager, + "config_mcp_servers", + {server[0]: _mcp_server(*server) for server in servers}, + ) + + assert ( + request_skips_budget_checks(route=route, model=None, llm_router=None, request_body=request_body) is expected + ), f"{route} {request_body}" + + @pytest.mark.asyncio async def test_get_skill_names_from_access_groups_reads_cached_group_rows(): from litellm.proxy._types import LiteLLM_AccessGroupTable @@ -2339,10 +2637,7 @@ async def test_bulk_eviction_clears_this_worker_before_waiting_on_a_wedged_redis monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", 0.5) client = _WedgedPublishRedisClient() cache: Final = UserApiKeyCache(key_object_in_memory_cache=InMemoryCache(max_size_in_memory=2_000)) - hashed_tokens: Final = tuple( - hashlib.sha256(f"token-{index}".encode()).hexdigest() - for index in range(600) - ) + hashed_tokens: Final = tuple(hashlib.sha256(f"token-{index}".encode()).hexdigest() for index in range(600)) for hashed_token in hashed_tokens: cache.set_local(hashed_token, {"token": hashed_token}, None) seeded: Final = tuple(token for token in hashed_tokens if cache.get_local(token) is not None) @@ -2398,10 +2693,7 @@ async def test_bulk_eviction_clears_the_redis_copies_before_waiting_on_a_wedged_ monkeypatch.setattr(pubsub_module, "_pending_publishes", set()) monkeypatch.setattr(pubsub_module, "_BULK_EVICTION_WAIT_SECONDS", 0.5) client = _WedgedPublishRedisClient() - hashed_tokens: Final = tuple( - hashlib.sha256(f"token-{index}".encode()).hexdigest() - for index in range(600) - ) + hashed_tokens: Final = tuple(hashlib.sha256(f"token-{index}".encode()).hexdigest() for index in range(600)) cache, redis = shared_cache(FakeAuthCache(redis={token: {"token": token} for token in hashed_tokens})) for hashed_token in hashed_tokens: cache.set_local(hashed_token, {"token": hashed_token}, None) diff --git a/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py b/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py index 408d8a29743..8cf30ca370e 100644 --- a/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py +++ b/tests/unit/proxy/auth/test_auth_checks_object_access_and_lookup.py @@ -11261,9 +11261,9 @@ def test_route_skips_budget_checks_marks_only_spend_free_routes() -> None: ("/v1/chat/completions", None, "free", True), ("/v1/chat/completions", None, "paid", False), ("/mcp", {"method": "tools/list"}, "paid", True), - ("/mcp", {"method": "tools/call"}, "paid", False), + ("/mcp", {"method": "tools/call"}, "paid", True), ("/mcp", None, "paid", False), - ("/mcp/tools/call", {"method": "initialize"}, "paid", False), + ("/mcp/tools/call", {"method": "initialize"}, "paid", True), ("/health/services", None, "paid", False), ], ) diff --git a/tests/unit/proxy/auth/test_user_api_key_auth.py b/tests/unit/proxy/auth/test_user_api_key_auth.py index 4b749aa99b8..ea0487d2b18 100644 --- a/tests/unit/proxy/auth/test_user_api_key_auth.py +++ b/tests/unit/proxy/auth/test_user_api_key_auth.py @@ -2490,6 +2490,24 @@ async def test_jwt_path_skips_user_model_budget_only_for_zero_spend_mcp_methods( from litellm.caching.dual_cache import DualCache from litellm.proxy._types import LiteLLM_JWTAuth from litellm.proxy.auth.user_api_key_auth import user_api_key_auth_builder + from litellm.proxy.mcp_server.mcp_server_manager import global_mcp_server_manager + from litellm.types.mcp import MCPTransport + from litellm.types.mcp_server.mcp_server_manager import MCPServer + + monkeypatch.setattr( # test-quality-ok: the registry is a module singleton read by the budget check; no injection seam + global_mcp_server_manager, + "config_mcp_servers", + { + "paid-id": MCPServer( + server_id="paid-id", + name="paid_tools", + alias="paid_tools", + url="https://example.invalid/mcp", + transport=MCPTransport.http, + mcp_info={"server_name": "paid_tools", "mcp_server_cost_info": {"default_cost_per_query": 0.5}}, + ) + }, + ) jwt_response: Final = { "is_proxy_admin": False, diff --git a/tests/unit/proxy/auth/test_user_api_key_auth_request_flow.py b/tests/unit/proxy/auth/test_user_api_key_auth_request_flow.py index dfc974afc81..3e4ce710930 100644 --- a/tests/unit/proxy/auth/test_user_api_key_auth_request_flow.py +++ b/tests/unit/proxy/auth/test_user_api_key_auth_request_flow.py @@ -13760,6 +13760,27 @@ async def test_lens_oauth2_dispatch_preserves_opt_in_premium_and_route_boundary( assert "No connected db" in error.value.message +def _register_a_charging_mcp_server(monkeypatch: pytest.MonkeyPatch) -> None: + from litellm.proxy.mcp_server.mcp_server_manager import global_mcp_server_manager + from litellm.types.mcp import MCPTransport + from litellm.types.mcp_server.mcp_server_manager import MCPServer + + monkeypatch.setattr( # test-quality-ok: the registry is a module singleton read by the budget check; no injection seam + global_mcp_server_manager, + "config_mcp_servers", + { + "paid-id": MCPServer( + server_id="paid-id", + name="paid_tools", + alias="paid_tools", + url="https://example.invalid/mcp", + transport=MCPTransport.http, + mcp_info={"server_name": "paid_tools", "mcp_server_cost_info": {"default_cost_per_query": 0.5}}, + ) + }, + ) + + @pytest.mark.asyncio @pytest.mark.parametrize( "route, request_data, expect_blocked", @@ -13774,10 +13795,12 @@ async def test_lens_oauth2_dispatch_preserves_opt_in_premium_and_route_boundary( ], ) async def test_exhausted_key_budget_spares_only_zero_spend_mcp_discovery( - route: str, request_data: dict[str, object], expect_blocked: bool + monkeypatch: pytest.MonkeyPatch, route: str, request_data: dict[str, object], expect_blocked: bool ) -> None: from litellm.proxy.utils import hash_token + _register_a_charging_mcp_server(monkeypatch) + api_key: Final = "sk-exhausted-mcp-discovery" hashed_token: Final = hash_token(api_key) user_api_key_cache: Final = DualCache() @@ -14061,10 +14084,12 @@ async def test_exhausted_key_budget_is_waived_only_when_the_served_model_is_free ], ) async def test_should_skip_budget_checks_spares_only_zero_spend_mcp_discovery( - route: str, request_data: dict[str, object], expected: bool + monkeypatch: pytest.MonkeyPatch, route: str, request_data: dict[str, object], expected: bool ) -> None: from litellm.proxy.auth.user_api_key_auth import _should_skip_budget_checks + _register_a_charging_mcp_server(monkeypatch) + assert ( await _should_skip_budget_checks( request_data=request_data, route=route, request=None, llm_router=None, valid_token=UserAPIKeyAuth() diff --git a/tests/unit/proxy/mcp_server/test_mcp_server_tool_calls_and_headers.py b/tests/unit/proxy/mcp_server/test_mcp_server_tool_calls_and_headers.py index 5a4e039c56a..9a32f227a57 100644 --- a/tests/unit/proxy/mcp_server/test_mcp_server_tool_calls_and_headers.py +++ b/tests/unit/proxy/mcp_server/test_mcp_server_tool_calls_and_headers.py @@ -4172,12 +4172,26 @@ def test_jsonrpc_text_has_top_level_method_ignores_nested_method(): ("text", "method"), [ pytest.param('{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}', "tools/list", id="complete"), - pytest.param('{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{"cursor":"' + "c" * 5000, "tools/list", id="cut-after-method"), + pytest.param( + '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{"cursor":"' + "c" * 5000, + "tools/list", + id="cut-after-method", + ), pytest.param('{"jsonrpc":"2.0","params":{"cursor":"' + "c" * 5000, None, id="cut-before-method"), pytest.param('{"jsonrpc":"2.0","id":1,"method":"tools/li', None, id="cut-inside-method"), - pytest.param('{"jsonrpc":"2.0","params":{"method":"tools/list"},"method":"ping"}', "ping", id="nested-method-first"), - pytest.param('{"jsonrpc":"2.0","id":1,"method":"tools\\/list","params":{"cursor":"' + "c" * 5000, "tools/list", id="escaped-slash"), - pytest.param('{"jsonrpc":"2.0","id":1,"method":"\\u0074ools/list","params":{"cursor":"' + "c" * 5000, "tools/list", id="unicode-escape"), + pytest.param( + '{"jsonrpc":"2.0","params":{"method":"tools/list"},"method":"ping"}', "ping", id="nested-method-first" + ), + pytest.param( + '{"jsonrpc":"2.0","id":1,"method":"tools\\/list","params":{"cursor":"' + "c" * 5000, + "tools/list", + id="escaped-slash", + ), + pytest.param( + '{"jsonrpc":"2.0","id":1,"method":"\\u0074ools/list","params":{"cursor":"' + "c" * 5000, + "tools/list", + id="unicode-escape", + ), pytest.param('{"jsonrpc":"2.0","id":1,"method":"bad\\x","params":{}}', None, id="invalid-escape"), pytest.param('{"jsonrpc":"2.0","id":1,"method":7,"params":{}}', None, id="not-a-string"), pytest.param('{"jsonrpc":"2.0","id":1,"result":{"method":"tools/list"}}', None, id="response"), @@ -12359,6 +12373,53 @@ async def test_streamable_http_admission_auth_sees_only_peeked_jsonrpc_method() send.assert_not_awaited() +_CUT_TOOL_CALL: Final = ( + b'{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"arguments": {"text": "' + b"a" * 4096 +) + + +@pytest.mark.parametrize( + "body, expected", + [ + pytest.param( + json.dumps( + { + "jsonrpc": "2.0", + "id": 1, + "method": "tools/call", + "params": {"name": "free_tools-echo", "arguments": {"text": "secret"}, "_meta": {"k": "v"}}, + } + ).encode(), + {"method": "tools/call", "params": {"name": "free_tools-echo"}, "mcp_servers": ["paid_tools"]}, + id="tool-call-keeps-only-the-method-the-tool-name-and-the-server-scope", + ), + pytest.param( + json.dumps({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": 7}}).encode(), + {"method": "tools/call"}, + id="tool-call-with-a-non-string-name-keeps-only-the-method", + ), + pytest.param( + json.dumps({"jsonrpc": "2.0", "id": 1, "method": "resources/read", "params": {"name": "x"}}).encode(), + {"method": "resources/read"}, + id="other-methods-never-carry-params", + ), + pytest.param(_CUT_TOOL_CALL, {"method": "tools/call"}, id="tool-call-cut-by-the-peek-keeps-its-method"), + pytest.param(b'{"method": "tools/call", "params": {"na', {}, id="short-malformed-body-carries-nothing"), + pytest.param( + b'{"jsonrpc": "2.0", "id": 1, "method": "ping", "params": {"padding": "' + b"a" * 4096, + {}, + id="other-method-cut-by-the-peek-carries-nothing-because-a-later-method-key-would-win", + ), + ], +) +def test_admission_auth_body_carries_the_method_and_only_the_name_of_a_tool_call( + body: bytes, expected: dict[str, object] +) -> None: + from litellm.proxy.mcp_server.server import _admission_auth_body + + assert json.loads(_admission_auth_body(body, scoped_to=("paid_tools",))) == expected + + @pytest.mark.asyncio @pytest.mark.parametrize( "payload, valid",