Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions python/agent-framework/sample-agent/ToolingManifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"mcpServers": [
{
"mcpServerName": "mcp_MailTools",
"mcpServerUniqueName": "mcp_MailTools",
"url": "https://agent365.svc.cloud.microsoft/agents/servers/mcp_MailTools",
"scope": "McpServers.Mail.All",
"audience": "ea9ffc3e-8a23-4a7d-836d-234d7c7565c1"
"mcpServerUniqueName": "mcp_MailTools"
Comment thread
rahuldevikar761 marked this conversation as resolved.
}
]
}
12 changes: 11 additions & 1 deletion python/agent-framework/sample-agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def _create_chat_client(self):
endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
deployment = os.getenv("AZURE_OPENAI_DEPLOYMENT")
api_version = os.getenv("AZURE_OPENAI_API_VERSION")
api_key = os.getenv("AZURE_OPENAI_API_KEY")

if not endpoint:
raise ValueError("AZURE_OPENAI_ENDPOINT environment variable is required")
Expand All @@ -130,9 +131,18 @@ def _create_chat_client(self):
"AZURE_OPENAI_API_VERSION environment variable is required"
)

# Use API key if provided, otherwise fall back to Azure CLI credential
if api_key:
from azure.core.credentials import AzureKeyCredential
credential = AzureKeyCredential(api_key)
logger.info("Using API key authentication for Azure OpenAI")
else:
credential = AzureCliCredential()
logger.info("Using Azure CLI authentication for Azure OpenAI")

Comment thread
rahuldevikar761 marked this conversation as resolved.
self.chat_client = AzureOpenAIChatClient(
endpoint=endpoint,
credential=AzureCliCredential(),
credential=credential,
deployment_name=deployment,
api_version=api_version,
)
Expand Down
28 changes: 22 additions & 6 deletions python/agent-framework/sample-agent/host_agent_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ def __init__(self, agent_class: type[AgentInterface], *agent_args, **agent_kwarg
f"Agent class {agent_class.__name__} must inherit from AgentInterface"
)

self.auth_handler_name = "AGENTIC"
# Auth handler name can be configured via environment
# Defaults to empty (no auth handler) - set AUTH_HANDLER_NAME=AGENTIC for production agentic auth
self.auth_handler_name = os.getenv("AUTH_HANDLER_NAME", "") or None
Comment thread
rahuldevikar761 marked this conversation as resolved.
Comment thread
rahuldevikar761 marked this conversation as resolved.
if self.auth_handler_name:
logger.info(f"🔐 Using auth handler: {self.auth_handler_name}")
else:
logger.info("🔓 No auth handler configured (AUTH_HANDLER_NAME not set)")

self.agent_class = agent_class
self.agent_args = agent_args
Expand All @@ -118,19 +124,28 @@ def __init__(self, agent_class: type[AgentInterface], *agent_args, **agent_kwarg
async def _setup_observability_token(
self, context: TurnContext, tenant_id: str, agent_id: str
):
# Only attempt token exchange when auth handler is configured
if not self.auth_handler_name:
logger.debug("Skipping observability token exchange (no auth handler)")
return

try:
logger.info(f"🔐 Attempting token exchange for observability...")
exaau_token = await self.agent_app.auth.exchange_token(
context,
scopes=get_observability_authentication_scope(),
auth_handler_id=self.auth_handler_name,
)
cache_agentic_token(tenant_id, agent_id, exaau_token.token)
logger.info(f"✅ Token exchange successful")
Comment thread
rahuldevikar761 marked this conversation as resolved.
Outdated
Comment thread
rahuldevikar761 marked this conversation as resolved.
Outdated
except Exception as e:
logger.warning(f"⚠️ Failed to cache observability token: {e}")

async def _validate_agent_and_setup_context(self, context: TurnContext):
logger.info(f"🔍 Validating agent and setting up context...")
Comment thread
rahuldevikar761 marked this conversation as resolved.
Outdated
tenant_id = context.activity.recipient.tenant_id
agent_id = context.activity.recipient.agentic_app_id
logger.info(f"🔍 tenant_id={tenant_id}, agent_id={agent_id}")

if not self.agent_instance:
logger.error("Agent not available")
Expand All @@ -143,18 +158,19 @@ async def _validate_agent_and_setup_context(self, context: TurnContext):
# --- Handlers (Messages & Notifications) ---
def _setup_handlers(self):
"""Setup message and notification handlers"""
handler = [self.auth_handler_name]
# Configure auth handlers - only required when auth_handler_name is set
handler_config = {"auth_handlers": [self.auth_handler_name]} if self.auth_handler_name else {}

async def help_handler(context: TurnContext, _: TurnState):
await context.send_activity(
f"👋 **Hi there!** I'm **{self.agent_class.__name__}**, your AI assistant.\n\n"
"How can I help you today?"
)

self.agent_app.conversation_update("membersAdded", auth_handlers=handler)(help_handler)
self.agent_app.message("/help", auth_handlers=handler)(help_handler)
self.agent_app.conversation_update("membersAdded", **handler_config)(help_handler)
self.agent_app.message("/help", **handler_config)(help_handler)

@self.agent_app.activity("message", auth_handlers=handler)
@self.agent_app.activity("message", **handler_config)
async def on_message(context: TurnContext, _: TurnState):
try:
result = await self._validate_agent_and_setup_context(context)
Expand All @@ -179,7 +195,7 @@ async def on_message(context: TurnContext, _: TurnState):

@self.agent_notification.on_agent_notification(
channel_id=ChannelId(channel="agents", sub_channel="*"),
auth_handlers=handler,
**handler_config,
)
async def on_notification(
context: TurnContext,
Expand Down
Loading