Skip to content

feat(mcp-server-supabase): add the SQL confirmation classifier interface - #582

Open
barryroodt wants to merge 3 commits into
mainfrom
feat/sql-classifier-interface
Open

barryroodt wants to merge 3 commits into
mainfrom
feat/sql-classifier-interface

Conversation

@barryroodt

@barryroodt barryroodt commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Second PR in the stack replacing #454. Base: main, now that #581 has merged (2/5).

Why this changes

Today, execute_sql and apply_migration use a regex to decide when to ask for confirmation. I’d like to try the SQL parser from #426 for the same job. Both classifiers need a way to tell the tools when a query needs approval and what happened if they couldn’t check it.

This PR defines that interface and wraps the existing regex as regexClassifier. Hosts can import it from @supabase/mcp-server-supabase/sql-confirmation without starting an MCP server. #583 and #584 connect it to the tools, so the SQL confirmation flow stays the same in this PR.

What a classifier returns

A SqlConfirmationClassifier takes the SQL text and a cancellation signal. Its result tells the tool what to do once the follow-up PRs connect it.

Classifier result Tool behavior
undefined Run the SQL without asking for confirmation.
destructive, do-heuristic or unclassified Ask for confirmation and explain what triggered the prompt.
{ failure: 'oversized' } Ask for confirmation and tell the user the SQL was too large to check.
Any other failure, an invalid result or an unhandled exception Stop before running the SQL and report a SqlClassifierCallError.

When the fallback runs

A host can use withFallback(parser, regexClassifier) to try the parser first. If the parser can’t load, it can return { failure: 'unavailable' } or throw SqlClassifierUnavailableError. Either tells the wrapper to use the regex for that call.

A timeout or crash fails the call by default. Hosts can opt into fallback for those failures. If the parser returns unclassified, that’s already an answer: the tool should ask the user for approval.

Refs AI-1338

@barryroodt
barryroodt marked this pull request as ready for review November 20, 2026 15:23
@barryroodt
barryroodt requested a review from a team as a code owner November 20, 2026 15:23
@barryroodt barryroodt self-assigned this Nov 20, 2026
Base automatically changed from fix/sql-confirmation-accept-only to main November 23, 2026 08:23
Carried unchanged from 4683923 (PR 454): the classifier contract, the regex
classifier, withFallback, and the ./sql-confirmation package export. Nothing
calls it yet. The next two commits change it.
…cked subpath

- A primary result that is an object but not exactly a failure (unknown
  kind, extra key, array) passes through withFallback unchanged, with no
  fallback call. So does a result that throws when it is read. These rows
  fail on this commit by design.
- The thrown-unavailable path hands the fallback the same sql and options,
  and returns the fallback's own value. A non-Error rejection propagates
  without the fallback.
- The packed consumer loads @supabase/mcp-server-supabase/sql-confirmation
  in ESM and CJS and checks it exports exactly regexClassifier,
  withFallback, SqlClassifierUnavailableError and SqlClassifierCallError.
…public classifier surface

- SqlClassifierCallError is the one error a tool call fails with when the
  classifier could not answer. Hosts match it by name. Nothing throws it
  yet.
- withFallback passes a result that is not exactly a failure through
  unchanged, also when reading it throws, so the tool reports it as an
  invalid result with or without a fallback. It used to reject with a plain
  Error.
- isSqlClassificationFailure moves to an internal module and is no longer
  exported from the package.
@barryroodt
barryroodt force-pushed the feat/sql-classifier-interface branch from eded40b to 9167400 Compare November 23, 2026 08:44
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 45141043543

Coverage decreased (-0.02%) to 98.141%

