Skip to content

fix(mcp-server-supabase): match Studio's notebook row_limit and clarify skip and blank-input messages - #593

Merged
barryroodt merged 3 commits into
supabase:mainfrom
officialbidisha:fix/notebook-row-limit
Nov 25, 2026
Merged

barryroodt merged 3 commits into
supabase:mainfrom
officialbidisha:fix/notebook-row-limit

Conversation

@officialbidisha

@officialbidisha officialbidisha commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

applyRowLimit now ignores semicolons inside literals via lexSql, and treats a read-only CTE (with … select) as a select, the same way Studio's applyAutoLimit does. Cells skipped after an oversized result get their own reason instead of 'run result budget spent'. Blank notebook SQL and names get explicit validation messages. tools/list is unchanged.

What kind of change does this PR introduce?

Bug fix. There are no changes to tools/list: tool names, descriptions, and input/output schemas are byte-identical.

What is the current behavior?

Fixes #595

  1. row_limit is silently ignored for common SELECT shapes. applyRowLimit counts every ; with a regex over the raw text, including ones inside string or $$ literals, and it skips every CTE, including read-only ones that Studio limits. Each cell below has row_limit 5 (the $$ cell has row_limit 3):

    SQL Rows returned
    select g from generate_series(1,50) g 5
    select g, 'a;b' as s from generate_series(1,50) g 50
    with s as (select g from generate_series(1,50) g) select * from s 50
    select g, $$a;b$$ as body from generate_series(1,20) g 20
  2. After an oversized cell, later cells are reported as "run result budget spent", even though about 998 KB of the budget was never used. The cell ran fine on its own.

  3. create_notebook with whitespace-only SQL (" ") fails with Too small: expected string to have >=1 characters, because the schema trims before checking length. That message is misleading for a 3-character input.

What is the new behavior?

  1. Statement count: applyRowLimit lexes the SQL with the existing lexSql, so only ; in code spans separates statements. A single SELECT with ; inside a string, E-string, quoted identifier or dollar-quoted body now gets limit N. Unterminated SQL is returned unchanged.
  2. CTEs, as in Studio: a query starting with with counts as a select unless a write keyword (insert, update, delete, merge, into) appears in its code, outside literals and quoted identifiers. This mirrors applyAutoLimit in apps/studio/data/sql/utils.ts. The limit stays in SQL, so a large read-only CTE no longer fills the run's result budget.
  3. row_limit stays an auto-limit: as in Studio, an explicit LIMIT wins, and rows are never trimmed after the fetch. select … limit 100 with row_limit 5 returns 100 rows, and SQL that is left as written (for example, with comments) returns all its rows. All other conservative rules are unchanged.
  4. Skip reason: runQueryCell returns an explicit oversized flag.
    • Cells skipped after an oversized result get the reason "an earlier cell's result was too large for this run".
    • "run result budget spent" is kept for real exhaustion by returned rows.
    • The observation record is unchanged.
  5. Blank input: create_notebook now returns SQL must not be empty or only whitespace. and Notebook name must not be empty or only whitespace. The published JSON schema is identical, because these are only custom messages on the existing .trim().min(1).

How to Review

Review area

packages/mcp-server-supabase/src/tools/notebook-cells.ts

  • In applyRowLimit, non-code spans are replaced with a space. That code-only text is used for the statement count and for the CTE write-keyword check.
  • isSelectLike replaces the old ^select test. ^select is unchanged, and ^with passes only without insert|update|delete|merge|into in code.
  • The comment, limit/fetch word and limit > 0 checks are untouched.

Next review area

packages/mcp-server-supabase/src/tools/notebook-tools.ts

  • runQueryCell returns rows exactly as the database returned them. There is no post-fetch trimming.
  • The run loop: budgetSkipReason switches only after an oversized result. Look at the BudgetSkipReason union in CellResult.

Next review area

packages/mcp-server-supabase/src/platform/types.ts

  • In newNotebookSqlSchema and createNotebookOptionsSchema.name, only the custom .min(1, { message }) text was added.

