From 611230cc8ebde031d6c15dbc15d7053ddf56b40c Mon Sep 17 00:00:00 2001 From: Evan Mattson <35585003+moonbox3@users.noreply.github.com> Date: Thu, 16 Apr 2026 11:34:28 +0900 Subject: [PATCH] Python: improve misc-integration test robustness (#5295) * Python: use local MCP server for hosted tools test and broaden image assertion The hosted tools integration test was hitting rate limits on the external learn.microsoft.com MCP server, causing persistent failures that retries couldn't recover from. Switch to the local MCP server already spun up in CI via LOCAL_MCP_URL, skipping when the env var isn't set. Also broaden the image description assertion to accept common synonyms (cottage, mansion, villa, etc.) instead of just "house", since the model legitimately uses varied vocabulary for the same image. * Address review feedback: validate LOCAL_MCP_URL scheme and use word boundaries - Skip hosted tools test when LOCAL_MCP_URL lacks http/https scheme, matching the pattern used in test_mcp.py. - Use regex word boundaries for image assertion to avoid false matches like "villain" matching "villa". --- .../anthropic/tests/test_anthropic_client.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/python/packages/anthropic/tests/test_anthropic_client.py b/python/packages/anthropic/tests/test_anthropic_client.py index 52bb4c3a49..a10a8830b4 100644 --- a/python/packages/anthropic/tests/test_anthropic_client.py +++ b/python/packages/anthropic/tests/test_anthropic_client.py @@ -1,5 +1,6 @@ # Copyright (c) Microsoft. All rights reserved. import os +import re from pathlib import Path from typing import Annotated, Any from unittest.mock import MagicMock, patch @@ -1503,6 +1504,10 @@ async def test_anthropic_client_integration_function_calling() -> None: @skip_if_anthropic_integration_tests_disabled async def test_anthropic_client_integration_hosted_tools() -> None: """Integration test for hosted tools.""" + local_mcp_url = os.environ.get("LOCAL_MCP_URL", "") + if not local_mcp_url or not local_mcp_url.startswith(("http://", "https://")): + pytest.skip("LOCAL_MCP_URL not set or not an HTTP URL; skipping hosted tools test") + client = AnthropicClient() messages = [Message(role="user", contents=["What tools do you have available?"])] @@ -1510,8 +1515,8 @@ async def test_anthropic_client_integration_hosted_tools() -> None: AnthropicClient.get_web_search_tool(), AnthropicClient.get_code_interpreter_tool(), AnthropicClient.get_mcp_tool( - name="example-mcp", - url="https://learn.microsoft.com/api/mcp", + name="local-mcp", + url=local_mcp_url, ), ] @@ -1607,7 +1612,8 @@ async def test_anthropic_client_integration_images() -> None: assert response is not None assert response.messages[0].text is not None - assert "house" in response.messages[0].text.lower() + text = response.messages[0].text.lower() + assert re.search(r"\b(house|home|building|cottage|mansion|villa)\b", text) # Response Format Tests