From 8f8c0fea7c207464dfef53bc32b60df0022ce02a Mon Sep 17 00:00:00 2001 From: Purv Kabaria Date: Thu, 12 Nov 2026 15:08:02 +0530 Subject: [PATCH 1/2] fix(mcp): allow locked exists on vector fields with index_missing A locked exists filter on a vector field was rejected at startup even when the field declares index_missing. Since #771, Redis accepts INDEXMISSING on vector fields and answers ismissing() on them, so vector fields now go through the same index_missing check as every other field type. Closes #788 --- redisvl/mcp/tools/profiles.py | 18 +++++--------- tests/unit/test_mcp/test_profiles_unit.py | 30 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/redisvl/mcp/tools/profiles.py b/redisvl/mcp/tools/profiles.py index 674ef770e..8542f41e8 100644 --- a/redisvl/mcp/tools/profiles.py +++ b/redisvl/mcp/tools/profiles.py @@ -136,7 +136,7 @@ def validate_profile_against_schema( f"vector field '{field_name}'" ) - _validate_locked_exists_fields(profile, schema, vector_field_names) + _validate_locked_exists_fields(profile, schema) # Raises RedisVLMCPError(INVALID_FILTER) naming the offending field. resolve_locked_filter(profile, schema) @@ -150,16 +150,15 @@ def validate_profile_against_schema( def _validate_locked_exists_fields( profile: MCPCustomToolConfig, schema: IndexSchema, - vector_field_names: set[str], ) -> None: """Reject a locked ``exists`` clause the index cannot actually answer. ``exists`` compiles to ``ismissing``, which Redis refuses unless the field was - declared with ``INDEXMISSING``. The DSL also lets ``exists`` through for vector - fields, which no other operator allows. Either way the profile parses fine and - then fails every single call as a *retryable* backend error, so a model retries - a permanently broken tool forever. Catching it here turns that into a startup - failure the operator sees once. + declared with ``INDEXMISSING``. That holds for vector fields too, so a vector + field passes only when it declares ``index_missing``. Otherwise the profile + parses fine and then fails every single call as a *retryable* backend error, so + a model retries a permanently broken tool forever. Catching it here turns that + into a startup failure the operator sees once. """ if profile.lock.filter is None: return @@ -172,11 +171,6 @@ def _validate_locked_exists_fields( if field is None: continue # resolve_locked_filter reports unknown fields. - if field_name in vector_field_names: - raise ValueError( - f"custom_tools '{profile.name}' lock.filter cannot use 'exists' on " - f"vector field '{field_name}'" - ) if not getattr(field.attrs, "index_missing", False): raise ValueError( f"custom_tools '{profile.name}' lock.filter uses 'exists' on field " diff --git a/tests/unit/test_mcp/test_profiles_unit.py b/tests/unit/test_mcp/test_profiles_unit.py index 00a798f2b..874d322e8 100644 --- a/tests/unit/test_mcp/test_profiles_unit.py +++ b/tests/unit/test_mcp/test_profiles_unit.py @@ -19,6 +19,7 @@ validate_profile_against_schema, ) from redisvl.mcp.tools.search import merge_locked_filter +from redisvl.schema import IndexSchema LOCKED_CATEGORY_FILTER = {"field": "category", "op": "eq", "value": "resolved"} FILTER_HINT = "Object filter fields: content(text), category(tag), rating(numeric)." @@ -618,6 +619,35 @@ def test_validate_profile_against_schema_rejects_locked_filter_on_unknown_field( assert exc_info.value.code == MCPErrorCode.INVALID_FILTER +def _schema_with_index_missing(*field_names: str) -> IndexSchema: + schema_dict = _schema().to_dict() + for field in schema_dict["fields"]: + if field["name"] in field_names: + field.setdefault("attrs", {})["index_missing"] = True + return IndexSchema.from_dict(schema_dict) + + +@pytest.mark.parametrize("field_name", ["category", "embedding"]) +def test_validate_profile_against_schema_rejects_locked_exists_without_index_missing( + field_name, +): + profile = _profile(lock={"filter": {"field": field_name, "op": "exists"}}) + + with pytest.raises(ValueError, match="not indexed with INDEXMISSING"): + validate_profile_against_schema(profile, _schema()) + + +@pytest.mark.parametrize("field_name", ["category", "embedding"]) +def test_validate_profile_against_schema_accepts_locked_exists_with_index_missing( + field_name, +): + # Redis answers ismissing() on vector fields declared with INDEXMISSING too, + # so a vector field is held to the same rule as every other field type. + profile = _profile(lock={"filter": {"field": field_name, "op": "exists"}}) + + validate_profile_against_schema(profile, _schema_with_index_missing(field_name)) + + def test_resolve_locked_filter_returns_none_when_no_filter_is_locked(): assert resolve_locked_filter(_profile(), _schema()) is None From 962055560352cf6771237f6466aa833a1ed9185b Mon Sep 17 00:00:00 2001 From: Purv Kabaria Date: Sat, 21 Nov 2026 11:46:38 +0530 Subject: [PATCH 2/2] test(mcp): exercise locked vector exists against Redis Signed-off-by: Purv Kabaria --- .../test_mcp/test_locked_exists_vector.py | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 tests/integration/test_mcp/test_locked_exists_vector.py diff --git a/tests/integration/test_mcp/test_locked_exists_vector.py b/tests/integration/test_mcp/test_locked_exists_vector.py new file mode 100644 index 000000000..de30926e4 --- /dev/null +++ b/tests/integration/test_mcp/test_locked_exists_vector.py @@ -0,0 +1,118 @@ +"""A locked ``exists`` filter on a vector field, checked against live Redis. + +``validate_profile_against_schema`` accepts ``exists`` on a vector field only +when the field declares ``index_missing``, on the grounds that Redis then answers +``ismissing()`` for it. These tests pin that assumption: if ``FT.CREATE`` ever +stopped carrying ``INDEXMISSING`` for a vector field, the validator would still +pass the profile and every call would fail as a retryable backend error. +""" + +import numpy as np +import pytest + +from redisvl.index import SearchIndex +from redisvl.mcp.filters import parse_filter +from redisvl.query import FilterQuery, VectorQuery +from redisvl.redis.connection import supports_svs +from redisvl.redis.utils import array_to_buffer +from redisvl.schema import IndexSchema + +DIMS = 4 +EMBEDDING = [0.1, 0.2, 0.3, 0.4] + + +@pytest.fixture( + params=[ + pytest.param({"algorithm": "flat"}, id="flat"), + pytest.param({"algorithm": "hnsw"}, id="hnsw"), + pytest.param({"algorithm": "svs-vamana"}, id="svs-vamana"), + ] +) +def vector_attrs(request, client): + if request.param["algorithm"] == "svs-vamana" and not supports_svs(client): + pytest.skip("SVS-VAMANA requires Redis 8.2+ with SVS support") + return request.param + + +@pytest.fixture(params=["hash", "json"]) +def storage_type(request): + return request.param + + +@pytest.fixture +def index_with_missing_embedding( + redis_url, redis_test_name, vector_attrs, storage_type +): + name = redis_test_name("locked_exists_vector") + schema = IndexSchema.from_dict( + { + "index": { + "name": name, + "prefix": name, + "storage_type": storage_type, + }, + "fields": [ + {"name": "doc_id", "type": "tag"}, + { + "name": "embedding", + "type": "vector", + "attrs": { + **vector_attrs, + "dims": DIMS, + "distance_metric": "cosine", + "datatype": "float32", + "index_missing": True, + }, + }, + ], + } + ) + index = SearchIndex(schema=schema, redis_url=redis_url) + index.create(overwrite=True, drop=True) + + embedding = ( + array_to_buffer(EMBEDDING, "float32") if storage_type == "hash" else EMBEDDING + ) + index.load( + [ + {"doc_id": "with-embedding", "embedding": embedding}, + {"doc_id": "without-embedding"}, + ], + id_field="doc_id", + ) + + yield index + + index.delete(drop=True) + + +def _locked_exists_filter(index): + # The same parser a profile's lock.filter goes through at startup. + return parse_filter({"field": "embedding", "op": "exists"}, index.schema) + + +def test_locked_exists_on_vector_field_as_filter(index_with_missing_embedding): + index = index_with_missing_embedding + locked = _locked_exists_filter(index) + assert str(locked) == "(-ismissing(@embedding))" + + results = index.query( + FilterQuery(filter_expression=locked, return_fields=["doc_id"]) + ) + + assert [doc["doc_id"] for doc in results] == ["with-embedding"] + + +def test_locked_exists_on_vector_field_as_knn_prefilter(index_with_missing_embedding): + index = index_with_missing_embedding + query = VectorQuery( + vector=np.array(EMBEDDING, dtype=np.float32).tolist(), + vector_field_name="embedding", + return_fields=["doc_id"], + filter_expression=_locked_exists_filter(index), + num_results=10, + ) + + results = index.query(query) + + assert [doc["doc_id"] for doc in results] == ["with-embedding"]