Skip to content

Commit f010d18

Browse files
authored
fix(core): preserve closed parent constraints in singleton allOf (#5131)
1 parent b3a1601 commit f010d18

3 files changed

Lines changed: 158 additions & 11 deletions

File tree

‎src/agents/strict_schema.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ def _ensure_strict_json_schema(
167167
if not is_dict(json_schema):
168168
raise TypeError(f"Expected {json_schema} to be a dictionary; path={path}")
169169

170+
# Remember author-supplied closure before strict normalization adds its own.
171+
explicitly_closed = json_schema.get("additionalProperties") is False
172+
170173
# Bound the total number of nodes we expand so a malicious `$ref` fan-out cannot expand
171174
# exponentially and exhaust CPU and memory.
172175
if budget is None:
@@ -337,6 +340,9 @@ def _ensure_strict_json_schema(
337340
inside_nested_resource=inside_nested_resource,
338341
)
339342
json_schema.pop("allOf")
343+
if not explicitly_closed:
344+
# Close the final merged object below, after any remaining allOf is expanded.
345+
json_schema.pop("additionalProperties", None)
340346
merged = _merge_single_all_of(entry=strict_entry, parent=json_schema)
341347
json_schema.clear()
342348
json_schema.update(merged)
@@ -468,6 +474,14 @@ def _merge_single_all_of(
468474
) -> dict[str, Any]:
469475
merged = dict(entry)
470476
incompatible_overlaps: list[str] = []
477+
if (
478+
parent.get("additionalProperties") is False
479+
and ("properties" not in parent or parent["properties"] == {})
480+
and is_dict(entry.get("properties"))
481+
and entry["properties"] != {}
482+
):
483+
# additionalProperties only sees properties declared in its own schema, not allOf.
484+
incompatible_overlaps.append("properties")
471485
for key, parent_value in parent.items():
472486
if key not in merged:
473487
merged[key] = parent_value

‎tests/mcp/test_mcp_util.py‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,25 +2165,49 @@ def test_to_function_tool_single_all_of_annotated_alias_remains_strict():
21652165
}
21662166
},
21672167
"type": "object",
2168-
"allOf": [
2169-
{
2170-
"$ref": "#/components/schemas/Outer",
2171-
"title": "entry",
2172-
}
2173-
],
2168+
"properties": {
2169+
"payload": {"allOf": [{"$ref": "#/components/schemas/Outer", "title": "entry"}]}
2170+
},
21742171
"additionalProperties": False,
21752172
}
21762173
tool = MCPTool(name="test_tool", inputSchema=schema)
21772174

21782175
function_tool = MCPUtil.to_function_tool(tool, FakeMCPServer(), convert_schemas_to_strict=True)
21792176

21802177
assert function_tool.strict_json_schema is True
2181-
assert function_tool.params_json_schema["description"] == "outer"
2182-
assert function_tool.params_json_schema["title"] == "entry"
2183-
assert function_tool.params_json_schema["properties"] == {"value": {"type": "string"}}
2184-
assert function_tool.params_json_schema["required"] == ["value"]
2178+
payload = function_tool.params_json_schema["properties"]["payload"]
2179+
assert payload["description"] == "outer"
2180+
assert payload["title"] == "entry"
2181+
assert payload["properties"] == {"value": {"type": "string"}}
2182+
assert payload["required"] == ["value"]
21852183
assert function_tool.params_json_schema["additionalProperties"] is False
2186-
assert "$ref" not in function_tool.params_json_schema
2184+
assert "$ref" not in payload
2185+
2186+
2187+
@pytest.mark.parametrize("declare_properties", [True, False])
2188+
def test_to_function_tool_single_all_of_closed_parent_falls_back(declare_properties):
2189+
schema = {
2190+
"type": "object",
2191+
"additionalProperties": False,
2192+
"$defs": {
2193+
"Entry": {
2194+
"type": "object",
2195+
"properties": {"value": {"type": "string"}},
2196+
"additionalProperties": False,
2197+
}
2198+
},
2199+
"allOf": [{"$ref": "#/$defs/Entry"}],
2200+
}
2201+
if declare_properties:
2202+
schema["properties"] = {}
2203+
original = copy.deepcopy(schema)
2204+
tool = MCPTool(name="test_tool", inputSchema=schema)
2205+
2206+
function_tool = MCPUtil.to_function_tool(tool, FakeMCPServer(), convert_schemas_to_strict=True)
2207+
2208+
assert function_tool.strict_json_schema is False
2209+
assert function_tool.params_json_schema == {**original, "properties": {}}
2210+
assert tool_input_schema(tool) == original
21872211

21882212

21892213
def test_to_function_tool_single_all_of_annotated_entry_conflict_falls_back():

‎tests/test_function_tool.py‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Annotated, Any, cast
1010

1111
import pytest
12+
from jsonschema import Draft202012Validator
1213
from pydantic import BaseModel, Field
1314
from typing_extensions import TypedDict
1415

@@ -1063,6 +1064,114 @@ async def noop(ctx: ToolContext[Any], input: str) -> str:
10631064
assert tool.params_json_schema["required"] == ["x"]
10641065

