Skip to content

feat(d1-studio): D1 Studio MCP App - #444

Open
geoquant wants to merge 8 commits into
geoquant/mcp-app-workers-usage-alertfrom
geoquant/d1-mcp-app
Open

geoquant wants to merge 8 commits into
geoquant/mcp-app-workers-usage-alertfrom
geoquant/d1-mcp-app

Conversation

@geoquant

@geoquant geoquant commented Nov 25, 2026 •

Copy link
Copy Markdown

Stacked on #421 (the shared MCP Apps foundation); retarget to main once it merges.

What

d1_studio is a read-only MCP App for D1. Each call renders one card:

  • The answer: its rows in the dashboard Studio's table look, sortable, paged and copyable. The SQL is folded, and you can edit it and re-run it in the view.
  • Paging: the card shows 10 rows a page, with ‹ › and Go to row ("Rows 11–20 of 1,284"). The tool returns at most 200 rows, so later pages are read from D1 200 at a time (sliceSql). A query's own small LIMIT pages by writing OFFSET into its SQL. Sorting a partial result asks D1 to sort the whole of it and keeps your page, as Studio's table explorer does.
  • The steps: the earlier queries the agent ran on the way to the answer.
  • Fullscreen: the view becomes a console with the run history, the database's tables, an editor and the results 100 rows a page.

Reads re-run in the view through d1_raw, with no model turn. Writes are never run by the view: they go to the chat as the user's message (ui/message), and the agent runs them with d1_query once the user agrees.

Reads only, enforced twice

  • The d1_studio tool: it runs only statements classifyStatement proves are one read, and refuses the whole call before sending anything otherwise, because D1's /raw has no read-only mode. This round also fixes a bypass where a replace() call hid a later REPLACE INTO.
  • The view's re-runs (new): in Code Mode the server refuses the view's d1_raw calls unless the body is one sql read, never a write or a batch. This uses rawCallRefusal through a new optional appToolCheck on defineMcpApp. Direct tools (?codemode=false) keep d1_raw as the model's own tool, unchanged.

State machines, model-checked

The view's states are explicit reducers in app/machines.ts. Five TLA+ models, checked with TLC, cover:

  1. the card's run and send-to-chat hand-off;
  2. paging and D1 sort;
  3. the fullscreen console;
  4. copy feedback;
  5. the mount lifecycle.

Each model comes with a replay of its paths against the real code. Bugs they found and this PR fixes:

  • Write sent twice: ⌘↵ could send a write the chat already had.
  • Lost write confirmation: a Revert or a display switch while a write was sending forgot that the chat got it.
  • Unsupported hosts: the keyboard and the LIMIT pager could call tools the host doesn't offer.
  • Lost console work: the console lost its history on leaving fullscreen, threw away re-read rows, and dropped reads you navigated away from. Reads you leave now finish and land in history, as a Studio tab's run does.
  • Wrong page: a page asked for during another read, or during a D1 sort, could be dropped. Sort and page now combine, as in Studio's table explorer.
  • Stale copies: a slow "Copy all" could overwrite a later copy; the toast and ✓ now follow the order copies land.
  • Mount edge cases: a late cancel or a failed connect could hide a result already shown, and Try again could run without the call's arguments.

The models live in the second commit as project skills (.agents/skills/verify-d1-*/, linked from .claude/skills). Every rule holds at 1a0a9230, and each fails on a variant that restores the bug it guards. That commit can be split into its own PR if reviewers prefer.

