Skip to content

feat: expose the whole catalog filter surface over MCP - #1429

Merged
neuromechanist merged 3 commits into
devfrom
feature/mcp-full-search-surface
Sep 17, 2026
Merged

neuromechanist merged 3 commits into
devfrom
feature/mcp-full-search-surface

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

search_datasets advertised six parameters. The catalog can filter on far more, and the gap is what let the OSA assistant's prompt drift onto a parameter name that never existed.

Why

The MCP tool already calls buildDatasetFilterClauses, which understands eleven bespoke filters plus the twenty declared facets. It was passing four of them: modality, task, has_hed, has_zarr. Everything else the catalog can do was unreachable from an agent: author, license tier, DOI presence, fidelity-verified Zarr, data completeness, recency, and every facet in shared/facets.ts (subject and channel counts, sampling rate, duration, electrode system, BIDS and HED version, and the rest).

The tool's input schema was hand-written, which is the real defect. ADR 0032 makes shared/facets.ts the single source of truth for which columns are filterable and has a test asserting both directions between it and dataset-facets.ts. The MCP schema was the one consumer that opted out, and it went stale: OSA's system prompt told the model to call nemar_search_datasets(modality_filter="EEG"). Because the schema is .passthrough(), the server accepted the undeclared argument and dropped it. Measured against production before this change:

search_datasets(query="attention", modality_filter="MEG", limit=3)  ->  count: 92, isError unset
search_datasets(query="attention", modality="MEG",        limit=3)  ->  count: 3

A researcher asking for MEG datasets got 92 mostly-EEG results presented as filtered, with nothing marking it. That prompt is fixed separately in OpenScience-Collective/osa#367; this is the half that stops the class of bug rather than the instance.

