-
Notifications
You must be signed in to change notification settings - Fork 580
test(mcp): Use AsyncClient for SSE
#5396
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
Open
alexander-alderman-webb
wants to merge
6
commits into
webb/test-fastmcp-streamable-http
Choose a base branch
from
webb/test-mcp-sse
base: webb/test-fastmcp-streamable-http
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+231
−38
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
960d76c
test(mcp): Use AsyncClient for SSE
alexander-alderman-webb c675fb9
merge and remove mocks :)
alexander-alderman-webb 8749ad3
merge
alexander-alderman-webb 7db7680
Merge branch 'webb/test-fastmcp-streamable-http' into webb/test-mcp-sse
alexander-alderman-webb d6c2fa5
simplify streaming transport
alexander-alderman-webb 0f47f06
remove unused import
alexander-alderman-webb 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import asyncio | ||
| from httpx import ASGITransport, Request, Response, AsyncByteStream | ||
| import anyio | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Callable, MutableMapping | ||
|
|
||
|
|
||
| class StreamingASGITransport(ASGITransport): | ||
| """ | ||
| Simple transport whose only purpose is to keep GET request alive in SSE connections, allowing | ||
| tests involving SSE interactions to run in-process. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| app: "Callable", | ||
| keep_sse_alive: "asyncio.Event", | ||
| ) -> None: | ||
| self.keep_sse_alive = keep_sse_alive | ||
| super().__init__(app) | ||
|
|
||
| async def handle_async_request(self, request: "Request") -> "Response": | ||
| scope = { | ||
| "type": "http", | ||
| "method": request.method, | ||
| "headers": [(k.lower(), v) for (k, v) in request.headers.raw], | ||
| "path": request.url.path, | ||
| "query_string": request.url.query, | ||
| } | ||
|
|
||
| is_streaming_sse = scope["method"] == "GET" and scope["path"] == "/sse" | ||
| if not is_streaming_sse: | ||
| return await super().handle_async_request(request) | ||
|
|
||
| request_body = b"" | ||
| if request.content: | ||
| request_body = await request.aread() | ||
|
|
||
| body_sender, body_receiver = anyio.create_memory_object_stream[bytes](0) | ||
|
|
||
| async def receive() -> "dict[str, Any]": | ||
| if self.keep_sse_alive.is_set(): | ||
| return {"type": "http.disconnect"} | ||
|
|
||
| await self.keep_sse_alive.wait() # Keep alive :) | ||
| return {"type": "http.request", "body": request_body, "more_body": False} | ||
|
|
||
| async def send(message: "MutableMapping[str, Any]") -> None: | ||
| if message["type"] == "http.response.body": | ||
| body = message.get("body", b"") | ||
| more_body = message.get("more_body", False) | ||
|
|
||
| if body == b"" and not more_body: | ||
| return | ||
|
|
||
| if body: | ||
| await body_sender.send(body) | ||
|
|
||
| if not more_body: | ||
| await body_sender.aclose() | ||
|
|
||
| async def run_app(): | ||
| await self.app(scope, receive, send) | ||
|
|
||
| class StreamingBodyStream(AsyncByteStream): | ||
| def __init__(self, receiver, task): | ||
| self.receiver = receiver | ||
| self.task = task | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| async def __aiter__(self): | ||
| try: | ||
| async for chunk in self.receiver: | ||
| yield chunk | ||
| except anyio.EndOfStream: | ||
| pass | ||
|
|
||
| stream = StreamingBodyStream(body_receiver, asyncio.create_task(run_app())) | ||
sentry[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| response = Response(status_code=200, headers=[], stream=stream) | ||
|
|
||
| return response | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.