Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Resolve ASGIServer.http_client relative URLs against the server origin (#5341)

Asem ahmed7d89a65ba7de
diff --git a/fastmcp_slim/fastmcp/utilities/tests.py b/fastmcp_slim/fastmcp/utilities/tests.py
index aad3176e9..e787bc3c9 100644
--- a/fastmcp_slim/fastmcp/utilities/tests.py
+++ b/fastmcp_slim/fastmcp/utilities/tests.py
@@ -279,9 +279,9 @@ class ASGIServer:
) -> httpx2.AsyncClient:
"""An `httpx2.AsyncClient` bound to the in-process app, for raw HTTP assertions.
- Relative URLs resolve against the server's base URL, and absolute URLs on the
- same origin work too, so `client.get(f"{server.url}/health")` reads the same as
- it would against a real server.
+ Relative URLs resolve against the server's origin, so `client.get("/health")`
+ reaches a custom route just as it would against a real server, and absolute
+ URLs on the same origin, such as `server.url`, work too.
The signature matches `McpHttpClientFactory`, so this method can also be handed
to anything that takes an `httpx_client_factory`.
@@ -292,7 +292,7 @@ class ASGIServer:
cancel_on_close = self.transport_type != "sse"
return httpx2.AsyncClient(
transport=StreamingASGITransport(self.app, cancel_on_close=cancel_on_close),
- base_url=self.url,
+ base_url=httpx2.URL(self.url).copy_with(path="/"),
headers=headers,
timeout=timeout,
auth=auth,
diff --git a/tests/utilities/test_asgi_transport.py b/tests/utilities/test_asgi_transport.py
index 5a473b5c1..770c42584 100644
--- a/tests/utilities/test_asgi_transport.py
+++ b/tests/utilities/test_asgi_transport.py
@@ -230,6 +230,24 @@ class TestRunServerInMemory:
assert response.status_code in {400, 405}
+ @pytest.mark.parametrize("transport", ["http", "sse"])
+ async def test_http_client_resolves_relative_urls_against_origin(
+ self, transport: Literal["http", "sse"]
+ ):
+ server = build_server()
+
+ @server.custom_route("/health", methods=["GET"])
+ async def health(request: Request) -> Response:
+ return Response("OK")
+
+ async with asgi_server(server, transport=transport) as running_server:
+ async with running_server.http_client() as http:
+ response = await http.get("/health")
+
+ assert str(response.request.url) == "http://127.0.0.1/health"
+ assert response.status_code == 200
+ assert response.text == "OK"
+
async def test_auth_middleware_runs(self):
"""A migrated test must not pass by bypassing the real middleware stack."""
key_pair = RSAKeyPair.generate()