mirror of
https://github.com/supabase/supabase.git
synced 2026-10-09 11:25:06 +03:00
## I have read the [CONTRIBUTING.md](<https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md>) file. YES ## What kind of change does this PR introduce? Feature / refactor. ## What is the current behavior? The dashboard assistant runs `@supabase/mcp-server-supabase` in-process over an in-memory transport (`lib/ai/supabase-mcp.ts`). ## What is the new behavior? The assistant connects to the **remote MCP server** over HTTP (`@ai-sdk/mcp`), forwarding the dashboard session token as a bearer. URL comes from `NEXT_PUBLIC_MCP_URL` with a local-dev fallback; platform-only, and Nimbus works via the same env var. * **Tool model unchanged:** UI-controlled `execute_sql` (with `needsApproval`) and `deploy_edge_function` still come from Studio; the allowlist (`TOOL_CATEGORY_MAP`) remains the gate keeping the remote's write tools away from the assistant (`read_only` is defense-in-depth). * **Attribution:** sends `x-source-name: supabase-studio` (+ `x-source-version`) → logged as `source_name`/`client_name`. * **Connection lifecycle:** the HTTP client is closed via the request's `AbortSignal` (tools execute later during streaming); `signal` is required on `getTools`/`getMcpTools`. * **Resilience:** a remote-MCP failure degrades to the remaining tools instead of failing the assistant. * **Drift protection:** relied-upon tools are typed against `keyof typeof supabaseMcpToolSchemas`, so a package bump that renames/removes one fails `pnpm typecheck`; a runtime check also warns if the deployed server returns fewer tools. * Adds unit tests for the above. ## Additional context * Verified end-to-end against a local remote MCP server with a dashboard token: `initialize` 200, tools listed, a tool executed, client closed cleanly. * The remote MCP (mgmt-api) already accepts dashboard session tokens (GoTrue-JWT auth path) — no backend change needed. `NEXT_PUBLIC_MCP_URL` must point at each env's `/mcp`. * `@supabase/mcp-server-supabase` is kept — still used by the self-hosted `/api/mcp` routes. Closes [AI-137](https://linear.app/supabase/issue/AI-137/switch-dashboard-assistant-to-remote-mcp) ## Rollout * **Rollout:** merges with `USE_REMOTE_MCP` off (in-process); flip it to `true` per environment (staging → prod → Nimbus) once each one's prerequisites land. * **Rollback:** unset `USE_REMOTE_MCP` and redeploy to fall back to the in-process client — no revert needed. ## Summary by CodeRabbit * **Bug Fixes** * Improved AI request handling so tool loading and generation clean up properly when a request is cancelled or the browser connection closes. * Added safer fallback behavior when remote tool loading fails, so AI features can continue with available tools instead of stopping entirely. * Updated remote tool access to use the current project reference and preserve the correct access headers. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * AI tools now connect more reliably to remote services and stop cleanly when requests end or are canceled. * Tool loading is more resilient, continuing with available tools if remote access is unavailable. * **Bug Fixes** * Improved cleanup to prevent lingering connections during SQL generation and policy workflows. * Added safer handling for remote tool changes and invalid responses. * **Tests** * Expanded automated coverage for remote tool setup, cancellation, and fallback behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
133 lines
4.9 KiB
TypeScript
133 lines
4.9 KiB
TypeScript
import { createMCPClient } from '@ai-sdk/mcp'
|
|
|
|
/**
|
|
* Default MCP server URL used when `NEXT_PUBLIC_MCP_URL` is not configured (local
|
|
* development). Mirrors `DEFAULT_MCP_URL_PLATFORM` in `ui-patterns/McpUrlBuilder`
|
|
* so the assistant resolves the same endpoint as the Connect sheet. It's
|
|
* duplicated here (rather than imported) to keep
|
|
* `ui-patterns/McpUrlBuilder/constants` — which pulls in `next/image` and image
|
|
* assets — out of this server-side bundle.
|
|
*/
|
|
const DEFAULT_MCP_URL = 'http://localhost:8080/mcp'
|
|
|
|
/**
|
|
* Identifies assistant traffic to the remote MCP server. Sent both as the MCP
|
|
* client name (logged as `client_name`) and via the `x-source-name` header
|
|
* (logged as `source_name`) by the mgmt-api McpLogger, so assistant requests are
|
|
* attributable in the MCP server's logs.
|
|
*/
|
|
const SOURCE_NAME = 'supabase-studio'
|
|
|
|
/**
|
|
* Builds the remote MCP endpoint URL for the dashboard assistant.
|
|
*
|
|
* Points at the remote MCP server configured via `NEXT_PUBLIC_MCP_URL` (e.g.
|
|
* https://mcp.supabase.com/mcp), falling back to a local default for development.
|
|
* The query parameters (`project_ref`, `read_only`) mirror `getMcpUrl` in
|
|
* `ui-patterns/McpUrlBuilder/utils/getMcpUrl` so the assistant and the Connect sheet stay in
|
|
* sync. The assistant only performs read operations, so `read_only` is always
|
|
* set.
|
|
*
|
|
* Note: the assistant only talks to the remote MCP server on the hosted platform
|
|
* (see `getTools` / `getMcpTools`), so no self-hosted branch is needed here.
|
|
*/
|
|
function getRemoteMcpUrl(projectRef: string) {
|
|
// `||` (not `??`) so an empty-string env var falls back instead of producing
|
|
// an invalid `new URL('')`.
|
|
const url = new URL(process.env.NEXT_PUBLIC_MCP_URL || DEFAULT_MCP_URL)
|
|
if (projectRef) {
|
|
url.searchParams.set('project_ref', projectRef)
|
|
}
|
|
url.searchParams.set('read_only', 'true')
|
|
|
|
return url.toString()
|
|
}
|
|
|
|
/**
|
|
* Creates an MCP client connected to the remote Supabase MCP server over HTTP.
|
|
*
|
|
* Previously the assistant instantiated the MCP server in-process and connected
|
|
* to it via an in-memory transport. It now connects to the remote MCP server so
|
|
* the dashboard assistant shares the same MCP surface as external clients.
|
|
*
|
|
* The dashboard session `accessToken` is forwarded as a bearer token. The remote
|
|
* MCP server is responsible for validating it and scoping access to the project.
|
|
*/
|
|
export async function createSupabaseMCPClient({
|
|
accessToken,
|
|
projectRef,
|
|
}: {
|
|
accessToken: string
|
|
projectRef: string
|
|
}) {
|
|
// Identifies the deployed build in the MCP server's `source_version` log field.
|
|
const sourceVersion = process.env.VERCEL_GIT_COMMIT_SHA
|
|
|
|
const client = await createMCPClient({
|
|
name: SOURCE_NAME,
|
|
transport: {
|
|
type: 'http',
|
|
url: getRemoteMcpUrl(projectRef),
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
// Identify assistant traffic in the remote MCP server's logs
|
|
'x-source-name': SOURCE_NAME,
|
|
...(sourceVersion ? { 'x-source-version': sourceVersion } : {}),
|
|
},
|
|
},
|
|
})
|
|
|
|
return client
|
|
}
|
|
|
|
/**
|
|
* Legacy in-process MCP client — the pre-migration behavior, kept as a fallback
|
|
* behind the `USE_REMOTE_MCP` gate (see `tools/mcp-tools.ts`).
|
|
*
|
|
* Instantiates `@supabase/mcp-server-supabase` in-process and connects to it over
|
|
* an in-memory transport. The heavy server package is imported dynamically so it
|
|
* is code-split into its own chunk and stays out of the (default, post-migration)
|
|
* remote path's bundle.
|
|
*
|
|
* TODO(AI-897): remove in process mcp — delete this once every environment has
|
|
* been flipped to the remote MCP server and has been stable. Tracked alongside
|
|
* the `USE_REMOTE_MCP` rollout.
|
|
*/
|
|
export async function createInProcessSupabaseMCPClient({
|
|
accessToken,
|
|
projectRef,
|
|
}: {
|
|
accessToken: string
|
|
projectRef: string
|
|
}) {
|
|
// Dynamic imports keep the in-process server + its transport out of the remote
|
|
// path's bundle (loaded only when this fallback is actually taken).
|
|
// `.js` is required for esbuild ESM resolution.
|
|
const { InMemoryTransport } = await import('@modelcontextprotocol/sdk/inMemory.js')
|
|
const { createSupabaseMcpServer } = await import('@supabase/mcp-server-supabase')
|
|
const { createSupabaseApiPlatform } = await import('@supabase/mcp-server-supabase/platform/api')
|
|
const { API_URL } = await import('@/lib/constants')
|
|
|
|
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair()
|
|
|
|
// Instantiate the MCP server and connect to its transport
|
|
const apiUrl = API_URL?.replace('/platform', '')
|
|
const server = createSupabaseMcpServer({
|
|
platform: createSupabaseApiPlatform({
|
|
accessToken,
|
|
apiUrl,
|
|
}),
|
|
contentApiUrl: process.env.NEXT_PUBLIC_CONTENT_API_URL,
|
|
projectId: projectRef,
|
|
readOnly: true,
|
|
})
|
|
await server.connect(serverTransport)
|
|
|
|
const client = await createMCPClient({
|
|
name: SOURCE_NAME,
|
|
transport: clientTransport,
|
|
})
|
|
|
|
return client
|
|
}
|