Skip to content

Commit b4cfcbb

Browse files
committed
fix(mcp): keep explicit empty list/dict tool arguments when cleaning MCP calls
Fixes crewAIInc#8045
1 parent fa6d712 commit b4cfcbb

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

‎lib/crewai/src/crewai/mcp/client.py‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,10 @@ async def call_tool_result(
563563
def _clean_tool_arguments(self, arguments: dict[str, Any]) -> dict[str, Any]:
564564
"""Clean tool arguments by removing None values and fixing formats.
565565
566+
Only ``None`` values are removed. Explicit empty containers (``[]``,
567+
``{}``) and list items are kept as sent, because the model chose them
568+
and a server may require the field or rely on list positions.
569+
566570
Args:
567571
arguments: Raw tool arguments.
568572
@@ -590,20 +594,15 @@ def _clean_tool_arguments(self, arguments: dict[str, Any]) -> dict[str, Any]:
590594
continue
591595

592596
if isinstance(value, dict):
593-
nested_cleaned = self._clean_tool_arguments(value)
594-
if nested_cleaned: # Only add if not empty
595-
cleaned[key] = nested_cleaned
597+
cleaned[key] = self._clean_tool_arguments(value)
596598
elif isinstance(value, list):
597599
cleaned_list = []
598600
for item in value:
599601
if isinstance(item, dict):
600-
cleaned_item = self._clean_tool_arguments(item)
601-
if cleaned_item:
602-
cleaned_list.append(cleaned_item)
602+
cleaned_list.append(self._clean_tool_arguments(item))
603603
elif item is not None:
604604
cleaned_list.append(item)
605-
if cleaned_list:
606-
cleaned[key] = cleaned_list
605+
cleaned[key] = cleaned_list
607606
else:
608607
cleaned[key] = value
609608

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from crewai.mcp.client import MCPClient
2+
from crewai.mcp.transports.stdio import StdioTransport
3+
4+
5+
def _client() -> MCPClient:
6+
return MCPClient(StdioTransport(command="python", args=["server.py"]))
7+
8+
9+
def test_explicit_empty_containers_reach_the_server():
10+
cleaned = _client()._clean_tool_arguments(
11+
{"query": "cats", "tags": [], "filters": {}}
12+
)
13+
assert cleaned == {"query": "cats", "tags": [], "filters": {}}
14+
15+
16+
def test_list_items_that_clean_to_empty_keep_their_position():
17+
cleaned = _client()._clean_tool_arguments({"mixed": [{"opt": None}, 1]})
18+
assert cleaned == {"mixed": [{}, 1]}
19+
20+
21+
def test_none_values_are_still_removed():
22+
cleaned = _client()._clean_tool_arguments(
23+
{
24+
"query": "cats",
25+
"limit": None,
26+
"nested": {"keep": 1, "drop": None},
27+
"items": [None, {"a": None, "b": 2}],
28+
}
29+
)
30+
assert cleaned == {
31+
"query": "cats",
32+
"nested": {"keep": 1},
33+
"items": [{"b": 2}],
34+
}
35+
36+
37+
def test_falsy_scalars_are_preserved():
38+
cleaned = _client()._clean_tool_arguments({"label": "", "count": 0, "flag": False})
39+
assert cleaned == {"label": "", "count": 0, "flag": False}

0 commit comments

Comments
 (0)