@@ -24,20 +24,12 @@ async def sse_client(
2424 headers : dict [str , Any ] | None = None ,
2525 timeout : float = 5 ,
2626 sse_read_timeout : float = 60 * 5 ,
27- client : httpx .AsyncClient | None = None ,
2827):
2928 """
3029 Client transport for SSE.
3130
3231 `sse_read_timeout` determines how long (in seconds) the client will wait for a new
3332 event before disconnecting. All other HTTP operations are controlled by `timeout`.
34-
35- Args:
36- url: The URL to connect to
37- headers: Optional headers to send with the request
38- timeout: Connection timeout in seconds
39- sse_read_timeout: Read timeout in seconds
40- client: Optional httpx.AsyncClient instance to use for requests
4133 """
4234 read_stream : MemoryObjectReceiveStream [types .JSONRPCMessage | Exception ]
4335 read_stream_writer : MemoryObjectSendStream [types .JSONRPCMessage | Exception ]
@@ -51,13 +43,7 @@ async def sse_client(
5143 async with anyio .create_task_group () as tg :
5244 try :
5345 logger .info (f"Connecting to SSE endpoint: { remove_request_params (url )} " )
54- if client is None :
55- client = httpx .AsyncClient (headers = headers )
56- should_close_client = True
57- else :
58- should_close_client = False
59-
60- try :
46+ async with httpx .AsyncClient (headers = headers ) as client :
6147 async with aconnect_sse (
6248 client ,
6349 "GET" ,
@@ -151,9 +137,6 @@ async def post_writer(endpoint_url: str):
151137 yield read_stream , write_stream
152138 finally :
153139 tg .cancel_scope .cancel ()
154- finally :
155- if should_close_client :
156- await client .aclose ()
157140 finally :
158141 await read_stream_writer .aclose ()
159142 await write_stream .aclose ()
0 commit comments