@@ -46,8 +46,8 @@ def __init__(
4646 team_config : TeamConfiguration | None = None ,
4747 memory_store : DatabaseBase | None = None ,
4848 ) -> None :
49- # Get project_client before calling super().__init__
50- project_client = config . get_ai_project_client ()
49+ # Defer project_client creation until async open() to use async credentials
50+ project_client = None # Will be created in parent's open() method
5151
5252 super ().__init__ (
5353 mcp = mcp_config ,
@@ -223,7 +223,7 @@ async def _create_azure_search_enabled_client(self, chatClient=None) -> Optional
223223 # The async_credential is needed for inference/streaming operations
224224 chat_client = AzureAIClient (
225225 project_client = self .project_client ,
226- # async_credential=self.creds if hasattr(self, 'creds') and self.creds else None,
226+ async_credential = self .creds if hasattr (self , 'creds' ) and self .creds else None ,
227227 agent_name = self .agent_name ,
228228 use_latest_version = True ,
229229 )
@@ -281,35 +281,53 @@ async def _after_open(self) -> None:
281281 self .logger .info ("Initializing agent in MCP mode." )
282282
283283 tools = await self ._collect_tools ()
284- # NOTE: Following Microsoft reference pattern - ChatAgent used directly
285284 self ._tools_for_invoke = tools if tools else None
286285 self ._tool_choice = "auto" if tools else "none"
287286
288- self ._agent = AzureAIClient (
289- project_client = self .project_client ,
290- use_latest_version = True ).create_agent (
291- name = self .agent_name ,
287+ # Convert agent_framework tools to Foundry FunctionTool format
288+ foundry_tools = []
289+ for tool in (tools or []):
290+ if hasattr (tool , 'to_function_tool' ):
291+ foundry_tools .append (tool .to_function_tool ())
292+ elif hasattr (tool , 'function' ):
293+ # For MCP tools or other function-based tools
294+ foundry_tools .append (FunctionTool (function = tool .function ))
295+
296+ # Create Azure agent version with tools
297+ azure_agent = await self .project_client .agents .create_version (
298+ agent_name = self .agent_name ,
299+ definition = PromptAgentDefinition (
300+ model = self .model_deployment_name ,
292301 instructions = self .agent_instructions ,
293- tools = tools if tools else None
302+ tools = foundry_tools if foundry_tools else None
294303 )
295- print ("mcp based agent: " ,self .agent_name )
296- # Create ChatAgent following reference pattern
297- # self._agent = ChatAgent(
298- # chat_client=self.get_chat_client(chatClient),
299- # instructions=self.agent_instructions,
300- # name=self.agent_name,
301- # description=self.agent_description,
302- # tools=tools if tools else None,
303- # tool_choice="auto" if tools else "none",
304- # temperature=temp,
305- # model_id=self.model_deployment_name,
306- # )
304+ )
305+
306+ self .logger .info (
307+ "Created Azure agent version (agent_name=%s) with %d tools." ,
308+ self .agent_name ,
309+ len (foundry_tools )
310+ )
311+
312+ # Use AzureAIClient with the created agent
313+ chat_client = AzureAIClient (
314+ project_client = self .project_client ,
315+ async_credential = self .creds if hasattr (self , 'creds' ) and self .creds else None ,
316+ agent_name = self .agent_name ,
317+ use_latest_version = True ,
318+ )
319+
320+ # Create ChatAgent with the Azure-backed client
321+ self ._agent = ChatAgent (
322+ chat_client = self .get_chat_client (chat_client ),
323+ tools = self ._tools_for_invoke ,
324+ tool_choice = self ._tool_choice ,
325+ store = True ,
326+ )
327+
307328 async for agent in self .project_client .agents .list ():
308329 print (f" Agent: { agent .name } " )
309330
310- async for version in self .project_client .agents .list_versions (agent_name = self .agent_name ):
311- print (f" Version: { version } " )
312-
313331 self .logger .info ("Initialized ChatAgent '%s'" , self .agent_name )
314332
315333 except Exception as ex :
@@ -338,7 +356,7 @@ async def invoke(self, prompt: str):
338356 messages = [ChatMessage (role = Role .USER , text = prompt )]
339357
340358 agent_saved = False
341- print ("invoke stream..." , self ._agent . name )
359+ print (f "invoke stream... agent_name= { self .agent_name } " )
342360 async for update in self ._agent .run_stream (messages ):
343361 # Save agent only once on first update (using agent name)
344362 if not agent_saved and self ._agent .id :
0 commit comments