Skip to content

feat: add run_notebook with destructive SQL confirmation - #445

Merged
barryroodt merged 15 commits into
mainfrom
feat/run-notebook-tool
Oct 21, 2026
Merged

barryroodt merged 15 commits into
mainfrom
feat/run-notebook-tool

Conversation

@SaxonF

@SaxonF SaxonF commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Feature: adds run_notebook so agents can execute a saved notebook in one call instead of calling execute_sql for each cell.

What is the current behavior?

MCP can list and retrieve notebooks, but cannot run them as a unit. Notebook creation is added separately in #446.

What is the new behavior?

  • Runs database and log cells in order, returning each cell's rows or error; markdown is skipped and individual cell failures do not stop later cells.
  • Requires notebooks plus database or debugging. Each cell still requires its own feature, so enabling logs does not grant database execution.
  • Checks expected_updated_at against the saved notebook and enforces database read-only mode.
  • Caps a run at about 1 MB of rows across all cells. Each database cell gets the budget earlier cells left as max_result_bytes, so a platform can stop reading an oversized result early; a cell whose rows don't fit returns an error and later cells still run. execute_sql is unchanged.
  • Non-destructive and read-only runs do not prompt. With confirmation enabled, destructive database SQL requires form elicitation before any cell runs. Clients without form support are rejected; decline/cancel executes nothing. The prompt links to the notebook in Studio with query counts instead of listing SQL. Signed approval is bound to the notebook, cells, and execution mode. Explicitly disabling run confirmation preserves the opt-out.

How to Review

  1. Registration and execution
    • packages/mcp-server-supabase/src/server.ts, packages/mcp-server-supabase/src/tools/notebook-tools.ts, packages/mcp-server-supabase/src/platform/types.ts
    • Follow feature gating, stale-content checks, confirmation, sequential execution, and the per-run result budget (max_result_bytes).
  2. Cell handling and approval state
    • packages/mcp-server-supabase/src/tools/notebook-cells.ts, packages/mcp-server-supabase/src/logs.ts, packages/mcp-server-supabase/src/tools/confirmation.ts
    • Check the shared primary-database predicate, SQL row limits, log windows/results, and signed approval binding. query_logs and notebook execution share window validation through logs.ts.
  3. Tests
    • packages/mcp-server-supabase/src/server.notebooks.test.ts, packages/mcp-server-supabase/src/tools/notebook-cells.test.ts, packages/mcp-server-supabase/src/logs.test.ts, packages/mcp-server-supabase/src/transports/local-http-entry.test.ts
    • Server tests cover execution, feature isolation, and approval continuations; helper tests cover response shapes; HTTP tests cover skip configuration.

Review questions

  • Is the tool contract minimal?
  • Do cells respect feature and read-only restrictions?
  • Can any cell execute before required approval?
  • Do tests cover observable behavior without unnecessary duplication?

Verification

  • Shared log-window helper move: 79 focused tests passed across logs, debugging helpers, debugging server, and notebook server suites; typecheck and Biome passed. The helper and its eight tests moved unchanged.

  • Focused notebook, cell-helper, local HTTP and core server tests passed on this branch (173 tests), including the result budget.

  • Full combined stack: pnpm --filter @supabase/mcp-server-supabase test:unit --run — 519 tests passed.

  • pnpm build (including typechecks), pnpm exec biome check ., and git diff --check passed.

  • Regenerated Management API types from the current v1/v2 specifications; only the retired logs.all endpoint changed. Re-running the CI generation/diff check is clean, and both stack layers pass typechecking.

  • Claude Desktop rendering and live-project execution have not been retested.

Manual test

  1. In Supabase Studio, create and save a notebook in a test project. Add markdown and separate database cells containing select 1 as first_result;, select 1 / 0;, and select 2 as final_result;. Optionally add a working log query cell with a time range of at most 24 hours.
  2. Run pnpm install and pnpm dev:http from this checkout.
  3. Connect your MCP client as supabase-local to http://127.0.0.1:3111/mcp?project_ref=<ref>&features=notebooks,database,debugging&read_only=true, using Authorization: Bearer <Supabase PAT>. Leave skip_elicitations unset.
  4. Ask: “Using only supabase-local, retrieve the notebook '' and run it with run_notebook, passing its updated_at as expected_updated_at. Report each cell's result in order.” This read-only run should not request server elicitation. Verify that the first and last database cells succeed, the middle cell reports an error, and markdown is skipped. Repeat with a client without form elicitation support; the same read-only run should work.
  5. Retrieve the notebook, edit and save it in Studio, then call run_notebook with the old expected_updated_at. Verify it rejects the stale timestamp before running any cells.
  6. To test destructive-query confirmation without touching persistent tables, add a cell containing drop table if exists pg_temp.mcp_notebook_confirmation_probe;. Reconnect with read_only=false and a client that supports form elicitation. Retrieve the notebook again and run it. The prompt should show short query counts and a link to https://supabase.com/dashboard/project/<ref>/explorer/notebook/<id>, with no SQL listing. Open the link and verify it targets the saved notebook. Decline or cancel: no cells should run. Repeat and accept: cells should run in order.
  7. Repeat step 6 with a client without form support: the entire run should fail before executing any cell. With features=notebooks,debugging, database cells should instead report that database queries are unavailable while log cells can still run.

