Skip to content

The agent that grows with you

fix(tools): coerce a missing `required` to [] and pin the MCP normalizer entry (#56123)

teknium13dbaffd814e0
diff --git a/tests/tools/test_mcp_tool.py b/tests/tools/test_mcp_tool.py
index 8831d4e61c1..6a2abaf5bb8 100644
--- a/tests/tools/test_mcp_tool.py
+++ b/tests/tools/test_mcp_tool.py
@@ -601,56 +601,38 @@ class TestSchemaConversion:
assert set(props) == {"table", "required"}
# The legitimately-named `required` parameter keeps its array schema.
assert props["required"] == {"type": "array", "items": {"type": "string"}}
- # No non-list `required` keyword was synthesised at the object level.
- assert "required" not in normalized
+ # The object-level `required` keyword is the coerced empty list, not a schema
+ # synthesised from the same-named property.
+ assert normalized["required"] == []
- def test_required_array_filters_entries_not_in_properties(self):
- """``required`` entries absent from ``properties`` are pruned by repair.
+ def test_normalized_mcp_schema_keeps_required_as_list(self):
+ """``_normalize_mcp_input_schema`` (the production MCP entry) always emits a
+ ``required`` list.
- Regression: when an MCP tool's input schema lists ``required`` keys
- that do not appear in ``properties`` (common with loosely-defined
- upstream MCP tools), the sanitizer used to silently drop the entire
- ``required`` array, losing legitimately-required field metadata.
- The fix preserves ``required`` but filters it to only entries that
- actually exist in ``properties``.
+ Regression (#56123): when every ``required`` entry pointed at a property missing
+ from ``properties``, the repair pass pruned them all and dropped the key
+ entirely (partial pruning already worked). Strict OpenAI-compatible backends read
+ the missing key as ``null`` and reject the whole request with
+ ``null is not of type "array"``. The key now survives as ``[]``, and an object
+ node that never had a ``required`` key is coerced to ``[]`` too.
"""
- from tools.mcp_tool_schema import _repair_object_shape
-
- schema = {
- "type": "object",
- "properties": {
- "command": {"type": "string"},
- "path": {"type": "string"},
- },
- "required": ["command", "path", "missing_entry"],
- }
-
- repaired = _repair_object_shape(schema)
+ from tools.mcp_tool_schema import _normalize_mcp_input_schema
- # valid entries are kept; entries not in properties are removed
- assert repaired["required"] == ["command", "path"]
- assert "missing_entry" not in repaired["required"]
+ stale = _normalize_mcp_input_schema({
+ "type": "object", "properties": {}, "required": ["stale"],
+ })
+ assert stale["required"] == []
- # all entries valid: required is unchanged
- schema_all_valid = {
+ partial = _normalize_mcp_input_schema({
"type": "object",
- "properties": {
- "a": {"type": "string"},
- "b": {"type": "integer"},
- },
- "required": ["a", "b"],
- }
- assert _repair_object_shape(schema_all_valid)["required"] == ["a", "b"]
+ "properties": {"command": {"type": "string"}, "path": {"type": "string"}},
+ "required": ["command", "path", "missing_entry"],
+ })
+ assert partial["required"] == ["command", "path"]
- # all entries invalid: required becomes an empty list
- schema_all_invalid = {
- "type": "object",
- "properties": {"x": {"type": "string"}},
- "required": ["y", "z"],
- }
- result = _repair_object_shape(schema_all_invalid)
- assert result["required"] == []
+ absent = _normalize_mcp_input_schema({"type": "object", "properties": {"a": {"type": "string"}}})
+ assert absent["required"] == []
def test_optional_nullable_field_is_collapsed_to_non_null_schema(self):
"""Anthropic rejects MCP/Pydantic anyOf-null optional parameter schemas."""
@@ -2416,7 +2398,7 @@ class TestSamplingCallbackText:
"function": {
"name": "ask",
"description": "Ask Crawl4AI",
- "parameters": {"type": "object", "properties": {}},
+ "parameters": {"type": "object", "properties": {}, "required": []},
},
}]
diff --git a/tests/tools/test_schema_sanitizer.py b/tests/tools/test_schema_sanitizer.py
index e6b11bea817..e685a5b0b84 100644
--- a/tests/tools/test_schema_sanitizer.py
+++ b/tests/tools/test_schema_sanitizer.py
@@ -23,7 +23,7 @@ def _tool(name: str, parameters: dict) -> dict:
def test_object_without_properties_gets_empty_properties():
tools = [_tool("t", {"type": "object"})]
out = sanitize_tool_schemas(tools)
- assert out[0]["function"]["parameters"] == {"type": "object", "properties": {}}
+ assert out[0]["function"]["parameters"] == {"type": "object", "properties": {}, "required": []}
def test_nested_object_without_properties_gets_empty_properties():
@@ -130,20 +130,20 @@ def test_anyof_nested_objects_sanitized():
})]
out = sanitize_tool_schemas(tools)
variants = out[0]["function"]["parameters"]["properties"]["opt"]["anyOf"]
- assert variants[0] == {"type": "object", "properties": {}}
+ assert variants[0] == {"type": "object", "properties": {}, "required": []}
assert variants[1] == {"type": "string"}
def test_missing_parameters_gets_default_object_schema():
tools = [{"type": "function", "function": {"name": "t"}}]
out = sanitize_tool_schemas(tools)
- assert out[0]["function"]["parameters"] == {"type": "object", "properties": {}}
+ assert out[0]["function"]["parameters"] == {"type": "object", "properties": {}, "required": []}
def test_non_dict_parameters_gets_default_object_schema():
tools = [_tool("t", "object")] # pathological
out = sanitize_tool_schemas(tools)
- assert out[0]["function"]["parameters"] == {"type": "object", "properties": {}}
+ assert out[0]["function"]["parameters"] == {"type": "object", "properties": {}, "required": []}
def test_required_pruned_to_existing_properties():
@@ -196,7 +196,7 @@ def test_additional_properties_schema_sanitized():
})]
out = sanitize_tool_schemas(tools)
field = out[0]["function"]["parameters"]["properties"]["dict_field"]
- assert field["additionalProperties"] == {"type": "object", "properties": {}}
+ assert field["additionalProperties"] == {"type": "object", "properties": {}, "required": []}
def test_items_sanitized_in_array_schema():
@@ -211,7 +211,7 @@ def test_items_sanitized_in_array_schema():
})]
out = sanitize_tool_schemas(tools)
items = out[0]["function"]["parameters"]["properties"]["bag"]["items"]
- assert items == {"type": "object", "properties": {}}
+ assert items == {"type": "object", "properties": {}, "required": []}
# ─────────────────────────────────────────────────────────────────────────
@@ -357,7 +357,7 @@ def test_dependent_schemas_still_recursively_sanitized():
tools = [_tool("t", copy.deepcopy(schema))]
out = sanitize_tool_schemas(tools)
dep_schemas = out[0]["function"]["parameters"]["dependentSchemas"]
- assert dep_schemas["owner"] == {"type": "object", "properties": {}}, (
+ assert dep_schemas["owner"] == {"type": "object", "properties": {}, "required": []}, (
f"dependentSchemas['owner'] was not fully sanitized: {dep_schemas['owner']!r}"
)
@@ -531,3 +531,18 @@ def test_collapse_is_deterministic():
first = collapse_const_unions(copy.deepcopy(schema))
second = collapse_const_unions(copy.deepcopy(schema))
assert first == second == {"type": "string", "enum": ["b", "a"]}
+
+
+def test_builtin_tool_without_required_gets_empty_required_list():
+ """Object nodes that never had ``required`` are coerced to ``required: []`` (#56123):
+ strict OpenAI-compatible backends read a missing key as ``null`` and 400 the request."""
+ from tools.read_window_tool import READ_WINDOW_BELOW_SCHEMA
+
+ tools = [{"type": "function", "function": copy.deepcopy(READ_WINDOW_BELOW_SCHEMA)}]
+ params = sanitize_tool_schemas(tools)[0]["function"]["parameters"]
+ assert params["required"] == []
+ nested = sanitize_tool_schemas([_tool("t", {
+ "type": "object",
+ "properties": {"opts": {"type": "object", "properties": {"k": {"type": "string"}}}},
+ })])[0]["function"]["parameters"]
+ assert nested["properties"]["opts"]["required"] == []
diff --git a/tools/mcp_tool_schema.py b/tools/mcp_tool_schema.py
index 5c94a1a72ad..d31d3387ff5 100644
--- a/tools/mcp_tool_schema.py
+++ b/tools/mcp_tool_schema.py
@@ -93,12 +93,12 @@ def _repair_object_shape(node):
if repaired.get("type") == "object":
if not isinstance(repaired.get("properties"), dict):
repaired["properties"] = {}
+ # Always a list: a missing/non-list ``required`` reads as ``null`` on strict
+ # OpenAI-compatible backends (#56123); ``[]`` is valid everywhere (Gemini included).
required = repaired.get("required")
- if isinstance(required, list):
- props = repaired.get("properties") or {}
- valid = [r for r in required if isinstance(r, str) and r in props]
- if len(valid) != len(required):
- repaired["required"] = valid
+ props = repaired.get("properties") or {}
+ repaired["required"] = ([r for r in required if isinstance(r, str) and r in props]
+ if isinstance(required, list) else [])
return repaired
diff --git a/tools/schema_sanitizer.py b/tools/schema_sanitizer.py
index a8e8d8e6270..9df3c4ef687 100644
--- a/tools/schema_sanitizer.py
+++ b/tools/schema_sanitizer.py
@@ -23,7 +23,7 @@ _UNION_META_KEYS = ("title", "description", "default", "examples") # copied ont
def _empty_object() -> dict:
- return {"type": "object", "properties": {}}
+ return {"type": "object", "properties": {}, "required": []}
def _rewrite(schema: Any, fn: Callable[[dict], Any]) -> Any:
@@ -98,6 +98,8 @@ def _sanitize_single_tool(tool: dict) -> dict:
top["type"] = "object"
if not isinstance(top.get("properties"), dict):
top["properties"] = {}
+ if not isinstance(top.get("required"), list):
+ top["required"] = []
# The recursive pass only handles array-form ``type: [X, "null"]``; collapse anyOf unions
# here, keeping ``nullable: true`` so ``tools.arg_coercion._schema_allows_null`` still coerces.
top = strip_nullable_unions(top, keep_nullable_hint=True)
@@ -314,10 +316,11 @@ def _sanitize_node(node: Any, path: str) -> Any:
if out.get("type") == "object":
if not isinstance(out.get("properties"), dict):
out["properties"] = {}
- if isinstance(out.get("required"), list):
- # Keep the key even when nothing survives: ``required: []`` is valid everywhere,
- # while a missing key reads as ``null`` on strict OpenAI-compatible proxies.
- out["required"] = [r for r in out["required"] if isinstance(r, str) and r in out["properties"]]
+ # Always emit a list: ``required: []`` is valid everywhere, while a missing or
+ # non-list key reads as ``null`` on strict OpenAI-compatible proxies (#56123).
+ required = out.get("required")
+ out["required"] = ([r for r in required if isinstance(r, str) and r in out["properties"]]
+ if isinstance(required, list) else [])
return out