@@ -379,6 +379,43 @@ def __iter__(self) -> Iterable[Callable]:
379379 """
380380 return iter (self ._tools )
381381
382+ def _fix_array_schemas (self , schema ):
383+ """
384+ Fix array schemas by adding missing 'items' attribute required by OpenAI.
385+
386+ This ensures compatibility with OpenAI's function calling format which
387+ requires array types to specify the type of items they contain.
388+
389+ Args:
390+ schema: The schema dictionary to fix
391+
392+ Returns:
393+ dict: The fixed schema
394+ """
395+ if not isinstance (schema , dict ):
396+ return schema
397+
398+ # Create a copy to avoid modifying the original
399+ fixed_schema = schema .copy ()
400+
401+ # Fix array types at the current level
402+ if fixed_schema .get ("type" ) == "array" and "items" not in fixed_schema :
403+ # Add a default items schema for arrays without it
404+ fixed_schema ["items" ] = {"type" : "string" }
405+
406+ # Recursively fix nested schemas
407+ if "properties" in fixed_schema :
408+ fixed_properties = {}
409+ for prop_name , prop_schema in fixed_schema ["properties" ].items ():
410+ fixed_properties [prop_name ] = self ._fix_array_schemas (prop_schema )
411+ fixed_schema ["properties" ] = fixed_properties
412+
413+ # Fix items schema if it exists
414+ if "items" in fixed_schema :
415+ fixed_schema ["items" ] = self ._fix_array_schemas (fixed_schema ["items" ])
416+
417+ return fixed_schema
418+
382419 def to_openai_tool (self ):
383420 """Convert the MCP tool to an OpenAI-compatible tool definition.
384421
@@ -404,7 +441,8 @@ def to_openai_tool(self):
404441 # Create OpenAI tool definition
405442 parameters = {}
406443 if hasattr (tool , 'inputSchema' ) and tool .inputSchema :
407- parameters = tool .inputSchema
444+ # Fix array schemas to include 'items' attribute
445+ parameters = self ._fix_array_schemas (tool .inputSchema )
408446 else :
409447 # Create a minimal schema if none exists
410448 parameters = {
0 commit comments