From b85630d2ed679fc4151f57ffdd2c3bfad005b341 Mon Sep 17 00:00:00 2001 From: Copilot Date: Mon, 23 Mar 2026 19:38:02 +0000 Subject: [PATCH 1/6] 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> --- .../tests/openai/test_openai_chat_client.py | 23 +++++++++++++++++++ .../agent_with_hosted_mcp/agent.yaml | 2 +- .../agent_with_hosted_mcp/main.py | 23 ++++++++++--------- .../agent_with_hosted_mcp/requirements.txt | 4 ++-- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/python/packages/core/tests/openai/test_openai_chat_client.py b/python/packages/core/tests/openai/test_openai_chat_client.py index 86e8b115d6..7279157b3b 100644 --- a/python/packages/core/tests/openai/test_openai_chat_client.py +++ b/python/packages/core/tests/openai/test_openai_chat_client.py @@ -188,6 +188,29 @@ class UnsupportedTool: 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 = OpenAIChatClient() + + 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" + + def test_prepare_tools_with_single_function_tool( openai_unit_test_env: dict[str, str], ) -> None: diff --git a/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/agent.yaml b/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/agent.yaml index 5a0f58554d..d68b32cbb4 100644 --- a/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/agent.yaml +++ b/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/agent.yaml @@ -22,7 +22,7 @@ template: environment_variables: - name: AZURE_OPENAI_ENDPOINT value: ${AZURE_OPENAI_ENDPOINT} - - name: AZURE_OPENAI_CHAT_DEPLOYMENT_NAME + - name: AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME value: "{{chat}}" resources: - kind: model diff --git a/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/main.py b/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/main.py index 53ee10e6bf..574bf731bd 100644 --- a/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/main.py +++ b/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/main.py @@ -1,6 +1,6 @@ # Copyright (c) Microsoft. All rights reserved. -from agent_framework.azure import AzureOpenAIChatClient +from agent_framework.azure import AzureOpenAIResponsesClient from azure.ai.agentserver.agentframework import from_agent_framework # pyright: ignore[reportUnknownVariableType] from azure.identity import DefaultAzureCredential from dotenv import load_dotenv @@ -10,18 +10,19 @@ def main(): - # Create MCP tool configuration as dict - mcp_tool = { - "type": "mcp", - "server_label": "Microsoft_Learn_MCP", - "server_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 = AzureOpenAIChatClient(credential=DefaultAzureCredential()).as_agent( + client = AzureOpenAIResponsesClient(credential=DefaultAzureCredential()) + + # Create MCP tool configuration using the Responses Client helper + mcp_tool = client.get_mcp_tool( + name="Microsoft Learn MCP", + url="https://learn.microsoft.com/api/mcp", + ) + + # Create an Agent using the Azure OpenAI Responses Client with a MCP Tool that connects to Microsoft Learn MCP + agent = client.as_agent( 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 diff --git a/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/requirements.txt b/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/requirements.txt index d05845588a..5ee6dd2eb5 100644 --- a/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/requirements.txt +++ b/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/requirements.txt @@ -1,2 +1,2 @@ -azure-ai-agentserver-agentframework==1.0.0b3 -agent-framework \ No newline at end of file +azure-ai-agentserver-agentframework==1.0.0b16 +agent-framework-azure-ai From 088071f55f8258f50e33757493abbf6f944cb083 Mon Sep 17 00:00:00 2001 From: Copilot Date: Mon, 23 Mar 2026 19:42:41 +0000 Subject: [PATCH 2/6] Python: Fix agent_with_hosted_mcp sample to use Responses API client for MCP tools Fixes #4861 --- .../core/tests/openai/REPRODUCTION_REPORT.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 python/packages/core/tests/openai/REPRODUCTION_REPORT.md diff --git a/python/packages/core/tests/openai/REPRODUCTION_REPORT.md b/python/packages/core/tests/openai/REPRODUCTION_REPORT.md new file mode 100644 index 0000000000..8e8c380047 --- /dev/null +++ b/python/packages/core/tests/openai/REPRODUCTION_REPORT.md @@ -0,0 +1,29 @@ +## Summary + +Issue #4861 is **reproduced**. The `AzureOpenAIChatClient._prepare_tools_for_openai()` method passes MCP tool dicts (with `"type": "mcp"`) through unchanged to the Chat Completions API, which only accepts `"function"` and `"custom"` tool types, resulting in a 400 error. + +## Reproduction Attempt + +Two tests were written and both pass, confirming the bug: + +1. **`test_mcp_tool_dict_is_passed_through_unchanged`**: Calls `_prepare_tools_for_openai()` with the exact MCP tool dict from the sample. Confirms the tool is included in the output with `type="mcp"` — an unsupported type for the Chat Completions API. + +2. **`test_mcp_tool_causes_api_rejection`**: Mocks the API call and verifies that when MCP tools are provided through `_inner_get_response`, the `type="mcp"` dict reaches the API call unchanged, which would cause the 400 error described in the issue. + +Test output: +``` +packages/core/tests/openai/test_issue_4861_mcp_tool_chat_client.py .. [100%] +2 passed in 3.74s +``` + +## Affected Code + +- **`python/packages/core/agent_framework/openai/_chat_client.py`** lines 357-364: `_prepare_tools_for_openai()` has a `MutableMapping` branch that passes through all dict-based tools unchanged (line 364: `chat_tools.append(typed_tool)`). Only `"web_search"` type gets special handling. MCP tools (`"type": "mcp"`) fall through to the generic pass-through. + +- **`python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/main.py`**: The sample uses `AzureOpenAIChatClient` with `{"type": "mcp", ...}` dict, but should use `AzureOpenAIResponsesClient` which natively supports MCP tools via its `get_mcp_tool()` helper. + +## Verdict + +**Reproduced** — High confidence. + +The bug is clearly present in the current codebase. The `_prepare_tools_for_openai` method passes MCP tool dicts through to the Chat Completions API, which rejects `"type": "mcp"`. The fix should change the sample to use `AzureOpenAIResponsesClient` instead, as the Chat Completions API fundamentally does not support MCP tools. From cf458ba04bcff7328abadee95432d177691b8235 Mon Sep 17 00:00:00 2001 From: Copilot Date: Mon, 23 Mar 2026 19:47:57 +0000 Subject: [PATCH 3/6] 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> --- .../core/tests/openai/REPRODUCTION_REPORT.md | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 python/packages/core/tests/openai/REPRODUCTION_REPORT.md diff --git a/python/packages/core/tests/openai/REPRODUCTION_REPORT.md b/python/packages/core/tests/openai/REPRODUCTION_REPORT.md deleted file mode 100644 index 8e8c380047..0000000000 --- a/python/packages/core/tests/openai/REPRODUCTION_REPORT.md +++ /dev/null @@ -1,29 +0,0 @@ -## Summary - -Issue #4861 is **reproduced**. The `AzureOpenAIChatClient._prepare_tools_for_openai()` method passes MCP tool dicts (with `"type": "mcp"`) through unchanged to the Chat Completions API, which only accepts `"function"` and `"custom"` tool types, resulting in a 400 error. - -## Reproduction Attempt - -Two tests were written and both pass, confirming the bug: - -1. **`test_mcp_tool_dict_is_passed_through_unchanged`**: Calls `_prepare_tools_for_openai()` with the exact MCP tool dict from the sample. Confirms the tool is included in the output with `type="mcp"` — an unsupported type for the Chat Completions API. - -2. **`test_mcp_tool_causes_api_rejection`**: Mocks the API call and verifies that when MCP tools are provided through `_inner_get_response`, the `type="mcp"` dict reaches the API call unchanged, which would cause the 400 error described in the issue. - -Test output: -``` -packages/core/tests/openai/test_issue_4861_mcp_tool_chat_client.py .. [100%] -2 passed in 3.74s -``` - -## Affected Code - -- **`python/packages/core/agent_framework/openai/_chat_client.py`** lines 357-364: `_prepare_tools_for_openai()` has a `MutableMapping` branch that passes through all dict-based tools unchanged (line 364: `chat_tools.append(typed_tool)`). Only `"web_search"` type gets special handling. MCP tools (`"type": "mcp"`) fall through to the generic pass-through. - -- **`python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/main.py`**: The sample uses `AzureOpenAIChatClient` with `{"type": "mcp", ...}` dict, but should use `AzureOpenAIResponsesClient` which natively supports MCP tools via its `get_mcp_tool()` helper. - -## Verdict - -**Reproduced** — High confidence. - -The bug is clearly present in the current codebase. The `_prepare_tools_for_openai` method passes MCP tool dicts through to the Chat Completions API, which rejects `"type": "mcp"`. The fix should change the sample to use `AzureOpenAIResponsesClient` instead, as the Chat Completions API fundamentally does not support MCP tools. From 16e796f093f745c09d6d12758117fc41ef58f2da Mon Sep 17 00:00:00 2001 From: Copilot Date: Mon, 23 Mar 2026 19:53:37 +0000 Subject: [PATCH 4/6] 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> --- .../tests/openai/test_openai_chat_client.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/python/packages/core/tests/openai/test_openai_chat_client.py b/python/packages/core/tests/openai/test_openai_chat_client.py index 7279157b3b..d13d5547b2 100644 --- a/python/packages/core/tests/openai/test_openai_chat_client.py +++ b/python/packages/core/tests/openai/test_openai_chat_client.py @@ -211,6 +211,40 @@ def test_mcp_tool_dict_passed_through_to_chat_api(openai_unit_test_env: dict[str 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 = OpenAIChatClient() + 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: From 16717cd5389b546b57f7e24c7cacca6b89e0b4b5 Mon Sep 17 00:00:00 2001 From: Giles Odigwe Date: Thu, 26 Mar 2026 14:30:11 -0700 Subject: [PATCH 5/6] small fix --- .../hosted_agents/agent_with_hosted_mcp/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/requirements.txt b/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/requirements.txt index 515a4ec3f1..250c059d77 100644 --- a/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/requirements.txt +++ b/python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/requirements.txt @@ -1,2 +1,2 @@ azure-ai-agentserver-agentframework==1.0.0b16 -agent-framework-foundry +agent-framework From bb4cca161f4db7ee56622164dad68f1bd4a3e9b0 Mon Sep 17 00:00:00 2001 From: Giles Odigwe Date: Tue, 31 Mar 2026 14:21:45 -0700 Subject: [PATCH 6/6] 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> --- .../04_WorkflowMcpTool/local.settings.json | 8 ++++++++ .../05_WorkflowAndAgents/local.settings.json | 10 ++++++++++ 2 files changed, 18 insertions(+) create mode 100644 dotnet/samples/04-hosting/DurableWorkflows/AzureFunctions/04_WorkflowMcpTool/local.settings.json create mode 100644 dotnet/samples/04-hosting/DurableWorkflows/AzureFunctions/05_WorkflowAndAgents/local.settings.json diff --git a/dotnet/samples/04-hosting/DurableWorkflows/AzureFunctions/04_WorkflowMcpTool/local.settings.json b/dotnet/samples/04-hosting/DurableWorkflows/AzureFunctions/04_WorkflowMcpTool/local.settings.json new file mode 100644 index 0000000000..fcb6658e92 --- /dev/null +++ b/dotnet/samples/04-hosting/DurableWorkflows/AzureFunctions/04_WorkflowMcpTool/local.settings.json @@ -0,0 +1,8 @@ +{ + "IsEncrypted": false, + "Values": { + "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated", + "AzureWebJobsStorage": "UseDevelopmentStorage=true", + "DURABLE_TASK_SCHEDULER_CONNECTION_STRING": "Endpoint=http://localhost:8080;TaskHub=default;Authentication=None" + } +} diff --git a/dotnet/samples/04-hosting/DurableWorkflows/AzureFunctions/05_WorkflowAndAgents/local.settings.json b/dotnet/samples/04-hosting/DurableWorkflows/AzureFunctions/05_WorkflowAndAgents/local.settings.json new file mode 100644 index 0000000000..5f6d7d3340 --- /dev/null +++ b/dotnet/samples/04-hosting/DurableWorkflows/AzureFunctions/05_WorkflowAndAgents/local.settings.json @@ -0,0 +1,10 @@ +{ + "IsEncrypted": false, + "Values": { + "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated", + "AzureWebJobsStorage": "UseDevelopmentStorage=true", + "DURABLE_TASK_SCHEDULER_CONNECTION_STRING": "Endpoint=http://localhost:8080;TaskHub=default;Authentication=None", + "AZURE_OPENAI_ENDPOINT": "", + "AZURE_OPENAI_DEPLOYMENT_NAME": "" + } +}