Skip to content
18 changes: 14 additions & 4 deletions python/openai/sample-agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,38 @@ def _initialize_services(self):
# return tool_service, auth_options

async def setup_mcp_servers(self, auth: Authorization, auth_handler_name: str, context: TurnContext):
"""Set up MCP server connections"""
"""Set up MCP server connections based on authentication configuration"""
try:

use_agentic_auth = os.getenv("USE_AGENTIC_AUTH", "false").lower() == "true"
Comment thread
rahuldevikar761 marked this conversation as resolved.
Outdated

# Scenario 1: Agentic Authentication (Production)
if use_agentic_auth:
logger.info("🔒 Using Agentic Authentication for MCP servers")
self.agent = await self.tool_service.add_tool_servers_to_agent(
agent=self.agent,
auth=auth,
auth_handler_name=auth_handler_name,
context=context,
)
else:
# Scenario 2: OBO with Bearer Token (Development/Testing)
elif self.auth_options.bearer_token:
logger.info("🔑 Using OBO Bearer Token for MCP servers")
Comment thread
rahuldevikar761 marked this conversation as resolved.
Outdated
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_token=self.auth_options.bearer_token,
)
# Scenario 3: No Authentication - Bare LLM only (no MCP servers)
Comment thread
rahuldevikar761 marked this conversation as resolved.
Outdated
else:
logger.warning("⚠️ No authentication available - running in bare LLM mode (no MCP servers)")
Comment thread
rahuldevikar761 marked this conversation as resolved.
Outdated
logger.info("💡 To enable MCP servers: Set USE_AGENTIC_AUTH=true OR provide BEARER_TOKEN")
# Agent already initialized without MCP tools - will use only base LLM capabilities

except Exception as e:
logger.error(f"Error setting up MCP servers: {e}")
logger.error(f"❌ Error setting up MCP servers: {e}")
logger.warning("⚠️ Falling back to bare LLM mode without MCP servers")

async def initialize(self):
"""Initialize the agent and MCP server connections"""
Expand Down
35 changes: 20 additions & 15 deletions python/openai/sample-agent/host_agent_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
rahuldevikar761 marked this conversation as resolved.
Outdated

self.agent_class = agent_class
self.agent_args = agent_args
Expand Down Expand Up @@ -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:
Expand All @@ -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
Comment thread
pontemonti marked this conversation as resolved.
Outdated
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}'")
Expand Down
Loading