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]

fix(mcp): keep the virtual tool required lists as JSON arrays so /mcp/ tools/call validates

mateo-berri6de53732ee9e
diff --git a/litellm/proxy/_experimental/mcp_server/tool_search.py b/litellm/proxy/_experimental/mcp_server/tool_search.py
index a33ab4de3b7..a5259e127fa 100644
--- a/litellm/proxy/_experimental/mcp_server/tool_search.py
+++ b/litellm/proxy/_experimental/mcp_server/tool_search.py
@@ -1,7 +1,7 @@
from __future__ import annotations
import json
-from collections.abc import Mapping
+from collections.abc import Mapping, Sequence
from datetime import datetime
from typing import TYPE_CHECKING, Any, Final, TypedDict, assert_never
@@ -51,7 +51,7 @@ class _ToolParamSchema(TypedDict, total=False):
class _ToolInputSchema(TypedDict):
type: ReadOnly[str]
properties: ReadOnly[Mapping[str, _ToolParamSchema]]
- required: ReadOnly[tuple[str, ...]]
+ required: ReadOnly[Sequence[str]]
class VirtualToolDefinition(TypedDict):
@@ -60,6 +60,10 @@ class VirtualToolDefinition(TypedDict):
inputSchema: ReadOnly[_ToolInputSchema]
+def _json_array(*items: str) -> Sequence[str]:
+ return list(items) # mutable-ok: jsonschema's metaschema only accepts a JSON array for required
+
+
_MCP_TOOL_SEARCH_DEFINITION: Final[VirtualToolDefinition] = {
"name": MCP_TOOL_SEARCH_TOOL_NAME,
"description": "Search for MCP tools by keyword. Returns top matching tools with names, descriptions, and input schemas.",
@@ -69,7 +73,7 @@ _MCP_TOOL_SEARCH_DEFINITION: Final[VirtualToolDefinition] = {
"query": {"type": "string", "description": "Keywords to search for in tool names and descriptions."},
"top_k": {"type": "integer", "description": "Maximum number of results to return.", "default": 5},
},
- "required": ("query",),
+ "required": _json_array("query"),
},
}
@@ -82,7 +86,7 @@ _MCP_TOOL_CALL_DEFINITION: Final[VirtualToolDefinition] = {
"tool_name": {"type": "string", "description": "The exact name of the MCP tool to call."},
"arguments": {"type": "object", "description": "Arguments to pass to the tool."},
},
- "required": ("tool_name",),
+ "required": _json_array("tool_name"),
},
}
@@ -99,7 +103,7 @@ _AGENT_SEARCH_DEFINITION: Final[VirtualToolDefinition] = {
"default": DEFAULT_AGENT_SEARCH_TOP_K,
},
},
- "required": ("query",),
+ "required": _json_array("query"),
},
}
diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_tool_search.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_tool_search.py
index dfe1e224c1f..bf0bd3c795e 100644
--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_tool_search.py
+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_tool_search.py
@@ -123,7 +123,7 @@ class TestGetVirtualToolDefinitions:
agent_tool = next(t for t in tools if t["name"] == AGENT_SEARCH_TOOL_NAME)
props = agent_tool["inputSchema"]["properties"]
assert set(props) == {"query", "top_k"}
- assert agent_tool["inputSchema"]["required"] == ("query",)
+ assert agent_tool["inputSchema"]["required"] == ["query"]
def test_has_mcp_tool_search(self) -> None:
names = [t["name"] for t in get_virtual_tool_definitions()]
@@ -138,7 +138,7 @@ class TestGetVirtualToolDefinitions:
search_tool = next(t for t in tools if t["name"] == MCP_TOOL_SEARCH_TOOL_NAME)
props = search_tool["inputSchema"]["properties"]
assert "query" in props
- assert search_tool["inputSchema"]["required"] == ("query",)
+ assert search_tool["inputSchema"]["required"] == ["query"]
def test_mcp_tool_call_schema_has_tool_name_and_arguments(self) -> None:
tools = get_virtual_tool_definitions()
@@ -148,6 +148,17 @@ class TestGetVirtualToolDefinitions:
assert "arguments" in props
assert "tool_name" in call_tool["inputSchema"]["required"]
+ def test_input_schemas_validate_arguments_like_the_mcp_server_does(self) -> None:
+ from jsonschema import ValidationError, validate
+ from mcp.types import Tool
+
+ for definition in get_virtual_tool_definitions():
+ tool = Tool.model_validate(definition)
+ required_arguments = {name: "x" for name in tool.inputSchema["required"]}
+ validate(instance=required_arguments, schema=tool.inputSchema)
+ with pytest.raises(ValidationError):
+ validate(instance={}, schema=tool.inputSchema)
+
def test_all_tools_have_description(self) -> None:
for tool in get_virtual_tool_definitions():
assert tool.get("description"), f"{tool['name']} missing description"