10651066

1067+
@pytest.mark.parametrize(
1068+
("parent", "use_ref"),
1069+
[
1070+
({"type": "object", "properties": {}}, True),
1071+
({"type": "object"}, True),
1072+
({}, True),
1073+
({"type": "object", "properties": {}}, False),
1074+
],
1075+
)
1076+
def test_function_tool_rejects_single_all_of_that_broadens_closed_parent(
1077+
parent: dict[str, Any], use_ref: bool
1078+
) -> None:
1079+
async def noop(ctx: ToolContext[Any], input: str) -> str:
1080+
return ""
1081+
1082+
entry = {"type": "object", "properties": {"value": {"type": "string"}}}
1083+
schema = {
1084+
**parent,
1085+
"additionalProperties": False,
1086+
"$defs": {"Entry": entry},
1087+
"allOf": [{"$ref": "#/$defs/Entry"} if use_ref else copy.deepcopy(entry)],
1088+
}
1089+
original = copy.deepcopy(schema)
1090+
validator = Draft202012Validator(original)
1091+
assert validator.is_valid({})
1092+
assert not validator.is_valid({"value": "example"})
1093+
1094+
with pytest.raises(UserError, match="singleton `allOf`.*properties"):
1095+
FunctionTool(name="t", description="d", params_json_schema=schema, on_invoke_tool=noop)
1096+
1097+
assert schema == original
1098+
non_strict_tool = FunctionTool(
1099+
name="t",
1100+
description="d",
1101+
params_json_schema=schema,
1102+
on_invoke_tool=noop,
1103+
strict_json_schema=False,
1104+
)
1105+
assert non_strict_tool.params_json_schema == original
1106+
assert not Draft202012Validator(non_strict_tool.params_json_schema).is_valid(
1107+
{"value": "example"}
1108+
)
1109+
1110+
1111+
@pytest.mark.parametrize("parent", [{"type": "object"}, {"type": "object", "properties": {}}])
1112+
def test_function_tool_single_all_of_wrapper_remains_strict(parent: dict[str, Any]) -> None:
1113+
async def noop(ctx: ToolContext[Any], input: str) -> str:
1114+
return ""
1115+
1116+
schema = {
1117+
**parent,
1118+
"description": "wrapper",
1119+
"allOf": [{"type": "object", "properties": {"value": {"type": "string"}}}],
1120+
}
1121+
original = copy.deepcopy(schema)
1122+
tool = FunctionTool(name="t", description="d", params_json_schema=schema, on_invoke_tool=noop)
1123+
1124+
assert schema == original
1125+
assert tool.params_json_schema["description"] == "wrapper"
1126+
assert tool.params_json_schema["required"] == ["value"]
1127+
validator = Draft202012Validator(tool.params_json_schema)
1128+
assert validator.is_valid({"value": "example"})
1129+
assert not validator.is_valid({})
1130+
assert not validator.is_valid({"value": "example", "extra": True})
1131+
1132+
1133+
def test_function_tool_nested_single_all_of_ref_wrapper_remains_strict() -> None:
1134+
async def noop(ctx: ToolContext[Any], input: str) -> str:
1135+
return ""
1136+
1137+
schema = {
1138+
"type": "object",
1139+
"components": {
1140+
"schemas": {
1141+
"Inner": {"type": "object", "properties": {"value": {"type": "string"}}},
1142+
"Outer": {"type": "object", "allOf": [{"$ref": "#/components/schemas/Inner"}]},
1143+
}
1144+
},
1145+
"allOf": [{"$ref": "#/components/schemas/Outer"}],
1146+
}
1147+
original = copy.deepcopy(schema)
1148+
tool = FunctionTool(name="t", description="d", params_json_schema=schema, on_invoke_tool=noop)
1149+
1150+
assert schema == original
1151+
assert tool.params_json_schema["required"] == ["value"]
1152+
assert "allOf" not in tool.params_json_schema
1153+
validator = Draft202012Validator(tool.params_json_schema)
1154+
assert validator.is_valid({"value": "example"})
1155+
assert not validator.is_valid({"value": "example", "extra": True})
1156+
1157+
1158+
def test_function_tool_single_all_of_closed_empty_object_remains_strict() -> None:
1159+
async def noop(ctx: ToolContext[Any], input: str) -> str:
1160+
return ""
1161+
1162+
schema = {
1163+
"type": "object",
1164+
"properties": {},
1165+
"additionalProperties": False,
1166+
"allOf": [{"type": "object", "properties": {}}],
1167+
}
1168+
tool = FunctionTool(name="t", description="d", params_json_schema=schema, on_invoke_tool=noop)
1169+
1170+
validator = Draft202012Validator(tool.params_json_schema)
1171+
assert validator.is_valid({})
1172+
assert not validator.is_valid({"value": "example"})
1173+
1174+
10661175
def test_function_tool_rejects_deep_schema_before_copying() -> None:
10671176
async def noop(ctx: ToolContext[Any], input: str) -> str:
10681177
return ""

0 commit comments

Comments
 (0)