Skip to content

Commit e0858c9

Browse files
CopilotCopilot
andcommitted
Fix streaming path to deliver mcp_server_tool_result content (#4814)
Remove premature mcp_server_tool_result emission from the response.output_item.added/mcp_call handler — at that point the MCP server has not yet responded and output is always None. Add a handler for response.mcp_call.completed that emits mcp_server_tool_result with the actual tool output, matching the non-streaming path behavior. Co-authored-by: Copilot <[email protected]>
1 parent 7645ec4 commit e0858c9

2 files changed

Lines changed: 81 additions & 31 deletions

File tree

‎python/packages/core/agent_framework/openai/_responses_client.py‎

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,27 +1932,7 @@ def _parse_chunk_from_openai(
19321932
raw_representation=event_item,
19331933
)
19341934
)
1935-
result_output = (
1936-
getattr(event_item, "result", None)
1937-
or getattr(event_item, "output", None)
1938-
or getattr(event_item, "outputs", None)
1939-
)
1940-
parsed_output: list[Content] | None = None
1941-
if result_output:
1942-
normalized = ( # pyright: ignore[reportUnknownVariableType]
1943-
result_output
1944-
if isinstance(result_output, Sequence)
1945-
and not isinstance(result_output, (str, bytes, MutableMapping))
1946-
else [result_output]
1947-
)
1948-
parsed_output = [Content.from_dict(output_item) for output_item in normalized] # pyright: ignore[reportArgumentType,reportUnknownVariableType]
1949-
contents.append(
1950-
Content.from_mcp_server_tool_result(
1951-
call_id=call_id,
1952-
output=parsed_output,
1953-
raw_representation=event_item,
1954-
)
1955-
)
1935+
# Result deferred to response.mcp_call.completed
19561936
case "code_interpreter_call": # ResponseOutputCodeInterpreterCall
19571937
call_id = getattr(event_item, "call_id", None) or getattr(event_item, "id", None)
19581938
outputs: list[Content] = []
@@ -2222,6 +2202,22 @@ def _get_ann_value(key: str) -> Any:
22222202
)
22232203
else:
22242204
logger.debug("Unparsed annotation type in streaming: %s", ann_type)
2205+
case "response.mcp_call.completed":
2206+
item = event.item
2207+
call_id = getattr(item, "id", None) or ""
2208+
output_text = getattr(item, "output", None)
2209+
parsed_output: list[Content] | None = (
2210+
[Content.from_text(text=output_text)]
2211+
if isinstance(output_text, str)
2212+
else None
2213+
)
2214+
contents.append(
2215+
Content.from_mcp_server_tool_result(
2216+
call_id=call_id,
2217+
output=parsed_output,
2218+
raw_representation=event,
2219+
)
2220+
)
22252221
case _:
22262222
logger.debug("Unparsed event of type: %s: %s", event.type, event)
22272223

‎python/packages/core/tests/openai/test_openai_responses_client.py‎

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

12261280
def test_prepare_message_for_openai_with_function_approval_response() -> None:

0 commit comments

Comments
 (0)