-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Feat/mcp tools list debug improvement #15180
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
uc4w6c
wants to merge
2
commits into
BerriAI:main
Choose a base branch
from
uc4w6c:feat/mcp-tools-list-debug-improvement
base: main
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
115 changes: 115 additions & 0 deletions
115
tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_rest_endpoints.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,115 @@ | ||
from unittest.mock import AsyncMock, MagicMock, patch | ||
|
||
import pytest | ||
from fastapi import HTTPException | ||
|
||
@pytest.mark.asyncio | ||
async def test_list_tool_rest_api_raises_http_exception_on_server_failure(): | ||
from litellm.proxy._experimental.mcp_server.rest_endpoints import list_tool_rest_api | ||
from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import ( | ||
MCPRequestHandler, | ||
) | ||
|
||
mock_request = MagicMock() | ||
mock_request.headers = {} | ||
mock_user_api_key_dict = MagicMock() | ||
|
||
with patch.object( | ||
MCPRequestHandler, "_get_mcp_auth_header_from_headers" | ||
) as mock_get_auth: | ||
with patch.object( | ||
MCPRequestHandler, "_get_mcp_server_auth_headers_from_headers" | ||
) as mock_get_server_auth: | ||
mock_get_auth.return_value = None | ||
mock_get_server_auth.return_value = {} | ||
|
||
with patch( | ||
"litellm.proxy._experimental.mcp_server.rest_endpoints.global_mcp_server_manager" | ||
) as mock_manager: | ||
mock_server = MagicMock() | ||
mock_server.name = "test-server" | ||
mock_server.alias = "test-server" | ||
mock_server.server_name = "test-server" | ||
mock_server.mcp_info = {"server_name": "test-server"} | ||
|
||
mock_manager.get_mcp_server_by_id.return_value = mock_server | ||
|
||
failing_get_tools = AsyncMock(side_effect=Exception("backend failure")) | ||
|
||
with patch( | ||
"litellm.proxy._experimental.mcp_server.rest_endpoints._get_tools_for_single_server", | ||
failing_get_tools, | ||
): | ||
with pytest.raises(HTTPException) as exc_info: | ||
await list_tool_rest_api( | ||
request=mock_request, | ||
server_id="test-server", | ||
user_api_key_dict=mock_user_api_key_dict, | ||
) | ||
|
||
exc = exc_info.value | ||
assert exc.status_code == 500 | ||
assert exc.detail["error"] == "server_error" | ||
assert exc.detail["tools"] == [] | ||
assert "An unexpected error occurred" in exc.detail["message"] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_list_tool_rest_api_returns_tools_successfully(): | ||
from litellm.proxy._experimental.mcp_server.rest_endpoints import list_tool_rest_api | ||
from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import ( | ||
MCPRequestHandler, | ||
) | ||
from litellm.proxy._experimental.mcp_server.server import ( | ||
ListMCPToolsRestAPIResponseObject, | ||
) | ||
|
||
mock_request = MagicMock() | ||
mock_request.headers = { | ||
"authorization": "Bearer user_token", | ||
"x-mcp-authorization": "Bearer default_token", | ||
} | ||
mock_user_api_key_dict = MagicMock() | ||
|
||
with patch.object( | ||
MCPRequestHandler, "_get_mcp_auth_header_from_headers" | ||
) as mock_get_auth: | ||
with patch.object( | ||
MCPRequestHandler, "_get_mcp_server_auth_headers_from_headers" | ||
) as mock_get_server_auth: | ||
mock_get_auth.return_value = "Bearer default_token" | ||
mock_get_server_auth.return_value = {} | ||
|
||
with patch( | ||
"litellm.proxy._experimental.mcp_server.rest_endpoints.global_mcp_server_manager" | ||
) as mock_manager: | ||
mock_server = MagicMock() | ||
mock_server.name = "test-server" | ||
mock_server.alias = "test-server" | ||
mock_server.server_name = "test-server" | ||
mock_server.mcp_info = {"server_name": "test-server"} | ||
|
||
mock_manager.get_mcp_server_by_id.return_value = mock_server | ||
|
||
tool = ListMCPToolsRestAPIResponseObject( | ||
name="send_email", | ||
description="Send an email", | ||
inputSchema={"type": "object"}, | ||
mcp_info={"server_name": "test-server"}, | ||
) | ||
|
||
with patch( | ||
"litellm.proxy._experimental.mcp_server.rest_endpoints._get_tools_for_single_server", | ||
AsyncMock(return_value=[tool]), | ||
) as mock_get_tools: | ||
result = await list_tool_rest_api( | ||
request=mock_request, | ||
server_id="test-server", | ||
user_api_key_dict=mock_user_api_key_dict, | ||
) | ||
|
||
assert result["error"] is None | ||
assert result["message"] == "Successfully retrieved tools" | ||
assert len(result["tools"]) == 1 | ||
assert result["tools"][0].name == "send_email" | ||
mock_get_tools.assert_awaited_once() |
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
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.
can we raise the raw error instead? @uc4w6c
this will make it easier to debug on the UI why the call is failing. instead of requiring the user to get access to server logs to see what's going on. e.g. a malformed header to the mcp server.
Uh oh!
There was an error while loading. Please reload this page.
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.
@krrishdholakia
When connecting to the MCP server, a CancelledError is raised inside the aenter call, which overwrites the original exception.
As a result, the error message becomes meaningless (as shown in this capture), so I handled it this way instead.
If you know a way to fix this, I’d appreciate your advice.
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.
ack - let me see how i can raise this