Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 5 additions & 6 deletions python/openai/sample-agent/AGENT-CODE-WALKTHROUGH.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,24 +236,23 @@ def _initialize_services(self):

# return tool_service, auth_options

async def setup_mcp_servers(self, auth: Authorization, context: TurnContext):
async def setup_mcp_servers(self, auth: Authorization, auth_handler_name: str, context: TurnContext):
"""Set up MCP server connections"""
try:
agent_user_id = os.getenv("AGENT_ID", "user123")

use_agentic_auth = os.getenv("USE_AGENTIC_AUTH", "false").lower() == "true"
if use_agentic_auth:
self.agent = await self.tool_service.add_tool_servers_to_agent(
agent=self.agent,
agent_user_id=agent_user_id,
auth=auth,
auth_handler_name=auth_handler_name,
context=context,
)
else:
self.agent = await self.tool_service.add_tool_servers_to_agent(
agent=self.agent,
agent_user_id=agent_user_id,
auth=auth,
auth_handler_name=auth_handler_name,
context=context,
auth_token=self.auth_options.bearer_token,
)
Expand Down Expand Up @@ -302,12 +301,12 @@ The agent supports multiple authentication modes and extensive configuration opt

```python
async def process_user_message(
self, message: str, auth: Authorization, context: TurnContext
self, message: str, auth: Authorization, auth_handler_name: str, context: TurnContext
) -> str:
"""Process user message using the OpenAI Agents SDK"""
try:
# Setup MCP servers
await self.setup_mcp_servers(auth, context)
await self.setup_mcp_servers(auth, auth_handler_name, context)

# Run the agent with the user message
result = await self.runner.run(starting_agent=self.agent, input=message)
Expand Down
13 changes: 6 additions & 7 deletions python/openai/sample-agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def token_resolver(self, agent_id: str, tenant_id: str) -> str | None:
"""
Token resolver function for Agent 365 Observability exporter.

Uses the cached agentic token obtained from AGENT_APP.auth.get_token(context, "AGENTIC").
Uses the cached agentic token obtained from AGENT_APP.auth.get_token(context, auth_handler_name).
This is the only valid authentication method for this context.
"""

Expand Down Expand Up @@ -207,24 +207,23 @@ def _initialize_services(self):

# return tool_service, auth_options

async def setup_mcp_servers(self, auth: Authorization, context: TurnContext):
async def setup_mcp_servers(self, auth: Authorization, auth_handler_name: str, context: TurnContext):
"""Set up MCP server connections"""
try:
agentic_app_id = os.getenv("AGENT_ID", "user123")

use_agentic_auth = os.getenv("USE_AGENTIC_AUTH", "false").lower() == "true"
if use_agentic_auth:
self.agent = await self.tool_service.add_tool_servers_to_agent(
agent=self.agent,
agentic_app_id=agentic_app_id,
auth=auth,
auth_handler_name=auth_handler_name,
context=context,
)
else:
self.agent = await self.tool_service.add_tool_servers_to_agent(
agent=self.agent,
agentic_app_id=agentic_app_id,
auth=auth,
auth_handler_name=auth_handler_name,
context=context,
auth_token=self.auth_options.bearer_token,
)
Expand Down Expand Up @@ -253,12 +252,12 @@ async def initialize(self):
# <MessageProcessing>

async def process_user_message(
self, message: str, auth: Authorization, context: TurnContext
self, message: str, auth: Authorization, auth_handler_name: str, context: TurnContext
) -> str:
"""Process user message using the OpenAI Agents SDK"""
try:
# Setup MCP servers
await self.setup_mcp_servers(auth, context)
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)
Expand Down
2 changes: 1 addition & 1 deletion python/openai/sample-agent/agent_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def initialize(self) -> None:

@abstractmethod
async def process_user_message(
self, message: str, auth: Authorization, context: TurnContext
self, message: str, auth: Authorization, auth_handler_name: str, context: TurnContext
) -> str:
"""Process a user message and return a response."""
pass
Expand Down
10 changes: 5 additions & 5 deletions python/openai/sample-agent/host_agent_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ 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"

self.agent_class = agent_class
self.agent_args = agent_args
self.agent_kwargs = agent_kwargs
Expand Down Expand Up @@ -108,9 +110,7 @@ async def help_handler(context: TurnContext, _: TurnState):
self.agent_app.conversation_update("membersAdded")(help_handler)
self.agent_app.message("/help")(help_handler)

use_agentic_auth = os.getenv("USE_AGENTIC_AUTH", "false").lower() == "true"
handler = ["AGENTIC"] if use_agentic_auth else None

handler = [self.auth_handler_name]
@self.agent_app.activity("message", auth_handlers=handler)
async def on_message(context: TurnContext, _: TurnState):
"""Handle all messages with the hosted agent"""
Expand All @@ -128,7 +128,7 @@ async def on_message(context: TurnContext, _: TurnState):
exaau_token = await self.agent_app.auth.exchange_token(
context,
scopes=get_observability_authentication_scope(),
auth_handler_id="AGENTIC",
auth_handler_id=self.auth_handler_name,
)

# Cache the agentic token for Agent 365 Observability exporter use
Expand All @@ -152,7 +152,7 @@ async def on_message(context: TurnContext, _: TurnState):
# Process with the hosted agent
logger.info(f"🤖 Processing with {self.agent_class.__name__}...")
response = await self.agent_instance.process_user_message(
user_message, self.agent_app.auth, context
user_message, self.agent_app.auth, self.auth_handler_name, context
)

# Send response back
Expand Down
15 changes: 7 additions & 8 deletions python/openai/sample-agent/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "sample-openai-agent"
version = "0.1.0"
description = "Sample OpenAI Agent using Microsoft Agent 365 SDK"
authors = [
{ name = "Microsoft", email = "example@microsoft.com" }
{ name = "Microsoft", email = "support@microsoft.com" }
]
dependencies = [
# OpenAI Agents SDK - The official package
Expand Down Expand Up @@ -32,13 +32,12 @@ dependencies = [
# Additional utilities
"typing-extensions>=4.0.0",

# Local packages from local index
# - Update package versions to match your built wheels
"microsoft_agents_a365_tooling >= 2025.10.20",
"microsoft_agents_a365_tooling_extensions_openai >= 2025.10.20",
"microsoft_agents_a365_observability_core >= 2025.10.20",
"microsoft_agents_a365_observability_extensions_openai >= 2025.10.20",
"microsoft_agents_a365_notifications >= 2025.10.20",
# Microsoft Agent 365 SDK packages
"microsoft_agents_a365_tooling >= 0.1.0",
"microsoft_agents_a365_tooling_extensions_openai >= 0.1.0",
"microsoft_agents_a365_observability_core >= 0.1.0",
"microsoft_agents_a365_observability_extensions_openai >= 0.1.0",
"microsoft_agents_a365_notifications >= 0.1.0",
]
requires-python = ">=3.11"

Expand Down
Loading