mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Fix agent_with_hosted_mcp sample to use Foundry client for MCP tools (#4867)
* Fix agent_with_hosted_mcp sample to use AzureOpenAIResponsesClient (#4861) The agent_with_hosted_mcp sample used AzureOpenAIChatClient with an MCP tool dict, but the Chat Completions API only supports 'function' and 'custom' tool types, not 'mcp'. This caused a 400 error at runtime. Switch the sample to AzureOpenAIResponsesClient which natively supports MCP tools via the Responses API. Use get_mcp_tool() to construct the tool config. Changes: - main.py: Replace AzureOpenAIChatClient with AzureOpenAIResponsesClient - requirements.txt: Update azure-ai-agentserver-agentframework to 1.0.0b16 and use agent-framework-azure-ai package - agent.yaml: Use AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME env var - Add regression test documenting chat client MCP tool passthrough behavior Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Python: Fix agent_with_hosted_mcp sample to use Responses API client for MCP tools Fixes #4861 * Remove REPRODUCTION_REPORT.md investigation artifact (#4861) Remove the reproduction report markdown file from the test directory. Investigation notes belong in the GitHub issue or PR description, not as committed files in the source tree. The regression test in test_openai_chat_client.py already provides automated verification. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Add MCP tool API rejection regression test (#4861) Add test_mcp_tool_dict_causes_api_rejection to verify that MCP tool dicts passed through to the Chat Completions API result in a clear ChatClientException rather than being silently dropped. This completes the regression test coverage requested in code review. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * small fix * Revert deletion of dotnet local.settings.json files Restore the two local.settings.json files that were accidentally deleted in this PR. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <copilot@github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
651e317907
commit
d992febe9b
@@ -237,6 +237,63 @@ def test_unsupported_tool_handling(openai_unit_test_env: dict[str, str]) -> None
|
||||
assert result["tools"] == [dict_tool]
|
||||
|
||||
|
||||
def test_mcp_tool_dict_passed_through_to_chat_api(openai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test that MCP tool dicts are passed through unchanged by the chat client.
|
||||
|
||||
The Chat Completions API does not support "type": "mcp" tools. MCP tools
|
||||
should be used with the Responses API client instead. This test documents
|
||||
that the chat client passes dict-based tools through without filtering,
|
||||
so callers must use the correct client for MCP tools.
|
||||
"""
|
||||
client = OpenAIChatCompletionClient()
|
||||
|
||||
mcp_tool = {
|
||||
"type": "mcp",
|
||||
"server_label": "Microsoft_Learn_MCP",
|
||||
"server_url": "https://learn.microsoft.com/api/mcp",
|
||||
}
|
||||
|
||||
result = client._prepare_tools_for_openai(mcp_tool)
|
||||
assert "tools" in result
|
||||
assert len(result["tools"]) == 1
|
||||
# The chat client passes dict tools through unchanged, including unsupported types
|
||||
assert result["tools"][0]["type"] == "mcp"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mcp_tool_dict_causes_api_rejection(openai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test that MCP tool dicts passed to the Chat Completions API cause a rejection.
|
||||
|
||||
The Chat Completions API only supports "type": "function" tools.
|
||||
When an MCP tool dict reaches the API, it returns a 400 error.
|
||||
This regression test for #4861 verifies the chat client does not
|
||||
silently drop or transform MCP dicts, so callers get a clear error
|
||||
rather than a silent no-op.
|
||||
"""
|
||||
client = OpenAIChatCompletionClient()
|
||||
messages = [Message(role="user", text="test message")]
|
||||
|
||||
mcp_tool = {
|
||||
"type": "mcp",
|
||||
"server_label": "Microsoft_Learn_MCP",
|
||||
"server_url": "https://learn.microsoft.com/api/mcp",
|
||||
}
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_error = BadRequestError(
|
||||
message="Invalid tool type: mcp",
|
||||
response=mock_response,
|
||||
body={"error": {"code": "invalid_request", "message": "Invalid tool type: mcp"}},
|
||||
)
|
||||
mock_error.code = "invalid_request"
|
||||
|
||||
with (
|
||||
patch.object(client.client.chat.completions, "create", side_effect=mock_error),
|
||||
pytest.raises(ChatClientException),
|
||||
):
|
||||
await client._inner_get_response(messages=messages, options={"tools": mcp_tool}) # type: ignore
|
||||
|
||||
|
||||
def test_prepare_tools_with_single_function_tool(
|
||||
openai_unit_test_env: dict[str, str],
|
||||
) -> None:
|
||||
|
||||
@@ -11,18 +11,20 @@ load_dotenv()
|
||||
|
||||
|
||||
def main():
|
||||
client = FoundryChatClient(credential=AzureCliCredential())
|
||||
|
||||
# Create MCP tool configuration as dict
|
||||
mcp_tool = {
|
||||
"type": "mcp",
|
||||
"server_label": "Microsoft_Learn_MCP",
|
||||
"server_url": "https://learn.microsoft.com/api/mcp",
|
||||
}
|
||||
mcp_tool = client.get_mcp_tool(
|
||||
name="Microsoft_Learn_MCP",
|
||||
url="https://learn.microsoft.com/api/mcp",
|
||||
)
|
||||
|
||||
# Create an Agent using the Azure OpenAI Chat Client with a MCP Tool that connects to Microsoft Learn MCP
|
||||
agent = Agent(
|
||||
client=FoundryChatClient(credential=AzureCliCredential()),
|
||||
client=client,
|
||||
name="DocsAgent",
|
||||
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
|
||||
tools=mcp_tool,
|
||||
tools=[mcp_tool],
|
||||
)
|
||||
# Run the agent as a hosted agent
|
||||
from_agent_framework(agent).run()
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
azure-ai-agentserver-agentframework==1.0.0b3
|
||||
agent-framework
|
||||
azure-ai-agentserver-agentframework==1.0.0b16
|
||||
agent-framework
|
||||
|
||||
Reference in New Issue
Block a user