5555 HostedVectorStoreContent ,
5656 ImageGenerationToolCallContent ,
5757 ImageGenerationToolResultContent ,
58+ MCPServerToolCallContent ,
59+ MCPServerToolResultContent ,
5860 Role ,
5961 TextContent ,
6062 TextReasoningContent ,
@@ -322,7 +324,17 @@ def _prepare_tools_for_openai(
322324 case HostedImageGenerationTool ():
323325 mapped_tool : dict [str , Any ] = {"type" : "image_generation" }
324326 if tool .options :
325- mapped_tool .update (tool .options )
327+ option_mapping = {
328+ "count" : "n" ,
329+ "image_size" : "size" ,
330+ "media_type" : "media_type" ,
331+ "model_id" : "model" ,
332+ "response_format" : "response_format" ,
333+ "streaming_count" : "partial_images" ,
334+ }
335+ for key , value in tool .options .items ():
336+ mapped_key = option_mapping .get (key , key )
337+ mapped_tool [mapped_key ] = value
326338 if tool .additional_properties :
327339 mapped_tool .update (tool .additional_properties )
328340 response_tools .append (mapped_tool )
@@ -334,7 +346,19 @@ def _prepare_tools_for_openai(
334346
335347 # Special handling for image_generation tools
336348 if tool_dict .get ("type" ) == "image_generation" :
337- response_tools .append (tool_dict .copy ())
349+ mapped_tool = tool_dict .copy ()
350+ option_mapping = {
351+ "count" : "n" ,
352+ "image_size" : "size" ,
353+ "media_type" : "media_type" ,
354+ "model_id" : "model" ,
355+ "response_format" : "response_format" ,
356+ "streaming_count" : "partial_images" ,
357+ }
358+ for key , value in list (mapped_tool .items ()):
359+ if key in option_mapping :
360+ mapped_tool [option_mapping [key ]] = mapped_tool .pop (key )
361+ response_tools .append (mapped_tool )
338362 else :
339363 response_tools .append (tool_dict )
340364 return response_tools
@@ -802,6 +826,37 @@ def _parse_response_from_openai(
802826 ),
803827 )
804828 )
829+ case "mcp_call" :
830+ call_id = getattr (item , "id" , None ) or getattr (item , "call_id" , None ) or ""
831+ contents .append (
832+ MCPServerToolCallContent (
833+ call_id = call_id ,
834+ tool_name = getattr (item , "name" , "" ) or "" ,
835+ server_name = getattr (item , "server_label" , None ),
836+ arguments = getattr (item , "arguments" , None ),
837+ raw_representation = item ,
838+ )
839+ )
840+ result_output = (
841+ getattr (item , "result" , None )
842+ or getattr (item , "output" , None )
843+ or getattr (item , "outputs" , None )
844+ )
845+ parsed_output : list [Contents ] | None = None
846+ if result_output :
847+ normalized = (
848+ result_output
849+ if isinstance (result_output , Sequence ) and not isinstance (result_output , (str , bytes , MutableMapping ))
850+ else [result_output ]
851+ )
852+ parsed_output = [_parse_content (output_item ) for output_item in normalized ]
853+ contents .append (
854+ MCPServerToolResultContent (
855+ call_id = call_id ,
856+ output = parsed_output ,
857+ raw_representation = item ,
858+ )
859+ )
805860 case "image_generation_call" : # ResponseOutputImageGenerationCall
806861 outputs : list ["Contents" ] = []
807862 if item .result :
@@ -992,6 +1047,38 @@ def _parse_chunk_from_openai(
9921047 ),
9931048 )
9941049 )
1050+ case "mcp_call" :
1051+ call_id = getattr (event_item , "id" , None ) or getattr (event_item , "call_id" , None ) or ""
1052+ contents .append (
1053+ MCPServerToolCallContent (
1054+ call_id = call_id ,
1055+ tool_name = getattr (event_item , "name" , "" ) or "" ,
1056+ server_name = getattr (event_item , "server_label" , None ),
1057+ arguments = getattr (event_item , "arguments" , None ),
1058+ raw_representation = event_item ,
1059+ )
1060+ )
1061+ result_output = (
1062+ getattr (event_item , "result" , None )
1063+ or getattr (event_item , "output" , None )
1064+ or getattr (event_item , "outputs" , None )
1065+ )
1066+ parsed_output : list [Contents ] | None = None
1067+ if result_output :
1068+ normalized = (
1069+ result_output
1070+ if isinstance (result_output , Sequence )
1071+ and not isinstance (result_output , (str , bytes , MutableMapping ))
1072+ else [result_output ]
1073+ )
1074+ parsed_output = [_parse_content (output_item ) for output_item in normalized ]
1075+ contents .append (
1076+ MCPServerToolResultContent (
1077+ call_id = call_id ,
1078+ output = parsed_output ,
1079+ raw_representation = event_item ,
1080+ )
1081+ )
9951082 case "code_interpreter_call" : # ResponseOutputCodeInterpreterCall
9961083 call_id = getattr (event_item , "call_id" , None ) or getattr (event_item , "id" , None )
9971084 outputs : list [Contents ] = []
0 commit comments