Skip to content

Commit 124726c

Browse files
author
Jesus Terrazas
committed
Modify agent instead of creating a new instance
1 parent a9ecaeb commit 124726c

2 files changed

Lines changed: 32 additions & 40 deletions

File tree

libraries/microsoft-agents-a365-tooling-extensions-google/microsoft_agents_a365/tooling/extensions/google/services/mcp_tool_registration_service.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ async def add_tool_servers_to_agent(
5555
auth_handler_name: str,
5656
context: TurnContext,
5757
auth_token: Optional[str] = None,
58-
) -> Agent:
58+
) -> None:
5959
"""
60-
Add new MCP servers to the agent by creating a new Agent instance.
60+
Add new MCP servers to the agent from MCP Platform.
6161
62-
Note: Due to Google ADK Agent design, this method creates a new Agent
63-
instance with all MCP servers (existing + new) properly initialized.
62+
Note: Modifies the provided agent in place to add new MCP tool servers.
6463
6564
Args:
6665
agent: The existing agent to add servers to.
@@ -71,7 +70,7 @@ async def add_tool_servers_to_agent(
7170
If not provided, will be obtained using `auth` and `context`.
7271
7372
Returns:
74-
New Agent instance with all MCP servers configured.
73+
None
7574
"""
7675
if not auth_token:
7776
scopes = get_mcp_platform_authentication_scope()
@@ -121,15 +120,12 @@ async def add_tool_servers_to_agent(
121120
# Combine existing tools with new MCP servers
122121
all_tools = list(agent.tools) + mcp_servers_info
123122

124-
self._logger.info(f"Creating new agent with {len(all_tools)} total tools")
125-
126-
return Agent(
127-
name=agent.name,
128-
model=agent.model,
129-
description=agent.description,
130-
tools=all_tools,
123+
self._logger.info(
124+
f"Successfully configured {len(all_tools)} total MCP tool servers for agent"
131125
)
132126

127+
agent.tools = all_tools
128+
133129
async def cleanup(self):
134130
"""Clean up any resources used by the service."""
135131
try:

tests/tooling/extensions/google/test_mcp_tool_registration_service.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,6 @@ async def test_add_tool_servers_creates_mcp_toolsets(
192192
patch(
193193
"microsoft_agents_a365.tooling.extensions.google.services.mcp_tool_registration_service.McpToolset"
194194
) as mock_toolset_class,
195-
patch(
196-
"microsoft_agents_a365.tooling.extensions.google.services.mcp_tool_registration_service.Agent"
197-
) as mock_agent_class,
198195
):
199196
# Setup mocks
200197
mock_utility.resolve_agent_identity.return_value = "agent-123"
@@ -207,12 +204,14 @@ async def test_add_tool_servers_creates_mcp_toolsets(
207204
mock_toolset = MagicMock()
208205
mock_toolset_class.return_value = mock_toolset
209206

210-
mock_agent_class.return_value = mock_agent
211-
212207
from microsoft_agents_a365.tooling.extensions.google import McpToolRegistrationService
213208

214209
service = McpToolRegistrationService()
215210

211+
# Set up existing tools on the agent
212+
existing_tool = MagicMock()
213+
mock_agent.tools = [existing_tool]
214+
216215
# Act
217216
await service.add_tool_servers_to_agent(
218217
agent=mock_agent,
@@ -225,22 +224,22 @@ async def test_add_tool_servers_creates_mcp_toolsets(
225224
# Assert
226225
mock_toolset_class.assert_called_once()
227226
assert mock_toolset in service._connected_servers
227+
# Verify agent tools were updated in place with both existing and new tools
228+
assert existing_tool in mock_agent.tools
229+
assert mock_toolset in mock_agent.tools
228230

229231
@pytest.mark.asyncio
230-
async def test_add_tool_servers_returns_new_agent(
232+
async def test_add_tool_servers_modifies_agent_in_place(
231233
self, mock_agent, mock_authorization, mock_turn_context
232234
):
233-
"""Test that a new Agent instance is returned."""
235+
"""Test that the agent is modified in place and method returns None."""
234236
with (
235237
patch(
236238
"microsoft_agents_a365.tooling.extensions.google.services.mcp_tool_registration_service.McpToolServerConfigurationService"
237239
) as mock_config_service_class,
238240
patch(
239241
"microsoft_agents_a365.tooling.extensions.google.services.mcp_tool_registration_service.Utility"
240242
) as mock_utility,
241-
patch(
242-
"microsoft_agents_a365.tooling.extensions.google.services.mcp_tool_registration_service.Agent"
243-
) as mock_agent_class,
244243
):
245244
# Setup mocks
246245
mock_utility.resolve_agent_identity.return_value = "agent-123"
@@ -250,13 +249,14 @@ async def test_add_tool_servers_returns_new_agent(
250249
mock_config_service.list_tool_servers = AsyncMock(return_value=[])
251250
mock_config_service_class.return_value = mock_config_service
252251

253-
new_agent = MagicMock()
254-
mock_agent_class.return_value = new_agent
255-
256252
from microsoft_agents_a365.tooling.extensions.google import McpToolRegistrationService
257253

258254
service = McpToolRegistrationService()
259255

256+
# Set up existing tools on the agent
257+
existing_tool = MagicMock()
258+
mock_agent.tools = [existing_tool]
259+
260260
# Act
261261
result = await service.add_tool_servers_to_agent(
262262
agent=mock_agent,
@@ -266,14 +266,9 @@ async def test_add_tool_servers_returns_new_agent(
266266
auth_token="test-token",
267267
)
268268

269-
# Assert
270-
assert result == new_agent
271-
mock_agent_class.assert_called_once_with(
272-
name=mock_agent.name,
273-
model=mock_agent.model,
274-
description=mock_agent.description,
275-
tools=[],
276-
)
269+
# Assert - method returns None and modifies agent in place
270+
assert result is None
271+
assert existing_tool in mock_agent.tools
277272

278273
@pytest.mark.asyncio
279274
async def test_add_tool_servers_handles_toolset_creation_error(
@@ -290,9 +285,6 @@ async def test_add_tool_servers_handles_toolset_creation_error(
290285
patch(
291286
"microsoft_agents_a365.tooling.extensions.google.services.mcp_tool_registration_service.McpToolset"
292287
) as mock_toolset_class,
293-
patch(
294-
"microsoft_agents_a365.tooling.extensions.google.services.mcp_tool_registration_service.Agent"
295-
) as mock_agent_class,
296288
):
297289
# Setup mocks
298290
mock_utility.resolve_agent_identity.return_value = "agent-123"
@@ -305,12 +297,14 @@ async def test_add_tool_servers_handles_toolset_creation_error(
305297
# Make toolset creation fail
306298
mock_toolset_class.side_effect = Exception("Connection failed")
307299

308-
mock_agent_class.return_value = mock_agent
309-
310300
from microsoft_agents_a365.tooling.extensions.google import McpToolRegistrationService
311301

312302
service = McpToolRegistrationService()
313303

304+
# Set up existing tools on the agent
305+
existing_tool = MagicMock()
306+
mock_agent.tools = [existing_tool]
307+
314308
# Act - should not raise
315309
result = await service.add_tool_servers_to_agent(
316310
agent=mock_agent,
@@ -320,9 +314,11 @@ async def test_add_tool_servers_handles_toolset_creation_error(
320314
auth_token="test-token",
321315
)
322316

323-
# Assert - should still return an agent, just without the failed toolset
324-
assert result is not None
317+
# Assert - returns None, agent modified in place, no failed toolsets added
318+
assert result is None
325319
assert len(service._connected_servers) == 0
320+
# Existing tools should still be present
321+
assert existing_tool in mock_agent.tools
326322

327323

328324
class TestCleanup:

0 commit comments

Comments
 (0)