-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add streamable_http_client
and deprecate old usage
#869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
""" | ||
|
||
import logging | ||
import warnings | ||
from collections.abc import AsyncGenerator, Awaitable, Callable | ||
from contextlib import asynccontextmanager | ||
from dataclasses import dataclass | ||
|
@@ -71,7 +72,7 @@ class RequestContext: | |
session_message: SessionMessage | ||
metadata: ClientMessageMetadata | None | ||
read_stream_writer: StreamWriter | ||
sse_read_timeout: timedelta | ||
sse_read_timeout: float | ||
|
||
|
||
class StreamableHTTPTransport: | ||
|
@@ -81,8 +82,8 @@ def __init__( | |
self, | ||
url: str, | ||
headers: dict[str, Any] | None = None, | ||
timeout: timedelta = timedelta(seconds=30), | ||
sse_read_timeout: timedelta = timedelta(seconds=60 * 5), | ||
timeout: float | timedelta = 30, | ||
sse_read_timeout: float | timedelta = 60 * 5, | ||
Comment on lines
+85
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is backwards compatible. |
||
auth: httpx.Auth | None = None, | ||
) -> None: | ||
"""Initialize the StreamableHTTP transport. | ||
|
@@ -96,8 +97,25 @@ def __init__( | |
""" | ||
self.url = url | ||
self.headers = headers or {} | ||
|
||
if isinstance(timeout, timedelta): | ||
warnings.warn( | ||
"`timeout` as `timedelta` is deprecated. Use `float` instead.", | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
timeout = timeout.total_seconds() | ||
self.timeout = timeout | ||
|
||
if isinstance(sse_read_timeout, timedelta): | ||
warnings.warn( | ||
"`sse_read_timeout` as `timedelta` is deprecated. Use `float` instead.", | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
sse_read_timeout = sse_read_timeout.total_seconds() | ||
self.sse_read_timeout = sse_read_timeout | ||
|
||
self.auth = auth | ||
self.session_id: str | None = None | ||
self.request_headers = { | ||
|
@@ -194,9 +212,7 @@ async def handle_get_stream( | |
"GET", | ||
self.url, | ||
headers=headers, | ||
timeout=httpx.Timeout( | ||
self.timeout.seconds, read=self.sse_read_timeout.seconds | ||
), | ||
timeout=httpx.Timeout(self.timeout, read=self.sse_read_timeout), | ||
) as event_source: | ||
event_source.response.raise_for_status() | ||
logger.debug("GET SSE connection established") | ||
|
@@ -225,9 +241,7 @@ async def _handle_resumption_request(self, ctx: RequestContext) -> None: | |
"GET", | ||
self.url, | ||
headers=headers, | ||
timeout=httpx.Timeout( | ||
self.timeout.seconds, read=ctx.sse_read_timeout.seconds | ||
), | ||
timeout=httpx.Timeout(self.timeout, read=ctx.sse_read_timeout), | ||
) as event_source: | ||
event_source.response.raise_for_status() | ||
logger.debug("Resumption GET SSE connection established") | ||
|
@@ -446,6 +460,52 @@ async def streamablehttp_client( | |
`sse_read_timeout` determines how long (in seconds) the client will wait for a new | ||
event before disconnecting. All other HTTP operations are controlled by `timeout`. | ||
|
||
Yields: | ||
Tuple containing: | ||
- read_stream: Stream for reading messages from the server | ||
- write_stream: Stream for sending messages to the server | ||
- get_session_id_callback: Function to retrieve the current session ID | ||
""" | ||
warnings.warn( | ||
"`streamablehttp_client` is deprecated. Use `streamable_http_client` instead.", | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
async with streamable_http_client( | ||
url, | ||
headers, | ||
timeout.total_seconds(), | ||
sse_read_timeout.total_seconds(), | ||
Comment on lines
+477
to
+478
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use |
||
terminate_on_close, | ||
httpx_client_factory, | ||
auth, | ||
) as (read_stream, write_stream, get_session_id): | ||
yield (read_stream, write_stream, get_session_id) | ||
|
||
|
||
@asynccontextmanager | ||
async def streamable_http_client( | ||
url: str, | ||
headers: dict[str, Any] | None = None, | ||
timeout: float = 30, | ||
sse_read_timeout: float = 60 * 5, | ||
terminate_on_close: bool = True, | ||
httpx_client_factory: McpHttpClientFactory = create_mcp_http_client, | ||
auth: httpx.Auth | None = None, | ||
) -> AsyncGenerator[ | ||
tuple[ | ||
MemoryObjectReceiveStream[SessionMessage | Exception], | ||
MemoryObjectSendStream[SessionMessage], | ||
GetSessionIdCallback, | ||
], | ||
None, | ||
]: | ||
""" | ||
Client transport for StreamableHTTP. | ||
|
||
`sse_read_timeout` determines how long (in seconds) the client will wait for a new | ||
event before disconnecting. All other HTTP operations are controlled by `timeout`. | ||
|
||
Yields: | ||
Tuple containing: | ||
- read_stream: Stream for reading messages from the server | ||
|
@@ -468,7 +528,7 @@ async def streamablehttp_client( | |
async with httpx_client_factory( | ||
headers=transport.request_headers, | ||
timeout=httpx.Timeout( | ||
transport.timeout.seconds, read=transport.sse_read_timeout.seconds | ||
transport.timeout, read=transport.sse_read_timeout | ||
), | ||
auth=transport.auth, | ||
) as client: | ||
|
@@ -489,11 +549,7 @@ def start_get_stream() -> None: | |
) | ||
|
||
try: | ||
yield ( | ||
read_stream, | ||
write_stream, | ||
transport.get_session_id, | ||
) | ||
yield (read_stream, write_stream, transport.get_session_id) | ||
finally: | ||
if transport.session_id and terminate_on_close: | ||
await transport.terminate_session(client) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My IDE added those... Which seems correct.