Description
Issue: How to access HTTP Request Information in Hosted Workflow Agents
I have a Foundry Hosted Agent. I created a Executor. Code in code sample section below.
I call this Agent over /responses api like below:
{
"agent": {
"name": "Super-Agent",
"type": "agent_reference"
},
"stream": false,
"input": "Which phone from Google is allowed in University? Answer using tools that you have available.",
"metadata": {
"x-some-token": "some token here"
}
}
Related Questions:
-
How do I read this metadata input parameter. When I had basic Agent without a Workflow, I could get the metadata in AgentMiddleware like below, but now that I am using Workflow, how do I get this metadata in my Workflow Executor?
-
Is AgentMiddleware not supported when using Workflows? I see FunctionMiddleware gets called, but FunctionMiddleware kwargs was empty, probably as I populate them in AgentMiddleware and AgentMiddleware is not invoked.
class UserContextAgentMiddleware(AgentMiddleware):
"""Agent middleware that obtains user context."""
async def process(
self,
context: AgentContext,
next: Callable[[], Awaitable[None]],
) -> None:
if hasattr(context.agent, '_request_headers'):
request_data = context.agent._request_headers
# Runtime values that will be forwarded to tools
if 'x-some-token' in request_data:
context.kwargs.setdefault("some_token", request_data['x-some-token'])
context.metadata["some_token"] = request_data['x-some-token']
Code Sample
Run Workflow as HostedAgent
agent_executor = AgentExecutorWithMCPTools(config)
# Build workflow with connected executors
workflow = (
WorkflowBuilder(start_executor=agent_executor)
.build()
)
# Run the agent - this handles its own event loop
from_agent_framework(workflow.as_agent()).run()
Workflow Executor
class AgentExecutorWithMCPTools(Executor):
"""
Workflow executor that runs an agent with the initialized MCP tools.
This executor receives the MCP tools from the initialization node
and creates an agent with those tools to process the user's request.
"""
def __init__(self, config: AppConfig, id: str = "agent_with_mcp_tools"):
super().__init__(id=id)
self.token_service = None
self.config = config
@handler
async def run_agent(
self,
user_input: List[Message],
ctx: WorkflowContext[Dict, str]
) -> None:
async with mcp_tool: # Ensure proper cleanup
# Create agent with the initialized MCP tool
agent = chat_client.as_agent(
name="SomeAgent",
instructions=instructions,
tools=[mcp_tool],
middleware=[UserContextAgentMiddleware(), UserContextFunctionMiddleware()]
)
logger.info("Running agent with user query...")
# Run agent with user input
response = await agent.run(user_input)
logger.info("Agent completed successfully")
Error Messages / Stack Traces
Package Versions
azure-ai-agentserver-agentframework==1.0.0b17 agent-framework
Python Version
3.12
Additional Context
No response
Description
Issue: How to access HTTP Request Information in Hosted Workflow Agents
I have a Foundry Hosted Agent. I created a Executor. Code in code sample section below.
I call this Agent over /responses api like below:
{
"agent": {
"name": "Super-Agent",
"type": "agent_reference"
},
"stream": false,
"input": "Which phone from Google is allowed in University? Answer using tools that you have available.",
"metadata": {
"x-some-token": "some token here"
}
}
Related Questions:
How do I read this metadata input parameter. When I had basic Agent without a Workflow, I could get the metadata in AgentMiddleware like below, but now that I am using Workflow, how do I get this metadata in my Workflow Executor?
Is AgentMiddleware not supported when using Workflows? I see FunctionMiddleware gets called, but FunctionMiddleware kwargs was empty, probably as I populate them in AgentMiddleware and AgentMiddleware is not invoked.
class UserContextAgentMiddleware(AgentMiddleware):
"""Agent middleware that obtains user context."""
Code Sample
Run Workflow as HostedAgent agent_executor = AgentExecutorWithMCPTools(config) # Build workflow with connected executors workflow = ( WorkflowBuilder(start_executor=agent_executor) .build() ) # Run the agent - this handles its own event loop from_agent_framework(workflow.as_agent()).run() Workflow Executor class AgentExecutorWithMCPTools(Executor): """ Workflow executor that runs an agent with the initialized MCP tools. This executor receives the MCP tools from the initialization node and creates an agent with those tools to process the user's request. """ def __init__(self, config: AppConfig, id: str = "agent_with_mcp_tools"): super().__init__(id=id) self.token_service = None self.config = config @handler async def run_agent( self, user_input: List[Message], ctx: WorkflowContext[Dict, str] ) -> None: async with mcp_tool: # Ensure proper cleanup # Create agent with the initialized MCP tool agent = chat_client.as_agent( name="SomeAgent", instructions=instructions, tools=[mcp_tool], middleware=[UserContextAgentMiddleware(), UserContextFunctionMiddleware()] ) logger.info("Running agent with user query...") # Run agent with user input response = await agent.run(user_input) logger.info("Agent completed successfully")Error Messages / Stack Traces
Package Versions
azure-ai-agentserver-agentframework==1.0.0b17 agent-framework
Python Version
3.12
Additional Context
No response