Repository navigation
fix(mcp): run pre_call_tool_check on OpenAPI/local-registry path (VERIA-7) #27016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yuneng-berri
merged 2 commits into
BerriAI:litellm_internal_staging
from
stuxf:fix/mcp-openapi-tool-auth-bypass
May 2, 2026
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
tests/test_litellm/proxy/_experimental/mcp_server/test_openapi_tool_auth.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| """ | ||
| VERIA-7 regression: OpenAPI-backed (local-registry) MCP tools must run | ||
| through `pre_call_tool_check` before dispatch, the same as managed | ||
| MCP server tools. | ||
| """ | ||
|
|
||
| from datetime import datetime, timezone | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuth | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_openapi_local_tool_runs_pre_call_tool_check(): | ||
| """When `execute_mcp_tool` resolves a local-registry (OpenAPI) tool | ||
| AND a server, the pre-call hook must fire before the local handler | ||
| runs. Pre-fix this path skipped the hook entirely.""" | ||
| from litellm.proxy._experimental.mcp_server import server as mcp_module | ||
|
|
||
| user = UserAPIKeyAuth( | ||
| api_key="sk-user", | ||
| user_id="alice", | ||
| user_role=LitellmUserRoles.INTERNAL_USER.value, | ||
| ) | ||
|
|
||
| fake_server = MagicMock() | ||
| fake_server.name = "openapi-petstore" | ||
| fake_server.is_byok = False | ||
| fake_server.auth_type = None | ||
| fake_server.mcp_info = None | ||
| fake_server.server_id = "srv-1" | ||
| fake_server.server_name = "openapi-petstore" | ||
|
|
||
| fake_tool = MagicMock() | ||
| fake_tool.name = "list_pets" | ||
|
|
||
| pre_call = AsyncMock(return_value={}) | ||
| handle_local = AsyncMock(return_value=[]) | ||
|
|
||
| with ( | ||
| patch.object( | ||
| mcp_module.global_mcp_server_manager, | ||
| "_get_mcp_server_from_tool_name", | ||
| return_value=fake_server, | ||
| ), | ||
| patch.object( | ||
| mcp_module.global_mcp_server_manager, | ||
| "pre_call_tool_check", | ||
| new=pre_call, | ||
| ), | ||
| patch.object( | ||
| mcp_module.global_mcp_tool_registry, | ||
| "get_tool", | ||
| return_value=fake_tool, | ||
| ), | ||
| patch( | ||
| "litellm.proxy._experimental.mcp_server.server._handle_local_mcp_tool", | ||
| new=handle_local, | ||
| ), | ||
| patch( | ||
| "litellm.proxy._experimental.mcp_server.server.MCPRequestHandler.is_tool_allowed", | ||
| return_value=True, | ||
| ), | ||
| ): | ||
| await mcp_module.execute_mcp_tool( | ||
| name="list_pets", | ||
| arguments={"limit": 10}, | ||
| allowed_mcp_servers=[fake_server], | ||
| start_time=datetime.now(timezone.utc), | ||
| user_api_key_auth=user, | ||
| ) | ||
|
|
||
| pre_call.assert_awaited_once() | ||
| handle_local.assert_awaited_once() | ||
|
|
||
| # The pre-call hook must run before _handle_local_mcp_tool so an | ||
| # unauthorized tool is blocked before any work runs. AsyncMock | ||
| # records call order indirectly — we already asserted both were | ||
| # called; the relative ordering is enforced by the source change. | ||
| pre_call_kwargs = pre_call.await_args.kwargs | ||
| assert pre_call_kwargs["name"] == "list_pets" | ||
| assert pre_call_kwargs["server"] is fake_server | ||
| assert pre_call_kwargs["user_api_key_auth"] is user | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_openapi_local_tool_blocked_when_pre_call_check_raises(): | ||
| """If the pre-call check raises (caller not authorized for this | ||
| tool), the local handler must NOT be invoked.""" | ||
| from fastapi import HTTPException | ||
|
|
||
| from litellm.proxy._experimental.mcp_server import server as mcp_module | ||
|
|
||
| user = UserAPIKeyAuth( | ||
| api_key="sk-user", | ||
| user_id="alice", | ||
| user_role=LitellmUserRoles.INTERNAL_USER.value, | ||
| ) | ||
|
|
||
| fake_server = MagicMock() | ||
| fake_server.name = "openapi-petstore" | ||
| fake_server.is_byok = False | ||
| fake_server.auth_type = None | ||
| fake_server.mcp_info = None | ||
| fake_server.server_id = "srv-1" | ||
| fake_server.server_name = "openapi-petstore" | ||
|
|
||
| fake_tool = MagicMock() | ||
| fake_tool.name = "delete_pet" | ||
|
|
||
| pre_call = AsyncMock( | ||
| side_effect=HTTPException(status_code=403, detail="not allowed") | ||
| ) | ||
| handle_local = AsyncMock(return_value=[]) | ||
|
|
||
| with ( | ||
| patch.object( | ||
| mcp_module.global_mcp_server_manager, | ||
| "_get_mcp_server_from_tool_name", | ||
| return_value=fake_server, | ||
| ), | ||
| patch.object( | ||
| mcp_module.global_mcp_server_manager, | ||
| "pre_call_tool_check", | ||
| new=pre_call, | ||
| ), | ||
| patch.object( | ||
| mcp_module.global_mcp_tool_registry, | ||
| "get_tool", | ||
| return_value=fake_tool, | ||
| ), | ||
| patch( | ||
| "litellm.proxy._experimental.mcp_server.server._handle_local_mcp_tool", | ||
| new=handle_local, | ||
| ), | ||
| patch( | ||
| "litellm.proxy._experimental.mcp_server.server.MCPRequestHandler.is_tool_allowed", | ||
| return_value=True, | ||
| ), | ||
| ): | ||
| with pytest.raises(HTTPException) as exc: | ||
| await mcp_module.execute_mcp_tool( | ||
| name="delete_pet", | ||
| arguments={}, | ||
| allowed_mcp_servers=[fake_server], | ||
| start_time=datetime.now(timezone.utc), | ||
| user_api_key_auth=user, | ||
| ) | ||
|
|
||
| assert exc.value.status_code == 403 | ||
| pre_call.assert_awaited_once() | ||
| handle_local.assert_not_awaited() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.