mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-06 02:48:13 +00:00
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ci: rename fork-flag to unit-flag now that it applies on every event * test: move tests/test_litellm root and small trees into tests/unit Pure renames, no content changes. Follow-up commits in this PR fix references, merge the three files that already existed in tests/unit, keep live-provider tests in tests/test_litellm and wire CI. * test: carry tests/test_litellm conftest isolation into tests/unit Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS, proxy-URL and keychain env, and session-end client cleanup now reset for unit tests too. The environment isolation owns its MonkeyPatch so a test's own monkeypatch is undone before the model-cost teardown runs. * test: merge, split and prune the moved root and small-tree tests Merge batches/test_batch_utils.py and the chat_completions and messages dispatch tests into the files that already existed in tests/unit. Keep the live Gemini interactions tests, the async image-fetch format test and the OpenAI embedding scorer test in tests/test_litellm since they need real network or keys. Put test_router.py under tests/unit/test_router so the existing package no longer shadows it. Delete eight tests the audit found superseded by stronger ones kept in this move. * ci: run the moved root and small-tree tests under their legacy flags Add the misc and responses-caching-types flags to unit_selection.sh and CircleCI, extend enterprise-routing and mcp-integration, and point the legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest and change classifier at the new paths. * test: make the new tests/unit directories packages tests/unit/test_package_layout.py requires every directory to carry an __init__.py, and without one the moved and retained test_litellm_responses_bridge.py modules collide on import. * test: scope the unit socket block to tests/unit in shared sessions The GHA shards collect the legacy test-path and the unit selection in one pytest session. The unit conftest's loopback-only block leaked into legacy modules that reach the network at import. The legacy conftest now lifts the restriction at collect and setup time, and the unit conftest re-applies it when collecting its own modules. * test: move tests/test_litellm/llms into tests/unit/llms Rename-only. Moves the provider tests and the fine-tuning fixtures they load, mirroring the old paths. Follow-up commits merge, split and wire them. * test: merge, split and prune the moved llms tests Merges the Databricks chat transformation tests into the existing unit file, keeps the tests that need real keys or the network in tests/test_litellm, deletes the audited tests a stronger unit test already covers, and points imports at tests.unit.llms. * ci: run the moved llms tests under their legacy flags The Vertex AI and All Other Providers shards keep their legacy test-path for the retained files and add the llm-vertex-ai and llm-other-providers unit selections. CircleCI gets matching unit jobs. * test: make the tests/unit/llms directories packages Adds __init__.py to the moved dirs and drops the legacy ones whose directories no longer hold tests. * test: drop script runners and path hacks the llms split left dangling The __main__ runners in the split openai_like files and the Databricks e2e runner called tests that now live in the other half of the split or were deleted. The retained legacy halves also no longer need sys.path edits. * test: give the shard-script tests their own GITHUB_OUTPUT They only passed where the runner set it. The CircleCI unit job's env allowlist drops it, so the script's redirect failed there. * test: point the router and module-deletion checks at tests/unit router_code_coverage and code_qa_check_tests only searched tests/test_litellm, so the moved router tests no longer counted. The two silent-experiment tests the audit deleted were the only direct callers of those methods; they are replaced with tests that assert the forwarded shadow request and the recursion guard. * test: move tests/test_litellm integrations and secret_managers into tests/unit Rename-only. Mirrors the old paths, including the directory conftests and the prompt and JSON fixtures. Follow-up commits prune and wire them. * test: prune and repoint the moved integrations tests Deletes the 7 audited tests a stronger test in the same tree already covers, imports the TLS sink helpers from their new conftest path, and restores os.environ after each integrations test. Some presets write OTEL_EXPORTER_OTLP_HEADERS straight into os.environ, and without the legacy tree's test ordering that header leaked into the AgentOps tests. * ci: run the moved integrations tests under their legacy flag The integrations GHA shard and a new CircleCI job run the integrations unit selection. secret_managers joins the misc selection. * docs: point integrations and secret_managers references at tests/unit * test: make the moved integrations directories packages * test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path The Databricks e2e file is a manual script whose main() calls the tests that were pruned, so pruning them broke the documented run. It is back to its main version. The SageMaker Nova docstring now points at the file's real location in tests/local_testing. * test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit Rename-only. Mirrors the old paths, including fixtures, the stubtest config and the native-route wheel script. Two files that collide with existing unit files are merged in a follow-up commit. * test: merge, prune and repoint the moved core, routing, responses, caching and rust_bridge tests Merges the two files that collided with existing unit files, folding the legacy extra case into test_is_chat_completion_cached_dict, and deletes the 9 audited tests a stronger test in the same file already covers. Keeps what needs the network in tests/test_litellm: test_tokenizers pulls a tokenizer from the Hugging Face hub, and the gpt2 and r50k_base tokenizer cases download their BPE files. The unit core_utils conftest points TIKTOKEN_CACHE_DIR at litellm's bundled encodings so the rest never depend on import order to stay offline, and FakeSecretVault moves to a shared module so both trees can build it. * ci: run the moved core, routing, responses, caching and rust_bridge tests under their flags core_utils gets a core-utils flag and CircleCI job, and its GHA shard keeps the legacy path for the retained network tests. router_utils and router_strategy join enterprise-routing, responses joins responses-caching-types (minus responses/mcp, which mcp-integration owns), caching joins caching-local and rust_bridge joins misc. The redis-compat, test-rust, stubtest and merge-smoke paths follow the move. * docs: point the Rust crate references at tests/unit * test: make the moved core, routing and rust_bridge directories packages * test: keep the no-loop DualCache batch_get_cache regression test It runs the sync path outside any event loop, which the inside-loop test cannot, so a change that picks the Redis client by loop state would only show up there. * test: keep the job's UNIT_FLAG out of the shard-script tests * fix(url_utils): block 192.0.0.0/24 on every Python patch release * test: move the new budget limiter tests into tests/unit/router_strategy * test: move the new sentry scrubbing tests into tests/unit/litellm_core_utils * test: move the new zerobus tests into tests/unit/integrations * test: make tests/unit/integrations/zerobus a package * test: load litellm's own tiktoken cache setup once instead of resetting it per test --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1481 lines
50 KiB
Python
1481 lines
50 KiB
Python
import json
|
|
import sys
|
|
import types
|
|
|
|
import pytest
|
|
import respx
|
|
from httpx import Response
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import litellm
|
|
from litellm.types.utils import ModelResponse
|
|
|
|
from litellm.responses.mcp import chat_completions_handler
|
|
from litellm.responses.mcp.chat_completions_handler import (
|
|
acompletion_with_mcp,
|
|
)
|
|
from litellm.responses.mcp.litellm_proxy_mcp_handler import (
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
)
|
|
from litellm.responses.utils import ResponsesAPIRequestUtils
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_with_mcp_returns_normal_completion_without_tools(
|
|
monkeypatch,
|
|
):
|
|
mock_acompletion = AsyncMock(return_value="normal_response")
|
|
|
|
with patch("litellm.acompletion", mock_acompletion):
|
|
result = await acompletion_with_mcp(
|
|
model="test-model",
|
|
messages=[],
|
|
tools=None,
|
|
)
|
|
|
|
assert result == "normal_response"
|
|
mock_acompletion.assert_awaited_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_with_mcp_without_auto_execution_calls_model(monkeypatch):
|
|
tools = [{"type": "function", "function": {"name": "tool"}}]
|
|
mock_acompletion = AsyncMock(return_value="ok")
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_use_litellm_mcp_gateway",
|
|
staticmethod(lambda tools: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_parse_mcp_tools",
|
|
staticmethod(lambda tools: (tools, [])),
|
|
)
|
|
|
|
async def mock_process(**_):
|
|
return ([], {})
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_process_mcp_tools_without_openai_transform",
|
|
mock_process,
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_transform_mcp_tools_to_openai",
|
|
staticmethod(lambda *_, **__: ["openai-tool"]),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_auto_execute_tools",
|
|
staticmethod(lambda **_: False),
|
|
)
|
|
captured_secret_fields = {}
|
|
|
|
def mock_extract(**kwargs):
|
|
captured_secret_fields["value"] = kwargs.get("secret_fields")
|
|
return (None, None, None, None)
|
|
|
|
monkeypatch.setattr(
|
|
ResponsesAPIRequestUtils,
|
|
"extract_mcp_headers_from_request",
|
|
staticmethod(mock_extract),
|
|
)
|
|
|
|
with patch("litellm.acompletion", mock_acompletion):
|
|
result = await acompletion_with_mcp(
|
|
model="test-model",
|
|
messages=[],
|
|
tools=tools,
|
|
secret_fields={"api_key": "value"},
|
|
)
|
|
|
|
assert result == "ok"
|
|
mock_acompletion.assert_awaited_once()
|
|
assert mock_acompletion.await_args is not None
|
|
kwargs = mock_acompletion.await_args.kwargs
|
|
assert kwargs.get("_skip_mcp_handler") is True
|
|
assert kwargs.get("tools") == ["openai-tool"]
|
|
assert captured_secret_fields["value"] == {"api_key": "value"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_with_mcp_passes_mcp_server_auth_headers_to_process_tools(
|
|
monkeypatch,
|
|
):
|
|
"""
|
|
Test that MCP auth headers extracted from secret_fields (e.g. x-mcp-linear_config-authorization)
|
|
are passed to _process_mcp_tools_without_openai_transform for dynamic auth when fetching tools.
|
|
"""
|
|
tools = [{"type": "mcp", "server_url": "litellm_proxy"}]
|
|
mock_acompletion = AsyncMock(return_value="ok")
|
|
|
|
captured_process_kwargs = {}
|
|
|
|
async def mock_process(**kwargs):
|
|
captured_process_kwargs.update(kwargs)
|
|
return ([], {})
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_use_litellm_mcp_gateway",
|
|
staticmethod(lambda t: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_parse_mcp_tools",
|
|
staticmethod(lambda t: (t, [])),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_process_mcp_tools_without_openai_transform",
|
|
mock_process,
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_transform_mcp_tools_to_openai",
|
|
staticmethod(lambda *_, **__: ["openai-tool"]),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_auto_execute_tools",
|
|
staticmethod(lambda **_: False),
|
|
)
|
|
|
|
# secret_fields with raw_headers containing MCP auth - extract_mcp_headers_from_request
|
|
# will parse these and pass to _process_mcp_tools_without_openai_transform
|
|
secret_fields = {
|
|
"raw_headers": {
|
|
"x-mcp-linear_config-authorization": "Bearer linear-token",
|
|
},
|
|
}
|
|
|
|
with patch("litellm.acompletion", mock_acompletion):
|
|
await acompletion_with_mcp(
|
|
model="test-model",
|
|
messages=[],
|
|
tools=tools,
|
|
secret_fields=secret_fields,
|
|
)
|
|
|
|
assert captured_process_kwargs["raw_headers"] == secret_fields["raw_headers"]
|
|
assert "mcp_server_auth_headers" in captured_process_kwargs
|
|
mcp_server_auth_headers = captured_process_kwargs["mcp_server_auth_headers"]
|
|
assert mcp_server_auth_headers is not None
|
|
assert "linear_config" in mcp_server_auth_headers
|
|
assert (
|
|
mcp_server_auth_headers["linear_config"]["Authorization"]
|
|
== "Bearer linear-token"
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_with_mcp_auto_exec_performs_follow_up(monkeypatch):
|
|
from litellm.utils import CustomStreamWrapper
|
|
from litellm.types.utils import (
|
|
ModelResponseStream,
|
|
StreamingChoices,
|
|
Delta,
|
|
ChatCompletionDeltaToolCall,
|
|
Function,
|
|
)
|
|
from unittest.mock import MagicMock
|
|
|
|
tools = [{"type": "function", "function": {"name": "tool"}}]
|
|
|
|
# Create mock streaming chunks for initial response
|
|
def create_chunk(content, finish_reason=None, tool_calls=None):
|
|
return ModelResponseStream(
|
|
id="test-stream",
|
|
model="test",
|
|
created=1234567890,
|
|
object="chat.completion.chunk",
|
|
choices=[
|
|
StreamingChoices(
|
|
index=0,
|
|
delta=Delta(
|
|
content=content,
|
|
role="assistant",
|
|
tool_calls=tool_calls,
|
|
),
|
|
finish_reason=finish_reason,
|
|
)
|
|
],
|
|
)
|
|
|
|
initial_chunks = [
|
|
create_chunk(
|
|
"",
|
|
finish_reason="tool_calls",
|
|
tool_calls=[
|
|
ChatCompletionDeltaToolCall(
|
|
id="call-1",
|
|
type="function",
|
|
function=Function(name="tool", arguments="{}"),
|
|
index=0,
|
|
)
|
|
],
|
|
),
|
|
]
|
|
|
|
follow_up_chunks = [
|
|
create_chunk("Hello"),
|
|
create_chunk(" world", finish_reason="stop"),
|
|
]
|
|
|
|
logging_obj = MagicMock()
|
|
logging_obj.model_call_details = {}
|
|
|
|
class InitialStreamingResponse(CustomStreamWrapper):
|
|
def __init__(self):
|
|
super().__init__(
|
|
completion_stream=None,
|
|
model="test",
|
|
logging_obj=logging_obj,
|
|
)
|
|
self.chunks = initial_chunks
|
|
self._index = 0
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self):
|
|
if self._index < len(self.chunks):
|
|
chunk = self.chunks[self._index]
|
|
self._index += 1
|
|
return chunk
|
|
raise StopAsyncIteration
|
|
|
|
class FollowUpStreamingResponse(CustomStreamWrapper):
|
|
def __init__(self):
|
|
super().__init__(
|
|
completion_stream=None,
|
|
model="test",
|
|
logging_obj=logging_obj,
|
|
)
|
|
self.chunks = follow_up_chunks
|
|
self._index = 0
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self):
|
|
if self._index < len(self.chunks):
|
|
chunk = self.chunks[self._index]
|
|
self._index += 1
|
|
return chunk
|
|
raise StopAsyncIteration
|
|
|
|
async def mock_acompletion(**kwargs):
|
|
if kwargs.get("stream", False):
|
|
messages = kwargs.get("messages", [])
|
|
is_follow_up = any(
|
|
msg.get("role") == "tool"
|
|
or (isinstance(msg, dict) and "tool_call_id" in str(msg))
|
|
for msg in messages
|
|
)
|
|
if is_follow_up:
|
|
return FollowUpStreamingResponse()
|
|
else:
|
|
return InitialStreamingResponse()
|
|
# Non-streaming should not happen
|
|
return ModelResponse(
|
|
id="1",
|
|
model="test",
|
|
choices=[],
|
|
created=0,
|
|
object="chat.completion",
|
|
)
|
|
|
|
mock_acompletion_func = AsyncMock(side_effect=mock_acompletion)
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_use_litellm_mcp_gateway",
|
|
staticmethod(lambda tools: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_parse_mcp_tools",
|
|
staticmethod(lambda tools: (tools, [])),
|
|
)
|
|
|
|
async def mock_process(**_):
|
|
return (tools, {"tool": "server"})
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_process_mcp_tools_without_openai_transform",
|
|
mock_process,
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_transform_mcp_tools_to_openai",
|
|
staticmethod(lambda *_, **__: tools),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_auto_execute_tools",
|
|
staticmethod(lambda **_: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_extract_tool_calls_from_chat_response",
|
|
staticmethod(
|
|
lambda **_: [
|
|
{
|
|
"id": "call-1",
|
|
"type": "function",
|
|
"function": {"name": "tool", "arguments": "{}"},
|
|
}
|
|
]
|
|
),
|
|
)
|
|
|
|
async def mock_execute(**_):
|
|
return [{"tool_call_id": "call-1", "result": "executed"}]
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_execute_tool_calls",
|
|
mock_execute,
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_create_follow_up_messages_for_chat",
|
|
staticmethod(
|
|
lambda **_: [
|
|
{"role": "user", "content": "hello"},
|
|
{
|
|
"role": "assistant",
|
|
"tool_calls": [
|
|
{
|
|
"id": "call-1",
|
|
"type": "function",
|
|
"function": {"name": "tool", "arguments": "{}"},
|
|
}
|
|
],
|
|
},
|
|
{
|
|
"role": "tool",
|
|
"tool_call_id": "call-1",
|
|
"name": "tool",
|
|
"content": "executed",
|
|
},
|
|
]
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
ResponsesAPIRequestUtils,
|
|
"extract_mcp_headers_from_request",
|
|
staticmethod(lambda **_: (None, None, None, None)),
|
|
)
|
|
|
|
# Patch litellm.acompletion at module level to catch function-level imports
|
|
with (
|
|
patch("litellm.acompletion", mock_acompletion_func),
|
|
patch.object(
|
|
chat_completions_handler,
|
|
"litellm_acompletion",
|
|
mock_acompletion_func,
|
|
create=True,
|
|
),
|
|
):
|
|
result = await acompletion_with_mcp(
|
|
model="gpt-4o-mini",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
tools=tools,
|
|
stream=True,
|
|
)
|
|
|
|
# Consume the stream to trigger the iterator and follow-up call
|
|
# The initial stream has one chunk with finish_reason="tool_calls"
|
|
# which will trigger tool execution and follow-up call
|
|
chunks = []
|
|
async for chunk in result:
|
|
chunks.append(chunk)
|
|
# After consuming the initial chunk, the follow-up call should be made
|
|
# Break after first chunk since that's when follow-up is triggered
|
|
break
|
|
|
|
# With new implementation, first call should be streaming
|
|
assert mock_acompletion_func.await_count >= 2
|
|
first_call = mock_acompletion_func.await_args_list[0].kwargs
|
|
# First call should be streaming in new implementation
|
|
assert first_call["stream"] is True
|
|
# Find the follow-up call (should have tool role messages)
|
|
follow_up_call = None
|
|
for call in mock_acompletion_func.await_args_list:
|
|
messages = call.kwargs.get("messages", [])
|
|
if messages and any(
|
|
msg.get("role") == "tool" for msg in messages if isinstance(msg, dict)
|
|
):
|
|
follow_up_call = call.kwargs
|
|
break
|
|
assert follow_up_call is not None, "Should have a follow-up call"
|
|
assert follow_up_call["stream"] is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_with_mcp_adds_metadata_to_streaming(monkeypatch):
|
|
"""
|
|
Test that acompletion_with_mcp adds MCP metadata to CustomStreamWrapper
|
|
and it appears in the final chunk's delta.provider_specific_fields.
|
|
"""
|
|
from litellm.utils import CustomStreamWrapper
|
|
from litellm.types.utils import ModelResponseStream, StreamingChoices, Delta
|
|
from litellm.litellm_core_utils.litellm_logging import Logging
|
|
|
|
tools = [{"type": "mcp", "server_url": "litellm_proxy/mcp/local"}]
|
|
openai_tools = [{"type": "function", "function": {"name": "local_search"}}]
|
|
tool_calls = [
|
|
{
|
|
"id": "call-1",
|
|
"type": "function",
|
|
"function": {"name": "local_search", "arguments": "{}"},
|
|
}
|
|
]
|
|
tool_results = [{"tool_call_id": "call-1", "result": "executed"}]
|
|
|
|
# Create mock streaming chunks
|
|
def create_chunk(content, finish_reason=None):
|
|
return ModelResponseStream(
|
|
id="test-stream",
|
|
model="test-model",
|
|
created=1234567890,
|
|
object="chat.completion.chunk",
|
|
choices=[
|
|
StreamingChoices(
|
|
index=0,
|
|
delta=Delta(
|
|
content=content,
|
|
role="assistant",
|
|
),
|
|
finish_reason=finish_reason,
|
|
)
|
|
],
|
|
)
|
|
|
|
chunks = [
|
|
create_chunk("Hello"),
|
|
create_chunk(" world", finish_reason="stop"), # Final chunk
|
|
]
|
|
|
|
# Create a proper CustomStreamWrapper
|
|
from unittest.mock import MagicMock
|
|
|
|
logging_obj = MagicMock()
|
|
logging_obj.model_call_details = {}
|
|
|
|
class MockStreamingResponse(CustomStreamWrapper):
|
|
def __init__(self):
|
|
super().__init__(
|
|
completion_stream=None,
|
|
model="test-model",
|
|
logging_obj=logging_obj,
|
|
)
|
|
self.chunks = chunks
|
|
self._index = 0
|
|
self.sent_last_chunk = False
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self):
|
|
if self._index < len(self.chunks):
|
|
chunk = self.chunks[self._index]
|
|
self._index += 1
|
|
if self._index == len(self.chunks):
|
|
self.sent_last_chunk = True
|
|
# Add mcp_list_tools to first chunk if present
|
|
if not self.sent_first_chunk:
|
|
chunk = self._add_mcp_list_tools_to_first_chunk(chunk)
|
|
self.sent_first_chunk = True
|
|
return chunk
|
|
raise StopAsyncIteration
|
|
|
|
mock_acompletion = AsyncMock(return_value=MockStreamingResponse())
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_use_litellm_mcp_gateway",
|
|
staticmethod(lambda tools: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_parse_mcp_tools",
|
|
staticmethod(lambda tools: (tools, [])),
|
|
)
|
|
|
|
async def mock_process(**_):
|
|
return (tools, {"local_search": "local"})
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_process_mcp_tools_without_openai_transform",
|
|
mock_process,
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_transform_mcp_tools_to_openai",
|
|
staticmethod(lambda *_, **__: openai_tools),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_auto_execute_tools",
|
|
staticmethod(lambda **_: False),
|
|
)
|
|
monkeypatch.setattr(
|
|
ResponsesAPIRequestUtils,
|
|
"extract_mcp_headers_from_request",
|
|
staticmethod(lambda **_: (None, None, None, None)),
|
|
)
|
|
|
|
with patch("litellm.acompletion", mock_acompletion):
|
|
result = await acompletion_with_mcp(
|
|
model="gpt-4o-mini",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
tools=tools,
|
|
stream=True,
|
|
)
|
|
|
|
# Verify result is CustomStreamWrapper
|
|
assert isinstance(result, CustomStreamWrapper)
|
|
|
|
# Verify _hidden_params contains mcp_metadata
|
|
assert hasattr(result, "_hidden_params")
|
|
assert "mcp_metadata" in result._hidden_params
|
|
mcp_metadata = result._hidden_params["mcp_metadata"]
|
|
assert "mcp_list_tools" in mcp_metadata
|
|
assert mcp_metadata["mcp_list_tools"] == openai_tools
|
|
|
|
# Consume the stream and check chunks
|
|
all_chunks = []
|
|
async for chunk in result:
|
|
all_chunks.append(chunk)
|
|
assert len(all_chunks) > 0
|
|
|
|
# Verify mcp_list_tools is in the first chunk
|
|
first_chunk = all_chunks[0]
|
|
assert (
|
|
hasattr(first_chunk, "choices") and first_chunk.choices
|
|
), "First chunk must have choices"
|
|
choice = first_chunk.choices[0]
|
|
assert hasattr(choice, "delta") and choice.delta, "First choice must have delta"
|
|
provider_fields = getattr(choice.delta, "provider_specific_fields", None)
|
|
assert (
|
|
provider_fields is not None
|
|
), f"First chunk should have provider_specific_fields. Delta: {choice.delta}"
|
|
assert (
|
|
"mcp_list_tools" in provider_fields
|
|
), f"First chunk should have mcp_list_tools. Fields: {provider_fields}"
|
|
assert provider_fields["mcp_list_tools"] == openai_tools
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_with_mcp_streaming_initial_call_is_streaming(monkeypatch):
|
|
"""
|
|
Test that acompletion_with_mcp makes the initial LLM call with streaming=True
|
|
when stream=True is requested, instead of making a non-streaming call first.
|
|
"""
|
|
from litellm.utils import CustomStreamWrapper
|
|
from litellm.types.utils import ModelResponseStream, StreamingChoices, Delta
|
|
|
|
tools = [{"type": "mcp", "server_url": "litellm_proxy/mcp/local"}]
|
|
openai_tools = [{"type": "function", "function": {"name": "local_search"}}]
|
|
|
|
# Create mock streaming chunks
|
|
def create_chunk(content, finish_reason=None):
|
|
return ModelResponseStream(
|
|
id="test-stream",
|
|
model="test-model",
|
|
created=1234567890,
|
|
object="chat.completion.chunk",
|
|
choices=[
|
|
StreamingChoices(
|
|
index=0,
|
|
delta=Delta(
|
|
content=content,
|
|
role="assistant",
|
|
),
|
|
finish_reason=finish_reason,
|
|
)
|
|
],
|
|
)
|
|
|
|
chunks = [
|
|
create_chunk("", finish_reason="tool_calls"), # Final chunk with tool_calls
|
|
]
|
|
|
|
# Create a proper CustomStreamWrapper
|
|
from unittest.mock import MagicMock
|
|
|
|
logging_obj = MagicMock()
|
|
logging_obj.model_call_details = {}
|
|
|
|
class MockStreamingResponse(CustomStreamWrapper):
|
|
def __init__(self):
|
|
super().__init__(
|
|
completion_stream=None,
|
|
model="test-model",
|
|
logging_obj=logging_obj,
|
|
)
|
|
self.chunks = chunks
|
|
self._index = 0
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self):
|
|
if self._index < len(self.chunks):
|
|
chunk = self.chunks[self._index]
|
|
self._index += 1
|
|
return chunk
|
|
raise StopAsyncIteration
|
|
|
|
mock_acompletion = AsyncMock(return_value=MockStreamingResponse())
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_use_litellm_mcp_gateway",
|
|
staticmethod(lambda tools: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_parse_mcp_tools",
|
|
staticmethod(lambda tools: (tools, [])),
|
|
)
|
|
|
|
async def mock_process(**_):
|
|
return (tools, {"local_search": "local"})
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_process_mcp_tools_without_openai_transform",
|
|
mock_process,
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_transform_mcp_tools_to_openai",
|
|
staticmethod(lambda *_, **__: openai_tools),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_auto_execute_tools",
|
|
staticmethod(lambda **_: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_extract_tool_calls_from_chat_response",
|
|
staticmethod(
|
|
lambda **_: [
|
|
{
|
|
"id": "call-1",
|
|
"type": "function",
|
|
"function": {"name": "local_search", "arguments": "{}"},
|
|
}
|
|
]
|
|
),
|
|
)
|
|
|
|
async def mock_execute(**_):
|
|
return [{"tool_call_id": "call-1", "result": "executed"}]
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_execute_tool_calls",
|
|
mock_execute,
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_create_follow_up_messages_for_chat",
|
|
staticmethod(
|
|
lambda **_: [
|
|
{"role": "user", "content": "hello"},
|
|
{
|
|
"role": "assistant",
|
|
"tool_calls": [
|
|
{
|
|
"id": "call-1",
|
|
"type": "function",
|
|
"function": {"name": "local_search", "arguments": "{}"},
|
|
}
|
|
],
|
|
},
|
|
{
|
|
"role": "tool",
|
|
"tool_call_id": "call-1",
|
|
"name": "local_search",
|
|
"content": "executed",
|
|
},
|
|
]
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
ResponsesAPIRequestUtils,
|
|
"extract_mcp_headers_from_request",
|
|
staticmethod(lambda **_: (None, None, None, None)),
|
|
)
|
|
|
|
# Patch litellm.acompletion at module level to catch function-level imports
|
|
with (
|
|
patch("litellm.acompletion", mock_acompletion),
|
|
patch.object(
|
|
chat_completions_handler,
|
|
"litellm_acompletion",
|
|
mock_acompletion,
|
|
create=True,
|
|
),
|
|
):
|
|
result = await acompletion_with_mcp(
|
|
model="gpt-4o-mini",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
tools=tools,
|
|
stream=True,
|
|
)
|
|
|
|
# Verify result is CustomStreamWrapper
|
|
assert isinstance(result, CustomStreamWrapper)
|
|
|
|
# Verify that the first call was made with stream=True
|
|
assert mock_acompletion.await_count >= 1
|
|
first_call = mock_acompletion.await_args_list[0].kwargs
|
|
assert (
|
|
first_call["stream"] is True
|
|
), "First call should be streaming with new implementation"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_with_mcp_streaming_metadata_in_correct_chunks(monkeypatch):
|
|
"""
|
|
Test that MCP metadata is added to the correct chunks:
|
|
- mcp_list_tools should be in the first chunk
|
|
- mcp_tool_calls and mcp_call_results should be in the final chunk of initial response
|
|
"""
|
|
from litellm.utils import CustomStreamWrapper
|
|
from litellm.types.utils import (
|
|
ModelResponseStream,
|
|
StreamingChoices,
|
|
Delta,
|
|
ChatCompletionDeltaToolCall,
|
|
Function,
|
|
)
|
|
|
|
tools = [{"type": "mcp", "server_url": "litellm_proxy/mcp/local"}]
|
|
openai_tools = [{"type": "function", "function": {"name": "local_search"}}]
|
|
tool_calls = [
|
|
{
|
|
"id": "call-1",
|
|
"type": "function",
|
|
"function": {"name": "local_search", "arguments": "{}"},
|
|
}
|
|
]
|
|
tool_results = [{"tool_call_id": "call-1", "result": "executed"}]
|
|
|
|
# Create mock streaming chunks
|
|
def create_chunk(content, finish_reason=None, tool_calls=None):
|
|
return ModelResponseStream(
|
|
id="test-stream",
|
|
model="test-model",
|
|
created=1234567890,
|
|
object="chat.completion.chunk",
|
|
choices=[
|
|
StreamingChoices(
|
|
index=0,
|
|
delta=Delta(
|
|
content=content,
|
|
role="assistant",
|
|
tool_calls=tool_calls,
|
|
),
|
|
finish_reason=finish_reason,
|
|
)
|
|
],
|
|
)
|
|
|
|
initial_chunks = [
|
|
create_chunk(
|
|
"",
|
|
finish_reason="tool_calls",
|
|
tool_calls=[
|
|
ChatCompletionDeltaToolCall(
|
|
id="call-1",
|
|
type="function",
|
|
function=Function(name="local_search", arguments="{}"),
|
|
index=0,
|
|
)
|
|
],
|
|
), # Final chunk with tool_calls
|
|
]
|
|
|
|
follow_up_chunks = [
|
|
create_chunk("Hello"),
|
|
create_chunk(" world", finish_reason="stop"),
|
|
]
|
|
|
|
# Create a proper CustomStreamWrapper
|
|
from unittest.mock import MagicMock
|
|
|
|
logging_obj = MagicMock()
|
|
logging_obj.model_call_details = {}
|
|
|
|
class InitialStreamingResponse(CustomStreamWrapper):
|
|
def __init__(self):
|
|
super().__init__(
|
|
completion_stream=None,
|
|
model="test-model",
|
|
logging_obj=logging_obj,
|
|
)
|
|
self.chunks = initial_chunks
|
|
self._index = 0
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self):
|
|
if self._index < len(self.chunks):
|
|
chunk = self.chunks[self._index]
|
|
self._index += 1
|
|
return chunk
|
|
raise StopAsyncIteration
|
|
|
|
class FollowUpStreamingResponse(CustomStreamWrapper):
|
|
def __init__(self):
|
|
super().__init__(
|
|
completion_stream=None,
|
|
model="test-model",
|
|
logging_obj=logging_obj,
|
|
)
|
|
self.chunks = follow_up_chunks
|
|
self._index = 0
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self):
|
|
if self._index < len(self.chunks):
|
|
chunk = self.chunks[self._index]
|
|
self._index += 1
|
|
return chunk
|
|
raise StopAsyncIteration
|
|
|
|
acompletion_calls = []
|
|
|
|
async def mock_acompletion(**kwargs):
|
|
acompletion_calls.append(kwargs)
|
|
if kwargs.get("stream", False):
|
|
messages = kwargs.get("messages", [])
|
|
is_follow_up = any(
|
|
msg.get("role") == "tool"
|
|
or (isinstance(msg, dict) and "tool_call_id" in str(msg))
|
|
for msg in messages
|
|
)
|
|
if is_follow_up:
|
|
return FollowUpStreamingResponse()
|
|
else:
|
|
return InitialStreamingResponse()
|
|
pytest.fail("Non-streaming call should not happen with new implementation")
|
|
|
|
mock_acompletion_func = AsyncMock(side_effect=mock_acompletion)
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_use_litellm_mcp_gateway",
|
|
staticmethod(lambda tools: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_parse_mcp_tools",
|
|
staticmethod(lambda tools: (tools, [])),
|
|
)
|
|
|
|
async def mock_process(**_):
|
|
return (tools, {"local_search": "local"})
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_process_mcp_tools_without_openai_transform",
|
|
mock_process,
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_transform_mcp_tools_to_openai",
|
|
staticmethod(lambda *_, **__: openai_tools),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_auto_execute_tools",
|
|
staticmethod(lambda **_: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_extract_tool_calls_from_chat_response",
|
|
staticmethod(lambda **_: tool_calls),
|
|
)
|
|
|
|
async def mock_execute(**_):
|
|
return tool_results
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_execute_tool_calls",
|
|
mock_execute,
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_create_follow_up_messages_for_chat",
|
|
staticmethod(
|
|
lambda **_: [
|
|
{"role": "user", "content": "hello"},
|
|
{
|
|
"role": "assistant",
|
|
"tool_calls": [
|
|
{
|
|
"id": "call-1",
|
|
"type": "function",
|
|
"function": {"name": "local_search", "arguments": "{}"},
|
|
}
|
|
],
|
|
},
|
|
{
|
|
"role": "tool",
|
|
"tool_call_id": "call-1",
|
|
"name": "local_search",
|
|
"content": "executed",
|
|
},
|
|
]
|
|
),
|
|
)
|
|
monkeypatch.setattr(
|
|
ResponsesAPIRequestUtils,
|
|
"extract_mcp_headers_from_request",
|
|
staticmethod(lambda **_: (None, None, None, None)),
|
|
)
|
|
|
|
# Patch litellm.acompletion at module level to catch function-level imports
|
|
with (
|
|
patch("litellm.acompletion", mock_acompletion_func),
|
|
patch.object(
|
|
chat_completions_handler,
|
|
"litellm_acompletion",
|
|
side_effect=mock_acompletion,
|
|
create=True,
|
|
),
|
|
):
|
|
result = await acompletion_with_mcp(
|
|
model="gpt-4o-mini",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
tools=tools,
|
|
stream=True,
|
|
)
|
|
|
|
# Verify result is CustomStreamWrapper
|
|
assert isinstance(result, CustomStreamWrapper)
|
|
|
|
# Consume the stream and verify metadata placement
|
|
# NOTE: Stream consumption must be inside the patch context to avoid real API calls
|
|
all_chunks = []
|
|
async for chunk in result:
|
|
all_chunks.append(chunk)
|
|
assert len(all_chunks) > 0
|
|
|
|
# Find first chunk and final chunk from initial response
|
|
# mcp_list_tools is added to the first chunk (all_chunks[0])
|
|
first_chunk = all_chunks[0] if all_chunks else None
|
|
initial_final_chunk = None
|
|
|
|
for chunk in all_chunks:
|
|
if hasattr(chunk, "choices") and chunk.choices:
|
|
choice = chunk.choices[0]
|
|
if (
|
|
hasattr(choice, "finish_reason")
|
|
and choice.finish_reason == "tool_calls"
|
|
):
|
|
initial_final_chunk = chunk
|
|
|
|
assert first_chunk is not None, "Should have a first chunk"
|
|
assert (
|
|
initial_final_chunk is not None
|
|
), "Should have a final chunk from initial response"
|
|
|
|
# Verify mcp_list_tools is in the first chunk
|
|
assert (
|
|
hasattr(first_chunk, "choices") and first_chunk.choices
|
|
), "First chunk must have choices"
|
|
first_choice = first_chunk.choices[0]
|
|
assert (
|
|
hasattr(first_choice, "delta") and first_choice.delta
|
|
), "First choice must have delta"
|
|
first_provider_fields = getattr(
|
|
first_choice.delta, "provider_specific_fields", None
|
|
)
|
|
assert (
|
|
first_provider_fields is not None
|
|
), "First chunk should have provider_specific_fields"
|
|
assert (
|
|
"mcp_list_tools" in first_provider_fields
|
|
), "First chunk should have mcp_list_tools"
|
|
|
|
# Verify mcp_tool_calls and mcp_call_results are in the final chunk of initial response
|
|
assert (
|
|
hasattr(initial_final_chunk, "choices") and initial_final_chunk.choices
|
|
), "Final chunk must have choices"
|
|
final_choice = initial_final_chunk.choices[0]
|
|
assert (
|
|
hasattr(final_choice, "delta") and final_choice.delta
|
|
), "Final choice must have delta"
|
|
final_provider_fields = getattr(
|
|
final_choice.delta, "provider_specific_fields", None
|
|
)
|
|
assert (
|
|
final_provider_fields is not None
|
|
), "Final chunk should have provider_specific_fields"
|
|
assert "mcp_tool_calls" in final_provider_fields, "Should have mcp_tool_calls"
|
|
assert (
|
|
"mcp_call_results" in final_provider_fields
|
|
), "Should have mcp_call_results"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_execute_tool_calls_sets_proxy_server_request_arguments(monkeypatch):
|
|
"""
|
|
Test that _execute_tool_calls sets proxy_server_request with arguments in logging_request_data
|
|
so that arguments are available in callbacks.
|
|
"""
|
|
import importlib
|
|
from unittest.mock import MagicMock
|
|
|
|
# Capture the kwargs passed to function_setup
|
|
captured_kwargs = {}
|
|
|
|
def mock_function_setup(original_function, rules_obj, start_time, **kwargs):
|
|
captured_kwargs.update(kwargs)
|
|
# Return a mock logging object
|
|
logging_obj = MagicMock()
|
|
logging_obj.model_call_details = {}
|
|
logging_obj.pre_call = MagicMock()
|
|
logging_obj.post_call = MagicMock()
|
|
logging_obj.async_post_mcp_tool_call_hook = AsyncMock()
|
|
logging_obj.async_success_handler = AsyncMock()
|
|
return logging_obj, kwargs
|
|
|
|
# Mock the MCP server manager
|
|
mock_result = MagicMock()
|
|
mock_result.content = [MagicMock(text="test result")]
|
|
|
|
async def mock_call_tool(**kwargs):
|
|
return mock_result
|
|
|
|
# NOTE: avoid monkeypatch string path here because `litellm.responses` is also
|
|
# exported as a function on the top-level `litellm` package, which can confuse
|
|
# pytest's dotted-path resolver.
|
|
mcp_handler_module = importlib.import_module(
|
|
"litellm.responses.mcp.litellm_proxy_mcp_handler"
|
|
)
|
|
monkeypatch.setattr(mcp_handler_module, "function_setup", mock_function_setup)
|
|
monkeypatch.setattr(
|
|
"litellm.proxy._experimental.mcp_server.mcp_server_manager.global_mcp_server_manager.call_tool",
|
|
mock_call_tool,
|
|
)
|
|
|
|
# Create test data
|
|
tool_calls = [
|
|
{
|
|
"id": "call-1",
|
|
"type": "function",
|
|
"function": {
|
|
"name": "test_tool",
|
|
"arguments": '{"param1": "value1", "param2": 123}',
|
|
},
|
|
}
|
|
]
|
|
tool_server_map = {"test_tool": "test_server"}
|
|
user_api_key_auth = MagicMock()
|
|
user_api_key_auth.api_key = "test_key"
|
|
|
|
# Call _execute_tool_calls
|
|
result = await LiteLLM_Proxy_MCP_Handler._execute_tool_calls(
|
|
tool_server_map=tool_server_map,
|
|
tool_calls=tool_calls,
|
|
user_api_key_auth=user_api_key_auth,
|
|
)
|
|
|
|
# Verify that proxy_server_request was set with arguments
|
|
assert (
|
|
"proxy_server_request" in captured_kwargs
|
|
), "proxy_server_request should be in logging_request_data"
|
|
proxy_server_request = captured_kwargs["proxy_server_request"]
|
|
assert "body" in proxy_server_request, "proxy_server_request should have body"
|
|
assert "name" in proxy_server_request["body"], "body should have name"
|
|
assert "arguments" in proxy_server_request["body"], "body should have arguments"
|
|
assert proxy_server_request["body"]["name"] == "test_tool", "name should match"
|
|
assert proxy_server_request["body"]["arguments"] == {
|
|
"param1": "value1",
|
|
"param2": 123,
|
|
}, "arguments should be parsed correctly"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_with_mcp_streaming_drain_error_does_not_drop_final_chunk(monkeypatch):
|
|
"""
|
|
Regression test: after yielding the final chunk, MCPStreamingIterator drains
|
|
the inner CustomStreamWrapper to fire end-of-stream spend logging. If the
|
|
inner stream raises a non-StopAsyncIteration error during that drain (e.g.
|
|
a transient APIError on the trailing usage chunk), the error must not
|
|
escape __anext__ and drop the already-assembled final chunk.
|
|
"""
|
|
from unittest.mock import MagicMock
|
|
|
|
from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
|
|
from litellm.utils import CustomStreamWrapper
|
|
|
|
tools = [{"type": "mcp", "server_url": "litellm_proxy/mcp/local"}]
|
|
openai_tools = [{"type": "function", "function": {"name": "local_search"}}]
|
|
|
|
def create_chunk(content, finish_reason=None):
|
|
return ModelResponseStream(
|
|
id="test-stream",
|
|
model="test-model",
|
|
created=1234567890,
|
|
object="chat.completion.chunk",
|
|
choices=[
|
|
StreamingChoices(
|
|
index=0,
|
|
delta=Delta(content=content, role="assistant"),
|
|
finish_reason=finish_reason,
|
|
)
|
|
],
|
|
)
|
|
|
|
chunks = [
|
|
create_chunk("Hello"),
|
|
create_chunk(" world", finish_reason="stop"),
|
|
]
|
|
|
|
logging_obj = MagicMock()
|
|
logging_obj.model_call_details = {}
|
|
|
|
class DrainErrorStreamingResponse(CustomStreamWrapper):
|
|
def __init__(self):
|
|
super().__init__(
|
|
completion_stream=None,
|
|
model="test-model",
|
|
logging_obj=logging_obj,
|
|
)
|
|
self.chunks = chunks
|
|
self._index = 0
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self):
|
|
if self._index < len(self.chunks):
|
|
chunk = self.chunks[self._index]
|
|
self._index += 1
|
|
return chunk
|
|
if self._index == len(self.chunks):
|
|
self._index += 1
|
|
raise RuntimeError("connection dropped on trailing usage chunk")
|
|
raise StopAsyncIteration
|
|
|
|
mock_acompletion = AsyncMock(return_value=DrainErrorStreamingResponse())
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_use_litellm_mcp_gateway",
|
|
staticmethod(lambda tools: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_parse_mcp_tools",
|
|
staticmethod(lambda tools: (tools, [])),
|
|
)
|
|
|
|
async def mock_process(**_):
|
|
return (tools, {"local_search": "local"})
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_process_mcp_tools_without_openai_transform",
|
|
mock_process,
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_transform_mcp_tools_to_openai",
|
|
staticmethod(lambda *_, **__: openai_tools),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_auto_execute_tools",
|
|
staticmethod(lambda **_: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_extract_tool_calls_from_chat_response",
|
|
staticmethod(lambda **_: []),
|
|
)
|
|
monkeypatch.setattr(
|
|
ResponsesAPIRequestUtils,
|
|
"extract_mcp_headers_from_request",
|
|
staticmethod(lambda **_: (None, None, None, None)),
|
|
)
|
|
|
|
with patch("litellm.acompletion", mock_acompletion):
|
|
result = await acompletion_with_mcp(
|
|
model="gpt-4o-mini",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
tools=tools,
|
|
stream=True,
|
|
)
|
|
|
|
all_chunks = []
|
|
async for chunk in result:
|
|
all_chunks.append(chunk)
|
|
|
|
final_chunks = [
|
|
chunk
|
|
for chunk in all_chunks
|
|
if chunk.choices and chunk.choices[0].finish_reason == "stop"
|
|
]
|
|
assert len(final_chunks) == 1, f"Final chunk must survive a drain error. Got chunks: {all_chunks}"
|
|
assert all_chunks[-1].choices[0].finish_reason == "stop"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_with_mcp_streaming_drains_inner_stream_after_exhaustion(monkeypatch):
|
|
from unittest.mock import MagicMock
|
|
|
|
from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
|
|
from litellm.utils import CustomStreamWrapper
|
|
|
|
tools = [{"type": "mcp", "server_url": "litellm_proxy/mcp/local"}]
|
|
openai_tools = [{"type": "function", "function": {"name": "local_search"}}]
|
|
|
|
def create_chunk(content):
|
|
return ModelResponseStream(
|
|
id="test-stream",
|
|
model="test-model",
|
|
created=1234567890,
|
|
object="chat.completion.chunk",
|
|
choices=[
|
|
StreamingChoices(
|
|
index=0,
|
|
delta=Delta(content=content, role="assistant"),
|
|
finish_reason=None,
|
|
)
|
|
],
|
|
)
|
|
|
|
chunks = [create_chunk("Hello"), create_chunk(" world")]
|
|
logging_obj = MagicMock()
|
|
logging_obj.model_call_details = {}
|
|
|
|
class ExhaustingStreamingResponse(CustomStreamWrapper):
|
|
def __init__(self):
|
|
super().__init__(
|
|
completion_stream=None,
|
|
model="test-model",
|
|
logging_obj=logging_obj,
|
|
)
|
|
self.chunks = chunks
|
|
self._index = 0
|
|
self.drained_after_exhaustion = False
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self):
|
|
if self._index < len(self.chunks):
|
|
chunk = self.chunks[self._index]
|
|
self._index += 1
|
|
return chunk
|
|
if self._index == len(self.chunks):
|
|
self._index += 1
|
|
raise StopAsyncIteration
|
|
self.drained_after_exhaustion = True
|
|
raise StopAsyncIteration
|
|
|
|
initial_stream = ExhaustingStreamingResponse()
|
|
mock_acompletion = AsyncMock(return_value=initial_stream)
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_use_litellm_mcp_gateway",
|
|
staticmethod(lambda tools: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_parse_mcp_tools",
|
|
staticmethod(lambda tools: (tools, [])),
|
|
)
|
|
|
|
async def mock_process(**_):
|
|
return (tools, {"local_search": "local"})
|
|
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_process_mcp_tools_without_openai_transform",
|
|
mock_process,
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_transform_mcp_tools_to_openai",
|
|
staticmethod(lambda *_, **__: openai_tools),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_should_auto_execute_tools",
|
|
staticmethod(lambda **_: True),
|
|
)
|
|
monkeypatch.setattr(
|
|
LiteLLM_Proxy_MCP_Handler,
|
|
"_extract_tool_calls_from_chat_response",
|
|
staticmethod(lambda **_: []),
|
|
)
|
|
monkeypatch.setattr(
|
|
ResponsesAPIRequestUtils,
|
|
"extract_mcp_headers_from_request",
|
|
staticmethod(lambda **_: (None, None, None, None)),
|
|
)
|
|
|
|
with patch("litellm.acompletion", mock_acompletion):
|
|
result = await acompletion_with_mcp(
|
|
model="gpt-4o-mini",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
tools=tools,
|
|
stream=True,
|
|
)
|
|
|
|
all_chunks = []
|
|
async for chunk in result:
|
|
all_chunks.append(chunk)
|
|
|
|
assert len(all_chunks) == 3
|
|
assert initial_stream.drained_after_exhaustion is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@respx.mock
|
|
async def test_acompletion_with_mcp_forwards_unserved_external_mcp_tool_to_the_provider(monkeypatch):
|
|
from litellm.proxy._experimental.mcp_server.mcp_server_manager import global_mcp_server_manager
|
|
|
|
zapier_tool = {"type": "mcp", "server_label": "zapier", "server_url": "https://mcp.zapier.com/api/mcp/mcp"}
|
|
monkeypatch.setitem(sys.modules, "litellm.proxy.proxy_server", types.SimpleNamespace(prisma_client=None))
|
|
monkeypatch.setattr(global_mcp_server_manager, "get_registry", lambda: {})
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
provider = respx.post("https://api.openai.com/v1/chat/completions").mock(
|
|
return_value=Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-zapier",
|
|
"object": "chat.completion",
|
|
"created": 0,
|
|
"model": "gpt-4.1",
|
|
"choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
|
},
|
|
)
|
|
)
|
|
|
|
result = await acompletion_with_mcp(
|
|
model="openai/gpt-4.1",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
tools=[zapier_tool],
|
|
api_key="sk-test",
|
|
acompletion=True,
|
|
)
|
|
|
|
assert isinstance(result, ModelResponse)
|
|
assert result.id == "chatcmpl-zapier"
|
|
assert json.loads(provider.calls.last.request.content)["tools"] == [zapier_tool]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("selected", [False, True])
|
|
@pytest.mark.parametrize("stream", [False, True])
|
|
@pytest.mark.parametrize("selection_source", ["metadata", "litellm_metadata", "body"])
|
|
@pytest.mark.parametrize("logging_failure", [False, True])
|
|
async def test_request_selected_mcp_guardrail_blocks_before_upstream(monkeypatch, selected, stream, selection_source, logging_failure):
|
|
from litellm.exceptions import GuardrailRaisedException
|
|
from mcp.types import Tool
|
|
from litellm.caching.caching import DualCache
|
|
from litellm.integrations.custom_guardrail import CustomGuardrail
|
|
from litellm.proxy import proxy_server
|
|
from litellm.proxy._types import UserAPIKeyAuth, LiteLLM_ObjectPermissionTable
|
|
from litellm.proxy._experimental.mcp_server import mcp_server_manager, server, tool_registry
|
|
from litellm.proxy._experimental.mcp_server.faults.list_outcomes import AggregateToolListing
|
|
from litellm.proxy.utils import ProxyLogging
|
|
from litellm.types.guardrails import GuardrailEventHooks
|
|
from litellm.types.mcp_server.mcp_server_manager import MCPServer
|
|
|
|
class BlockSelected(CustomGuardrail):
|
|
async def async_pre_call_hook(self, user_api_key_dict, cache, data, call_type):
|
|
if self.should_run_guardrail(data, GuardrailEventHooks.pre_mcp_call):
|
|
raise GuardrailRaisedException(message="request-selected MCP block", blocked_content=True)
|
|
return data
|
|
|
|
guardrail = BlockSelected(guardrail_name="block-all", event_hook="pre_mcp_call", default_on=False)
|
|
monkeypatch.setattr(litellm, "callbacks", [guardrail])
|
|
manager = mcp_server_manager.MCPServerManager()
|
|
manager.registry = {"observer": MCPServer(
|
|
server_id="observer", name="observer", server_name="observer", transport="http",
|
|
url="https://observer.example/mcp", spec_path="observer.json", auth_type="none",
|
|
)}
|
|
manager.tool_name_to_mcp_server_name_mapping = {"observer-execute": "observer"}
|
|
upstream = AsyncMock(return_value={"executed": True})
|
|
registry = tool_registry.MCPToolRegistry()
|
|
registry.register_tool("observer-execute", "Execute", {"type": "object"}, upstream)
|
|
monkeypatch.setattr(tool_registry, "global_mcp_tool_registry", registry)
|
|
monkeypatch.setattr(mcp_server_manager, "global_mcp_server_manager", manager)
|
|
monkeypatch.setattr(proxy_server, "proxy_logging_obj", ProxyLogging(user_api_key_cache=DualCache()))
|
|
monkeypatch.setattr(server, "_get_tools_from_mcp_servers", AsyncMock(return_value=AggregateToolListing(
|
|
tools=[Tool(name="observer-execute", inputSchema={"type": "object"})], outcomes={}
|
|
)))
|
|
responses = [
|
|
ModelResponse(choices=[{"message": {"role": "assistant", "content": None, "tool_calls": [
|
|
{"id": "call-1", "type": "function", "function": {"name": "observer-execute", "arguments": "{}"}}
|
|
]}, "finish_reason": "tool_calls"}]),
|
|
ModelResponse(choices=[{"message": {"role": "assistant", "content": "done"}}]),
|
|
]
|
|
if stream:
|
|
from litellm.types.utils import ModelResponseStream
|
|
responses = [
|
|
await litellm.acompletion(
|
|
model="openai/gpt-5", messages=[{"role": "user", "content": "execute"}], stream=True,
|
|
mock_response=ModelResponseStream(choices=[{"index": 0, "delta": {
|
|
"role": "assistant", "content": None, "tool_calls": [{
|
|
"index": 0, "id": "call-1", "type": "function",
|
|
"function": {"name": "observer-execute", "arguments": "{}"},
|
|
}],
|
|
}, "finish_reason": "tool_calls"}]),
|
|
),
|
|
await litellm.acompletion(
|
|
model="openai/gpt-5", messages=[{"role": "user", "content": "done"}],
|
|
stream=True, mock_response="done",
|
|
),
|
|
]
|
|
if logging_failure:
|
|
from litellm.responses.mcp import litellm_proxy_mcp_handler
|
|
def fail_logging(*args, **kwargs):
|
|
raise RuntimeError("logging initialization failed")
|
|
monkeypatch.setattr(litellm_proxy_mcp_handler, "function_setup", fail_logging)
|
|
model_call = AsyncMock(side_effect=responses)
|
|
monkeypatch.setattr(litellm, "acompletion", model_call)
|
|
result = await acompletion_with_mcp(
|
|
model="test-model", messages=[{"role": "user", "content": "execute"}],
|
|
tools=[{"type": "mcp", "server_url": "litellm_proxy/observer", "require_approval": "never"}],
|
|
stream=stream,
|
|
user_api_key_auth=UserAPIKeyAuth(
|
|
object_permission=LiteLLM_ObjectPermissionTable(object_permission_id="test", mcp_servers=["observer"])
|
|
),
|
|
**({"guardrails": ["block-all"] if selected else []} if selection_source == "body" else {
|
|
selection_source: {"guardrails": ["block-all"] if selected else []}
|
|
}),
|
|
)
|
|
if stream:
|
|
chunks = [chunk async for chunk in result]
|
|
assert chunks
|
|
assert model_call.await_count == 2
|
|
assert upstream.await_count == (0 if selected else 1)
|
|
tool_message = model_call.await_args.kwargs["messages"][-1]
|
|
assert ("request-selected MCP block" in tool_message["content"]) is selected
|