The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
perf(mcp): count gateway session groups with Counter and pin the oversized initialize peek invariant
yassin33531649c305
diff --git a/litellm/proxy/_experimental/mcp_server/server.py b/litellm/proxy/_experimental/mcp_server/server.pyindex 9c8ad2f4613..9adc203b092 100644--- a/litellm/proxy/_experimental/mcp_server/server.py+++ b/litellm/proxy/_experimental/mcp_server/server.py@@ -14,6 +14,7 @@ import timeimport tracebackimport typesimport uuid+from collections import Counterfrom collections.abc import AsyncIterator, Callable, Mapping, Sequencefrom datetime import datetimefrom typing import TYPE_CHECKING, Any, Final, NoReturn, Protocol@@ -3837,10 +3838,10 @@ if MCP_AVAILABLE:sessions: Sequence[MCPGatewaySession],label_for: Callable[[MCPGatewaySession], str | None],) -> tuple[MCPGatewaySessionGroupCount, ...]:- labels: Final = tuple(label_for(session) for session in sessions)+ counts: Final = Counter(label_for(session) for session in sessions)return tuple(sorted(- (MCPGatewaySessionGroupCount(label=label, count=labels.count(label)) for label in frozenset(labels)),+ (MCPGatewaySessionGroupCount(label=label, count=count) for label, count in counts.items()),key=lambda group: (-group.count, group.label is None, group.label or ""),))diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.pyindex bbc36991e21..ef424255f04 100644--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py@@ -2658,6 +2658,28 @@ def test_extract_initialize_client_info_returns_none_without_client_info(body):assert mcp_server._extract_initialize_client_info(body) is None+def test_oversized_initialize_peek_neither_routes_stateful_nor_attributes_client():+ """The routing sniff and the clientInfo parse read the same capped peek, so+ an initialize larger than the peek can never become a tracked session that+ then reports an unknown client."""+ try:+ from litellm.proxy._experimental.mcp_server import server as mcp_server+ except ImportError:+ pytest.skip("MCP server not available")++ padding = "x" * (mcp_server._MCP_ROUTING_PEEK_MAX_BYTES + 512)+ full_body = (+ b'{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18",'+ b'"capabilities":{"experimental":{"pad":{"value":"' + padding.encode() + b'"}}},'+ b'"clientInfo":{"name":"claude-code","version":"1.0.0"}}}'+ )+ peeked = full_body[: mcp_server._MCP_ROUTING_PEEK_MAX_BYTES]++ assert mcp_server._extract_initialize_client_info(full_body) is not None+ assert mcp_server._is_initialize_request(peeked) is False+ assert mcp_server._extract_initialize_client_info(peeked) is None++@pytest.mark.asyncioasync def test_initialize_request_records_client_name_in_gateway_sessions_report():"""The real initialize body's clientInfo is attributed to the session the