5050if _QDRANT_URL is None and _QDRANT_HOST is None :
5151 _QDRANT_URL = ":memory:"
5252
53- # Instantiate global client
54- _client = AsyncQdrantClient (
55- location = _QDRANT_URL ,
56- api_key = _QDRANT_API_KEY ,
57- host = _QDRANT_HOST ,
58- port = _QDRANT_PORT ,
59- grpc_port = _QDRANT_GRPC_PORT ,
60- prefer_grpc = _QDRANT_PREFER_GRPC ,
61- https = _QDRANT_HTTPS ,
62- )
53+
54+ class PlexServer (FastMCP ):
55+ """FastMCP server with an attached Qdrant client."""
56+
57+ def __init__ (self ) -> None : # noqa: D401 - short description inherited
58+ super ().__init__ ()
59+ self .qdrant_client = AsyncQdrantClient (
60+ location = _QDRANT_URL ,
61+ api_key = _QDRANT_API_KEY ,
62+ host = _QDRANT_HOST ,
63+ port = _QDRANT_PORT ,
64+ grpc_port = _QDRANT_GRPC_PORT ,
65+ prefer_grpc = _QDRANT_PREFER_GRPC ,
66+ https = _QDRANT_HTTPS ,
67+ )
6368
6469_USE_RERANKER = os .getenv ("USE_RERANKER" , "1" ) == "1"
6570_reranker = None
6974 except Exception :
7075 _reranker = None
7176
72- server = FastMCP ()
77+ server = PlexServer ()
7378
7479
7580_CACHE_SIZE = 128
@@ -98,7 +103,9 @@ async def _find_records(identifier: str, limit: int = 5) -> list[models.Record]:
98103 # First, try direct ID lookup
99104 try :
100105 record_id : Any = int (identifier ) if identifier .isdigit () else identifier
101- recs = await _client .retrieve ("media-items" , ids = [record_id ], with_payload = True )
106+ recs = await server .qdrant_client .retrieve (
107+ "media-items" , ids = [record_id ], with_payload = True
108+ )
102109 if recs :
103110 return recs
104111 except Exception :
@@ -119,7 +126,7 @@ async def _find_records(identifier: str, limit: int = 5) -> list[models.Record]:
119126 models .FieldCondition (key = "title" , match = models .MatchText (text = identifier ))
120127 )
121128 flt = models .Filter (should = should )
122- points , _ = await _client .scroll (
129+ points , _ = await server . qdrant_client .scroll (
123130 collection_name = "media-items" ,
124131 limit = limit ,
125132 scroll_filter = flt ,
@@ -199,7 +206,7 @@ async def search_media(
199206 limit = candidate_limit ,
200207 ),
201208 ]
202- res = await _client .query_points (
209+ res = await server . qdrant_client .query_points (
203210 collection_name = "media-items" ,
204211 query = models .FusionQuery (fusion = models .Fusion .RRF ),
205212 prefetch = prefetch ,
@@ -274,7 +281,7 @@ async def recommend_media(
274281 record = records [0 ]
275282 if record is None :
276283 return []
277- recs = await _client .recommend (
284+ recs = await server . qdrant_client .recommend (
278285 collection_name = "media-items" ,
279286 positive = [record .id ],
280287 limit = limit ,
@@ -307,7 +314,7 @@ async def new_movies(
307314 )
308315 ]
309316 )
310- res = await _client .query_points (
317+ res = await server . qdrant_client .query_points (
311318 collection_name = "media-items" ,
312319 query = query ,
313320 query_filter = flt ,
@@ -340,7 +347,7 @@ async def new_shows(
340347 )
341348 ]
342349 )
343- res = await _client .query_points (
350+ res = await server . qdrant_client .query_points (
344351 collection_name = "media-items" ,
345352 query = query ,
346353 query_filter = flt ,
@@ -393,7 +400,7 @@ async def actor_movies(
393400 query = models .OrderByQuery (
394401 order_by = models .OrderBy (key = "year" , direction = models .Direction .DESC )
395402 )
396- res = await _client .query_points (
403+ res = await server . qdrant_client .query_points (
397404 collection_name = "media-items" ,
398405 query = query ,
399406 query_filter = flt ,
0 commit comments