fix: add lifespan context manager to StreamableHTTP mounting examples #1669
+36
−3
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.
Summary
This PR fixes issue #1484 by adding the missing lifespan context manager to the StreamableHTTP mounting examples.
Problem
The current examples in
examples/snippets/servers/for mounting StreamableHTTP servers to existing ASGI applications are incomplete. When developers follow these examples, they encounter:This happens because the session manager is never properly initialized.
Solution
Added the proper lifespan context manager pattern to initialize the session manager before handling requests:
This pattern was already correctly implemented in
streamable_starlette_mount.pybut was missing from:streamable_http_basic_mounting.pystreamable_http_host_mounting.pystreamable_http_multiple_servers.pyFiles Changed
examples/snippets/servers/streamable_http_basic_mounting.py- Added lifespan for single serverexamples/snippets/servers/streamable_http_host_mounting.py- Added lifespan for host-based routingexamples/snippets/servers/streamable_http_multiple_servers.py- Added combined lifespan for multiple servers using AsyncExitStackTest Plan
ruff formatruff checkpyrighttype checkingCloses #1484