Skip to content

Commit 261acbb

Browse files
Fix test failures from httpx_client API changes
This commit fixes all test failures introduced by the API change from httpx_client_factory to direct httpx_client parameter: 1. Updated deprecated imports: Changed streamablehttp_client to streamable_http_client in test files 2. Fixed 307 redirect errors: Replaced httpx.AsyncClient with create_mcp_http_client which includes follow_redirects=True by default 3. Fixed test assertion: Updated test_session_group.py to mock create_mcp_http_client and verify the new API signature where streamable_http_client receives httpx_client parameter instead of individual headers/timeout parameters 4. Removed unused httpx import from main.py after inlining client creation All tests now pass with the new API.
1 parent 74fa629 commit 261acbb

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

examples/clients/simple-auth-client/mcp_simple_auth_client/main.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
from typing import Any
1616
from urllib.parse import parse_qs, urlparse
1717

18-
import httpx
19-
2018
from mcp.client.auth import OAuthClientProvider, TokenStorage
2119
from mcp.client.session import ClientSession
2220
from mcp.client.sse import sse_client

tests/client/test_http_unicode.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import pytest
1414

1515
from mcp.client.session import ClientSession
16-
from mcp.client.streamable_http import streamablehttp_client
16+
from mcp.client.streamable_http import streamable_http_client
1717

