Skip to content

Commit 5f0b3b9

Browse files
CopilotCopilot
andcommitted
Address review feedback for #4540: improve test robustness and add defensive guard
- Look up loaded functions by name instead of index to avoid brittle ordering assumptions - Add negative-path test cases: non-object schema (type: string) and empty schema ({}) to verify guard clause skips them correctly - Assert original inputSchema dicts are not mutated by load_tools() - Add defensive guard for tool.inputSchema being None Co-authored-by: Copilot <[email protected]>
1 parent 0862303 commit 5f0b3b9

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

‎python/packages/core/agent_framework/_mcp.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ async def load_tools(self) -> None:
905905
# Normalize inputSchema: ensure "properties" exists for object schemas.
906906
# Some MCP servers (e.g. zero-argument tools) omit "properties",
907907
# which causes OpenAI API to reject the schema with a 400 error.
908-
input_schema = dict(tool.inputSchema)
908+
input_schema = dict(tool.inputSchema or {})
909909
if input_schema.get("type") == "object" and "properties" not in input_schema:
910910
input_schema["properties"] = {}
911911
# Create FunctionTools out of each tool

‎python/packages/core/tests/core/test_mcp.py‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,42 +2060,69 @@ async def test_load_tools_adds_properties_to_zero_arg_tool_schema():
20602060
tool.session = mock_session
20612061
tool.load_tools_flag = True
20622062

2063+
original_zero_arg_schema = {"type": "object"}
2064+
original_string_schema = {"type": "string"}
2065+
original_empty_schema: dict[str, object] = {}
2066+
20632067
page = MagicMock()
20642068
page.tools = [
20652069
types.Tool(
20662070
name="zero_arg_tool",
20672071
description="A tool with no parameters",
2068-
inputSchema={"type": "object"},
2072+
inputSchema=original_zero_arg_schema,
20692073
),
20702074
types.Tool(
20712075
name="normal_tool",
20722076
description="A tool with parameters",
20732077
inputSchema={"type": "object", "properties": {"x": {"type": "string"}}, "required": ["x"]},
20742078
),
2079+
types.Tool(
2080+
name="string_schema_tool",
2081+
description="A tool with a non-object schema",
2082+
inputSchema=original_string_schema,
2083+
),
2084+
types.Tool(
2085+
name="empty_schema_tool",
2086+
description="A tool with an empty schema",
2087+
inputSchema=original_empty_schema,
2088+
),
20752089
]
20762090
page.nextCursor = None
20772091

20782092
mock_session.list_tools = AsyncMock(return_value=page)
20792093

20802094
await tool.load_tools()
20812095

2082-
assert len(tool._functions) == 2
2096+
assert len(tool._functions) == 4
20832097

2084-
zero_arg = tool._functions[0]
2085-
normal = tool._functions[1]
2098+
funcs_by_name = {f.name: f for f in tool._functions}
20862099

20872100
# Zero-arg tool must have "properties" injected
2088-
zero_params = zero_arg.parameters()
2101+
zero_params = funcs_by_name["zero_arg_tool"].parameters()
20892102
assert "properties" in zero_params
20902103
assert zero_params["properties"] == {}
20912104
assert zero_params["type"] == "object"
20922105

20932106
# Normal tool must retain its existing properties
2094-
normal_params = normal.parameters()
2107+
normal_params = funcs_by_name["normal_tool"].parameters()
20952108
assert "properties" in normal_params
20962109
assert "x" in normal_params["properties"]
20972110
assert normal_params["required"] == ["x"]
20982111

2112+
# Non-object schema must NOT have "properties" injected
2113+
string_params = funcs_by_name["string_schema_tool"].parameters()
2114+
assert "properties" not in string_params
2115+
assert string_params["type"] == "string"
2116+
2117+
# Empty schema (no "type" key) must NOT have "properties" injected
2118+
empty_params = funcs_by_name["empty_schema_tool"].parameters()
2119+
assert "properties" not in empty_params
2120+
2121+
# Original inputSchema dicts must not be mutated
2122+
assert "properties" not in original_zero_arg_schema
2123+
assert "properties" not in original_string_schema
2124+
assert "properties" not in original_empty_schema
2125+
20992126

21002127
async def test_load_prompts_with_pagination():
21012128
"""Test that load_prompts handles pagination correctly."""

0 commit comments

Comments
 (0)