Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions mcp_plex/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import inspect
import json
import os
from contextlib import asynccontextmanager
from typing import Annotated, Any, Callable

from fastapi import FastAPI
Expand Down Expand Up @@ -57,12 +56,18 @@ def __init__(
https=self.settings.qdrant_https,
)

@asynccontextmanager
async def _lifespan(app: FastMCP): # noqa: ARG001
try:
yield
finally:
await self.close()
class _ServerLifespan:
def __init__(self, plex_server: "PlexServer") -> None:
self._plex_server = plex_server

async def __aenter__(self) -> None: # noqa: D401 - matching protocol
return None

async def __aexit__(self, exc_type, exc, tb) -> None: # noqa: ANN001
await self._plex_server.close()

def _lifespan(app: FastMCP) -> _ServerLifespan: # noqa: ARG001
return _ServerLifespan(self)

super().__init__(lifespan=_lifespan)
self._reranker: CrossEncoder | None = None
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "mcp-plex"
version = "0.26.29"
version = "0.26.30"

description = "Plex-Oriented Model Context Protocol Server"
requires-python = ">=3.11,<3.13"
Expand Down
9 changes: 9 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,17 @@ def test_rest_endpoints(monkeypatch):

def test_server_lifespan_context(monkeypatch):
with _load_server(monkeypatch) as module:
closed = False

async def fake_close() -> None:
nonlocal closed
closed = True

monkeypatch.setattr(module.server, "close", fake_close)

async def _lifespan() -> None:
async with module.server._mcp_server.lifespan(module.server):
pass

asyncio.run(_lifespan())
assert closed is True
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.