Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions litellm/proxy/_experimental/mcp_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
)
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.auth.ip_address_utils import IPAddressUtils
from litellm.proxy.litellm_pre_call_utils import (
LiteLLMProxyRequestSetup,
)
from litellm.types.mcp import MCPAuth
from litellm.types.mcp_server.mcp_server_manager import MCPInfo, MCPServer
from litellm.types.utils import CallTypes, StandardLoggingMCPToolCall
Expand Down Expand Up @@ -842,6 +845,7 @@ async def _get_tools_from_mcp_servers( # noqa: PLR0915
raw_headers: Optional[Dict[str, str]] = None,
log_list_tools_to_spendlogs: bool = False,
list_tools_log_source: Optional[str] = None,
litellm_trace_id: Optional[str] = None,
) -> List[MCPTool]:
"""
Helper method to fetch tools from MCP servers based on server filtering criteria.
Expand Down Expand Up @@ -879,6 +883,7 @@ async def _get_tools_from_mcp_servers( # noqa: PLR0915
"model": "MCP: list_tools",
"call_type": CallTypes.list_mcp_tools.value,
"litellm_call_id": list_tools_call_id,
"litellm_trace_id": litellm_trace_id,
"metadata": {
"spend_logs_metadata": spend_logs_metadata,
},
Expand All @@ -894,13 +899,14 @@ async def _get_tools_from_mcp_servers( # noqa: PLR0915
],
}

# Attach user identifiers when available (matches call_mcp_tool style)
# Attach user identifiers using the standard helper
if user_api_key_auth is not None:
user_api_key = getattr(user_api_key_auth, "api_key", None)
if user_api_key:
cast(dict, list_tools_request_data["metadata"])[
"user_api_key"
] = user_api_key

LiteLLMProxyRequestSetup.add_user_api_key_auth_to_request_metadata(
data=list_tools_request_data,
user_api_key_dict=user_api_key_auth,
_metadata_variable_name="metadata",
)

user_identifier = getattr(
user_api_key_auth, "end_user_id", None
Expand Down
87 changes: 80 additions & 7 deletions litellm/proxy/spend_tracking/spend_management_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1850,13 +1850,15 @@ def parse_date(date_str: str) -> datetime:

verbose_proxy_logger.debug("data= %s", json.dumps(data, indent=4, default=str))

return {
"data": data,
"total": total_records,
"page": page,
"page_size": page_size,
"total_pages": total_pages,
}
return await _build_ui_spend_logs_response(
prisma_client,
data,
total_records,
page,
page_size,
total_pages,
enrich_session_counts=not is_v2,
)
except Exception as e:
verbose_proxy_logger.exception(f"Error in ui_view_spend_logs: {e}")
raise handle_exception_on_proxy(e)
Expand Down Expand Up @@ -3101,6 +3103,77 @@ async def ui_view_session_spend_logs(
)


async def _build_ui_spend_logs_response(
prisma_client: "PrismaClient",
data: list,
total_records: int,
page: int,
page_size: int,
total_pages: int,
enrich_session_counts: bool = True,
) -> dict:
"""
Build the paginated response for the UI spend-logs endpoint.

When ``enrich_session_counts`` is ``True`` (the default for the v1/UI
endpoint), each row is enriched with ``session_total_count`` so the
frontend knows which sessions are expandable (multi-call sessions).
For every row that carries a ``session_id``, a single ``GROUP BY`` query
fetches the total number of logs in each referenced session. Rows without
a ``session_id`` default to ``1``.

When ``enrich_session_counts`` is ``False`` (v2 endpoint), rows are
serialised without the extra query.

Args:
prisma_client: The connected Prisma client instance.
data: A list of Prisma model instances (must support ``.model_dump()``
and have a ``session_id`` attribute).
total_records: Total number of matching records (for pagination).
page: Current page number.
page_size: Number of items per page.
total_pages: Total number of pages.
enrich_session_counts: Whether to add ``session_total_count`` to each
row. Defaults to ``True``.