Tests

  • src/tools/notebook-cells.test.ts:
    • Now limited: 'a;b', $$a;b$$;, $fn$ x; y $fn$, "a;b", an E-string with an escaped quote and ;, and read-only CTEs, including WITH RECURSIVE.
    • Write keywords inside a string, quoted identifier or $$ body, and longer words such as updated_at and deleted, do not block the limit.
    • Still unchanged: CTEs that insert, update, merge, select … into, for update, or use a data-modifying CTE (with d as (delete … returning *) select …). CTEs with a limit or comments, an unterminated select 'a;b, and select 'a'; select 2 are also unchanged.
  • src/server.notebooks.test.ts:
    • "applies row_limit as an auto-limit, like Studio": the CTE cell is sent with limit 3; and returns 3 rows. select … limit 6 with row_limit 3 is sent as written and returns 6 rows. A ---commented cell is sent as written and returns all its rows.
    • The oversized scenarios (the MCP-side byte check and the adapter's ResultTooLargeError) expect the new skip reason. The genuine-exhaustion tests still expect "run result budget spent".
    • Whitespace-only SQL (database and log cells) and whitespace-only names return the new messages at the right path, and nothing is saved.

Review questions

  • Is the public contract minimal and intentional? tools/list is unchanged. The new skip-reason string appears only inside the run result payload, which is a wrapped string in the output schema.
  • Do the main execution paths preserve existing behavior? SQL that was limited before is limited the same way. Two shapes are newly limited: a single SELECT with ; inside a literal, and a read-only CTE. Rows are returned as the database returned them.
  • Do failures stop safely? Unterminated SQL is not rewritten. CTEs that write are not rewritten. Oversized cells still error, and later cells are still skipped, as documented.
  • Do tests protect observable behavior rather than implementation details? Yes. They assert the rows returned, the SQL sent to the mock API, the skip reasons, and the validation errors.

Verification

From the repo root, on this branch:

pnpm --filter @supabase/mcp-server-supabase typecheck
pnpm exec biome check packages
pnpm --filter @supabase/mcp-server-supabase test:unit --run
# Test Files  35 passed (35)
#      Tests  954 passed (954)

The new server test fails when the old post-fetch rows.slice is restored: the explicit-limit and commented cells come back trimmed to 3 rows.

Live check: with the --http server built from this branch, I ran a notebook against a real project (row_limit 5 on each query cell).

semicolon in string: success rows=5      (was 50)
dollar body:         success rows=5      (was 50)
cte:                 success rows=5      (was 50, now limited in SQL)
explicit limit:      success rows=20     (limit 20 wins over row_limit, as in Studio)
trailing comment:    success rows=50     (left as written, as in Studio)
big:                 error The query ran, but its result is larger than the 999148 bytes left for this run ...
after big:           skipped an earlier cell's result was too large for this run   (was "run result budget spent")

The change was written with AI assistance (Claude Code). I reviewed the diff and re-ran the commands above.

Additional context

Remaining difference from Studio, unchanged by this PR: Studio only skips the auto-limit when the query ends in a LIMIT or contains fetch first|next. applyRowLimit skips on any limit or fetch word, including one inside a subquery or a string. Those shapes still return more rows here than in Studio. Commented SQL is left as written in both places, so it can still exceed the run budget.

Out of scope: argument validation errors (including the new messages) still arrive inside the shared raw ZodError wrapper from @supabase/mcp-utils. Changing that affects every tool and is better as its own PR.

Generated with Claude Code

… and blank-input messages

applyRowLimit now ignores semicolons inside literals via lexSql, and run_notebook caps returned rows at row_limit for SQL it does not rewrite (CTEs, comments). Cells skipped after an oversized result get their own reason instead of 'run result budget spent'. Blank notebook SQL and names get explicit validation messages. tools/list is unchanged.

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

@barryroodt barryroodt left a comment

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.

Thanks for the detailed write-up and the live check! The lexer change, the new skip reason and the validation messages all look good.

One change please: the post-fetch rows.slice makes row_limit a hard cap, so select … limit 100 with row_limit: 5 now returns 5 rows. Studio treats row_limit as an auto-limit: an explicit LIMIT wins, and it never trims rows after the fetch (applyAutoLimit). The same notebook should return the same rows in both places.

Could you drop the slice, and cover the CTE case the way Studio does instead? It treats with … select as a select unless a write keyword (insert, update, delete, merge, into) appears outside literals. That also keeps the limit in SQL, so a big CTE doesn't hit the result budget.

…ike Studio

Drop the post-fetch row slice, so an explicit LIMIT wins and rows are never
trimmed after the fetch. Treat a  query as a select unless insert,
update, delete, merge or into appears outside literals, matching Studio's
applyAutoLimit, so read-only CTEs are limited in SQL.
@officialbidisha officialbidisha changed the title fix(mcp-server-supabase): enforce notebook row_limit and clarify skip and blank-input messages fix(mcp-server-supabase): match Studio's notebook row_limit and clarify skip and blank-input messages Nov 25, 2026
@officialbidisha

officialbidisha commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed write-up and the live check! The lexer change, the new skip reason and the validation messages all look good.

One change please: the post-fetch rows.slice makes row_limit a hard cap, so select … limit 100 with row_limit: 5 now returns 5 rows. Studio treats row_limit as an auto-limit: an explicit LIMIT wins, and it never trims rows after the fetch (applyAutoLimit). The same notebook should return the same rows in both places.

Could you drop the slice, and cover the CTE case the way Studio does instead? It treats with … select as a select unless a write keyword (insert, update, delete, merge, into) appears outside literals. That also keeps the limit in SQL, so a big CTE doesn't hit the result budget.

Thanks! Done in 1d6d70e.

  • Dropped the rows.slice. row_limit is an auto-limit again: an explicit LIMIT wins, and rows come back as the database returns them.
  • applyRowLimit now treats with … select as a select unless insert, update, delete, merge or into appears outside literals. It reuses the lexSql code spans, so keywords inside strings, quoted identifiers and $$ bodies don't count. CTEs that write, including a data-modifying CTE (with d as (delete … returning *) select …), are left as written, as in Studio.
  • The server test now checks that a CTE is sent with limit 3;, that limit 6 with row_limit: 3 returns 6 rows, and that commented SQL isn't trimmed.
  • I reran the live check (results in the description): the CTE returns 5 rows because the limit is now added to the SQL, limit 20 returns 20, and the commented cell returns all 50.

One difference from Studio remains, and this PR doesn't change it: applyRowLimit skips the limit whenever the word limit or fetch appears anywhere, such as in a subquery or a string. Studio only checks for a trailing LIMIT or fetch first|next. So with u as (select … limit 1) select * from u gets the limit in Studio but not here. Happy to align that in this PR or a follow-up, whichever you prefer.

FYKI: barryroodt

@barryroodt barryroodt left a comment

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.

Thanks for the quick turnaround, this is exactly what I was after! Reusing the lexer spans for the write keywords is a nice touch.

On the remaining limit/fetch difference: let's leave it as is for this PR. Erring toward not adding a limit is the safer side, and the result budget still bounds what comes back.

@officialbidisha

Copy link
Copy Markdown
Contributor Author

I merged main into this branch (ee7c73b). The two failing checks are unrelated to the notebook changes:

Tests / test: all unit and integration tests pass, including every notebook test. The only failures are the 5 e2e tests (functions.e2e.ts, projects.e2e.ts, prompt-injection.e2e.ts), which fail with x-api-key header is required. They call the Anthropic API using ANTHROPIC_API_KEY, and GitHub does not pass repository secrets to workflows triggered from fork PRs, so the key is empty here. Could a maintainer re-run them with the secret available?

Check Management API Types: the live v2 spec has changed since #607 (it now includes the /v2/projects/{ref}/warehouse/ingestion-stats and /v2/projects/{ref}/warehouse/storage-stats endpoints), so main fails this check as well right now. #609 regenerates both type files, and I confirmed locally that its output matches the current spec exactly. Once #609 is merged, I'll update this branch again and the check should pass.

Locally, the unit suite passes on this branch merged with current main (1001/1001).

@barryroodt could you take another look and re-approve when you have a moment? The workflows for the new push are also waiting for maintainer approval. Thanks!

@barryroodt
barryroodt merged commit ad07f1b into supabase:main Nov 25, 2026
5 of 7 checks passed
@supabase-releaser supabase-releaser Bot mentioned this pull request Nov 25, 2026
barryroodt pushed a commit that referenced this pull request Nov 26, 2026
🤖 I have created a release *beep* *boop*
---


<details><summary>mcp-utils: 0.11.1</summary>

##
[0.11.1](mcp-utils-v0.11.0...mcp-utils-v0.11.1)
(2026-11-26)


### Bug Fixes

* **mcp-ui:** shared project card for create-project and create-branch
([#578](#578))
([efe189f](efe189f))
</details>

<details><summary>mcp-server-supabase: 0.20.0</summary>

##
[0.20.0](mcp-server-supabase-v0.19.0...mcp-server-supabase-v0.20.0)
(2026-11-26)


### Features

* **debugging:** add project issues DEBUG-311
([#546](#546))
([01f8021](01f8021))
* **mcp-server-supabase:** add optional schemas parameter to
generate_typescript_types
([#592](#592))
([7532f53](7532f53))


### Bug Fixes

* **debugging:** link docs once per issue in get_issues
([#610](#610))
([74801d8](74801d8))
* **mcp-server-supabase:** detect import maps uploaded with a ./ prefix
([#591](#591))
([6b8f4dc](6b8f4dc))
* **mcp-server-supabase:** keep a declined or cancelled SQL confirmation
final ([#581](#581))
([03afe8e](03afe8e))
* **mcp-server-supabase:** match Studio's notebook row_limit and clarify
skip and blank-input messages
([#593](#593))
([ad07f1b](ad07f1b))
* **mcp-server-supabase:** skip cost confirmation for $0 projects on
clients without forms
([#608](#608))
([50cfc37](50cfc37))
* **mcp-server-supabase:** stop reusing a removed import map on edge
function redeploy ([#588](#588))
([56093a6](56093a6))
* **mcp-ui:** shared project card for create-project and create-branch
([#578](#578))
([efe189f](efe189f))
* **mcp:** confirm UPDATEs without WHERE that follow a comment and set a
dollar-quoted value ([#612](#612))
([468907a](468907a))
* **mcp:** confirm UPDATEs without WHERE that use inheritance syntax
([#565](#565))
([687b3ee](687b3ee))
* **mcp:** confirm UPDATEs without WHERE when a column name ends in
where ([#557](#557))
([57fa34b](57fa34b))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: supabase-releaser[bot] <223506987+supabase-releaser[bot]@users.noreply.github.com>
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.

run_notebook: row_limit is silently ignored for CTEs, comments, and ; inside literals

2 participants