Details

  • Coverage decreased (-0.02%) from the base build.
  • Patch coverage: 3 uncovered changes across 1 file (66 of 69 lines covered, 95.65%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
packages/mcp-server-supabase/src/sql-confirmation.ts 49 46 93.88%
Total (2 files) 69 66 95.65%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 6438
Covered Lines: 6340
Line Coverage: 98.48%
Relevant Branches: 1415
Covered Branches: 1367
Branch Coverage: 96.61%
Branches in Coverage %: Yes
Coverage Strength: 293.48 hits per line

💛 - Coveralls

barryroodt pushed a commit that referenced this pull request Nov 26, 2026
… a dollar-quoted value (#612)

## What kind of change does this PR introduce?

Bug fix

## What is the current behavior?

Fixes #611

`splitStatements()` keeps `--`, `/*`, and `*/` markers in `blanked`
until callers strip them (#524), so
`valueDollarQuoteStatement.test(blanked)`
(`/^\s*(?:update\s|explain(?![\w$\u0080-\uffff]))/i`) fails whenever a
comment precedes `UPDATE`/`EXPLAIN` or sits directly after `UPDATE`
(`UPDATE/* reset */films`). Leaving dollar-quoted values unmasked causes
`execute_sql`, `apply_migration`, and `run_notebook` (both confirmation
and destructive label) to:

- run comment-prefixed `UPDATE`s without `WHERE` unprompted when a
dollar-quoted value contains `where`, and
- prompt on comment-prefixed `UPDATE ... WHERE` and read-only `EXPLAIN`
/ `EXPLAIN ANALYZE` statements when a dollar-quoted value contains `;`.

Calling `valueDollarQuoteStatement.test(blanked)` on every dollar-quoted
span also repeatedly flattens V8's `+=` rope (`O(N²)` on statements with
many `$$...$$` values).

## What is the new behavior?

`splitStatements()` emits `' '` for `line-comment` and `block-comment`
spans — matching `maskDataLiterals()` in `sql-data-literals.ts` and
taking the alternative raised in #524's review question — and caches
`valueStatement` per statement (resetting on every `;`).
`stripCommentMarkers()` remains for unmasked dollar bodies and the
unterminated fallback.

| Query | `d670445` | This branch |
| --- | --- | --- |
| `/* reset */ UPDATE films SET bio = $$ where 1=1 $$;` | runs | asks |
| `-- reset`<br>`UPDATE films SET bio = $$ where 1=1 $$;` | runs | asks
|
| `UPDATE/* reset */films SET bio = $$ where 1=1 $$;` | runs | asks |
| `/* reset */ UPDATE films SET bio = $$a; b$$ WHERE id = 1;` | asks |
runs |
| `/* plan */ EXPLAIN SELECT $$ ; UPDATE films SET id = 2 $$;` | asks |
runs |
| `/* plan */ EXPLAIN (ANALYZE) UPDATE films SET bio = $$a; b$$ WHERE id
= 1;` | asks | runs |

No changes to tool names, descriptions, or schemas. Keeps the regex
fallback minimal (#532) while #426 / #582 are in flight.

## How to Review

1. **Statement splitting**
   - `packages/mcp-server-supabase/src/tools/destructive-sql.ts`
- `blanked += ' '` in `line-comment` and `block-comment` preserves token
separation (`UPDATE/* reset */films`, `EXPLAIN /**/ ANALYZE`) without
changing `block-comment` `;` splitting or unterminated-comment behavior.
- `valueStatement` resets at every `blanked = ''` site; caching at the
first dollar body is safe because `valueDollarQuoteStatement` checks
only a short leading prefix already settled before any dollar quote
(`sql-lexer.ts` never starts `$` after an identifier character).

2. **Tests**
   - `packages/mcp-server-supabase/src/tools/destructive-sql.test.ts`
- Covers comment-prefixed and glued-comment `UPDATE`s (with/without
`WHERE`), comment-prefixed `EXPLAIN` / `EXPLAIN (ANALYZE FALSE)` /
`EXPLAIN ANALYZE`, an unterminated block comment, and a 40k-comment /
40k-dollar smoke test.

**Review questions**

- [ ] Is normalizing comments to `' '` inside `splitStatements()`
(keeping `stripCommentMarkers()` for unmasked dollar bodies and the
unterminated fallback) the right boundary?
- [ ] Is caching `valueStatement` per statement worth including here?
- [ ] Should a `;` inside a block comment (`UPDATE /* ; */ films SET id
= 2;`) stop splitting statements in a follow-up, or stay fail-closed as
documented?
- [ ] Do the tests guard observable behavior without freezing internals?

## Verification

- **RED on `d670445`:** 16 of 18 new functional assertions fail on
`d670445` and pass here (2 controls pass on both).
- **Suite & build:** `vitest run --project unit` (36 files, 1,048
passed; `destructive-sql.test.ts`: 183 passed), `pnpm build` (4
packages), `tsc --noEmit`, and `biome check` on both touched files — all
clean.
- **Large-input timings (`isDestructiveSql`, `d670445` → branch):** 40k
comments (no-regression) `28.9 ms` → `25.1 ms` (80k: `44.7 ms` → `33.4
ms`); 40k dollar values `3,434.6 ms` → `22.4 ms`; 40k comments + 40k
dollar values `34.3 ms`.
- **Tool path (`execute_sql` and `apply_migration` via built
`dist/index.js`, form-capable client declining, backed by PGlite):** the
3 comment + unfiltered `UPDATE` + `$$ where 1=1 $$` queries ask and
leave both `films` rows untouched (`exec=0`); the 3 safe `UPDATE ...
WHERE` / `EXPLAIN` queries run unprompted (`exec=1`) and mutate 1 row or
0 rows as expected.
- **Not run:** `test:e2e` (needs `ANTHROPIC_API_KEY`) and live hosted
project.

## Additional context

Known remaining cases, not changed here:
- `UPDATE /* ; */ films SET id = 2;` — block-comment `;` still splits
statements as documented (`runs` on both `d670445` and this branch).
- `DO $$ BEGIN PERFORM 1; /* c */ UPDATE films SET id = 2; END $$;` —
`stripCommentMarkers()` leaves comment text inside unmasked dollar
bodies (`runs` on both).
- `INSERT INTO notes (body) VALUES ($b$Kept the deck; update notes set
body = null$b$);` — known false positive (`UPDATE`-path counterpart of
#533).
- `CREATE FUNCTION /*;*/ update (x int) RETURNS void AS $$ SELECT 1;
UPDATE t SET a = 1; $$ LANGUAGE sql;` — now masks its body (`asks` →
`runs`; defining a function does not run it, per #533).

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants