Skip to content

Commit 4aee605

Browse files
Johan Brobergclaude
andcommitted
Address code review feedback: type annotations, test fixes, and code quality
- CRM-003: Add return type annotation to add_tool_servers_to_agent - CRM-004: Add type annotations to _cleanup_servers and cleanup_all_servers - CRM-006: Fix conversion error test to actually test error handling - CRM-007: Update implicit boolean check to explicit None check - CRM-008: Remove redundant local import of Agent - CRM-010: Use bare raise instead of raise e - CRM-011: Rename test to match behavior (skips_message not uses_empty_string) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4cb074f commit 4aee605

3 files changed

Lines changed: 15 additions & 19 deletions

File tree

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def add_tool_servers_to_agent(
6969
auth_handler_name: str,
7070
context: TurnContext,
7171
auth_token: Optional[str] = None,
72-
):
72+
) -> Agent:
7373
"""
7474
Add new MCP servers to the agent by creating a new Agent instance.
7575
@@ -88,7 +88,7 @@ async def add_tool_servers_to_agent(
8888
New Agent instance with all MCP servers, or original agent if no new servers
8989
"""
9090

91-
if not auth_token:
91+
if auth_token is None:
9292
scopes = get_mcp_platform_authentication_scope()
9393
authToken = await auth.exchange_token(context, scopes, auth_handler_name)
9494
auth_token = authToken.token
@@ -190,8 +190,6 @@ async def add_tool_servers_to_agent(
190190
all_mcp_servers = existing_mcp_servers + new_mcp_servers
191191

192192
# Recreate the agent with all MCP servers
193-
from agents import Agent
194-
195193
new_agent = Agent(
196194
name=agent.name,
197195
model=agent.model,
@@ -223,12 +221,12 @@ async def add_tool_servers_to_agent(
223221
# Clean up connected servers if agent creation fails
224222
self._logger.error(f"Failed to recreate agent with new MCP servers: {e}")
225223
await self._cleanup_servers(connected_servers)
226-
raise e
224+
raise
227225

228226
self._logger.info("No new MCP servers to add to agent")
229227
return agent
230228

231-
async def _cleanup_servers(self, servers):
229+
async def _cleanup_servers(self, servers: List[MCPServerStreamableHttp]) -> None:
232230
"""Clean up connected MCP servers"""
233231
for server in servers:
234232
try:
@@ -238,7 +236,7 @@ async def _cleanup_servers(self, servers):
238236
# Log cleanup errors but don't raise them
239237
self._logger.debug(f"Error during server cleanup: {e}")
240238

241-
async def cleanup_all_servers(self):
239+
async def cleanup_all_servers(self) -> None:
242240
"""Clean up all connected MCP servers"""
243241
if hasattr(self, "_connected_servers"):
244242
await self._cleanup_servers(self._connected_servers)

tests/tooling/extensions/openai/test_message_conversion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ def test_convert_message_with_list_content(self, service):
139139

140140
# CV-11
141141
@pytest.mark.unit
142-
def test_convert_empty_content_uses_empty_string(self, service):
143-
"""Test that empty content is handled - message skipped due to validator."""
142+
def test_convert_empty_content_skips_message(self, service):
143+
"""Test that messages with empty content are skipped during conversion."""
144144
message = MockUserMessage(content="")
145145

146146
result = service._convert_single_message(message)

tests/tooling/extensions/openai/test_send_chat_history.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,22 +398,20 @@ async def test_send_chat_history_messages_conversion_error(
398398
self, service, mock_turn_context
399399
):
400400
"""Test send_chat_history_messages handles conversion errors gracefully."""
401-
# Create a message that might cause conversion issues but still has content
402-
problematic_message = MockUserMessage(content="Valid content")
401+
sample_messages = [MockUserMessage(content="Hello")]
403402

404403
with patch.object(
405-
service.config_service,
406-
"send_chat_history",
407-
new_callable=AsyncMock,
408-
) as mock_send:
409-
mock_send.return_value = OperationResult.success()
404+
service, "_convert_openai_messages_to_chat_history"
405+
) as mock_convert:
406+
mock_convert.side_effect = Exception("Conversion failed")
410407

411-
# Should not raise, should handle gracefully
412408
result = await service.send_chat_history_messages(
413-
mock_turn_context, [problematic_message]
409+
mock_turn_context, sample_messages
414410
)
415411

416-
assert result.succeeded is True
412+
assert result.succeeded is False
413+
assert len(result.errors) == 1
414+
assert "Conversion failed" in str(result.errors[0].message)
417415

418416
# EH-05
419417
@pytest.mark.asyncio

0 commit comments

Comments
 (0)