|
3 | 3 | from collections.abc import AsyncIterable, AsyncIterator |
4 | 4 | from dataclasses import dataclass |
5 | 5 | from typing import Any, cast |
| 6 | +from unittest.mock import MagicMock |
6 | 7 |
|
7 | 8 | import pytest |
8 | 9 |
|
9 | 10 | from agent_framework import ( |
10 | 11 | AgentRunResponse, |
11 | 12 | AgentRunResponseUpdate, |
12 | 13 | BaseAgent, |
| 14 | + ChatAgent, |
13 | 15 | ChatMessage, |
14 | 16 | FunctionCallContent, |
15 | 17 | HandoffBuilder, |
|
20 | 22 | WorkflowEvent, |
21 | 23 | WorkflowOutputEvent, |
22 | 24 | ) |
| 25 | +from agent_framework._mcp import MCPTool |
| 26 | +from agent_framework._workflows._handoff import _clone_chat_agent |
23 | 27 |
|
24 | 28 |
|
25 | 29 | @dataclass |
@@ -368,3 +372,32 @@ async def async_termination(conv: list[ChatMessage]) -> bool: |
368 | 372 | user_messages = [msg for msg in final_conv_list if msg.role == Role.USER] |
369 | 373 | assert len(user_messages) == 2 |
370 | 374 | assert termination_call_count > 0 |
| 375 | + |
| 376 | + |
| 377 | +async def test_clone_chat_agent_preserves_mcp_tools() -> None: |
| 378 | + """Test that _clone_chat_agent preserves MCP tools when cloning an agent.""" |
| 379 | + mock_chat_client = MagicMock() |
| 380 | + |
| 381 | + mock_mcp_tool = MagicMock(spec=MCPTool) |
| 382 | + mock_mcp_tool.name = "test_mcp_tool" |
| 383 | + |
| 384 | + def sample_function() -> str: |
| 385 | + return "test" |
| 386 | + |
| 387 | + original_agent = ChatAgent( |
| 388 | + chat_client=mock_chat_client, |
| 389 | + name="TestAgent", |
| 390 | + instructions="Test instructions", |
| 391 | + tools=[mock_mcp_tool, sample_function], |
| 392 | + ) |
| 393 | + |
| 394 | + assert hasattr(original_agent, "_local_mcp_tools") |
| 395 | + assert len(original_agent._local_mcp_tools) == 1 |
| 396 | + assert original_agent._local_mcp_tools[0] == mock_mcp_tool |
| 397 | + |
| 398 | + cloned_agent = _clone_chat_agent(original_agent) |
| 399 | + |
| 400 | + assert hasattr(cloned_agent, "_local_mcp_tools") |
| 401 | + assert len(cloned_agent._local_mcp_tools) == 1 |
| 402 | + assert cloned_agent._local_mcp_tools[0] == mock_mcp_tool |
| 403 | + assert len(cloned_agent.chat_options.tools) == 1 |
0 commit comments