-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
fix(responses): fail loudly on MCP gateway failures (initial call, mid-stream, zero resolved tools) #32648
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
Open
tin-berri
wants to merge
15
commits into
litellm_internal_staging
Choose a base branch
from
litellm_fix_mcp_gateway_failure_handling
base: litellm_internal_staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(responses): fail loudly on MCP gateway failures (initial call, mid-stream, zero resolved tools) #32648
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c833b0c
fix(prometheus): bound per-request budget metric emission with a time…
fernando-izar c3fb286
fix: report the blocked LLM response's real token usage (#31217)
seph-barker bfeecc6
feat(guardrails): buffer + cleanly terminate streamed responses on bl…
seph-barker 84de0ce
fix: report real usage on streaming blocks, disable buffered mode for…
Sameerlite 735b14f
style: ruff format after greploop fixes
Sameerlite 576e417
fix: handle Anthropic streaming guardrail blocks
cursoragent 1667cd8
fix(responses): check terminal event type for streaming guardrail end…
Sameerlite 710f6b2
fix: preserve Anthropic blocked stream usage
cursoragent aa48016
fix(responses): surface MCP gateway initial-call failures instead of …
thibault-linktree 70656be
fix(responses): emit terminal error event on MCP tool-execution / fol…
thibault-linktree b6d9d85
fix(responses): reject MCP gateway requests that resolve zero tools
thibault-linktree d181176
fix(responses): monotonic sequence number for terminal error event; d…
thibault-linktree 092bc79
test: trim redundant MCP gateway tests
thibault-linktree 60412fc
Remove deepwiki MCP server configuration
tin-berri d089b2b
Merge litellm_internal_staging into litellm_fix_mcp_gateway_failure_h…
tin-berri 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
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
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
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
145 changes: 145 additions & 0 deletions
145
tests/test_litellm/responses/mcp/test_mcp_empty_resolved_tools.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,145 @@ | ||
| """ | ||
| Guard tests: a request that explicitly asks for MCP tools via the litellm_proxy | ||
| gateway but resolves zero of them (and has no other tools to fall back on) must | ||
| fail loudly with a 400 instead of silently calling the model with no tools — | ||
| which makes it hallucinate, with the only trace being a "success" | ||
| list_mcp_tools spend log with an empty response. | ||
| """ | ||
|
|
||
| import sys | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
|
|
||
| import litellm | ||
| from litellm.responses.mcp.litellm_proxy_mcp_handler import LiteLLM_Proxy_MCP_Handler | ||
| from litellm.types.llms.openai import ResponsesAPIResponse | ||
|
|
||
| # See test_mcp_streaming_iterator.py: look the real submodule up in sys.modules | ||
| # to sidestep litellm.responses being shadowed by the re-exported function. | ||
| responses_main_module = sys.modules["litellm.responses.main"] | ||
|
|
||
| MCP_TOOL = { | ||
| "type": "mcp", | ||
| "server_url": "litellm_proxy/mcp/nonexistent_server", | ||
| "require_approval": "never", | ||
| "allowed_tools": ["get_links"], | ||
| } | ||
|
|
||
|
|
||
| def _patch_resolved_tools(monkeypatch: pytest.MonkeyPatch, resolved_tools: list) -> None: | ||
| monkeypatch.setattr( | ||
| LiteLLM_Proxy_MCP_Handler, | ||
| "_process_mcp_tools_without_openai_transform", | ||
| AsyncMock(return_value=(resolved_tools, {})), | ||
| ) | ||
|
|
||
|
|
||
| def _model_response() -> ResponsesAPIResponse: | ||
| return ResponsesAPIResponse(id="resp-1", created_at=0, output=[]) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_zero_resolved_mcp_tools_raises_before_model_call(monkeypatch): | ||
| # The guard runs before the stream/non-stream branch in | ||
| # aresponses_api_with_mcp, so one case covers both. | ||
| _patch_resolved_tools(monkeypatch, []) | ||
| aresponses_mock = AsyncMock() | ||
| monkeypatch.setattr(responses_main_module, "aresponses", aresponses_mock) | ||
|
|
||
| with pytest.raises(litellm.BadRequestError) as excinfo: | ||
| await responses_main_module.aresponses_api_with_mcp( | ||
| input="how many links do i have?", | ||
| model="gpt-4", | ||
| stream=False, | ||
| tools=[MCP_TOOL], | ||
| ) | ||
|
|
||
| message = str(excinfo.value) | ||
| assert "resolved 0 tools" in message | ||
| assert "litellm_proxy/mcp/nonexistent_server" in message | ||
| assert "allow_all_keys" in message | ||
| # The model was never called without its tools. | ||
| aresponses_mock.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_zero_resolved_mcp_tools_with_function_tools_falls_back(monkeypatch): | ||
| """Mixed requests keep working: with other (function) tools present, the | ||
| request proceeds using those tools instead of hard-failing.""" | ||
| _patch_resolved_tools(monkeypatch, []) | ||
| response = _model_response() | ||
| aresponses_mock = AsyncMock(return_value=response) | ||
| monkeypatch.setattr(responses_main_module, "aresponses", aresponses_mock) | ||
|
|
||
| function_tool = {"type": "function", "name": "my_fn", "parameters": {}} | ||
| result = await responses_main_module.aresponses_api_with_mcp( | ||
| input="hello", | ||
| model="gpt-4", | ||
| stream=False, | ||
| tools=[MCP_TOOL, function_tool], | ||
| ) | ||
|
|
||
| assert result is response | ||
| aresponses_mock.assert_called_once() | ||
| assert aresponses_mock.call_args.kwargs["tools"] == [function_tool] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_zero_resolved_mcp_tools_flag_off_restores_old_behaviour(monkeypatch): | ||
| _patch_resolved_tools(monkeypatch, []) | ||
| response = _model_response() | ||
| aresponses_mock = AsyncMock(return_value=response) | ||
| monkeypatch.setattr(responses_main_module, "aresponses", aresponses_mock) | ||
| monkeypatch.setattr(litellm, "reject_empty_mcp_resolved_tools", False) | ||
|
|
||
| result = await responses_main_module.aresponses_api_with_mcp( | ||
| input="how many links do i have?", | ||
| model="gpt-4", | ||
| stream=False, | ||
| tools=[MCP_TOOL], | ||
| ) | ||
|
|
||
| assert result is response | ||
| aresponses_mock.assert_called_once() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_resolved_mcp_tools_proceed_to_model_call(monkeypatch): | ||
| from mcp.types import Tool as MCPTool | ||
|
|
||
| resolved = [MCPTool(name="get_links", description="List links", inputSchema={"type": "object"})] | ||
| _patch_resolved_tools(monkeypatch, resolved) | ||
|
|
||
| response = _model_response() | ||
| aresponses_mock = AsyncMock(return_value=response) | ||
| monkeypatch.setattr(responses_main_module, "aresponses", aresponses_mock) | ||
|
|
||
| result = await responses_main_module.aresponses_api_with_mcp( | ||
| input="how many links do i have?", | ||
| model="gpt-4", | ||
| stream=False, | ||
| tools=[MCP_TOOL], | ||
| ) | ||
|
|
||
| assert result is response | ||
| aresponses_mock.assert_called_once() | ||
| assert aresponses_mock.call_args.kwargs["tools"], "model call must carry the resolved tools" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_request_without_mcp_tools_is_unaffected(monkeypatch): | ||
| """Plain function-tool requests never hit the guard.""" | ||
| response = _model_response() | ||
| aresponses_mock = AsyncMock(return_value=response) | ||
| monkeypatch.setattr(responses_main_module, "aresponses", aresponses_mock) | ||
|
|
||
| result = await responses_main_module.aresponses_api_with_mcp( | ||
| input="hello", | ||
| model="gpt-4", | ||
| stream=False, | ||
| tools=[{"type": "function", "name": "my_fn", "parameters": {}}], | ||
| ) | ||
|
|
||
| assert result is response | ||
| aresponses_mock.assert_called_once() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
server_urlkey, sotool.get("server_url")silently populates the list withNonevalues, making the error message less actionable.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!