Fix base_url fallback when url is not set (#2776) (#2782)

Co-authored-by: Taisei Mima <bhbstar.me@gmail.com>
fix for httpx.URL("") being truthy but stringifying to empty string.
This commit is contained in:
Jeremiah Lowin 2025-12-30 17:51:57 -05:00 committed by GitHub
commit 1b637522d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 4 deletions

View file

@ -64,10 +64,8 @@ class OpenAPITool(Tool):
try:
# Get base URL from client
base_url = (
str(self._client.base_url)
if hasattr(self._client, "base_url") and self._client.base_url
else "http://localhost"
)
str(self._client.base_url) if hasattr(self._client, "base_url") else ""
) or "http://localhost"
# Get Headers from client
cli_headers = (

View file

@ -498,6 +498,38 @@ class TestOpenAPIComprehensive:
assert "123" in str(request.url)
assert "users/123" in str(request.url)
async def test_request_uses_localhost_fallback_when_no_base_url(
self, comprehensive_openapi_spec
):
"""Test that tool uses localhost fallback when client has no base_url."""
mock_client = Mock(spec=httpx.AsyncClient)
mock_client.base_url = httpx.URL("") # Empty URL, same as httpx default
mock_client.headers = None
mock_response = Mock(spec=Response)
mock_response.status_code = 200
mock_response.json.return_value = {
"id": 123,
"name": "Test User",
"email": "test@example.com",
}
mock_response.raise_for_status = Mock()
mock_client.send = AsyncMock(return_value=mock_response)
server = FastMCPOpenAPI(
openapi_spec=comprehensive_openapi_spec,
client=mock_client,
)
async with Client(server) as mcp_client:
await mcp_client.call_tool("get_user", {"id": 123})
# Verify request was made to localhost fallback
mock_client.send.assert_called_once()
request = mock_client.send.call_args[0][0]
assert str(request.url).startswith("http://localhost")
async def test_complex_request_with_body_and_parameters(
self, comprehensive_openapi_spec
):