Skip to content

Commit 48b3f26

Browse files
committed
Refactor agent client handling and improve error checks
Introduces a get_chat_client method to MCPEnabledBase for consistent agent client retrieval and updates agent initialization in FoundryAgentTemplate and ReasoningAgentTemplate to use it. Enhances error handling and validation in API router endpoints, including improved user and team configuration checks, and updates RAI validation calls to support additional parameters.
1 parent 8680db1 commit 48b3f26

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/backend/v4/magentic_agents/common/lifecycle.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ async def _after_open(self) -> None:
135135
"""Subclasses must build self._agent here."""
136136
raise NotImplementedError
137137

138+
def get_chat_client(self, chat_client) -> AzureAIAgentClient:
139+
"""Return the underlying ChatClientProtocol (AzureAIAgentClient)."""
140+
if chat_client:
141+
return chat_client
142+
if self._agent:
143+
return self._agent.chat_client # type: ignore
144+
chat_client = AzureAIAgentClient(
145+
project_endpoint=self.project_endpoint,
146+
model_deployment_name=self.model_deployment_name,
147+
async_credential=self.creds,
148+
)
149+
return chat_client
150+
138151
async def get_database_team_agent(self) -> Optional[AzureAIAgentClient]:
139152
"""Retrieve existing team agent from database, if any."""
140153
chat_client = None

src/backend/v4/magentic_agents/foundry_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ async def _after_open(self) -> None:
248248
self.logger.info("Initializing agent in MCP mode.")
249249
tools = await self._collect_tools()
250250
self._agent = ChatAgent(
251-
chat_client=chatClient or self.client,
251+
chat_client=self.get_chat_client(chatClient),
252252
instructions=self.agent_instructions,
253253
name=self.agent_name,
254254
description=self.agent_description,

src/backend/v4/magentic_agents/reasoning_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ async def _after_open(self) -> None:
110110
chatClient = await self.get_database_team_agent()
111111

112112
self._agent = ChatAgent(
113-
chat_client = chatClient or self.client,
113+
chat_client = self.get_chat_client(chatClient),
114114
instructions=self.agent_instructions,
115115
name=self.agent_name,
116116
description=self.agent_description,

0 commit comments

Comments
 (0)