Skip to content

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]

refactor(mcp): keep list pagination within type-discipline budget and ratchet LIT001

Devin AI26b48d58919e
diff --git a/litellm/experimental_mcp_client/client.py b/litellm/experimental_mcp_client/client.py
index 814074d35a4..46d8823e439 100644
--- a/litellm/experimental_mcp_client/client.py
+++ b/litellm/experimental_mcp_client/client.py
@@ -377,29 +377,31 @@ class MCPClient:
return provided_env
# Minimal allowlist of safe/standard environment variables
- safe_keys: Final = {
- "PATH",
- "HOME",
- "USER",
- "LOGNAME",
- "TMPDIR",
- "TMP",
- "TEMP",
- "SHELL",
- "LANG",
- "LC_ALL",
- # Node/Package manager caches
- "NPM_CONFIG_CACHE",
- "PNPM_HOME",
- "XDG_CACHE_HOME",
- "XDG_CONFIG_HOME",
- "XDG_DATA_HOME",
- # System info
- "SYSTEMROOT",
- "COMSPEC",
- "PATHEXT",
- "WINDIR",
- }
+ safe_keys: Final = frozenset(
+ {
+ "PATH",
+ "HOME",
+ "USER",
+ "LOGNAME",
+ "TMPDIR",
+ "TMP",
+ "TEMP",
+ "SHELL",
+ "LANG",
+ "LC_ALL",
+ # Node/Package manager caches
+ "NPM_CONFIG_CACHE",
+ "PNPM_HOME",
+ "XDG_CACHE_HOME",
+ "XDG_CONFIG_HOME",
+ "XDG_DATA_HOME",
+ # System info
+ "SYSTEMROOT",
+ "COMSPEC",
+ "PATHEXT",
+ "WINDIR",
+ }
+ )
safe_env: Final = {}
for key in safe_keys:
@@ -615,7 +617,7 @@ class MCPClient:
try:
tools: Final = await self.run_with_session(_list_tools_operation, quiet_on_error=raise_on_error)
tool_count: Final = len(tools)
- tool_names: Final = [tool.name for tool in tools]
+ tool_names: Final = tuple(tool.name for tool in tools)
verbose_logger.info(
"MCP client listed %s tools from %s: %s", tool_count, self.server_url or "stdio", tool_names
)
@@ -746,7 +748,7 @@ class MCPClient:
try:
prompts: Final = await self.run_with_session(_list_prompts_operation)
prompt_count: Final = len(prompts)
- prompt_names: Final = [prompt.name for prompt in prompts]
+ prompt_names: Final = tuple(prompt.name for prompt in prompts)
verbose_logger.info(
"MCP client listed %s prompts from %s: %s", prompt_count, self.server_url or "stdio", prompt_names
)
@@ -823,7 +825,7 @@ class MCPClient:
try:
resources: Final = await self.run_with_session(_list_resources_operation)
resource_count: Final = len(resources)
- resource_names: Final = [resource.name for resource in resources]
+ resource_names: Final = tuple(resource.name for resource in resources)
verbose_logger.info(
"MCP client listed %s resources from %s: %s", resource_count, self.server_url or "stdio", resource_names
)
@@ -859,7 +861,7 @@ class MCPClient:
try:
resource_templates: Final = await self.run_with_session(_list_resource_templates_operation)
resource_template_count: Final = len(resource_templates)
- resource_template_names: Final = [resource_template.name for resource_template in resource_templates]
+ resource_template_names: Final = tuple(resource_template.name for resource_template in resource_templates)
verbose_logger.info(
"MCP client listed %s resource templates from %s: %s",
resource_template_count,
diff --git a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py
index 3ca6b6c5f90..beee7c1db78 100644
--- a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py
+++ b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py
@@ -1,6 +1,6 @@
import asyncio
import importlib
-from collections.abc import Awaitable, Callable, Mapping
+from collections.abc import Awaitable, Callable, Mapping, Sequence
from datetime import datetime
from typing import TYPE_CHECKING, Any, Final, Literal
@@ -1402,7 +1402,7 @@ if MCP_AVAILABLE:
oauth2_headers = MCPRequestHandler._get_oauth2_headers_from_headers(headers)
async def _list_tools_operation(client):
- list_tools_result: Final[list[MCPTool]] = await client.list_tools(raise_on_error=True)
+ list_tools_result: Final[Sequence[MCPTool]] = await client.list_tools(raise_on_error=True)
model_dumped_tools: Final[list[dict]] = [tool.model_dump() for tool in list_tools_result]
return {
"tools": model_dumped_tools,
diff --git a/tests/test_litellm/experimental_mcp_client/test_pagination.py b/tests/test_litellm/experimental_mcp_client/test_pagination.py
index f588fdd1eee..a76f410ac3c 100644
--- a/tests/test_litellm/experimental_mcp_client/test_pagination.py
+++ b/tests/test_litellm/experimental_mcp_client/test_pagination.py
@@ -38,7 +38,9 @@ async def test_collect_pages_follows_next_cursor_until_exhausted():
tools = await collect_pages(upstream.fetch, lambda page: page.tools, method="tools/list", server="s")
assert [t.name for t in tools] == [f"tool_{i:02d}" for i in range(72)]
- assert upstream.cursors_seen == [None, "30", "60"], "each page must be requested with the cursor the previous one returned"
+ assert upstream.cursors_seen == [None, "30", "60"], (
+ "each page must be requested with the cursor the previous one returned"
+ )
@pytest.mark.asyncio
diff --git a/type-discipline-budget.json b/type-discipline-budget.json
index 3d2e97d55a5..b0c3cc7f9fd 100644
--- a/type-discipline-budget.json
+++ b/type-discipline-budget.json
@@ -1,6 +1,6 @@
{
"LIT001": {
- "limit": 22367
+ "limit": 22366
},
"LIT002": {
"limit": 26777