## Problem
Studio's AI API routes under `routes/api/ai/**` still run legacy Next
Pages Router handlers through `compat/next/api.ts`. Part 2 of 7 for
[FE-5114](https://linear.app/supabase/issue/FE-5114/rewrite-api-routes-as-native-tanstack-start-handlers-and-remove-the)
migrates the active AI routes to native TanStack Start (`Request` →
`Response`) handlers and drops 3 unused AI endpoints.
## Solution
**Changed:**
- Migrated 8 active AI routes (`ai/code/complete`,
`ai/feedback/{classify,rate}`, and
`ai/sql/{check-api-key,cron-v2,filter-v1,generate-v4,title-v2}`) to
`createApiHandlers` and `parseJsonBody`
- Replaced Node `pipeUIMessageStreamToResponse` in
`ai/sql/generate-v4.ts` with native `createUIMessageStreamResponse`,
preserving the `x-braintrust-span-id` header
- Replaced `req.on`/`res.on` in `ai/sql/generate-v4.ts` with
`AbortSignal.any([request.signal, done.signal])` and a `TransformStream`
whose `flush`/`cancel` tear down the MCP connection opened in `getTools`
on stream completion, cancellation, or setup error
- Removed the custom malformed-JSON handling in `ai/code/complete.ts` in
favor of the wrapper's `400`
- Updated `lib/api/generate-v4.test.ts` to assert against the real
native SSE `Response` stream instead of mocking
`pipeUIMessageStreamToResponse`, with a shared `mockOpenStream()`
helper, a stream-cancellation abort test, and a Braintrust tracing test
asserting `x-braintrust-span-id` matches the exported root span
- Updated `lib/ai/README.md` to drop references to removed endpoints
**Removed:**
- Removed 3 unused AI routes (`routes/api/ai/onboarding/design.ts`,
`routes/api/ai/sql/parse-client-code.ts`, and
`routes/api/ai/sql/policy.ts`), dropped them from
`lib/hosted-api-allowlist.ts`, and regenerated `routeTree.gen.ts`
- Removed the now-unused `withoutApprovalTools` helper from
`lib/ai/tool-filter.ts` and its test from `lib/ai/tool-filter.test.ts`
**Behavior changes vs master:**
- `ai/code/complete` returns the wrapper's `400` JSON `{ error: {
message: 'Invalid JSON' } }` for malformed bodies
- Unused `ai/onboarding/design`, `ai/sql/parse-client-code`, and
`ai/sql/policy` endpoints have been removed
## Review instructions
1. Run `pnpm --filter studio exec vitest run lib/api/generate-v4.test.ts
lib/api/rate.test.ts lib/ai/tool-filter.test.ts`.
2. Send a prompt in the AI Assistant panel, verify streaming output, and
cancel mid-stream.
3. Trigger SQL title, cron, and filter generation as well as AI code
completion.
## Checklist
- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
---------
Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
Moves every `pages/api/**` handler into its TanStack server route under
`routes/api/**` and deletes `pages/api/**`, part of the final pass of
the TanStack Start migration (FE-3106). Handlers move verbatim and still
run through `toWebHandler`, so auth (`apiWrapper`), response shapes and
headers are unchanged. Part 2 of the stack.
**Depends on:** #53199
**Changed:**
- `routes/api/**` – each route now holds its handler (formerly the
`pages/api` default export) as a local `nextHandler`/named function.
Next-only `config`/`maxDuration` exports are dropped: `vite.config.ts`
already sets the function `maxDuration`, and `toWebHandler` parses
bodies itself
- `routes/api/ai/docs.ts` – Web-native handler inlined, `NextRequest` →
`Request`
- `lib/api/apiHelpers.ts` – `getPgMetaRedirectUrl` moved here from the
old `tables` handler, which the other pg-meta routes imported it from
- `eslint.config.cjs`, `.github/CODEOWNERS` – `pages/api` paths →
`routes/api`
- `tests/routes/api/**`, `lib/api/{generate-v4,rate}.test.ts`,
`scripts/__tests__/server-source-maps.test.ts` – call the route's
registered handler with a Web `Request` (`tests/lib/server-route.ts`);
the MCP query-validation cases now run against the native route
**Removed:**
- `pages/api/**` (102 files), including the Next twins of the
already-native `routes/api/mcp/index.ts` and `functions/$slug/body.ts`
- unused `ProjectSettings` types and the `node-mocks-http` dev
dependency
Parity: every route registers the same methods its handler serves. The
exceptions are `check-cname`, `get-ip-address`, `get-utc-time`,
`get-s3-keys`, `get-deployment-commit`, `cli-release-version` and
`connect`: in Next they answered any method, and their routes register
`GET` only, as before this PR.
Addresses
[FE-3106](https://linear.app/supabase/issue/FE-3106/move-page-bodies-into-route-files-and-delete-pages).
## To test
- Load the table editor, SQL editor and Auth users on a self-hosted
build – `/api/platform/**` and `/api/v1/**` respond as before
- Use the Assistant and the docs search – `/api/ai/**` streams as before
- `GET /api/get-deployment-commit` – same JSON and `Cache-Control`
Ran typecheck, ESLint, lint ratchet, knip, Prettier, the TanStack build
and the full Studio vitest suite: all pass except
`lib/server/configcat.test.ts`, which also fails on `master` in an
environment with ConfigCat env vars set. Self-hosted E2E is CI-only.
---------
Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
**Stack 4/6** of the TanStack Start migration (#46424). Stacked on
**#47112** (S3).
> [!NOTE]
> Mechanical and homogeneous — every file is the same shape: a
`createFileRoute(...)` whose `server.handlers` delegate to the existing
`pages/api` handler via `toWebHandler` (the compat shim from S2). The
pages-router handlers are unchanged; Next still serves them directly and
ignores `routes/`.
## What's in this PR
- `routes/api/**` (~104 files): platform (`pg-meta`, auth, storage,
integrations, profile, telemetry, organizations, projects…), `ai/*`,
`v1/*`, `connect`, `content`/`mcp`, and standalone endpoints
(`deployment-mode`, `get-ip-address`, etc.).
- `routeTree.gen.ts` — **regenerated** for the routes present so far
(root + auth/app + api).
## Review tip
The route files are near-identical wrappers, so this is fast to skim.
The generated `routeTree.gen.ts` isn't meaningful review surface.
## Verification
On top of S1–S3: `studio` typecheck ✓, lint (0 errors) ✓.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added Model Context Protocol (MCP) API endpoint with configurable
feature support and read-only mode
* Added function artifact streaming capability for self-hosted functions
* **Chores**
* Migrated API route infrastructure for improved system architecture
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>