@@ -1184,11 +1184,13 @@ def test_parse_response_from_openai_with_mcp_server_tool_result() -> None:
11841184 assert result_content .output is not None
11851185
11861186
1187- def test_parse_chunk_from_openai_with_mcp_call_result () -> None :
1188- """Test _parse_chunk_from_openai with MCP call output."""
1187+ def test_parse_chunk_from_openai_with_mcp_call_added_defers_result () -> None :
1188+ """Test that response.output_item.added for mcp_call emits only the call, not the result.
1189+
1190+ The result is deferred to response.mcp_call.completed.
1191+ """
11891192 client = OpenAIResponsesClient (model_id = "test-model" , api_key = "test-key" )
11901193
1191- # Mock event with MCP call that has output
11921194 mock_event = MagicMock ()
11931195 mock_event .type = "response.output_item.added"
11941196
@@ -1199,8 +1201,9 @@ def test_parse_chunk_from_openai_with_mcp_call_result() -> None:
11991201 mock_item .name = "fetch_resource"
12001202 mock_item .server_label = "ResourceServer"
12011203 mock_item .arguments = {"resource_id" : "123" }
1202- # Use proper content structure that _parse_content can handle
1203- mock_item .result = [{"type" : "text" , "text" : "test result" }]
1204+ mock_item .result = None
1205+ mock_item .output = None
1206+ mock_item .outputs = None
12041207
12051208 mock_event .item = mock_item
12061209 mock_event .output_index = 0
@@ -1209,18 +1212,69 @@ def test_parse_chunk_from_openai_with_mcp_call_result() -> None:
12091212
12101213 update = client ._parse_chunk_from_openai (mock_event , options = {}, function_call_ids = function_call_ids )
12111214
1212- # Should have both call and result in contents
1213- assert len (update .contents ) == 2
1214- call_content , result_content = update .contents
1215+ # Should have only the call content — result is deferred
1216+ assert len (update .contents ) == 1
1217+ call_content = update .contents [ 0 ]
12151218
12161219 assert call_content .type == "mcp_server_tool_call"
12171220 assert call_content .call_id in ["mcp_call_456" , "call_456" ]
12181221 assert call_content .tool_name == "fetch_resource"
12191222
1223+ # No result should be emitted at this point
1224+ result_contents = [c for c in update .contents if c .type == "mcp_server_tool_result" ]
1225+ assert len (result_contents ) == 0
1226+
1227+
1228+ def test_parse_chunk_from_openai_with_mcp_call_completed () -> None :
1229+ """Test that response.mcp_call.completed emits mcp_server_tool_result with output."""
1230+ client = OpenAIResponsesClient (model_id = "test-model" , api_key = "test-key" )
1231+
1232+ mock_event = MagicMock ()
1233+ mock_event .type = "response.mcp_call.completed"
1234+
1235+ mock_item = MagicMock ()
1236+ mock_item .type = "mcp_call"
1237+ mock_item .id = "mcp_call_456"
1238+ mock_item .output = "The weather in Seattle is 72F and sunny."
1239+ mock_event .item = mock_item
1240+
1241+ function_call_ids : dict [int , tuple [str , str ]] = {}
1242+
1243+ update = client ._parse_chunk_from_openai (mock_event , options = {}, function_call_ids = function_call_ids )
1244+
1245+ assert len (update .contents ) == 1
1246+ result_content = update .contents [0 ]
1247+
12201248 assert result_content .type == "mcp_server_tool_result"
1221- assert result_content .call_id in ["mcp_call_456" , "call_456" ]
1222- # Verify the output was parsed
1249+ assert result_content .call_id == "mcp_call_456"
12231250 assert result_content .output is not None
1251+ assert len (result_content .output ) == 1
1252+ assert result_content .output [0 ].text == "The weather in Seattle is 72F and sunny."
1253+
1254+
1255+ def test_parse_chunk_from_openai_with_mcp_call_completed_no_output () -> None :
1256+ """Test that response.mcp_call.completed with no output emits result with None output."""
1257+ client = OpenAIResponsesClient (model_id = "test-model" , api_key = "test-key" )
1258+
1259+ mock_event = MagicMock ()
1260+ mock_event .type = "response.mcp_call.completed"
1261+
1262+ mock_item = MagicMock ()
1263+ mock_item .type = "mcp_call"
1264+ mock_item .id = "mcp_call_789"
1265+ mock_item .output = None
1266+ mock_event .item = mock_item
1267+
1268+ function_call_ids : dict [int , tuple [str , str ]] = {}
1269+
1270+ update = client ._parse_chunk_from_openai (mock_event , options = {}, function_call_ids = function_call_ids )
1271+
1272+ assert len (update .contents ) == 1
1273+ result_content = update .contents [0 ]
1274+
1275+ assert result_content .type == "mcp_server_tool_result"
1276+ assert result_content .call_id == "mcp_call_789"
1277+ assert result_content .output is None
12241278
12251279
12261280def test_prepare_message_for_openai_with_function_approval_response () -> None :
0 commit comments