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
23 changes: 23 additions & 0 deletions src/mcp_server_qdrant/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ class EmbeddingProviderSettings(BaseSettings):
)


# The conditions that `make_filter` can turn into a Qdrant filter, for each field type.
SUPPORTED_CONDITIONS: dict[str, tuple[str, ...]] = {
"keyword": ("==", "!=", "any", "except"),
"integer": ("==", "!=", ">", ">=", "<", "<=", "any", "except"),
"float": (">", ">=", "<", "<="),
"boolean": ("==", "!="),
}


class FilterableField(BaseModel):
name: str = Field(description="The name of the field payload field to filter on")
description: str = Field(
Expand All @@ -70,6 +79,20 @@ class FilterableField(BaseModel):
description="Whether the field is required for the filter.",
)

@model_validator(mode="after")
def check_condition_supported(self) -> "FilterableField":
"""
Reject a condition the field type does not support when the settings
load, instead of exposing a filter that fails every time it is used.
"""
supported = SUPPORTED_CONDITIONS[self.field_type]
if self.condition is not None and self.condition not in supported:
raise ValueError(
f"Condition '{self.condition}' is not supported for {self.field_type} "
f"field '{self.name}'. Supported conditions: {', '.join(supported)}."
)
return self


class QdrantSettings(BaseSettings):
"""
Expand Down
65 changes: 65 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import itertools
import typing

import pytest
from pydantic import ValidationError

from mcp_server_qdrant.common.filters import make_filter
from mcp_server_qdrant.embeddings.types import EmbeddingProviderType
from mcp_server_qdrant.settings import (
DEFAULT_TOOL_FIND_DESCRIPTION,
DEFAULT_TOOL_STORE_DESCRIPTION,
SUPPORTED_CONDITIONS,
EmbeddingProviderSettings,
FilterableField,
QdrantSettings,
ToolSettings,
)
Expand Down Expand Up @@ -107,3 +114,61 @@ def test_all_custom_values(self, monkeypatch):
settings = ToolSettings()
assert settings.tool_store_description == "Custom store description"
assert settings.tool_find_description == "Custom find description"


FIELD_TYPES = typing.get_args(FilterableField.model_fields["field_type"].annotation)
CONDITIONS = typing.get_args(
typing.get_args(FilterableField.model_fields["condition"].annotation)[0]
)
ALL_PAIRS = list(itertools.product(FIELD_TYPES, CONDITIONS))
VALID_PAIRS = [(t, c) for t, c in ALL_PAIRS if c in SUPPORTED_CONDITIONS[t]]
INVALID_PAIRS = [(t, c) for t, c in ALL_PAIRS if c not in SUPPORTED_CONDITIONS[t]]
SAMPLE_VALUES = {"keyword": "a", "integer": 1, "float": 1.5, "boolean": True}


class TestFilterableField:
@pytest.mark.parametrize("field_type, condition", VALID_PAIRS)
def test_supported_condition_is_accepted(self, field_type, condition):
FilterableField(
name="f", description="d", field_type=field_type, condition=condition
)

@pytest.mark.parametrize("field_type, condition", INVALID_PAIRS)
def test_unsupported_condition_is_rejected(self, field_type, condition):
with pytest.raises(ValidationError, match="is not supported for"):
FilterableField(
name="f", description="d", field_type=field_type, condition=condition
)

@pytest.mark.parametrize("field_type", FIELD_TYPES)
def test_no_condition_is_accepted_for_every_type(self, field_type):
FilterableField(name="f", description="d", field_type=field_type)

@pytest.mark.parametrize("field_type, condition", ALL_PAIRS)
def test_table_matches_make_filter(self, field_type, condition):
"""SUPPORTED_CONDITIONS must list exactly what make_filter can build."""
# model_construct skips validation, so make_filter sees invalid pairs too.
field = FilterableField.model_construct(
name="f", description="d", field_type=field_type, condition=condition
)
value = SAMPLE_VALUES[field_type]
if condition in ("any", "except"):
value = [value]
if condition in SUPPORTED_CONDITIONS[field_type]:
make_filter({"f": field}, {"f": value})
else:
with pytest.raises(ValueError):
make_filter({"f": field}, {"f": value})

def test_invalid_condition_fails_when_settings_load(self, monkeypatch):
monkeypatch.setenv(
"FILTERABLE_FIELDS",
'[{"name": "lang", "description": "Language", '
'"field_type": "keyword", "condition": ">"}]',
)
with pytest.raises(ValidationError) as error:
QdrantSettings()
assert (
"Condition '>' is not supported for keyword field 'lang'. "
"Supported conditions: ==, !=, any, except." in str(error.value)
)
Loading