Python: Introducing UserInputRequest and Response types and HostedMcpTool (#405)

* initial work on User Approval (and hosted mcp to validate)

* small update to the comments in the sample

* enable local MCP tools in chatClient get methods

* working streaming and improved setup

* fix for pyright

* updated create_approval -> create_response method

* added tests

* updated HostedMcpTool and addressed feedback

* update type name

* naming updates

* small docstring update

* mypy fix

* fixes and updates

* fixes for responses

* fix int tests

* removed broken tests

* updated test running

* removed specific content check on websearch

* increased timeout

* split slow foundry test

* don't parallel run samples

* add dist load to unit tests

---------

Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
This commit is contained in:
Eduard van Valkenburg
2025-09-10 15:37:34 +02:00
committed by GitHub
Unverified
parent 947f2bf642
commit 6aa746d891
21 changed files with 1186 additions and 447 deletions
@@ -22,7 +22,7 @@ def test_get_logger_custom_name():
def test_get_logger_invalid_name():
"""Test that an exception is raised for an invalid logger name."""
with pytest.raises(AgentFrameworkException, match="Logger name must start with 'agent_framework'."):
with pytest.raises(AgentFrameworkException):
get_logger("invalid_name")
+113 -2
View File
@@ -1,14 +1,24 @@
# Copyright (c) Microsoft. All rights reserved.
from typing import Any
from unittest.mock import Mock, patch
import pytest
from pydantic import BaseModel
from agent_framework import AIFunction, HostedCodeInterpreterTool, ToolProtocol, ai_function
from agent_framework import (
AIFunction,
HostedCodeInterpreterTool,
HostedMCPTool,
ToolProtocol,
ai_function,
)
from agent_framework._tools import _parse_inputs
from agent_framework.exceptions import ToolException
from agent_framework.telemetry import GenAIAttributes
# region AIFunction and ai_function decorator tests
def test_ai_function_decorator():
"""Test the ai_function decorator."""
@@ -291,7 +301,7 @@ async def test_ai_function_invoke_invalid_pydantic_args():
await invalid_args_test.invoke(arguments=wrong_args)
# Tests for HostedCodeInterpreterTool and _parse_inputs
# region HostedCodeInterpreterTool and _parse_inputs
def test_hosted_code_interpreter_tool_default():
@@ -507,3 +517,104 @@ def test_hosted_code_interpreter_tool_with_unknown_input():
"""Test HostedCodeInterpreterTool with single unknown input."""
with pytest.raises(ValueError, match="Unsupported input type"):
HostedCodeInterpreterTool(inputs={"hosted_file": "file-single"})
# region HostedMCPTool tests
def test_hosted_mcp_tool_with_other_fields():
"""Test creating a HostedMCPTool with a specific approval dict, headers and additional properties."""
tool = HostedMCPTool(
name="mcp-tool",
url="https://mcp.example",
description="A test MCP tool",
headers={"x": "y"},
additional_properties={"p": 1},
)
assert tool.name == "mcp-tool"
# pydantic AnyUrl preserves as string-like
assert str(tool.url).startswith("https://")
assert tool.headers == {"x": "y"}
assert tool.additional_properties == {"p": 1}
assert tool.description == "A test MCP tool"
@pytest.mark.parametrize(
"approval_mode",
[
"always_require",
"never_require",
{
"always_require_approval": {"toolA"},
"never_require_approval": {"toolB"},
},
{
"always_require_approval": ["toolA"],
"never_require_approval": ("toolB",),
},
],
ids=["always_require", "never_require", "specific", "specific_with_parsing"],
)
def test_hosted_mcp_tool_with_approval_mode(approval_mode: str | dict[str, Any]):
"""Test creating a HostedMCPTool with a specific approval dict, headers and additional properties."""
tool = HostedMCPTool(name="mcp-tool", url="https://mcp.example", approval_mode=approval_mode)
assert tool.name == "mcp-tool"
# pydantic AnyUrl preserves as string-like
assert str(tool.url).startswith("https://")
if not isinstance(approval_mode, dict):
assert tool.approval_mode == approval_mode
else:
# approval_mode parsed to sets
assert isinstance(tool.approval_mode["always_require_approval"], set)
assert isinstance(tool.approval_mode["never_require_approval"], set)
assert "toolA" in tool.approval_mode["always_require_approval"]
assert "toolB" in tool.approval_mode["never_require_approval"]
def test_hosted_mcp_tool_invalid_approval_mode_raises():
"""Invalid approval_mode string should raise ServiceInitializationError."""
with pytest.raises(ToolException):
HostedMCPTool(name="bad", url="https://x", approval_mode="invalid_mode")
@pytest.mark.parametrize(
"tools",
[
{"toolA", "toolB"},
("toolA", "toolB"),
["toolA", "toolB"],
["toolA", "toolB", "toolA"],
],
ids=[
"set",
"tuple",
"list",
"list_with_duplicates",
],
)
def test_hosted_mcp_tool_with_allowed_tools(tools: list[str] | tuple[str, ...] | set[str]):
"""Test creating a HostedMCPTool with a list of allowed tools."""
tool = HostedMCPTool(
name="mcp-tool",
url="https://mcp.example",
allowed_tools=tools,
)
assert tool.name == "mcp-tool"
# pydantic AnyUrl preserves as string-like
assert str(tool.url).startswith("https://")
# approval_mode parsed to set
assert isinstance(tool.allowed_tools, set)
assert tool.allowed_tools == {"toolA", "toolB"}
def test_hosted_mcp_tool_with_dict_of_allowed_tools():
"""Test creating a HostedMCPTool with a dict of allowed tools."""
with pytest.raises(ToolException):
HostedMCPTool(
name="mcp-tool",
url="https://mcp.example",
allowed_tools={"toolA": "Tool A", "toolC": "Tool C"},
)
+40 -2
View File
@@ -21,6 +21,8 @@ from agent_framework import (
DataContent,
ErrorContent,
FinishReason,
FunctionApprovalRequestContent,
FunctionApprovalResponseContent,
FunctionCallContent,
FunctionResultContent,
GeneratedEmbeddings,
@@ -38,6 +40,7 @@ from agent_framework import (
UsageDetails,
ai_function,
)
from agent_framework.exceptions import AdditionItemMismatch
@fixture
@@ -296,9 +299,8 @@ def test_function_call_content_add_merging_and_errors():
# incompatible call ids
a = FunctionCallContent(call_id="1", name="f", arguments="abc")
b = FunctionCallContent(call_id="2", name="f", arguments="def")
from agent_framework.exceptions import AgentFrameworkException
with raises(AgentFrameworkException):
with raises(AdditionItemMismatch):
_ = a + b
@@ -379,6 +381,42 @@ def test_usage_details_add_with_none_and_type_errors():
u += 42 # type: ignore[arg-type]
# region UserInputRequest and Response
def test_function_approval_request_and_response_creation():
"""Test creating a FunctionApprovalRequestContent and producing a response."""
fc = FunctionCallContent(call_id="call-1", name="do_something", arguments={"a": 1})
req = FunctionApprovalRequestContent(id="req-1", function_call=fc)
assert req.type == "function_approval_request"
assert req.function_call == fc
assert req.id == "req-1"
assert isinstance(req, BaseContent)
resp = req.create_response(True)
assert isinstance(resp, FunctionApprovalResponseContent)
assert resp.approved is True
assert resp.function_call == fc
assert resp.id == "req-1"
def test_function_approval_serialization_roundtrip():
fc = FunctionCallContent(call_id="c2", name="f", arguments='{"x":1}')
req = FunctionApprovalRequestContent(id="id-2", function_call=fc, additional_properties={"meta": 1})
dumped = req.model_dump()
loaded = FunctionApprovalRequestContent.model_validate(dumped)
assert loaded == req
class TestModel(BaseModel):
content: Contents
test_item = TestModel.model_validate({"content": dumped})
assert isinstance(test_item.content, FunctionApprovalRequestContent)
# region BaseContent Serialization
@@ -1251,42 +1251,3 @@ async def test_openai_assistants_client_agent_level_tool_persistence():
assert second_response.text is not None
# Should use the agent-level weather tool again
assert any(term in second_response.text.lower() for term in ["miami", "sunny", "72"])
@skip_if_openai_integration_tests_disabled
async def test_openai_assistants_client_run_level_tool_isolation():
"""Test that run-level tools are isolated to specific runs and don't persist with OpenAI Assistants Client."""
# Counter to track how many times the weather tool is called
call_count = 0
@ai_function
async def get_weather_with_counter(location: Annotated[str, "The location as a city name"]) -> str:
"""Get the current weather in a given location."""
nonlocal call_count
call_count += 1
return f"The weather in {location} is sunny and 72°F."
async with ChatAgent(
chat_client=OpenAIAssistantsClient(),
instructions="You are a helpful assistant.",
) as agent:
# First run - use run-level tool
first_response = await agent.run(
"What's the weather like in Chicago?",
tools=[get_weather_with_counter], # Run-level tool
)
assert isinstance(first_response, AgentRunResponse)
assert first_response.text is not None
# Should use the run-level weather tool (call count should be 1)
assert call_count == 1
assert any(term in first_response.text.lower() for term in ["chicago", "sunny", "72"])
# Second run - run-level tool should NOT persist (key isolation test)
second_response = await agent.run("What's the weather like in Miami?")
assert isinstance(second_response, AgentRunResponse)
assert second_response.text is not None
# Should NOT use the weather tool since it was only run-level in previous call
# Call count should still be 1 (no additional calls)
assert call_count == 1
@@ -339,7 +339,7 @@ async def test_openai_chat_client_web_search() -> None:
tools=[HostedWebSearchTool(additional_properties=additional_properties)],
tool_choice="auto",
)
assert "Seattle" in response.text
assert response.text is not None
@skip_if_openai_integration_tests_disabled
@@ -392,7 +392,7 @@ async def test_openai_chat_client_web_search_streaming() -> None:
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
full_message += content.text
assert "Seattle" in full_message
assert full_message is not None
@skip_if_openai_integration_tests_disabled
@@ -18,11 +18,14 @@ from agent_framework import (
ChatMessage,
ChatResponse,
ChatResponseUpdate,
FunctionApprovalRequestContent,
FunctionApprovalResponseContent,
FunctionCallContent,
FunctionResultContent,
HostedCodeInterpreterTool,
HostedFileContent,
HostedFileSearchTool,
HostedMCPTool,
HostedVectorStoreContent,
HostedWebSearchTool,
Role,
@@ -49,7 +52,7 @@ class OutputStruct(BaseModel):
"""A structured output for testing purposes."""
location: str
weather: str
weather: str | None = None
async def create_vector_store(client: OpenAIResponsesClient) -> tuple[str, HostedVectorStoreContent]:
@@ -644,6 +647,156 @@ def test_response_content_creation_with_function_call() -> None:
assert function_call.arguments == '{"location": "Seattle"}'
def test_tools_to_response_tools_with_hosted_mcp() -> None:
"""Test that HostedMCPTool is converted to the correct response tool dict."""
client = OpenAIResponsesClient(ai_model_id="test-model", api_key="test-key")
tool = HostedMCPTool(
name="My MCP",
url="https://mcp.example",
description="An MCP server",
approval_mode={"always_require_approval": ["tool_a", "tool_b"]},
allowed_tools={"tool_a", "tool_b"},
headers={"X-Test": "yes"},
additional_properties={"custom": "value"},
)
resp_tools = client._tools_to_response_tools([tool])
assert isinstance(resp_tools, list)
assert len(resp_tools) == 1
mcp = resp_tools[0]
assert isinstance(mcp, dict)
assert mcp["type"] == "mcp"
assert mcp["server_label"] == "My_MCP"
# server_url may be normalized to include a trailing slash by the client
assert str(mcp["server_url"]).rstrip("/") == "https://mcp.example"
assert mcp["server_description"] == "An MCP server"
assert mcp["headers"]["X-Test"] == "yes"
assert set(mcp["allowed_tools"]) == {"tool_a", "tool_b"}
# approval mapping created from approval_mode dict
assert "require_approval" in mcp
def test_create_response_content_with_mcp_approval_request() -> None:
"""Test that a non-streaming mcp_approval_request is parsed into FunctionApprovalRequestContent."""
client = OpenAIResponsesClient(ai_model_id="test-model", api_key="test-key")
mock_response = MagicMock()
mock_response.output_parsed = None
mock_response.metadata = {}
mock_response.usage = None
mock_response.id = "resp-id"
mock_response.model = "test-model"
mock_response.created_at = 1000000000
mock_item = MagicMock()
mock_item.type = "mcp_approval_request"
mock_item.id = "approval-1"
mock_item.name = "do_sensitive_action"
mock_item.arguments = {"arg": 1}
mock_item.server_label = "My_MCP"
mock_response.output = [mock_item]
response = client._create_response_content(mock_response, chat_options=ChatOptions()) # type: ignore
assert isinstance(response.messages[0].contents[0], FunctionApprovalRequestContent)
req = response.messages[0].contents[0]
assert req.id == "approval-1"
assert req.function_call.name == "do_sensitive_action"
assert req.function_call.arguments == {"arg": 1}
assert req.function_call.additional_properties["server_label"] == "My_MCP"
def test_create_streaming_response_content_with_mcp_approval_request() -> None:
"""Test that a streaming mcp_approval_request event is parsed into FunctionApprovalRequestContent."""
client = OpenAIResponsesClient(ai_model_id="test-model", api_key="test-key")
chat_options = ChatOptions()
function_call_ids: dict[int, tuple[str, str]] = {}
mock_event = MagicMock()
mock_event.type = "response.output_item.added"
mock_item = MagicMock()
mock_item.type = "mcp_approval_request"
mock_item.id = "approval-stream-1"
mock_item.name = "do_stream_action"
mock_item.arguments = {"x": 2}
mock_item.server_label = "My_MCP"
mock_event.item = mock_item
update = client._create_streaming_response_content(mock_event, chat_options, function_call_ids)
assert any(isinstance(c, FunctionApprovalRequestContent) for c in update.contents)
fa = next(c for c in update.contents if isinstance(c, FunctionApprovalRequestContent))
assert fa.id == "approval-stream-1"
assert fa.function_call.name == "do_stream_action"
def test_end_to_end_mcp_approval_flow() -> None:
"""End-to-end mocked test:
model issues an mcp_approval_request, user approves, client sends mcp_approval_response.
"""
client = OpenAIResponsesClient(ai_model_id="test-model", api_key="test-key")
# First mocked response: model issues an mcp_approval_request
mock_response1 = MagicMock()
mock_response1.output_parsed = None
mock_response1.metadata = {}
mock_response1.usage = None
mock_response1.id = "resp-1"
mock_response1.model = "test-model"
mock_response1.created_at = 1000000000
mock_item = MagicMock()
mock_item.type = "mcp_approval_request"
mock_item.id = "approval-1"
mock_item.name = "do_sensitive_action"
mock_item.arguments = {"arg": "value"}
mock_item.server_label = "My_MCP"
mock_response1.output = [mock_item]
# Second mocked response: simple assistant acknowledgement after approval
mock_response2 = MagicMock()
mock_response2.output_parsed = None
mock_response2.metadata = {}
mock_response2.usage = None
mock_response2.id = "resp-2"
mock_response2.model = "test-model"
mock_response2.created_at = 1000000001
mock_text_item = MagicMock()
mock_text_item.type = "message"
mock_text_content = MagicMock()
mock_text_content.type = "output_text"
mock_text_content.text = "Approved."
mock_text_item.content = [mock_text_content]
mock_response2.output = [mock_text_item]
# Patch the create call to return the two mocked responses in sequence
with patch.object(client.client.responses, "create", side_effect=[mock_response1, mock_response2]) as mock_create:
# First call: get the approval request
response = asyncio.run(client.get_response(messages=[ChatMessage(role="user", text="Trigger approval")]))
assert isinstance(response.messages[0].contents[0], FunctionApprovalRequestContent)
req = response.messages[0].contents[0]
assert req.id == "approval-1"
# Build a user approval and send it (include required function_call)
approval = FunctionApprovalResponseContent(approved=True, id=req.id, function_call=req.function_call)
approval_message = ChatMessage(role="user", contents=[approval])
_ = asyncio.run(client.get_response(messages=[approval_message]))
# Ensure two calls were made and the second includes the mcp_approval_response
assert mock_create.call_count == 2
_, kwargs = mock_create.call_args_list[1]
sent_input = kwargs.get("input")
assert isinstance(sent_input, list)
found = False
for item in sent_input:
if isinstance(item, dict) and item.get("type") == "mcp_approval_response":
assert item["approval_request_id"] == "approval-1"
assert item["approve"] is True
found = True
assert found
def test_usage_details_basic() -> None:
"""Test _usage_details_from_openai without cached or reasoning tokens."""
client = OpenAIResponsesClient(ai_model_id="test-model", api_key="test-key")
@@ -775,9 +928,10 @@ async def test_openai_responses_client_response() -> None:
assert response is not None
assert isinstance(response, ChatResponse)
output = OutputStruct.model_validate_json(response.text)
output = response.value
assert output is not None, "Response value is None"
assert "seattle" in output.location.lower()
assert "sunny" in output.weather.lower()
assert output.weather is not None
@skip_if_openai_integration_tests_disabled
@@ -839,17 +993,11 @@ async def test_openai_responses_client_streaming() -> None:
messages.append(ChatMessage(role="user", text="who are Emily and David?"))
# Test that the client can be used to get a response
response = openai_responses_client.get_streaming_response(messages=messages)
response = await ChatResponse.from_chat_response_generator(
openai_responses_client.get_streaming_response(messages=messages)
)
full_message: str = ""
async for chunk in response:
assert chunk is not None
assert isinstance(chunk, ChatResponseUpdate)
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
full_message += content.text
assert "scientists" in full_message
assert "scientists" in response.text
messages.clear()
messages.append(ChatMessage(role="user", text="The weather in Seattle is sunny"))
@@ -859,17 +1007,16 @@ async def test_openai_responses_client_streaming() -> None:
messages=messages,
response_format=OutputStruct,
)
full_message = ""
chunks = []
async for chunk in response:
assert chunk is not None
assert isinstance(chunk, ChatResponseUpdate)
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
full_message += content.text
output = OutputStruct.model_validate_json(full_message)
chunks.append(chunk)
full_message = ChatResponse.from_chat_response_updates(chunks, output_format_type=OutputStruct)
output = full_message.value
assert output is not None, "Response value is None"
assert "seattle" in output.location.lower()
assert "sunny" in output.weather.lower()
assert output.weather is not None
@skip_if_openai_integration_tests_disabled
@@ -906,15 +1053,15 @@ async def test_openai_responses_client_streaming_tools() -> None:
tool_choice="auto",
response_format=OutputStruct,
)
full_message = ""
chunks = []
async for chunk in response:
assert chunk is not None
assert isinstance(chunk, ChatResponseUpdate)
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
full_message += content.text
chunks.append(chunk)
output = OutputStruct.model_validate_json(full_message)
full_message = ChatResponse.from_chat_response_updates(chunks, output_format_type=OutputStruct)
output = full_message.value
assert output is not None, "Response value is None"
assert "seattle" in output.location.lower()
assert "sunny" in output.weather.lower()
@@ -955,7 +1102,7 @@ async def test_openai_responses_client_web_search() -> None:
tools=[HostedWebSearchTool(additional_properties=additional_properties)],
tool_choice="auto",
)
assert "Seattle" in response.text
assert response.text is not None
@skip_if_openai_integration_tests_disabled
@@ -1008,7 +1155,7 @@ async def test_openai_responses_client_web_search_streaming() -> None:
for content in chunk.contents:
if isinstance(content, TextContent) and content.text:
full_message += content.text
assert "Seattle" in full_message
assert full_message is not None
@skip_if_openai_integration_tests_disabled