Skip to content

Commit d992feb

Browse files
giles17CopilotCopilot
authored
Python: Fix agent_with_hosted_mcp sample to use Foundry client for MCP tools (microsoft#4867)
* Fix agent_with_hosted_mcp sample to use AzureOpenAIResponsesClient (microsoft#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 microsoft#4861 * Remove REPRODUCTION_REPORT.md investigation artifact (microsoft#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 (microsoft#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>
1 parent 651e317 commit d992feb

3 files changed

Lines changed: 68 additions & 9 deletions

File tree

python/packages/openai/tests/openai/test_openai_chat_completion_client.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,63 @@ class UnsupportedTool:
237237
assert result["tools"] == [dict_tool]
238238

239239

240+
def test_mcp_tool_dict_passed_through_to_chat_api(openai_unit_test_env: dict[str, str]) -> None:
241+
"""Test that MCP tool dicts are passed through unchanged by the chat client.
242+
243+
The Chat Completions API does not support "type": "mcp" tools. MCP tools
244+
should be used with the Responses API client instead. This test documents
245+
that the chat client passes dict-based tools through without filtering,
246+
so callers must use the correct client for MCP tools.
247+
"""
248+
client = OpenAIChatCompletionClient()
249+
250+
mcp_tool = {
251+
"type": "mcp",
252+
"server_label": "Microsoft_Learn_MCP",
253+
"server_url": "https://learn.microsoft.com/api/mcp",
254+
}
255+
256+
result = client._prepare_tools_for_openai(mcp_tool)
257+
assert "tools" in result
258+
assert len(result["tools"]) == 1
259+
# The chat client passes dict tools through unchanged, including unsupported types
260+
assert result["tools"][0]["type"] == "mcp"
261+
262+
263+
@pytest.mark.asyncio
264+
async def test_mcp_tool_dict_causes_api_rejection(openai_unit_test_env: dict[str, str]) -> None:
265+
"""Test that MCP tool dicts passed to the Chat Completions API cause a rejection.
266+
267+
The Chat Completions API only supports "type": "function" tools.
268+
When an MCP tool dict reaches the API, it returns a 400 error.
269+
This regression test for #4861 verifies the chat client does not
270+
silently drop or transform MCP dicts, so callers get a clear error
271+
rather than a silent no-op.
272+
"""
273+
client = OpenAIChatCompletionClient()
274+
messages = [Message(role="user", text="test message")]
275+
276+
mcp_tool = {
277+
"type": "mcp",
278+
"server_label": "Microsoft_Learn_MCP",
279+
"server_url": "https://learn.microsoft.com/api/mcp",
280+
}
281+
282+
mock_response = MagicMock()
283+
mock_error = BadRequestError(
284+
message="Invalid tool type: mcp",
285+
response=mock_response,
286+
body={"error": {"code": "invalid_request", "message": "Invalid tool type: mcp"}},
287+
)
288+
mock_error.code = "invalid_request"
289+
290+
with (
291+
patch.object(client.client.chat.completions, "create", side_effect=mock_error),
292+
pytest.raises(ChatClientException),
293+
):
294+
await client._inner_get_response(messages=messages, options={"tools": mcp_tool}) # type: ignore
295+
296+
240297
def test_prepare_tools_with_single_function_tool(
241298
openai_unit_test_env: dict[str, str],
242299
) -> None:

python/samples/05-end-to-end/hosted_agents/agent_with_hosted_mcp/main.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@
1111

1212

1313
def main():
14+
client = FoundryChatClient(credential=AzureCliCredential())
15+
1416
# Create MCP tool configuration as dict
15-
mcp_tool = {
16-
"type": "mcp",
17-
"server_label": "Microsoft_Learn_MCP",
18-
"server_url": "https://learn.microsoft.com/api/mcp",
19-
}
17+
mcp_tool = client.get_mcp_tool(
18+
name="Microsoft_Learn_MCP",
19+
url="https://learn.microsoft.com/api/mcp",
20+
)
21+
2022
# Create an Agent using the Azure OpenAI Chat Client with a MCP Tool that connects to Microsoft Learn MCP
2123
agent = Agent(
22-
client=FoundryChatClient(credential=AzureCliCredential()),
24+
client=client,
2325
name="DocsAgent",
2426
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
25-
tools=mcp_tool,
27+
tools=[mcp_tool],
2628
)
2729
# Run the agent as a hosted agent
2830
from_agent_framework(agent).run()
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
azure-ai-agentserver-agentframework==1.0.0b3
2-
agent-framework
1+
azure-ai-agentserver-agentframework==1.0.0b16
2+
agent-framework

0 commit comments

Comments
 (0)