Commits

  1. feat(d1-studio): the D1 Studio MCP App view: the App, the fixes above, tests, AGENTS.md.
  2. test(d1-studio): TLA+ verification skills: five skills with models and replays (about 70 small files, no product code).
  3. feat(d1-studio): bundle Paper Mono: the dashboard Studio's mono font. It's SIL OFL 1.1 and credited, with its license beside it. It is subset to 16 KB (adding 22 KB to the App's HTML) and falls back to the system mono.
  4. style(d1-studio): drops a repeated CSS rule.

Verification

  • vitest run: 1,066 passed, 1 skipped, before commits 3–4. Those two are CSS and font only; after them the 80 D1 Studio tests pass.
  • Both typechecks are clean, oxlint reports 0 errors, and npm run app:check is fresh.
  • The five models' TLC checks and 54 replay tests pass (bun test ./.agents/skills).
  • Rendered and driven in a local MCP Apps host with fixture data (local tooling, not part of this PR):
    • every inline state and the fullscreen console;
    • a 1,284-row result: ‹ ›, Go to row 501 (a d1_raw read past the 200 rows held), and a D1 sort that kept rows 501–510 in the new order.

Not covered / known

  • Not tried in a real host yet. It still needs a run inside Claude and ChatGPT, including whether each host's sandbox allows the inlined font (data: font); if not, it falls back to the system mono.
  • Inline row height: the inline card keeps 30 px rows, because the frame-filling layout counts on that height. The console uses Studio's 35 px.
  • Read timeout: reads from the view time out after 35 s (READ_TIMEOUT_MS), just past D1's 30 s query limit.

Screenshots

Screenshot 2026-11-25 at 5 00 59 PM

Local fixtures, fake data. To attach:

  • inline card with its rows open
  • card with the queries that led to it ("3 queries led here")
  • failed read with its fix
  • paging a 1,284-row result (rows 1–10, then 11–20)
  • fullscreen console

🤖 Generated with Claude Code

sidharthachatterjee and others added 8 commits November 23, 2026 13:24
…#427)

Switches the devDependency from a pkg.pr.new tarball URL off a forge main
commit to the versioned npm package @cloudflare/forge@0.2.0, published
with provenance via Trusted Publishing. The forge-tools workflow now reads
the version string from package.json directly and surfaces it in the PR
body, and the scripts/generate-tools.ts header describes the dep as a
published npm release. Cadence moves from 'every forge main commit' to
'every tagged forge release'.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
The d1_studio App now renders one card per call: the answer's rows in
Studio's table look (sortable, paged, copyable), the agent's earlier steps,
an editor that re-runs reads in the view (d1_raw) and hands writes to the
chat (ui/message), a frame-filling layout, and a fullscreen console with
the run history and the database's tables.

Its states are explicit machines (app/machines.ts). Five TLA+ models of
them (the next commit, .agents/skills/verify-d1-*/) found the following,
fixed here:

- A write already sent or unconfirmed is never sent to the chat again
  (⌘↵ now does what the primary button shows); Revert waits out a send.
- The card runs reads and sends writes only where the host offers each.
- The answer card's machine and the console's history live above the
  display switch, so runs, sends and history survive fullscreen.
- A console read you leave keeps going and lands in history; schema
  picks run at once; re-reads show the newest rows (as Studio's tabs do).
- Sort and page combine (as Studio's table explorer): the page asked for
  last, during a sort too, is the page shown.
- A slow Copy all never overwrites a later copy; toasts and ✓ follow the
  order copies land.
- mount.tsx: a late cancel or failed connect no longer hides a shown
  result, and Try again appears once the call's arguments are known.
- classifyStatement checks every REPLACE, so a replace() function can't
  hide a later REPLACE INTO from the read-only check.
- The "Use <column>" fix replaces whole identifiers only, never inside
  longer names, strings or comments.
- Picked tables are always quoted; a query ending in `; -- comment` can
  be paged; join keywords are no longer taken for table aliases.
- In Code Mode the server refuses the view's d1_raw unless it is one
  read (appToolCheck on defineMcpApp), so reads-only holds whatever the
  view sends; reads from the view time out after 35 s.
- The console matches the dashboard's Studio more closely: 35 px rows,
  no grid lines past the last row, and the column header stays
  clickable while D1 sorts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Five project skills, one per state machine of the D1 Studio MCP App, each
a TLA+ model checked through /statechart plus a replay of its paths
against the real code (bun test):

- verify-d1-card-run-send: the answer card's run / send-to-chat hand-off.
- verify-d1-result-paging: paging, D1 sort and held blocks against runs.
- verify-d1-console: the fullscreen console's history and background reads.
- verify-d1-copy: the copy toast, Copy all and the trios' ✓ marks.
- verify-d1-tool-lifecycle: mountMcpApp's pending/result/retry lifecycle.

Every rule holds at the sync commit 1a0a923 and each fails on a broken
variant restoring the bug it guards. .claude/skills links to
.agents/skills so Claude Code and other harnesses find the same skills.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The view's SQL, table and console text now render in Paper Mono, as the
dashboard's D1 Studio does, instead of falling back to the system mono.
Regular only, subset to Latin, Latin-1, Latin Extended-A and the symbols
the view draws: 16 KB, inlined by the build (the App's CSP allows no font
host), +22 KB on the App's HTML. Other glyphs fall back per character.

Paper Mono is SIL OFL 1.1 (no Reserved Font Name), Copyright 2025 Lost
Coast Labs, Inc. (Paper Design), based on Geist Mono, Copyright 2023
Vercel. The license ships beside the font (fonts/PaperMono-LICENSE.txt)
and in the font's name table; source paper-design/paper-mono @ 74fc5537.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@geoquant
geoquant requested review from a team as code owners November 25, 2026 23:52
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.

3 participants