Skip to content

feat: make the SQL confirmation classifier swappable, with a regex fallback - #454

Closed
barryroodt wants to merge 11 commits into
mainfrom
feat/sql-confirmation-classifier-interface
Closed

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

Conversation

@barryroodt

Copy link
Copy Markdown
Contributor

This makes the SQL confirmation classifier swappable, and falls back to the regex classifier when the plugged-in one can't load. I want to try Jev as a classifier later, and before that we need a clean way to plug one in (#426's AST parser included). Jev itself is out of scope here, this PR only adds the interface and the fallback.

With no host option set, behaviour matches main apart from two things: a request that's aborted while its SQL is being classified no longer runs the SQL, and a decline or cancel resend now returns before the classifier runs.

Once the server has asked for confirmation on a query, a resend that carries a decline, a cancel or no answer never runs the SQL, even if the classifier now returns nothing, which matters because with a fallback the AST can flag a query and then the regex finds nothing on the resend.

How #426 (and later Jev) plugs in

elicitation: {
  confirmation: {
    enabledTools,
    classifier: withFallback(astClassifier, regexClassifier),
  },
}

withFallback hands the SQL to regexClassifier when the primary resolves { failure: 'unavailable' } or throws SqlClassifierUnavailableError. #426's AST classifier doesn't do either yet. Today it throws a raw error when libpg-query can't load, so this setup would fail closed instead of falling back. When #426 rebases, that load failure needs to throw SqlClassifierUnavailableError (or resolve { failure: 'unavailable' }).

@coveralls

coveralls commented Oct 1, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 37011856462

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage increased (+0.1%) to 97.164%

Details

  • Coverage increased (+0.1%) from the base build.
  • Patch coverage: 189 of 189 lines across 4 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 4584
Covered Lines: 4476
Line Coverage: 97.64%
Relevant Branches: 775
Covered Branches: 731
Branch Coverage: 94.32%
Branches in Coverage %: Yes
Coverage Strength: 103.33 hits per line

💛 - Coveralls

@barryroodt
barryroodt marked this pull request as ready for review October 2, 2026 13:24
@barryroodt
barryroodt requested a review from a team as a code owner October 2, 2026 13:24
…llback

Adds the SqlConfirmationClassifier interface at ./sql-confirmation with regexClassifier (the default, unchanged regex logic) and withFallback, which falls back on an 'unavailable' failure or a SqlClassifierUnavailableError. Hosts set elicitation.confirmation.classifier for execute_sql and apply_migration. Only undefined skips confirmation; out-of-contract results, rejections and unavailable/timeout/crashed failures stop the call, and oversized SQL asks for confirmation with its own wording.
A primary result that is an object but not exactly { failure: <known kind> }
(unknown kind, extra keys, inherited failure, arrays) now rejects instead of
reaching the fallback. The tool validator shares the same check.
When a resend carries valid state this server issued for the same tool,
project, name and query hash, a decline, a cancel or no answer never runs the
SQL, even if the classifier now returns undefined (for example withFallback
after the primary sheds). Decline and cancel keep their result; no answer
re-prompts with a neutral reconfirm line. An accepted bound resend still runs
once, as on main.
F1: keep only the unanswered resend row and assert two classifications.
F2: keep the injected-classifier decline/cancel test as primary and cut the
spied loop back to its no-answer precedence row.
F3: drop the mockClear and not-called classifier assertions.
F4: fold the abort-signal test into the abort-during-classification test.
F5: drop the isDestructiveSql spy and classifier call count.
F6: fail any second classification of an accepted bound resend instead of
counting calls, and cut to the destructive and oversized rows.
F7: give the fallback a distinct failure so the test proves the fallback's
result is returned, and rename it to match.
F8: keep the regexClassifier test as the primary proof for the exported
./sql-confirmation entry point, cut to one destructive and one
non-destructive row.
F9: name and comment the fallback test as the documented
withFallback(parser, regexClassifier) recipe from the server.ts docs.
@barryroodt

Copy link
Copy Markdown
Contributor Author

Closing this in favour of a stack on main: #581 (a decline or cancel is final), #582 (the classifier interface), #583 (execute_sql + apply_migration), #584 (run_notebook) and #585 (a sql_classifier_call observation fact).

The interface here is the starting point of #582, which changes it in three small ways (a new error class, withFallback passing odd results through, one helper no longer exported). The bigger change is around it: run_notebook merged after this was written and never got the classifier, and a classifier failure left no observation fact, so this one PR had grown past a single purpose. Nothing here had a review yet, so no review context is lost.

@barryroodt barryroodt closed this Nov 20, 2026
barryroodt added a commit that referenced this pull request Nov 23, 2026
…on final (#581)

First PR in the stack replacing #454. Base: `main` (1/5).

## Why this changes

When `execute_sql` or `apply_migration` asks for confirmation, the
client sends a second tool call with the server-issued state and the
user’s answer. Today, only **Accept** skips a second SQL classification.
If the user clicks **Decline** or **Cancel**, the server checks the SQL
again before it honors that answer.

The current regex always gives the same answer for the same SQL. The
next PR makes the classifier replaceable, so a parser could flag a query
on the first call and a fallback could clear it on the second. Today,
that would run SQL the user just declined. #455 added observability and
a test that captures this behavior.

## What changes

The server checks that the signed state is valid and unexpired, then
matches it to the current tool, project ID and exact SQL text. For
`apply_migration`, the migration name must also match.

| Answer for a matching prompt | What the server does |
| --- | --- |
| Decline or Cancel | Returns the user’s answer immediately. It skips
classification and stops before executing the SQL. |
| No answer | Asks again, even if the classifier now clears the SQL. In
that case, the prompt says “This SQL was flagged for confirmation
earlier and still needs your approval.” |
| Accept | Runs the SQL, as it does today. |

A decline ends that call. The client can still send **Accept** for the
same prompt before the state expires. If the client changes the SQL, the
earlier answer doesn’t apply to the new query. The server classifies it
as before.

## Where to review

Start with `checkDestructiveSqlConfirmation`, which decides when to
classify the SQL. The updated tests show what happens when the
classifier changes its answer or throws after the first prompt. [SQL
confirmation
handling](https://github.com/supabase/mcp/blob/92be4920a465b507d2efc5196cff252eb9323126/packages/mcp-server-supabase/src/tools/database-operation-tools.ts#L67-L152)
· [PR diff and tests](https://github.com/supabase/mcp/pull/581/files)

Refs AI-1338
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