What

  • The facet parameters in BOTH schemas are now generated from FACETS, so a newly declared facet reaches MCP callers the day it lands and cannot be forgotten.
  • The six missing bespoke filters are wired through, plus include_unknown so a caller can see what a partly-populated facet is excluding (ADR 0032's stated reason for that flag).
  • Facet values are parsed by the existing parseFacetFilters, so range and enum syntax is validated once, in the same place as the HTTP route. A rejected value returns a tool error naming what it received and the accepted forms, rather than a bare validation failure.
  • The tool description now states that an undeclared parameter name is accepted and silently ignored. That is the property that made the original drift invisible, and an agent reading the description should know it.

Wire surface goes from 6 parameters to 33, each carrying a description built from the facet's own label, unit and enumValues.

The trap this hit, worth knowing

searchDatasetsInputSchema4 in backend/src/mcp/schemas.ts is a hand-written zod 4 mirror, and it is what registerTool advertises, so it is what the model actually reads. Widening only the zod 3 contract would have changed nothing on the wire.

The existing parity tests could not catch that: they feed sample objects to both schemas, and both are .passthrough(), so a key missing from one side is simply passed through rather than rejected. The new test compares the two declared shapes directly, which is the only form that bites.

Test

Three mutation checks, because the tests that should have caught this drift could not fail:

  • Dropping channels from the generated facet shape fails the facet-coverage test, naming channels.
  • Removing ...facetInputShape4 from the zod 4 mirror fails the new shape-parity test.
  • The bespoke-filter list is asserted by name, so wiring a new one into DatasetFilterOptions without exposing it here is visible.

MCP cluster: 321 pass, 0 fail. bun run typecheck and biome clean. backend/scripts/mcp-smoke.sh under real workerd: all required checks passed, tool list and calls still 200. Wire schema confirmed to carry the new parameters with generated descriptions, for example channels reads "Channel count. Range: 10..20, 64.. (at least), or ..128 (at most), in channels."

Note for whoever touches backend/src/ next: bun run lint is biome check src/ and does not cover backend/src/, so the pre-commit hook catches things the lint script does not. That is issue #936.

@github-actions

github-actions Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1429

Version: 0.10.5-PR1429.4d23d0c
Commit: 4d23d0c


🤖 Updated on each push.

@neuromechanist

neuromechanist commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Review folded in

Two reviewers. One would have blocked the merge, correctly, and the other found a test I wrote that could not fail. Both are fixed.

The block: generated descriptions misstated the syntax for 9 of 20 facets

My generator had three branches for five valueKinds, so bytes, duration, text and version were all described wrongly. Verbatim from the advertised schema before the fix:

size:         "Dataset size. Range: `10..20`, `64..` (at least), or `..128` (at most)."
duration:     "Total recording duration. Range: `10..20`, ..."
reference:    "EEG reference. Exact match on the declared value."
bids_version: "BIDS version. Exact match on the declared value."

size and duration declare no unit, so the unit clause contributed nothing and the bare form silently means bytes and SECONDS. A model passing duration: "10..60" for ten to sixty minutes gets ten to sixty seconds, faithfully applied, with no error. reference and placement are substring matches, not exact. bids_version and hed_version are exact-or-prefix. Enum facets accept comma-separated OR lists.

That is this pull request's own bug class one layer up: a filter that was applied correctly, to something other than what the caller meant.

Worse, it was a re-regression. src/lib/facet-options.ts already had the correct prose, with a comment naming the exact trap: "Showing only 10..50 here nudged users toward typing raw byte and second counts for --size and --duration, which is the difference between --duration 100h and --duration 360000 (#1169 review)."

Fixed by lifting describeFacet into shared/facets.ts, the zero-dependency module all three consumers already import, and having the CLI help, the wire contract and the zod 4 mirror call it. The CLI copy is deleted, so the prose is declared once, like the vocabulary it describes. Now:

size:         "Dataset size -- range with optional units (e.g. 10gb..2tb, 500mb.., ..1gb; bare = bytes)"
duration:     "Total recording duration -- range with optional units (e.g. 30m..2h, 100h.., ..90s; bare = seconds)"
reference:    "EEG reference -- substring match"
bids_version: "BIDS version -- exact or prefix match"

A test of mine that could not fail

test("a range facet value survives the schema") parsed {channels: "64..128"} and asserted the value came back. searchDatasetsInputSchema ends .passthrough(), so any key comes back: I confirmed parse({totally_made_up_param: "64..128"}) returns it unchanged. The assertion held whether or not channels was declared, and survived deleting the whole facet spread. I had mutation-checked the three tests around it and not this one.

Replaced with the collision and count check the other reviewer asked for, which is the gap all three declaration tests shared: a future facet whose queryParam matched a bespoke name would silently redefine it (both spreads land after the bespoke declarations), and one named limit would be silently dropped. Neither is visible to a key-set comparison. Mutation-checked in both directions: a colliding entry fails it, and removing include_unknown fails the exact count (33 to 32).

Also fixed

  • The catch around parseFacetFilters caught everything and reported it as "that filter value was not accepted", so an internal fault would be laundered into a user-input message and a model would retry reshaped values against it forever. Narrowed to RangeParseError and FacetEnumParseError, matching how the HTTP route narrows, with anything else rethrown into withToolMetrics. That is ADR 0051's posture applied here.
  • The tool description hand-listed 18 of the 20 facets, missing unavailable and zarr, and would have drifted again on facet 21. It now interpolates the labels from FACETS.

Corrected

There are 20 facets, not 21 as the description above said. I had counted the queryParam field declaration in the interface. The surface is 33 parameters: 20 facets, 12 filters, and limit.

Not in this pull request

Both reviewers independently found that no test invokes searchDatasetsTool at all, so any of the twelve wired filters could be dropped from the options bag with the suite green. One reviewer drove the tool and the real /mcp route against a migrated in-memory D1 and confirmed every new filter works today, so this is a coverage gap rather than a defect, but it is the gap that matters most. Also unfiled here: license silently drops unrecognized tokens on this brand-new surface, and excluded_unknown is computed on the query path and discarded. Tracked in #1436.

@neuromechanist
neuromechanist merged commit 032151f into dev Sep 17, 2026
12 checks passed
@neuromechanist
neuromechanist deleted the feature/mcp-full-search-surface branch September 17, 2026 00:40
neuromechanist added a commit to OpenScience-Collective/osa that referenced this pull request Sep 21, 2026
The prompt said the filters are exactly query, modality, task, has_hed,
has_zarr and limit, that there is NO participant-count filter, and to
pass only those six names. nemarOrg/nemar-cli#1429 takes the surface to
33 parameters and is in the release now being promoted to production, so
all three statements become false the moment it ships.

The third is the damaging one: it tells the model not to use filters that
work, so a question about channel counts or participant numbers takes a
worse route or gets refused, and nothing looks broken.

Rather than swapping one list for a longer list that goes stale the same
way, the prompt now points at the tool schema, which the server generates
from its facet definitions. The warning that matters is kept and is not a
function of how many filters exist: an undeclared argument is accepted and
silently ignored, so an invented name returns unfiltered results that look
filtered.

Partial: #370 asks for a whole-config rewrite and raises open questions
about how much belongs in the prompt at all. This is the correctness fix
the nemar-cli promotion forces, not that rewrite.
@neuromechanist neuromechanist mentioned this pull request Sep 21, 2026

This branch was previously deployed

1 inactive deployment
npm — 4d23d0ce Deployed Sep 17, 2026 by neuromechanist via publish #2695
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant