mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Fix hosted MCP tool approval flow for all session/streaming combinations (#4054)
* fix openai hosted mcp samples * addressed copilot comments * Update python/samples/02-agents/providers/azure_openai/azure_responses_client_with_hosted_mcp.py Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com> --------- Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
988ef6a50e
commit
21769e2cd1
+11
-6
@@ -70,13 +70,14 @@ async def handle_approvals_with_session_streaming(query: str, agent: "SupportsAg
|
||||
"""Here we let the session deal with the previous responses, and we just rerun with the approval."""
|
||||
from agent_framework import Message
|
||||
|
||||
new_input: list[Message] = []
|
||||
new_input: list[Message | str] = [query]
|
||||
new_input_added = True
|
||||
while new_input_added:
|
||||
new_input_added = False
|
||||
new_input.append(Message(role="user", text=query))
|
||||
async for update in agent.run(new_input, session=session, options={"store": True}, stream=True):
|
||||
if update.user_input_requests:
|
||||
# Reset input to only contain new approval responses for the next iteration
|
||||
new_input = []
|
||||
for user_input_needed in update.user_input_requests:
|
||||
print(
|
||||
f"User Input Request for function from {agent.name}: {user_input_needed.function_call.name}"
|
||||
@@ -114,7 +115,8 @@ async def run_hosted_mcp_without_session_and_specific_approval() -> None:
|
||||
async with Agent(
|
||||
client=client,
|
||||
name="DocsAgent",
|
||||
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
|
||||
instructions="You are a helpful assistant that uses your MCP tool "
|
||||
"to help with microsoft documentation questions.",
|
||||
tools=[mcp_tool],
|
||||
) as agent:
|
||||
# First query
|
||||
@@ -151,7 +153,8 @@ async def run_hosted_mcp_without_approval() -> None:
|
||||
async with Agent(
|
||||
client=client,
|
||||
name="DocsAgent",
|
||||
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
|
||||
instructions="You are a helpful assistant that uses your MCP tool "
|
||||
"to help with Microsoft documentation questions.",
|
||||
tools=[mcp_tool],
|
||||
) as agent:
|
||||
# First query
|
||||
@@ -186,7 +189,8 @@ async def run_hosted_mcp_with_session() -> None:
|
||||
async with Agent(
|
||||
client=client,
|
||||
name="DocsAgent",
|
||||
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
|
||||
instructions="You are a helpful assistant that uses your MCP tool "
|
||||
"to help with microsoft documentation questions.",
|
||||
tools=[mcp_tool],
|
||||
) as agent:
|
||||
# First query
|
||||
@@ -222,7 +226,8 @@ async def run_hosted_mcp_with_session_streaming() -> None:
|
||||
async with Agent(
|
||||
client=client,
|
||||
name="DocsAgent",
|
||||
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
|
||||
instructions="You are a helpful assistant that uses your MCP tool "
|
||||
"to help with microsoft documentation questions.",
|
||||
tools=[mcp_tool],
|
||||
) as agent:
|
||||
# First query
|
||||
|
||||
+5
-3
@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING, Any
|
||||
|
||||
from agent_framework import Agent
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
from dotenv import load_dotenv
|
||||
|
||||
"""
|
||||
OpenAI Responses Client with Hosted MCP Example
|
||||
@@ -12,7 +13,7 @@ OpenAI Responses Client with Hosted MCP Example
|
||||
This sample demonstrates integrating hosted Model Context Protocol (MCP) tools with
|
||||
OpenAI Responses Client, including user approval workflows for function call security.
|
||||
"""
|
||||
|
||||
load_dotenv() # Load environment variables from .env file if present
|
||||
if TYPE_CHECKING:
|
||||
from agent_framework import AgentSession, SupportsAgentRun
|
||||
|
||||
@@ -69,13 +70,14 @@ async def handle_approvals_with_session_streaming(query: str, agent: "SupportsAg
|
||||
"""Here we let the session deal with the previous responses, and we just rerun with the approval."""
|
||||
from agent_framework import Message
|
||||
|
||||
new_input: list[Message] = []
|
||||
new_input: list[Message | str] = [query]
|
||||
new_input_added = True
|
||||
while new_input_added:
|
||||
new_input_added = False
|
||||
new_input.append(Message(role="user", text=query))
|
||||
async for update in agent.run(new_input, session=session, stream=True, options={"store": True}):
|
||||
if update.user_input_requests:
|
||||
# Reset input to only contain new approval responses for the next iteration
|
||||
new_input = []
|
||||
for user_input_needed in update.user_input_requests:
|
||||
print(
|
||||
f"User Input Request for function from {agent.name}: {user_input_needed.function_call.name}"
|
||||
|
||||
Reference in New Issue
Block a user