Skip to content

Commit b95feaa

Browse files
committed
adjust deprecation handling
1 parent ac067ed commit b95feaa

File tree

3 files changed

+18
-36
lines changed

3 files changed

+18
-36
lines changed

src/fastmcp/server/server.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -287,28 +287,22 @@ def _handle_lifespan_settings(
287287
session_lifespan: LifespanCallable | None = None,
288288
server_lifespan: LifespanCallable | None = None,
289289
) -> tuple[LifespanCallable | None, LifespanCallable | None]:
290-
if session_lifespan is not None:
291-
if server_lifespan is not None:
292-
raise ValueError(
293-
"Cannot specify both session_lifespan and server_lifespan."
294-
)
295-
290+
if lifespan is not None:
296291
if fastmcp.settings.deprecation_warnings:
297292
import warnings
298293

299294
warnings.warn(
300-
"The session_lifespan parameter is deprecated (as of 2.13.0). For the same behavior, "
301-
+ "use the lifespan parameter instead.",
295+
"The lifespan parameter is deprecated (as of 2.13.0). For the same behavior, "
296+
+ "use the session_lifespan parameter instead.",
302297
DeprecationWarning,
303298
stacklevel=2,
304299
)
305-
# Keep the provided session_lifespan. If it was not provided, fall back to lifespan below.
306-
else:
307-
session_lifespan = lifespan
308300

309-
if all([session_lifespan, server_lifespan]):
301+
session_lifespan = lifespan or session_lifespan
302+
303+
if session_lifespan and server_lifespan:
310304
raise ValueError(
311-
"Cannot specify both session_lifespan and server_lifespan."
305+
"Cannot specify both session_lifespan (or lifespan) and server_lifespan."
312306
)
313307

314308
return session_lifespan, server_lifespan

tests/server/test_mount.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -890,19 +890,6 @@ async def test_as_proxy_true(self):
890890
assert mcp._tool_manager._mounted_servers[0].server is not sub
891891
assert isinstance(mcp._tool_manager._mounted_servers[0].server, FastMCPProxy)
892892

893-
async def test_as_proxy_defaults_true_if_lifespan(self):
894-
@asynccontextmanager
895-
async def lifespan(mcp: FastMCP):
896-
yield
897-
898-
mcp = FastMCP("Main")
899-
sub = FastMCP("Sub", lifespan=lifespan)
900-
901-
mcp.mount(sub, "sub")
902-
903-
assert mcp._tool_manager._mounted_servers[0].server is not sub
904-
assert isinstance(mcp._tool_manager._mounted_servers[0].server, FastMCPProxy)
905-
906893
async def test_as_proxy_defaults_true_if_server_lifespan(self):
907894
"""Test that as_proxy defaults to True when server_lifespan is provided."""
908895

@@ -969,7 +956,7 @@ async def lifespan(mcp: FastMCP):
969956
yield
970957

971958
mcp = FastMCP("Main")
972-
sub = FastMCP("Sub", lifespan=lifespan)
959+
sub = FastMCP("Sub", server_lifespan=lifespan)
973960

974961
@sub.tool
975962
def hello():

tests/server/test_server_lifespan.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,17 @@ async def session_lifespan(mcp: FastMCP) -> AsyncIterator[None]:
9898

9999
with warnings.catch_warnings(record=True) as w:
100100
warnings.simplefilter("always")
101-
_ = FastMCP(name="TestServer", session_lifespan=session_lifespan)
101+
_ = FastMCP(name="TestServer", lifespan=session_lifespan)
102102

103103
# Should have emitted a deprecation warning
104104
assert len(w) == 1
105105
assert issubclass(w[0].category, DeprecationWarning)
106-
assert "session_lifespan parameter is deprecated" in str(w[0].message)
107-
assert "use the lifespan parameter instead" in str(w[0].message)
106+
assert "lifespan parameter is deprecated" in str(w[0].message)
107+
assert "use the session_lifespan parameter instead" in str(w[0].message)
108108

109-
async def test_session_lifespan_still_works(self):
109+
async def test_lifespan_still_works(self):
110110
"""Test that session_lifespan still functions despite deprecation."""
111-
session_events = []
111+
session_events: list[str] = []
112112

113113
@asynccontextmanager
114114
async def session_lifespan(mcp: FastMCP) -> AsyncIterator[None]:
@@ -118,7 +118,7 @@ async def session_lifespan(mcp: FastMCP) -> AsyncIterator[None]:
118118

119119
with warnings.catch_warnings():
120120
warnings.simplefilter("ignore")
121-
mcp = FastMCP("TestServer", session_lifespan=session_lifespan)
121+
mcp = FastMCP("TestServer", lifespan=session_lifespan)
122122

123123
@mcp.tool
124124
def test_tool() -> str:
@@ -183,7 +183,8 @@ async def server_lifespan(mcp: FastMCP) -> AsyncIterator[None]:
183183
yield
184184

185185
with pytest.raises(
186-
ValueError, match="Cannot specify both session_lifespan and server_lifespan"
186+
expected_exception=ValueError,
187+
match="Cannot specify both",
187188
):
188189
_ = FastMCP(
189190
name="TestServer",
@@ -200,15 +201,15 @@ async def my_lifespan(mcp: FastMCP) -> AsyncIterator[None]:
200201

201202
with pytest.raises(
202203
expected_exception=ValueError,
203-
match="Cannot specify both session_lifespan and server_lifespan",
204+
match="Cannot specify both",
204205
):
205206
_ = FastMCP(
206207
name="TestServer", lifespan=my_lifespan, server_lifespan=my_lifespan
207208
)
208209

209210
with pytest.raises(
210211
expected_exception=ValueError,
211-
match="Cannot specify both session_lifespan and server_lifespan",
212+
match="Cannot specify both",
212213
):
213214
_ = FastMCP(
214215
name="TestServer",

0 commit comments

Comments
 (0)