44from contextlib import AsyncExitStack
55from typing import Any , Optional
66
7- from agent_framework import (AggregateContextProvider , ChatAgent ,
8- ChatClientProtocol , ChatMessage ,
9- ChatMessageStoreProtocol , ChatOptions ,
10- ContextProvider , HostedMCPTool ,
11- MCPStreamableHTTPTool , Middleware , Role , ToolMode ,
12- ToolProtocol )
7+ from agent_framework import (
8+ AggregateContextProvider ,
9+ ChatAgent ,
10+ ChatClientProtocol ,
11+ ChatMessage ,
12+ ChatMessageStoreProtocol ,
13+ ChatOptions ,
14+ ContextProvider ,
15+ HostedMCPTool ,
16+ MCPStreamableHTTPTool ,
17+ Middleware ,
18+ Role ,
19+ ToolMode ,
20+ ToolProtocol ,
21+ )
22+
1323# from agent_framework.azure import AzureAIAgentClient
1424from agent_framework_azure_ai import AzureAIAgentClient
1525from azure .ai .agents .aio import AgentsClient
1626from azure .identity .aio import DefaultAzureCredential
27+ from common .models .messages_af import TeamConfiguration
28+ from v4 .common .services .team_service import TeamService
1729from v4 .config .agent_registry import agent_registry
1830from v4 .magentic_agents .models .agent_models import MCPConfig
1931
@@ -25,18 +37,51 @@ class MCPEnabledBase:
2537 Subclasses must implement _after_open() and assign self._agent.
2638 """
2739
28- def __init__ (self , mcp : MCPConfig | None = None ) -> None :
40+ def __init__ (
41+ self ,
42+ mcp : MCPConfig | None = None ,
43+ team_service : TeamService | None = None ,
44+ team_config : TeamConfiguration | None = None ,
45+ project_endpoint : str | None = None ,
46+ ) -> None :
2947 self ._stack : AsyncExitStack | None = None
3048 self .mcp_cfg : MCPConfig | None = mcp
3149 self .mcp_tool : HostedMCPTool | None = None
32- self ._agent : ChatAgent | None = None
50+ self ._agent : ChatAgent | None = None
51+ self .team_service : TeamService | None = team_service
52+ self .team_config : TeamConfiguration | None = team_config
53+ self .client : Optional [AzureAIAgentClient ] = None
54+ self .project_endpoint = project_endpoint
55+ self .creds : Optional [DefaultAzureCredential ] = None
3356
3457 async def open (self ) -> "MCPEnabledBase" :
3558 if self ._stack is not None :
3659 return self
3760 self ._stack = AsyncExitStack ()
61+
62+ # Acquire credential
63+ self .creds = DefaultAzureCredential ()
64+ if self ._stack :
65+ await self ._stack .enter_async_context (self .creds )
66+ # Create AgentsClient
67+ self .client = AgentsClient (
68+ endpoint = self .project_endpoint ,
69+ credential = self .creds ,
70+ )
71+ if self ._stack :
72+ await self ._stack .enter_async_context (self .client )
73+ # Prepare MCP
3874 await self ._prepare_mcp_tool ()
75+
76+ # Let subclass build agent client
3977 await self ._after_open ()
78+
79+ # Register agent (best effort)
80+ try :
81+ agent_registry .register_agent (self )
82+ except Exception :
83+ pass
84+
4085 return self
4186
4287 async def close (self ) -> None :
@@ -47,12 +92,12 @@ async def close(self) -> None:
4792 if self ._agent and hasattr (self ._agent , "close" ):
4893 try :
4994 await self ._agent .close () # AzureAIAgentClient has async close
50- except Exception :
95+ except Exception :
5196 pass
5297 # Unregister from registry if present
5398 try :
5499 agent_registry .unregister_agent (self )
55- except Exception :
100+ except Exception :
56101 pass
57102 await self ._stack .aclose ()
58103 finally :
@@ -85,11 +130,11 @@ async def _prepare_mcp_tool(self) -> None:
85130 mcp_tool = MCPStreamableHTTPTool (
86131 name = self .mcp_cfg .name ,
87132 description = self .mcp_cfg .description ,
88- url = self .mcp_cfg .url
133+ url = self .mcp_cfg .url ,
89134 )
90135 await self ._stack .enter_async_context (mcp_tool )
91136 self .mcp_tool = mcp_tool # Store for later use
92- except Exception :
137+ except Exception :
93138 self .mcp_tool = None
94139
95140
@@ -102,41 +147,38 @@ class AzureAgentBase(MCPEnabledBase):
102147 - optionally register themselves via agent_registry
103148 """
104149
105- def __init__ (self , mcp : MCPConfig | None = None , model_deployment_name : str | None = None , project_endpoint : str | None = None ) -> None :
106- super ().__init__ (mcp = mcp )
107- self .creds : Optional [DefaultAzureCredential ] = None
108- self .client : Optional [AzureAIAgentClient ] = None
109-
150+ def __init__ (
151+ self ,
152+ mcp : MCPConfig | None = None ,
153+ model_deployment_name : str | None = None ,
154+ project_endpoint : str | None = None ,
155+ team_service : TeamService | None = None ,
156+ team_config : TeamConfiguration | None = None ,
157+ ) -> None :
158+ super ().__init__ (mcp = mcp , team_service = team_service , team_config = team_config , project_endpoint = project_endpoint )
159+
110160 self ._created_ephemeral : bool = (
111161 False # reserved if you add ephemeral agent cleanup
112162 )
113- self . project_endpoint = project_endpoint
163+
114164 self .model_deployment_name = model_deployment_name
115165
116166 async def open (self ) -> "AzureAgentBase" :
117167 if self ._stack is not None :
118168 return self
119169 self ._stack = AsyncExitStack ()
120170
121-
122171 # Acquire credential
123172 self .creds = DefaultAzureCredential ()
124- await self ._stack .enter_async_context (self .creds )
125-
126- # Create AIProjectClient
127- # self.client = AzureAIAgentClient(
128- # project_endpoint=self.project_endpoint,
129- # model_deployment_name=self.model_deployment_name,
130- # async_credential=self.creds,
131- # )
132-
133- #Create AgentsClient
173+ if self ._stack :
174+ await self ._stack .enter_async_context (self .creds )
175+ # Create AgentsClient
134176 self .client = AgentsClient (
135177 endpoint = self .project_endpoint ,
136178 credential = self .creds ,
137179 )
138- await self ._stack . enter_async_context ( self . client )
139-
180+ if self ._stack :
181+ await self . _stack . enter_async_context ( self . client )
140182 # Prepare MCP
141183 await self ._prepare_mcp_tool ()
142184
@@ -146,7 +188,7 @@ async def open(self) -> "AzureAgentBase":
146188 # Register agent (best effort)
147189 try :
148190 agent_registry .register_agent (self )
149- except Exception :
191+ except Exception :
150192 pass
151193
152194 return self
@@ -170,25 +212,25 @@ async def close(self) -> None:
170212 if self ._agent and hasattr (self ._agent , "close" ):
171213 try :
172214 await self ._agent .close ()
173- except Exception :
215+ except Exception :
174216 pass
175217
176218 # Unregister from registry
177219 try :
178220 agent_registry .unregister_agent (self )
179- except Exception :
221+ except Exception :
180222 pass
181223
182224 # Close credential and project client
183225 if self .client :
184226 try :
185227 await self .client .close ()
186- except Exception :
228+ except Exception :
187229 pass
188230 if self .creds :
189231 try :
190232 await self .creds .close ()
191- except Exception :
233+ except Exception :
192234 pass
193235
194236 finally :
0 commit comments