Skip to content

Commit 458950c

Browse files
committed
fix: properly close httpx.AsyncClient in tests
1 parent 605832e commit 458950c

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

tests/client/test_notification_response.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ async def message_handler( # pragma: no cover
8888
if isinstance(message, Exception):
8989
returned_exception = message
9090

91-
client = httpx.AsyncClient(transport=httpx.ASGITransport(app=_create_non_sdk_server_app()))
92-
async with streamable_http_client("http://localhost/mcp", http_client=client) as (read_stream, write_stream):
93-
async with ClientSession(read_stream, write_stream, message_handler=message_handler) as session:
94-
await session.initialize()
91+
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=_create_non_sdk_server_app())) as client:
92+
async with streamable_http_client("http://localhost/mcp", http_client=client) as (read_stream, write_stream):
93+
async with ClientSession(read_stream, write_stream, message_handler=message_handler) as session:
94+
await session.initialize()
9595

96-
# The test server returns a 204 instead of the expected 202
97-
await session.send_notification(RootsListChangedNotification(method="notifications/roots/list_changed"))
96+
# The test server returns a 204 instead of the expected 202
97+
await session.send_notification(RootsListChangedNotification(method="notifications/roots/list_changed"))
9898

9999
if returned_exception: # pragma: no cover
100100
pytest.fail(f"Server encountered an exception: {returned_exception}")
@@ -107,13 +107,13 @@ async def test_unexpected_content_type_sends_jsonrpc_error() -> None:
107107
the client should send a JSONRPCError so the pending request resolves immediately
108108
instead of hanging until timeout.
109109
"""
110-
client = httpx.AsyncClient(transport=httpx.ASGITransport(app=_create_unexpected_content_type_app()))
111-
async with streamable_http_client("http://localhost/mcp", http_client=client) as (read_stream, write_stream):
112-
async with ClientSession(read_stream, write_stream) as session:
113-
await session.initialize()
110+
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=_create_unexpected_content_type_app())) as client:
111+
async with streamable_http_client("http://localhost/mcp", http_client=client) as (read_stream, write_stream):
112+
async with ClientSession(read_stream, write_stream) as session:
113+
await session.initialize()
114114

115-
with pytest.raises(MCPError, match="Unexpected content type: text/plain"):
116-
await session.list_tools()
115+
with pytest.raises(MCPError, match="Unexpected content type: text/plain"):
116+
await session.list_tools()
117117

118118

119119
def _create_invalid_json_response_app() -> Starlette:
@@ -142,10 +142,10 @@ async def test_invalid_json_response_sends_jsonrpc_error() -> None:
142142
should send a JSONRPCError so the pending request resolves immediately
143143
instead of hanging until timeout.
144144
"""
145-
client = httpx.AsyncClient(transport=httpx.ASGITransport(app=_create_invalid_json_response_app()))
146-
async with streamable_http_client("http://localhost/mcp", http_client=client) as (read_stream, write_stream):
147-
async with ClientSession(read_stream, write_stream) as session:
148-
await session.initialize()
145+
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=_create_invalid_json_response_app())) as client:
146+
async with streamable_http_client("http://localhost/mcp", http_client=client) as (read_stream, write_stream):
147+
async with ClientSession(read_stream, write_stream) as session:
148+
await session.initialize()
149149

150-
with pytest.raises(MCPError, match="Failed to parse JSON response"):
151-
await session.list_tools()
150+
with pytest.raises(MCPError, match="Failed to parse JSON response"):
151+
await session.list_tools()

0 commit comments

Comments
 (0)