Returns:
A dict with ``data`` (enriched rows), ``total``, ``page``,
``page_size``, and ``total_pages``.
"""
count_map: dict[str, int] = {}
if enrich_session_counts:
session_ids = list(
{row.session_id for row in data if getattr(row, "session_id", None)}
)
if session_ids:
counts = await prisma_client.db.litellm_spendlogs.group_by(
by=["session_id"],
where={"session_id": {"in": session_ids}},
count={"session_id": True},
)
Comment on lines +3153 to +3157

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This GROUP BY query runs on every v1 UI page load. The IN clause is bounded by page_size (typically 25–50 distinct session IDs), so it should be fine at moderate scale. Consider a window function in the main query if performance becomes a concern at scale.

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!

count_map = {
r["session_id"]: r["_count"]["session_id"]
for r in counts
if r.get("session_id")
}
Comment thread
ishaan-jaff marked this conversation as resolved.
Resolved

enriched: List[dict] = []
for row in data:
row_dict = row.model_dump()
if enrich_session_counts:
sid = row_dict.get("session_id")
row_dict["session_total_count"] = count_map.get(sid, 1) if sid else 1
Comment thread
ishaan-jaff marked this conversation as resolved.
Resolved
enriched.append(row_dict)

return {
"data": enriched,
"total": total_records,
"page": page,
"page_size": page_size,
"total_pages": total_pages,
}


def _build_status_filter_condition(status_filter: Optional[str]) -> Dict[str, Any]:
"""
Helper function to build the status filter condition for database queries.
Expand Down
1 change: 1 addition & 0 deletions litellm/responses/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ async def aresponses_api_with_mcp(
) = await LiteLLM_Proxy_MCP_Handler._process_mcp_tools_without_openai_transform(
user_api_key_auth=user_api_key_auth,
mcp_tools_with_litellm_proxy=mcp_tools_with_litellm_proxy,
litellm_trace_id=kwargs.get("litellm_trace_id"),
)
openai_tools = LiteLLM_Proxy_MCP_Handler._transform_mcp_tools_to_openai(
original_mcp_tools
Expand Down
11 changes: 9 additions & 2 deletions litellm/responses/mcp/chat_completions_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ async def acompletion_with_mcp( # noqa: PLR0915
) = await LiteLLM_Proxy_MCP_Handler._process_mcp_tools_without_openai_transform(
user_api_key_auth=user_api_key_auth,
mcp_tools_with_litellm_proxy=mcp_tools_with_litellm_proxy,
litellm_trace_id=kwargs.get("litellm_trace_id"),
)

openai_tools = LiteLLM_Proxy_MCP_Handler._transform_mcp_tools_to_openai(
Expand Down Expand Up @@ -235,7 +236,10 @@ async def __aiter__(self):

def _add_mcp_list_tools_to_chunk(self, chunk: ModelResponseStream) -> ModelResponseStream:
"""Add mcp_list_tools to the first chunk."""
from litellm.types.utils import StreamingChoices, add_provider_specific_fields
from litellm.types.utils import (
StreamingChoices,
add_provider_specific_fields,
)

if not self.openai_tools:
return chunk
Expand All @@ -258,7 +262,10 @@ def _add_mcp_list_tools_to_chunk(self, chunk: ModelResponseStream) -> ModelRespo

def _add_mcp_tool_metadata_to_final_chunk(self, chunk: ModelResponseStream) -> ModelResponseStream:
"""Add mcp_tool_calls and mcp_call_results to the final chunk."""
from litellm.types.utils import StreamingChoices, add_provider_specific_fields
from litellm.types.utils import (
StreamingChoices,
add_provider_specific_fields,
)

if hasattr(chunk, "choices") and chunk.choices:
for choice in chunk.choices:
Expand Down
30 changes: 19 additions & 11 deletions litellm/responses/mcp/litellm_proxy_mcp_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
Dict,
Iterable,
List,
Literal,
Optional,
Tuple,
Union,
Literal,
)

from litellm._logging import verbose_logger
Expand All @@ -29,6 +29,7 @@

if TYPE_CHECKING:
from mcp.types import Tool as MCPTool

from litellm.proxy.utils import ProxyLogging
else:
MCPTool = Any
Expand Down Expand Up @@ -97,6 +98,7 @@ def _parse_mcp_tools(
async def _get_mcp_tools_from_manager(
user_api_key_auth: Any,
mcp_tools_with_litellm_proxy: Optional[Iterable[ToolParam]],
litellm_trace_id: Optional[str] = None,
) -> tuple[List[MCPTool], List[str]]:
"""
Get available tools from the MCP server manager.
Expand All @@ -109,13 +111,13 @@ async def _get_mcp_tools_from_manager(
List of MCP tools
List names of allowed MCP servers
"""
from litellm.proxy._experimental.mcp_server.server import (
_get_tools_from_mcp_servers,
_get_allowed_mcp_servers_from_mcp_server_names,
)
from litellm.proxy._experimental.mcp_server.mcp_server_manager import (
global_mcp_server_manager,
)
from litellm.proxy._experimental.mcp_server.server import (
_get_allowed_mcp_servers_from_mcp_server_names,
_get_tools_from_mcp_servers,
)

mcp_servers: List[str] = []
if mcp_tools_with_litellm_proxy:
Expand All @@ -136,6 +138,7 @@ async def _get_mcp_tools_from_manager(
mcp_server_auth_headers=None,
log_list_tools_to_spendlogs=True,
list_tools_log_source="responses",
litellm_trace_id=litellm_trace_id,
)
allowed_mcp_server_ids = (
await global_mcp_server_manager.get_allowed_mcp_servers(user_api_key_auth)
Expand Down Expand Up @@ -239,14 +242,17 @@ def _filter_mcp_tools_by_allowed_tools(

@staticmethod
async def _process_mcp_tools_to_openai_format(
user_api_key_auth: Any, mcp_tools_with_litellm_proxy: List[ToolParam]
user_api_key_auth: Any,
mcp_tools_with_litellm_proxy: List[ToolParam],
litellm_trace_id: Optional[str] = None,
) -> tuple[List[Any], dict[str, str]]:
"""
Centralized method to process MCP tools through the complete pipeline.

Args:
user_api_key_auth: User authentication info for access control
mcp_tools_with_litellm_proxy: ToolParam objects with server_url starting with "litellm_proxy"
litellm_trace_id: Optional trace ID for linking list_mcp_tools spend logs to parent request

Returns:
List of tools in OpenAI format ready to be sent to the LLM
Expand All @@ -258,6 +264,7 @@ async def _process_mcp_tools_to_openai_format(
) = await LiteLLM_Proxy_MCP_Handler._process_mcp_tools_without_openai_transform(
user_api_key_auth,
mcp_tools_with_litellm_proxy,
litellm_trace_id=litellm_trace_id,
)

openai_tools = LiteLLM_Proxy_MCP_Handler._transform_mcp_tools_to_openai(
Expand All @@ -268,7 +275,9 @@ async def _process_mcp_tools_to_openai_format(

@staticmethod
async def _process_mcp_tools_without_openai_transform(
user_api_key_auth: Any, mcp_tools_with_litellm_proxy: List[ToolParam]
user_api_key_auth: Any,
mcp_tools_with_litellm_proxy: List[ToolParam],
litellm_trace_id: Optional[str] = None,
) -> tuple[List[Any], dict[str, str]]:
"""
Process MCP tools through filtering and deduplication pipeline without OpenAI transformation.
Expand All @@ -291,6 +300,7 @@ async def _process_mcp_tools_without_openai_transform(
) = await LiteLLM_Proxy_MCP_Handler._get_mcp_tools_from_manager(
user_api_key_auth=user_api_key_auth,
mcp_tools_with_litellm_proxy=mcp_tools_with_litellm_proxy,
litellm_trace_id=litellm_trace_id,
)

# Step 2: Filter tools based on allowed_tools parameter
Expand Down Expand Up @@ -495,14 +505,13 @@ async def _execute_tool_calls( # noqa: PLR0915
"""Execute tool calls and return results."""
from fastapi import HTTPException

from litellm._uuid import uuid
from litellm.exceptions import BlockedPiiEntityError, GuardrailRaisedException
from litellm.proxy._experimental.mcp_server.mcp_server_manager import (
global_mcp_server_manager,
)
from litellm.proxy.proxy_server import proxy_logging_obj

from litellm._uuid import uuid

tool_results = []
tool_call_id: Optional[str] = None
rules_obj = Rules()
Expand Down Expand Up @@ -1025,7 +1034,6 @@ def _create_tool_execution_events(
List of MCP tool execution events for streaming
"""
from litellm._uuid import uuid

from litellm.responses.mcp.mcp_streaming_iterator import create_mcp_call_events

tool_execution_events: List[Any] = []
Expand Down Expand Up @@ -1108,8 +1116,8 @@ def _add_mcp_output_elements_to_response(
"""Add custom output elements to the final response for MCP tool execution."""
# Import the required classes for creating output items
import json
from litellm._uuid import uuid

from litellm._uuid import uuid
from litellm.types.responses.main import GenericResponseOutputItem, OutputText

# Create output element for initial MCP tools
Expand Down
Loading
Loading