1818
# Test constants with various Unicode characters
1919
UNICODE_TEST_STRINGS = {
@@ -189,7 +189,7 @@ async def test_streamable_http_client_unicode_tool_call(running_unicode_server:
189189
base_url = running_unicode_server
190190
endpoint_url = f"{base_url}/mcp"
191191

192-
async with streamablehttp_client(endpoint_url) as (read_stream, write_stream, _get_session_id):
192+
async with streamable_http_client(endpoint_url) as (read_stream, write_stream, _get_session_id):
193193
async with ClientSession(read_stream, write_stream) as session:
194194
await session.initialize()
195195

@@ -221,7 +221,7 @@ async def test_streamable_http_client_unicode_prompts(running_unicode_server: st
221221
base_url = running_unicode_server
222222
endpoint_url = f"{base_url}/mcp"
223223

224-
async with streamablehttp_client(endpoint_url) as (read_stream, write_stream, _get_session_id):
224+
async with streamable_http_client(endpoint_url) as (read_stream, write_stream, _get_session_id):
225225
async with ClientSession(read_stream, write_stream) as session:
226226
await session.initialize()
227227

tests/client/test_notification_response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from starlette.routing import Route
2020

2121
from mcp import ClientSession, types
22-
from mcp.client.streamable_http import streamablehttp_client
22+
from mcp.client.streamable_http import streamable_http_client
2323
from mcp.shared.session import RequestResponder
2424
from mcp.types import ClientNotification, RootsListChangedNotification
2525

@@ -132,7 +132,7 @@ async def message_handler(
132132
if isinstance(message, Exception):
133133
returned_exception = message
134134

135-
async with streamablehttp_client(server_url) as (read_stream, write_stream, _):
135+
async with streamable_http_client(server_url) as (read_stream, write_stream, _):
136136
async with ClientSession(
137137
read_stream,
138138
write_stream,

tests/client/test_session_group.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ async def test_establish_session_parameterized(
284284
):
285285
with mock.patch("mcp.client.session_group.mcp.ClientSession") as mock_ClientSession_class:
286286
with mock.patch(patch_target_for_client_func) as mock_specific_client_func:
287+
# For streamablehttp, also need to mock create_mcp_http_client
288+
if client_type_name == "streamablehttp":
289+
mock_create_http_client = mock.patch("mcp.client.session_group.create_mcp_http_client")
290+
mock_create_http_client_func = mock_create_http_client.start()
291+
# Mock httpx_client returned by create_mcp_http_client
292+
mock_httpx_client = mock.AsyncMock(name="MockHttpxClient")
293+
mock_httpx_client.__aenter__.return_value = mock_httpx_client
294+
mock_httpx_client.__aexit__ = mock.AsyncMock(return_value=None)
295+
mock_create_http_client_func.return_value = mock_httpx_client
296+
else:
297+
mock_create_http_client = None
298+
mock_create_http_client_func = None
299+
mock_httpx_client = None
300+
287301
mock_client_cm_instance = mock.AsyncMock(name=f"{client_type_name}ClientCM")
288302
mock_read_stream = mock.AsyncMock(name=f"{client_type_name}Read")
289303
mock_write_stream = mock.AsyncMock(name=f"{client_type_name}Write")
@@ -346,11 +360,22 @@ async def test_establish_session_parameterized(
346360
)
347361
elif client_type_name == "streamablehttp":
348362
assert isinstance(server_params_instance, StreamableHttpParameters)
363+
# Verify create_mcp_http_client was called with headers and timeout
364+
import httpx
365+
366+
assert mock_create_http_client_func is not None
367+
expected_timeout = httpx.Timeout(
368+
server_params_instance.timeout.total_seconds(),
369+
read=server_params_instance.sse_read_timeout.total_seconds(),
370+
)
371+
mock_create_http_client_func.assert_called_once_with(
372+
headers=server_params_instance.headers,
373+
timeout=expected_timeout,
374+
)
375+
# Verify streamable_http_client was called with url, httpx_client, and terminate_on_close
349376
mock_specific_client_func.assert_called_once_with(
350377
url=server_params_instance.url,
351-
headers=server_params_instance.headers,
352-
timeout=server_params_instance.timeout,
353-
sse_read_timeout=server_params_instance.sse_read_timeout,
378+
httpx_client=mock_httpx_client,
354379
terminate_on_close=server_params_instance.terminate_on_close,
355380
)
356381

@@ -364,3 +389,7 @@ async def test_establish_session_parameterized(
364389
# 3. Assert returned values
365390
assert returned_server_info is mock_initialize_result.serverInfo
366391
assert returned_session is mock_entered_session
392+
393+
# Clean up streamablehttp-specific mock
394+
if mock_create_http_client:
395+
mock_create_http_client.stop()

tests/shared/test_streamable_http.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from mcp.client.session import ClientSession
2626
from mcp.client.streamable_http import streamable_http_client
2727
from mcp.server import Server
28-
from mcp.shared._httpx_utils import create_mcp_http_client
2928
from mcp.server.streamable_http import (
3029
MCP_PROTOCOL_VERSION_HEADER,
3130
MCP_SESSION_ID_HEADER,
@@ -39,6 +38,7 @@
3938
)
4039
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
4140
from mcp.server.transport_security import TransportSecuritySettings
41+
from mcp.shared._httpx_utils import create_mcp_http_client
4242
from mcp.shared.context import RequestContext
4343
from mcp.shared.exceptions import McpError
4444
from mcp.shared.message import ClientMessageMetadata
@@ -1395,7 +1395,7 @@ async def test_streamablehttp_request_context_propagation(context_aware_server:
13951395
"X-Trace-Id": "trace-123",
13961396
}
13971397

1398-
async with httpx.AsyncClient(headers=custom_headers) as httpx_client:
1398+
async with create_mcp_http_client(headers=custom_headers) as httpx_client:
13991399
async with streamable_http_client(f"{basic_server_url}/mcp", httpx_client=httpx_client) as (
14001400
read_stream,
14011401
write_stream,
@@ -1433,8 +1433,12 @@ async def test_streamablehttp_request_context_isolation(context_aware_server: No
14331433
"Authorization": f"Bearer token-{i}",
14341434
}
14351435

1436-
async with httpx.AsyncClient(headers=headers) as httpx_client:
1437-
async with streamable_http_client(f"{basic_server_url}/mcp", httpx_client=httpx_client) as (read_stream, write_stream, _):
1436+
async with create_mcp_http_client(headers=headers) as httpx_client:
1437+
async with streamable_http_client(f"{basic_server_url}/mcp", httpx_client=httpx_client) as (
1438+
read_stream,
1439+
write_stream,
1440+
_,
1441+
):
14381442
async with ClientSession(read_stream, write_stream) as session:
14391443
await session.initialize()
14401444

0 commit comments

Comments
 (0)