BEGIN_COMMIT_OVERRIDE
chore: add run_notebook with destructive SQL confirmation
END_COMMIT_OVERRIDE

@coveralls

coveralls commented Sep 24, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 39609416938

Coverage increased (+0.2%) to 97.344%

Details

  • Coverage increased (+0.2%) from the base build.
  • Patch coverage: 3 uncovered changes across 1 file (396 of 399 lines covered, 99.25%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
packages/mcp-server-supabase/src/tools/notebook-cells.ts 95 92 96.84%
Total (10 files) 399 396 99.25%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 5442
Covered Lines: 5329
Line Coverage: 97.92%
Relevant Branches: 1035
Covered Branches: 976
Branch Coverage: 94.3%
Branches in Coverage %: Yes
Coverage Strength: 146.64 hits per line

💛 - Coveralls

@SaxonF
SaxonF added this pull request to stack #447 September 24, 2026 04:54
@SaxonF SaxonF mentioned this pull request Sep 24, 2026
4 tasks
SaxonF and others added 3 commits September 24, 2026 15:23
Logs notebooks are a primary observability use case, so run_notebook is
now offered when either the database or debugging feature can run cells.
Database cells return a per-cell error without the database feature, as
log cells already do without debugging.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@SaxonF SaxonF changed the title feat: add run_notebook with confirmation feat: add run_notebook with destructive SQL confirmation Sep 24, 2026
@SaxonF
SaxonF marked this pull request as ready for review September 25, 2026 01:20
@SaxonF
SaxonF requested a review from a team as a code owner September 25, 2026 01:20
Comment on lines +266 to +274
// Use the same SQL heuristic as execute_sql, but only for cells
// this server can execute. Check before running even the first cell.
const destructiveCells = queryCells.filter(
(cell) =>
!readOnly &&
database &&
isPrimaryDatabaseCell(cell, project_id) &&
isDestructiveSql(cell.sql)
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same mutation detection logic as the dashboard or slightly different? isDestructiveSql currently allows e.g. updates as long as there's a where clause. I think we'll want this logic to be identical between MCP and dashboard to align user expectations

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just confirming @SaxonF was this resolved? Looks like the mismatch still exists.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing the dashboard code prompting for destructive or mutating queries, while run_notebook checks only destructive ones.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gregnr @mattrossman this matches Studio's implementation. If any cell is destructive or mutating, it elicits before the first cell runs.

run_notebook buffers every cell's rows until the run ends, which a hosted
server can't afford. Cap a run at about 1 MB of rows across all cells:
each database cell gets the budget earlier cells left as
max_result_bytes, so platforms can stop reading early, and a cell whose
rows don't fit returns an error without stopping later cells.
execute_sql is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@mattrossman mattrossman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested working w/ Claude Code and staging API:

  • Non-destructive notebook runs w/o a prompt and returns each cell's rows
  • Destructive cell triggers the confirmation
  • Editing the notebook while the prompt is open rejects the old approval ("changed since...") and runs nothing
  • A failing cell returns an error while the cells before and after still run

I left some comments on simplifying the feature group gating, aligning the prompt copy with execute_sql, and matching Studio's mutating-query check for confirmation.

@barryroodt

Copy link
Copy Markdown
Contributor

@mattrossman @gregnr I've merged main and addressed the inline threads. Please re-review when you get a chance.

I've also stacked #478 on top, which adds observation facts to run_notebook.

For hosted: run_notebook only elicits when the embedder includes it in enabledTools. Clients without elicitation support (legacy clients) get blocked on mutating notebooks without exception.
This isn't really consistent with say execute_sql where legacy clients aren't blocked.

For now I've decided to keep that block since it will require a bigger update to maintain parity with execute_sql - open to suggestions if you have any.

Then just to confirm that supabase/platform#38799 adds skip_elicitations=run_notebook as the opt-out.

@mattrossman mattrossman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the revisions in Claude Code, working!

Not blocking, but it's unfortunate that when Claude truncates the notebook URL in the elicitation (if your terminal isn't wide) it leads to a broken link... but I don't suppose there's much we can do about that. Might also be terminal-specific.

Image Image

There's some holes in the destructive notebook heuristic, but that's consistent with Studio at least. Wondering if we want to make execute_sql align on the same heuristic since they appear to deviate a bit.

description: `${notebookToolDefs.run_notebook.description} For log-cell SQL: ${queryLogsByDialect[debugging?.logsDialect ?? DEFAULT_LOGS_DIALECT].schemaHint}`,
annotations: {
...notebookToolDefs.run_notebook.annotations,
readOnlyHint: readOnly ?? false,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, I notice in read_only=true mode this sets readOnlyHint: true but destructiveHint stays true from the static annotations, so clients list run_notebook as read-only and destructive which seems a bit odd.

@barryroodt
barryroodt merged commit 2e3f34d into main Oct 21, 2026
5 checks passed
@supabase-releaser supabase-releaser Bot mentioned this pull request Oct 21, 2026
barryroodt added a commit that referenced this pull request Oct 21, 2026
## What kind of change does this PR introduce?

Feature: adds `create_notebook` to save markdown, database SQL, and log
SQL cells without executing queries or requesting elicitation. Stacked
on #445.

## What is the current behavior?

MCP can list, retrieve, and run saved notebooks, but creation requires
Studio.

## What is the new behavior?

Uses the existing Management API v2 create endpoint. The server assigns
cell IDs and returns the notebook ID, name, and update timestamp.
Creation is available under `notebooks`, hidden and rejected in
read-only mode, and optional for custom platforms. Saved notebooks are
shared with project members.

## How to Review

1. **Tool and input contract**
- `packages/mcp-server-supabase/src/tools/notebook-tools.ts`,
`packages/mcp-server-supabase/src/platform/types.ts`
- Follow input validation, project scoping, read-only rejection, and the
optional creation operation.
2. **Persistence**
   - `packages/mcp-server-supabase/src/platform/api-platform.ts`
- Inspect the v2 POST and response mapping; creation never executes cell
SQL.
3. **Tests**
- `packages/mcp-server-supabase/src/server.notebooks.test.ts`,
`packages/mcp-server-supabase/src/server.test.ts`,
`packages/mcp-server-supabase/src/transports/local-http-entry.test.ts`
- Check round-tripping, invalid inputs, feature/read-only restrictions,
immediate creation, and create-then-run.

**Review questions**

- [ ] Is the creation contract minimal?
- [ ] Are server-assigned IDs and saved cell options preserved
correctly?
- [ ] Do feature, project, and read-only restrictions hold?
- [ ] Do tests establish that creation neither prompts nor executes
queries?

## Verification

- `pnpm --filter @supabase/mcp-server-supabase test:unit --run` — **519
tests passed** across the combined stack; `test:integration` — 7 passed.
- `pnpm build` (including typechecks), `pnpm exec biome check .`, and
`git diff --check` passed.
- Regenerated Management API types from the current v1/v2
specifications; only the retired `logs.all` endpoint changed. Re-running
the CI generation/diff check is clean, and both stack layers pass
typechecking.
- Claude Desktop and live-project creation have not been retested.

### Manual test

1. Run `pnpm install` and `pnpm dev:http` from this checkout. Connect
your MCP client as `supabase-local` to
`http://127.0.0.1:3111/mcp?project_ref=<test-project-ref>&features=notebooks,database,debugging&read_only=false`,
using `Authorization: Bearer <Supabase PAT>`. Leave `skip_elicitations`
unset.
2. Ask: **“Using only supabase-local, create a notebook called MCP
creation test with an introductory markdown cell and a database cell
containing `select 1 as result`, with row_limit 100. Save it without
running it.”** Creation should return the notebook ID, name, and
timestamp immediately, without server elicitation or query execution.
3. Open the notebook in **Supabase Studio**, or go directly to
`https://supabase.com/dashboard/project/<ref>/explorer/notebook/<returned-id>`.
Verify its name, markdown, and database cell were saved. Repeat creation
with a distinct name in a client without form elicitation support; it
should also succeed without a prompt.
4. Ask: **“Using only supabase-local, retrieve MCP creation test and run
it with run_notebook, using its updated_at as expected_updated_at.”**
This non-destructive query should run without elicitation and return
`result: 1`. Destructive-run confirmation is covered in #445's manual
test.
5. Reconnect with `read_only=true`. Verify that `create_notebook` is
absent from the tool list and a direct call is rejected without saving a
notebook.

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Barry Roodt <[email protected]>
barryroodt pushed a commit that referenced this pull request Oct 29, 2026
## What kind of change does this PR introduce?

Release gate (chore)

## What is the current behavior?

With `features=notebooks`, `tools/list` offers `create_notebook` and
`run_notebook`, and release PR #477 would ship them in 0.15.0.

## What is the new behavior?

The server no longer registers `create_notebook` or `run_notebook`. With
`notebooks` requested, `tools/list` shows only `list_notebooks` and
`get_notebook`, and a call to either hidden tool returns `tool not
found`. The static tool registry (`supabaseMcpToolSchemas` /
`createToolSchemas`) still lists both tools, so it no longer matches
what the server registers for notebooks.

An `@internal` server option, `unreleasedNotebookTools`, turns them back
on. It defaults to `false` and has no CLI flag or query param. Tests
that cover the hidden tools set it, including the local HTTP suite
through `startEntry`. `@internal` is only a doc comment, so the option
still ships as `unreleasedNotebookTools?: boolean` on the exported
`SupabaseMcpServerOptions`, and hosts using `createSupabaseMcpHandler`
can set it.

The commit type is `chore`, so this PR adds nothing to the changelog.
The notebook entries from #445, #446 and #478 come out of #477 through
`BEGIN_COMMIT_OVERRIDE` blocks on those PRs.

Closes [AI-1377](https://linear.app/supabase/issue/AI-1377)
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.

5 participants