fix(mcp): isolate keyword search admission

Keep the existing mcp_tool_call logger semantics untouched so keyword-search admission remains independent of the merge order with the call-accounting change.

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
panjianxing 2026-10-29 21:28:22 +08:00
parent 3eaf2879e4
commit 1a97ccc5af
2 changed files with 31 additions and 13 deletions

View file

@ -359,21 +359,45 @@ async def _build_admitted_mcp_logging_obj(
async def _build_virtual_call_logging_obj(
name: str,
arguments: dict[str, object], # mutable-ok: existing virtual-call request payload
arguments: dict[str, object],
user_api_key_auth: UserAPIKeyAuth,
logging_auth: UserAPIKeyAuth | None = None,
raw_headers: Mapping[str, str] | None = None,
client_ip: str | None = None,
) -> LiteLLMLoggingObj | None:
"""Run the pre-call pipeline for a virtual ``mcp_tool_call``."""
return await _build_admitted_mcp_logging_obj(
name=name,
arguments=arguments,
user_api_key_auth=user_api_key_auth,
logging_auth=logging_auth,
"""Run the pre-call pipeline (guardrails + logging setup) for a virtual
mcp_tool_call so the SSE path spend-logs like the REST path."""
from litellm.proxy.common_request_processing import (
ProxyBaseLLMRequestProcessing,
)
from litellm.proxy.proxy_server import (
general_settings,
proxy_config,
proxy_logging_obj,
)
request: Final = build_synthetic_mcp_request(
path="/mcp/tools/call",
raw_headers=raw_headers,
client_ip=client_ip,
)
pre_call_result: Final = await ProxyBaseLLMRequestProcessing(
data={"name": name, "arguments": arguments}
).common_processing_pre_call_logic(
request=request,
user_api_key_dict=user_api_key_auth,
proxy_config=proxy_config,
route_type=CallTypes.call_mcp_tool.value,
proxy_logging_obj=proxy_logging_obj,
general_settings=general_settings,
logging_auth=logging_auth,
)
_, logging_candidate = TypeAdapter(tuple[dict[str, object], object]).validate_python(pre_call_result)
if not isinstance(logging_candidate, LiteLLMLoggingObj):
verbose_logger.warning("MCP request processing did not return a logging object")
return None
virtual_logging_obj: Final = logging_candidate
return virtual_logging_obj
async def _build_keyword_search_logging_obj(

View file

@ -1077,8 +1077,6 @@ class TestDispatchVirtualMcpTool:
@pytest.mark.asyncio
async def test_routes_agent_search_to_its_handler(self) -> None:
from litellm.proxy.mcp_server import server as srv
uak = UserAPIKeyAuth(api_key="k", object_permission=_make_perm(mcp_tool_search_enabled=True))
with patch( # test-quality-ok: dispatch routing is the subject; the handler is faked like its siblings here
"litellm.proxy.mcp_server.tool_search.handle_agent_search",
@ -1112,8 +1110,6 @@ class TestDispatchVirtualMcpTool:
@pytest.mark.asyncio
async def test_routes_call_with_client_ip(self) -> None:
from litellm.proxy.mcp_server import server as srv
uak = UserAPIKeyAuth(api_key="k", object_permission=_make_perm(mcp_tool_search_enabled=True))
with patch(
"litellm.proxy.mcp_server.tool_search.handle_mcp_tool_call",
@ -1145,8 +1141,6 @@ class TestDispatchVirtualMcpTool:
"""Regression: the SSE dispatch must run the pre-call pipeline and forward
the resulting logging object to handle_mcp_tool_call, otherwise mcp_tool_call
over /mcp/ skips spend logging and guardrails (unlike the REST path)."""
from litellm.proxy.mcp_server import server as srv
uak = UserAPIKeyAuth(api_key="k", object_permission=_make_perm(mcp_tool_search_enabled=True))
sentinel_logging_obj = object()
with (