Skip to content

test(mcp): cover filter and request validation rejection branches - #898

Draft
vishal-bala wants to merge 2 commits into
mainfrom
test/mcp-filter-boundaries
Draft

vishal-bala wants to merge 2 commits into
mainfrom
test/mcp-filter-boundaries

Conversation

@vishal-bala

Copy link
Copy Markdown
Collaborator

Motivation

The MCP server's input-validation layer takes untrusted input from agents, and several of its rejection branches had no test. Among them were three refusals in the backstop that stops a caller's filter escaping a locked tenant filter. If any of those guards were deleted, the unit suite would still pass. This PR covers them, along with the matching branches in the structured-filter parser, search and upsert request validation, and check_operator_misuse.

Changes

One table of invalid inputs per module

  • tests/unit/test_mcp/test_filters.py: test_parse_filter_rejects_malformed_structured_filters is one table of malformed filters. It covers a non-object filter, two logical keys, not wrapping an array, a non-object logical child, an empty field, a missing op or value, a vector field, unknown operators per field type, empty, non-string, non-numeric and bool values, and a scalar where in needs a list. Each row asserts its own error message. The five older single-case rejection tests are folded in as rows.
  • The same file gets four new rows in the backstop refusal table: a closer with no opener, a stray ], an unclosed range and an escaped ] inside a range. None of them has a top-level |, so only the depth, bracket and range-span checks can refuse them. An older row whose label claimed a stray closer is relabelled by the check that actually refuses it, and a row that duplicated its branch is dropped.
  • test_parse_filter_renders_each_operator pins the rendered string for tag ne and like, text ne and like, and numeric eq, ne and lte. The two like rows check that query syntax is escaped and the * wildcard stays live.
  • tests/unit/test_mcp/test_search_tool_unit.py: test_search_records_rejects_invalid_request_arguments covers limit above max_limit, a non-list return_fields, non-string and empty entries, and the existing limit, offset, unknown-field, vector-field and empty-list cases.
  • tests/unit/test_mcp/test_upsert_tool_unit.py: the request-shape table gains an invalid id_field and a non-bool skip_embedding_if_present. A new two-row test covers both errors raised when the server has no embedding support. Its match= strings tell the two errors apart, which the older match="embedding" did not.
  • tests/unit/test_filter.py: test_comparing_a_field_to_a_field_raises checks that == and != between two Tag, Text or Geo fields raise.

AI Assistance

  • No AI assistance was used for this PR.
  • This PR is AI-assisted. I have reviewed and understood everything in it, and I take responsibility for it.

Claude Code (claude-opus-5-5): tests, pre-PR review and description.

Reviewer Brief

Start here: tests/unit/test_mcp/test_filters.py, test_merge_locked_filter_refuses_a_caller_rendering_that_could_escape. Its four new rows are the only tests of the backstop's depth, bracket and range-span refusals; every other row there is refused by the top-level | check.

Where it could fail

  • tests/unit/test_mcp/test_filters.py (renders_each_operator): the tag like row pins @category:{sci\-fi*}. Tag like still leaves | live, which is the known, deferred tag-widening issue. No row here has a |, so none certifies that behaviour.
  • redisvl/mcp/tools/search.py _reject_escapable_filter (pre-existing, not changed here): a raw caller rendering that ends in one unpaired \ is accepted. That backslash then escapes the merge's closing paren. No DSL operator renders that shape today. It is reported separately and deliberately not pinned in this PR.

Not verified: behaviour against a live Redis; nothing here builds a query that reaches Redis. Cluster and API-key paths are not touched.

Beyond CI: source is unchanged, so every new test passes on the base by construction. Instead, 17 mutations were each run against the new tests, and every one fails at least one row. Each mutation either replaces a guard's condition with False (depth < 0, the stray-] branch, limit > max_limit, return_fields list and entry checks, id_field, skip_embedding_if_present type and no-embedding checks, check_operator_misuse, five parser guards) or removes something: the backslash skip in _find_unescaped, the unclosed-range refusal, or the like escaper. With coverage, redisvl/mcp/filters.py goes from 82% to 99% under the unit suite. The one missed line, 224, is unreachable because both list helpers reject an empty list first.

Reviewed 830e89c2 · Claude Code (claude-opus-5-5) · lenses: correctness, security, design, tests, docs (Redis behaviour not run: no query construction changes)

vishal-bala and others added 2 commits December 4, 2026 12:01
Parametrised tables for the MCP structured-filter parser, the locked-filter
escape backstop, search and upsert request validation, and
check_operator_misuse. Folds the existing single-case rejection tests into
those tables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Relabels a backstop row by the check that refuses it, drops a duplicate
row, folds the empty return_fields and no-server-side-embedding tests into
tables, and tightens the latter's match so the two embedding errors are
told apart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.55%. Comparing base (0219a83) to head (830e89c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #898      +/-   ##
==========================================
+ Coverage   81.24%   81.55%   +0.31%     
==========================================
  Files         121      121              
  Lines       15145    15145              
  Branches     2661     2661              
==========================================
+ Hits        12304    12351      +47     
+ Misses       2201     2166      -35     
+ Partials      640      628      -12     
Flag Coverage Δ
core 80.40% <ø> (+0.42%) ⬆️
service 80.84% <ø> (+0.42%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vishal-bala vishal-bala added the auto:tests Add or improve existing tests label Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto:tests Add or improve existing tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants