Skip to content

Commit 50bbfdd

Browse files
authored
Ensure MCP works when inputSchema.properties is missing (#454)
Resolves #449 - TLDR, [OpenAI's API](https://platform.openai.com/docs/api-reference/responses/create) expects the properties field to be present, whereas the MCP schema explicitly allows omitting the properties field. [MCP Spec](https://github.com/modelcontextprotocol/specification/blob/main/schema/2025-03-26/schema.json)
1 parent 064e25b commit 50bbfdd

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

src/agents/mcp/util.py

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ def to_function_tool(
5959
"""Convert an MCP tool to an Agents SDK function tool."""
6060
invoke_func = functools.partial(cls.invoke_mcp_tool, server, tool)
6161
schema, is_strict = tool.inputSchema, False
62+
63+
# MCP spec doesn't require the inputSchema to have `properties`, but OpenAI spec does.
64+
if "properties" not in schema:
65+
schema["properties"] = {}
66+
6267
if convert_schemas_to_strict:
6368
try:
6469
schema = ensure_strict_json_schema(schema)

tests/mcp/test_mcp_util.py

+36-7
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ async def test_get_all_function_tools():
6363

6464
for idx, tool in enumerate(tools):
6565
assert isinstance(tool, FunctionTool)
66-
assert tool.params_json_schema == schemas[idx]
66+
if schemas[idx] == {}:
67+
assert tool.params_json_schema == snapshot({"properties": {}})
68+
else:
69+
assert tool.params_json_schema == schemas[idx]
6770
assert tool.name == names[idx]
6871

6972
# Also make sure it works with strict schemas
@@ -167,10 +170,7 @@ async def test_agent_convert_schemas_true():
167170

168171
# Checks that additionalProperties is set to False
169172
assert bar_tool.params_json_schema == snapshot(
170-
{
171-
"type": "object",
172-
"additionalProperties": {"type": "string"},
173-
}
173+
{"type": "object", "additionalProperties": {"type": "string"}, "properties": {}}
174174
)
175175
assert bar_tool.strict_json_schema is False, "bar_tool should not be strict"
176176

@@ -220,7 +220,9 @@ async def test_agent_convert_schemas_false():
220220
assert foo_tool.params_json_schema == strict_schema
221221
assert foo_tool.strict_json_schema is False, "Shouldn't be converted unless specified"
222222

223-
assert bar_tool.params_json_schema == non_strict_schema
223+
assert bar_tool.params_json_schema == snapshot(
224+
{"type": "object", "additionalProperties": {"type": "string"}, "properties": {}}
225+
)
224226
assert bar_tool.strict_json_schema is False
225227

226228
assert baz_tool.params_json_schema == possible_to_convert_schema
@@ -255,8 +257,35 @@ async def test_agent_convert_schemas_unset():
255257
assert foo_tool.params_json_schema == strict_schema
256258
assert foo_tool.strict_json_schema is False, "Shouldn't be converted unless specified"
257259

258-
assert bar_tool.params_json_schema == non_strict_schema
260+
assert bar_tool.params_json_schema == snapshot(
261+
{"type": "object", "additionalProperties": {"type": "string"}, "properties": {}}
262+
)
259263
assert bar_tool.strict_json_schema is False
260264

261265
assert baz_tool.params_json_schema == possible_to_convert_schema
262266
assert baz_tool.strict_json_schema is False, "Shouldn't be converted unless specified"
267+
268+
269+
@pytest.mark.asyncio
270+
async def test_util_adds_properties():
271+
"""The MCP spec doesn't require the inputSchema to have `properties`, so we need to add it
272+
if it's missing.
273+
"""
274+
schema = {
275+
"type": "object",
276+
"description": "Test tool",
277+
}
278+
279+
server = FakeMCPServer()
280+
server.add_tool("test_tool", schema)
281+
282+
tools = await MCPUtil.get_all_function_tools([server], convert_schemas_to_strict=False)
283+
tool = next(tool for tool in tools if tool.name == "test_tool")
284+
285+
assert isinstance(tool, FunctionTool)
286+
assert "properties" in tool.params_json_schema
287+
assert tool.params_json_schema["properties"] == {}
288+
289+
assert tool.params_json_schema == snapshot(
290+
{"type": "object", "description": "Test tool", "properties": {}}
291+
)

0 commit comments

Comments
 (0)