Skip to content

Commit 41da08b

Browse files
CopilotCopilot
andcommitted
Add regression test for MCP tool result event projection (microsoft#5897)
Add a streaming test that exercises multiple consecutive MCP tool invocations with JSON payloads, verifying that TOOL_EXECUTION_START and TOOL_EXECUTION_COMPLETE events are projected as function_call and function_result content items with correct call_id, tool name, and result payload. Removes unrelated formatting-only changes from prior iteration. Co-authored-by: Copilot <[email protected]>
1 parent edcc786 commit 41da08b

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

‎python/packages/github_copilot/tests/test_github_copilot_agent.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,87 @@ def mock_on(handler: Any) -> Any:
920920
assert responses[3].role == "assistant"
921921
assert responses[3].contents[0].type == "text"
922922

923+
async def test_run_streaming_multiple_tool_calls_with_json_results(
924+
self,
925+
mock_client: MagicMock,
926+
mock_session: MagicMock,
927+
session_idle_event: SessionEvent,
928+
) -> None:
929+
"""Test that multiple tool calls with JSON results are all projected correctly."""
930+
json_payloads = [
931+
'{"users": [{"id": "u1", "name": "Alice"}, {"id": "u2", "name": "Bob"}]}',
932+
'{"tickets": [{"id": "INC001", "state": "open", "short_description": "VPN issue"}]}',
933+
'{"groups": [{"id": "g1", "displayName": "IT Admins", "members": 42}]}',
934+
]
935+
tool_names = ["msgraph_list_users", "sn_query_table", "msgraph_list_groups"]
936+
937+
events: list[SessionEvent] = []
938+
for i, (name, payload) in enumerate(zip(tool_names, json_payloads)):
939+
call_id = f"call_{i:03d}"
940+
941+
# Tool start
942+
start_data = MagicMock()
943+
start_data.tool_call_id = call_id
944+
start_data.tool_name = name
945+
start_data.arguments = {"query": f"test_{i}"}
946+
events.append(
947+
SessionEvent(
948+
data=start_data,
949+
id=uuid4(),
950+
timestamp=datetime.now(timezone.utc),
951+
type=SessionEventType.TOOL_EXECUTION_START,
952+
)
953+
)
954+
955+
# Tool complete
956+
complete_data = MagicMock()
957+
complete_data.tool_call_id = call_id
958+
complete_data.result = ToolExecutionCompleteResult(content=payload)
959+
complete_data.success = True
960+
complete_data.error = None
961+
events.append(
962+
SessionEvent(
963+
data=complete_data,
964+
id=uuid4(),
965+
timestamp=datetime.now(timezone.utc),
966+
type=SessionEventType.TOOL_EXECUTION_COMPLETE,
967+
)
968+
)
969+
970+
events.append(session_idle_event)
971+
972+
def mock_on(handler: Any) -> Any:
973+
for event in events:
974+
handler(event)
975+
return lambda: None
976+
977+
mock_session.on = mock_on
978+
979+
agent = GitHubCopilotAgent(client=mock_client)
980+
responses: list[AgentResponseUpdate] = []
981+
async for update in agent.run("List users, tickets, and groups", stream=True):
982+
responses.append(update)
983+
984+
# Should have 3 tool calls + 3 tool results = 6 updates
985+
assert len(responses) == 6
986+
987+
# Verify each pair: function_call followed by function_result
988+
for i in range(3):
989+
call_update = responses[i * 2]
990+
result_update = responses[i * 2 + 1]
991+
call_id = f"call_{i:03d}"
992+
993+
assert call_update.role == "assistant"
994+
assert call_update.contents[0].type == "function_call"
995+
assert call_update.contents[0].call_id == call_id
996+
assert call_update.contents[0].name == tool_names[i]
997+
998+
assert result_update.role == "tool"
999+
assert result_update.contents[0].type == "function_result"
1000+
assert result_update.contents[0].call_id == call_id
1001+
assert result_update.contents[0].result == json_payloads[i]
1002+
assert result_update.contents[0].exception is None
1003+
9231004

9241005
class TestGitHubCopilotAgentSessionManagement:
9251006
"""Test cases for session management."""

0 commit comments

Comments
 (0)