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
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
`SPARSE_MODEL` environment variables or the corresponding CLI options.
- Hybrid search uses Qdrant's built-in `FusionQuery` with reciprocal rank fusion
to combine dense and sparse results before optional cross-encoder reranking.
- Qdrant client initialization moved into `PlexServer` to centralize state and
simplify testing.

## User Queries
The project should handle natural-language searches and recommendations such as:
Expand Down
43 changes: 25 additions & 18 deletions mcp_plex/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,21 @@
if _QDRANT_URL is None and _QDRANT_HOST is None:
_QDRANT_URL = ":memory:"

# Instantiate global client
_client = AsyncQdrantClient(
location=_QDRANT_URL,
api_key=_QDRANT_API_KEY,
host=_QDRANT_HOST,
port=_QDRANT_PORT,
grpc_port=_QDRANT_GRPC_PORT,
prefer_grpc=_QDRANT_PREFER_GRPC,
https=_QDRANT_HTTPS,
)

class PlexServer(FastMCP):
"""FastMCP server with an attached Qdrant client."""

def __init__(self) -> None: # noqa: D401 - short description inherited
super().__init__()
self.qdrant_client = AsyncQdrantClient(
location=_QDRANT_URL,
api_key=_QDRANT_API_KEY,
host=_QDRANT_HOST,
port=_QDRANT_PORT,
grpc_port=_QDRANT_GRPC_PORT,
prefer_grpc=_QDRANT_PREFER_GRPC,
https=_QDRANT_HTTPS,
)

_USE_RERANKER = os.getenv("USE_RERANKER", "1") == "1"
_reranker = None
Expand All @@ -69,7 +74,7 @@
except Exception:
_reranker = None

server = FastMCP()
server = PlexServer()


_CACHE_SIZE = 128
Expand Down Expand Up @@ -98,7 +103,9 @@ async def _find_records(identifier: str, limit: int = 5) -> list[models.Record]:
# First, try direct ID lookup
try:
record_id: Any = int(identifier) if identifier.isdigit() else identifier
recs = await _client.retrieve("media-items", ids=[record_id], with_payload=True)
recs = await server.qdrant_client.retrieve(
"media-items", ids=[record_id], with_payload=True
)
if recs:
return recs
except Exception:
Expand All @@ -119,7 +126,7 @@ async def _find_records(identifier: str, limit: int = 5) -> list[models.Record]:
models.FieldCondition(key="title", match=models.MatchText(text=identifier))
)
flt = models.Filter(should=should)
points, _ = await _client.scroll(
points, _ = await server.qdrant_client.scroll(
collection_name="media-items",
limit=limit,
scroll_filter=flt,
Expand Down Expand Up @@ -199,7 +206,7 @@ async def search_media(
limit=candidate_limit,
),
]
res = await _client.query_points(
res = await server.qdrant_client.query_points(
collection_name="media-items",
query=models.FusionQuery(fusion=models.Fusion.RRF),
prefetch=prefetch,
Expand Down Expand Up @@ -274,7 +281,7 @@ async def recommend_media(
record = records[0]
if record is None:
return []
recs = await _client.recommend(
recs = await server.qdrant_client.recommend(
collection_name="media-items",
positive=[record.id],
limit=limit,
Expand Down Expand Up @@ -307,7 +314,7 @@ async def new_movies(
)
]
)
res = await _client.query_points(
res = await server.qdrant_client.query_points(
collection_name="media-items",
query=query,
query_filter=flt,
Expand Down Expand Up @@ -340,7 +347,7 @@ async def new_shows(
)
]
)
res = await _client.query_points(
res = await server.qdrant_client.query_points(
collection_name="media-items",
query=query,
query_filter=flt,
Expand Down Expand Up @@ -393,7 +400,7 @@ async def actor_movies(
query = models.OrderByQuery(
order_by=models.OrderBy(key="year", direction=models.Direction.DESC)
)
res = await _client.query_points(
res = await server.qdrant_client.query_points(
collection_name="media-items",
query=query,
query_filter=flt,
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.9"
version = "0.26.10"

description = "Plex-Oriented Model Context Protocol Server"
requires-python = ">=3.11,<3.13"
Expand Down
3 changes: 2 additions & 1 deletion tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ def __init__(self, *args, **kwargs):
monkeypatch.setenv("QDRANT_GRPC_PORT", "5678")
monkeypatch.setenv("QDRANT_PREFER_GRPC", "1")
monkeypatch.setenv("QDRANT_HTTPS", "1")
importlib.reload(importlib.import_module("mcp_plex.server"))
module = importlib.reload(importlib.import_module("mcp_plex.server"))

assert captured["host"] == "example.com"
assert captured["port"] == 1234
assert captured["grpc_port"] == 5678
assert captured["prefer_grpc"] is True
assert captured["https"] is True
assert hasattr(module.server, "qdrant_client")


def test_server_tools(monkeypatch):
Expand Down
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.