Skip to content

Commit aab88b6

Browse files
authored
fix: repair server lifespan context manager (#62)
1 parent 9c3e0d1 commit aab88b6

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

mcp_plex/server.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import inspect
77
import json
88
import os
9-
from contextlib import asynccontextmanager
109
from typing import Annotated, Any, Callable
1110

1211
from fastapi import FastAPI
@@ -57,12 +56,18 @@ def __init__(
5756
https=self.settings.qdrant_https,
5857
)
5958

60-
@asynccontextmanager
61-
async def _lifespan(app: FastMCP): # noqa: ARG001
62-
try:
63-
yield
64-
finally:
65-
await self.close()
59+
class _ServerLifespan:
60+
def __init__(self, plex_server: "PlexServer") -> None:
61+
self._plex_server = plex_server
62+
63+
async def __aenter__(self) -> None: # noqa: D401 - matching protocol
64+
return None
65+
66+
async def __aexit__(self, exc_type, exc, tb) -> None: # noqa: ANN001
67+
await self._plex_server.close()
68+
69+
def _lifespan(app: FastMCP) -> _ServerLifespan: # noqa: ARG001
70+
return _ServerLifespan(self)
6671

6772
super().__init__(lifespan=_lifespan)
6873
self._reranker: CrossEncoder | None = None

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "mcp-plex"
7-
version = "0.26.29"
7+
version = "0.26.30"
88

99
description = "Plex-Oriented Model Context Protocol Server"
1010
requires-python = ">=3.11,<3.13"

tests/test_server.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,17 @@ def test_rest_endpoints(monkeypatch):
208208

209209
def test_server_lifespan_context(monkeypatch):
210210
with _load_server(monkeypatch) as module:
211+
closed = False
212+
213+
async def fake_close() -> None:
214+
nonlocal closed
215+
closed = True
216+
217+
monkeypatch.setattr(module.server, "close", fake_close)
218+
211219
async def _lifespan() -> None:
212220
async with module.server._mcp_server.lifespan(module.server):
213221
pass
214222

215223
asyncio.run(_lifespan())
224+
assert closed is True

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)