2424from token_cache import get_cached_agentic_token
2525
2626# Load environment variables
27- load_dotenv ()
27+ load_dotenv (override = True )
2828
2929# Configure logging
3030logging .basicConfig (level = logging .INFO )
@@ -239,25 +239,41 @@ async def setup_mcp_servers(self, auth: Authorization, auth_handler_name: str, c
239239 """Set up MCP server connections based on authentication configuration.
240240
241241 Authentication priority:
242- 1. Bearer token from config (BEARER_TOKEN ) - for local development/testing
243- 2. Auth handler (auth_handler_name ) - for production agentic auth
242+ 1. Agentic auth (USE_AGENTIC_AUTH=true ) - for production/Teams authentication
243+ 2. Bearer token from config (BEARER_TOKEN ) - for local development/testing
244244 3. No auth - gracefully skip MCP and run in bare LLM mode
245245
246246 If MCP connection fails for any reason, the agent will gracefully fall back
247247 to bare LLM mode without MCP tools.
248248 """
249249 try :
250- # Priority 1: Bearer token provided in config (for local dev/testing)
251- if self .auth_options .bearer_token :
252- logger .info ("π Using bearer token from config for MCP servers" )
250+ # Check if agentic auth is enabled
251+ use_agentic_auth = os .getenv ("USE_AGENTIC_AUTH" , "false" ).lower () == "true"
252+
253+ # Priority 1: Agentic auth enabled (production/Teams authentication)
254+ # When USE_AGENTIC_AUTH=true, always use agentic auth - never fall back to bearer token
255+ if use_agentic_auth :
256+ if auth_handler_name :
257+ logger .info (f"π Using agentic auth handler '{ auth_handler_name } ' for MCP servers (USE_AGENTIC_AUTH=true)" )
258+ else :
259+ logger .info ("π Using agentic auth for MCP servers (USE_AGENTIC_AUTH=true, no explicit handler)" )
260+ self .agent = await self .tool_service .add_tool_servers_to_agent (
261+ agent = self .agent ,
262+ auth = auth ,
263+ auth_handler_name = auth_handler_name ,
264+ context = context ,
265+ )
266+ # Priority 2: Bearer token provided in config (for local dev/testing when agentic auth is disabled)
267+ elif self .auth_options .bearer_token :
268+ logger .info ("π Using bearer token from config for MCP servers (USE_AGENTIC_AUTH=false)" )
253269 self .agent = await self .tool_service .add_tool_servers_to_agent (
254270 agent = self .agent ,
255271 auth = auth ,
256272 auth_handler_name = auth_handler_name ,
257273 context = context ,
258274 auth_token = self .auth_options .bearer_token ,
259275 )
260- # Priority 2 : Auth handler configured (production agentic auth)
276+ # Priority 3 : Auth handler configured without USE_AGENTIC_AUTH flag
261277 elif auth_handler_name :
262278 logger .info (f"π Using auth handler '{ auth_handler_name } ' for MCP servers" )
263279 self .agent = await self .tool_service .add_tool_servers_to_agent (
@@ -266,10 +282,10 @@ async def setup_mcp_servers(self, auth: Authorization, auth_handler_name: str, c
266282 auth_handler_name = auth_handler_name ,
267283 context = context ,
268284 )
269- # Priority 3 : No auth configured - skip MCP and run bare LLM
285+ # Priority 4 : No auth configured - skip MCP and run bare LLM
270286 else :
271287 logger .warning ("β οΈ No authentication configured - running in bare LLM mode without MCP tools" )
272- logger .info ("π‘ To enable MCP: provide BEARER_TOKEN or configure AUTH_HANDLER_NAME" )
288+ logger .info ("π‘ To enable MCP: set USE_AGENTIC_AUTH=true, provide BEARER_TOKEN, or configure AUTH_HANDLER_NAME" )
273289 # Agent already initialized without MCP tools
274290
275291 except Exception as e :
0 commit comments