diff --git a/python/openai/sample-agent/agent.py b/python/openai/sample-agent/agent.py index c510c3f6..97e189ea 100644 --- a/python/openai/sample-agent/agent.py +++ b/python/openai/sample-agent/agent.py @@ -232,11 +232,12 @@ async def setup_mcp_servers(self, auth: Authorization, auth_handler_name: str, c context=context, ) else: + # For anonymous mode, don't pass auth/context to avoid token retrieval self.agent = await self.tool_service.add_tool_servers_to_agent( agent=self.agent, - auth=auth, - auth_handler_name=auth_handler_name, - context=context, + auth=None, + auth_handler_name=None, + context=None, auth_token=self.auth_options.bearer_token, ) @@ -268,8 +269,11 @@ async def process_user_message( ) -> str: """Process user message using the OpenAI Agents SDK""" try: - # Setup MCP servers - await self.setup_mcp_servers(auth, auth_handler_name, context) + # Setup MCP servers only if not already done or if auth is available + # In anonymous mode (USE_AGENTIC_AUTH=false), skip setup to avoid auth errors + use_agentic_auth = os.getenv("USE_AGENTIC_AUTH", "false").lower() == "true" + if use_agentic_auth and auth is not None: + await self.setup_mcp_servers(auth, auth_handler_name, context) # Run the agent with the user message result = await Runner.run(starting_agent=self.agent, input=message, context=context) diff --git a/python/openai/sample-agent/agent_interface.py b/python/openai/sample-agent/agent_interface.py index 8640b185..86152893 100644 --- a/python/openai/sample-agent/agent_interface.py +++ b/python/openai/sample-agent/agent_interface.py @@ -46,8 +46,8 @@ def check_agent_inheritance(agent_class) -> bool: True if the agent inherits from AgentInterface, False otherwise """ if not issubclass(agent_class, AgentInterface): - print(f"❌ Agent {agent_class.__name__} does not inherit from AgentInterface") + print(f"ERROR: Agent {agent_class.__name__} does not inherit from AgentInterface") return False - print(f"✅ Agent {agent_class.__name__} properly inherits from AgentInterface") + print(f"SUCCESS: Agent {agent_class.__name__} properly inherits from AgentInterface") return True diff --git a/python/openai/sample-agent/host_agent_server.py b/python/openai/sample-agent/host_agent_server.py index d124d980..1d2d830d 100644 --- a/python/openai/sample-agent/host_agent_server.py +++ b/python/openai/sample-agent/host_agent_server.py @@ -68,7 +68,9 @@ def __init__(self, agent_class: type[AgentInterface], *agent_args, **agent_kwarg if not check_agent_inheritance(agent_class): raise TypeError(f"Agent class {agent_class.__name__} must inherit from AgentInterface") - self.auth_handler_name = "AGENTIC" + # Only use auth handler when agentic auth is enabled + use_agentic_auth = os.getenv("USE_AGENTIC_AUTH", "false").lower() == "true" + self.auth_handler_name = "AGENTIC" if use_agentic_auth else None self.agent_class = agent_class self.agent_args = agent_args @@ -110,8 +112,9 @@ async def help_handler(context: TurnContext, _: TurnState): self.agent_app.conversation_update("membersAdded")(help_handler) self.agent_app.message("/help")(help_handler) - handler = [self.auth_handler_name] - @self.agent_app.activity("message", auth_handlers=handler) + # Only require auth handlers if agentic auth is enabled + handler_config = {"auth_handlers": [self.auth_handler_name]} if self.auth_handler_name else {} + @self.agent_app.activity("message", **handler_config) async def on_message(context: TurnContext, _: TurnState): """Handle all messages with the hosted agent""" try: @@ -125,18 +128,20 @@ async def on_message(context: TurnContext, _: TurnState): await context.send_activity(error_msg) return - exaau_token = await self.agent_app.auth.exchange_token( - context, - scopes=get_observability_authentication_scope(), - auth_handler_id=self.auth_handler_name, - ) - - # Cache the agentic token for Agent 365 Observability exporter use - cache_agentic_token( - tenant_id, - agent_id, - exaau_token.token, - ) + # Only exchange token if agentic auth is enabled + if self.auth_handler_name: + exaau_token = await self.agent_app.auth.exchange_token( + context, + scopes=get_observability_authentication_scope(), + auth_handler_id=self.auth_handler_name, + ) + + # Cache the agentic token for Agent 365 Observability exporter use + cache_agentic_token( + tenant_id, + agent_id, + exaau_token.token, + ) user_message = context.activity.text or "" logger.info(f"📨 Processing message: '{user_message}'") diff --git a/python/openai/sample-agent/start_with_generic_host.py b/python/openai/sample-agent/start_with_generic_host.py index d54d2396..e49e84a2 100644 --- a/python/openai/sample-agent/start_with_generic_host.py +++ b/python/openai/sample-agent/start_with_generic_host.py @@ -27,7 +27,7 @@ def main(): create_and_run_host(OpenAIAgentWithMCP) except Exception as e: - print(f"❌ Failed to start server: {e}") + print(f"ERROR: Failed to start server: {e}") import traceback traceback.print_exc()