-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Switch Lifespan to being a Server Lifespan #2013
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
strawgate
wants to merge
13
commits into
main
Choose a base branch
from
contrib-lifespan
base: main
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.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ba0f35b
Add contrib server_lifespan module
strawgate fc83ab6
no more contrib
strawgate 6a80098
updates
strawgate 773debc
Merge branch 'main' into contrib-lifespan
strawgate ac067ed
Remove extra test file
strawgate c33d601
adjust deprecation handling
strawgate d134310
clean-up
strawgate 863b4cf
move into dedicated context manager and clean-up PR
strawgate 1179bcd
Merge branch 'main' into contrib-lifespan
strawgate 08597be
PR clean-up
strawgate 4c02a8e
add support for starlette servers
strawgate 97620b1
Merge branch 'main' into contrib-lifespan
strawgate e4af557
Merge branch 'main' into contrib-lifespan
jlowin 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
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""Tests for server_lifespan and session_lifespan behavior.""" | ||
|
||
from collections.abc import AsyncIterator | ||
from contextlib import asynccontextmanager | ||
from typing import Any | ||
|
||
from fastmcp import Client, FastMCP | ||
from fastmcp.server.context import Context | ||
|
||
|
||
class TestServerLifespan: | ||
"""Test server_lifespan functionality.""" | ||
|
||
async def test_server_lifespan_basic(self): | ||
"""Test that server_lifespan is entered once and persists across sessions.""" | ||
lifespan_events: list[str] = [] | ||
|
||
@asynccontextmanager | ||
async def server_lifespan(mcp: FastMCP) -> AsyncIterator[dict[str, Any]]: | ||
_ = lifespan_events.append("enter") | ||
yield {"initialized": True} | ||
_ = lifespan_events.append("exit") | ||
strawgate marked this conversation as resolved.
Show resolved
Hide resolved
strawgate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
mcp = FastMCP("TestServer", lifespan=server_lifespan) | ||
|
||
@mcp.tool | ||
def get_value() -> str: | ||
return "test" | ||
|
||
# Server lifespan should be entered when run_async starts | ||
assert lifespan_events == [] | ||
|
||
# Connect first client session | ||
async with Client(mcp) as client1: | ||
result1 = await client1.call_tool("get_value", {}) | ||
assert result1.data == "test" | ||
# Server lifespan should have been entered once | ||
assert lifespan_events == ["enter"] | ||
|
||
# Connect second client session while first is still active | ||
async with Client(mcp) as client2: | ||
result2 = await client2.call_tool("get_value", {}) | ||
assert result2.data == "test" | ||
# Server lifespan should still only have been entered once | ||
assert lifespan_events == ["enter"] | ||
|
||
# Because we're using a fastmcptransport, the server lifespan should be exited | ||
# when the client session closes | ||
assert lifespan_events == ["enter", "exit"] | ||
|
||
async def test_server_lifespan_context_available(self): | ||
"""Test that server_lifespan context is available to tools.""" | ||
|
||
@asynccontextmanager | ||
async def server_lifespan(mcp: FastMCP) -> AsyncIterator[dict]: | ||
yield {"db_connection": "mock_db"} | ||
|
||
mcp = FastMCP("TestServer", lifespan=server_lifespan) | ||
|
||
@mcp.tool | ||
def get_db_info(ctx: Context) -> str: | ||
# Access the server lifespan context | ||
lifespan_context = ctx.request_context.lifespan_context | ||
return lifespan_context.get("db_connection", "no_db") | ||
|
||
async with Client(mcp) as client: | ||
result = await client.call_tool("get_db_info", {}) | ||
assert result.data == "mock_db" |
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.
does this equality work for context managers? does it need to be
is
?