Add OAuth authentication client for HTTPX (#751)

Co-authored-by: Paul Carleton <paulc@anthropic.com>
This commit is contained in:
ihrpr 2025-05-19 20:38:04 +01:00 • committed by GitHub
commit e33cd41c7a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -83,6 +83,7 @@ class StreamableHTTPTransport:
headers: dict[str, Any] | None = None,
timeout: timedelta = timedelta(seconds=30),
sse_read_timeout: timedelta = timedelta(seconds=60 * 5),
auth: httpx.Auth | None = None,
) -> None:
"""Initialize the StreamableHTTP transport.
@ -91,11 +92,13 @@ class StreamableHTTPTransport:
headers: Optional headers to include in requests.
timeout: HTTP timeout for regular operations.
sse_read_timeout: Timeout for SSE read operations.
auth: Optional HTTPX authentication handler.
"""
self.url = url
self.headers = headers or {}
self.timeout = timeout
self.sse_read_timeout = sse_read_timeout
self.auth = auth
self.session_id: str | None = None
self.request_headers = {
ACCEPT: f"{JSON}, {SSE}",
@ -427,6 +430,7 @@ async def streamablehttp_client(
timeout: timedelta = timedelta(seconds=30),
sse_read_timeout: timedelta = timedelta(seconds=60 * 5),
terminate_on_close: bool = True,
auth: httpx.Auth | None = None,
) -> AsyncGenerator[
tuple[
MemoryObjectReceiveStream[SessionMessage | Exception],
@ -447,7 +451,7 @@ async def streamablehttp_client(
- write_stream: Stream for sending messages to the server
- get_session_id_callback: Function to retrieve the current session ID
"""
transport = StreamableHTTPTransport(url, headers, timeout, sse_read_timeout)
transport = StreamableHTTPTransport(url, headers, timeout, sse_read_timeout, auth)
read_stream_writer, read_stream = anyio.create_memory_object_stream[
SessionMessage | Exception
@ -465,6 +469,7 @@ async def streamablehttp_client(
timeout=httpx.Timeout(
transport.timeout.seconds, read=transport.sse_read_timeout.seconds
),
auth=transport.auth,
) as client:
# Define callbacks that need access to tg
def start_get_stream() -> None: