mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Problem Self-hosted Studio still uses `@supabase/mcp-server-supabase` 0.17.0, two releases behind even before [0.20.0](https://github.com/supabase/mcp/releases/tag/mcp-server-supabase-v0.20.0). ## Solution Bump `^0.17.0` to `^0.20.0` in `apps/studio/package.json` and update the lockfile, which takes `@supabase/mcp-utils` from 0.10.0 to 0.11.1. The platform interface only gained optional arguments, so the bump itself compiles without changes. - The debugging group gains `get_issues`, which covers security and performance checks on self-hosted. It reports the health check as unavailable because self-hosted has no health advisors. - `generate_typescript_types` now accepts an optional `schemas` parameter. Without this fix, self-hosted would silently ignore it and could return types for different schemas than the caller requested. `lib/api/self-hosted/mcp.ts` now passes `schemas` to `generateTypescriptTypes` in `lib/api/self-hosted/generate-types.ts`, which sends it to pg-meta as `included_schemas`. When the caller omits `schemas`, self-hosted still uses the schemas PostgREST exposes. Tests in `generate-types.test.ts` and `mcp.test.ts` cover both cases. The MCP Apps support from 0.18.0 and 0.19.0 stays off because Studio doesn’t opt in. ## Review instructions 1. Confirm CI passes. 2. With self-hosted Studio running, connect an MCP client to `/api/mcp` and confirm `tools/list` includes `get_issues`. 3. Call `generate_typescript_types` with `schemas: ["auth"]` (or another schema PostgREST doesn’t expose) and confirm it returns types only for the requested schema. Closes [AI-1582](https://linear.app/supabase/issue/AI-1582)
215 lines
7.3 KiB
TypeScript
215 lines
7.3 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { getDebuggingOperations, getDevelopmentOperations, getFeedbackOperations } from './mcp'
|
|
|
|
vi.mock('./settings', () => ({
|
|
getProjectSettings: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('./generate-types', () => ({
|
|
generateTypescriptTypes: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('./logs', () => ({
|
|
retrieveAnalyticsData: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('./lints', () => ({
|
|
getLints: vi.fn(),
|
|
}))
|
|
|
|
describe('api/self-hosted/mcp', () => {
|
|
describe('getDevelopmentOperations.getPublishableKeys', () => {
|
|
let getProjectSettingsMock: ReturnType<typeof vi.fn>
|
|
|
|
beforeEach(async () => {
|
|
vi.unstubAllEnvs()
|
|
const settings = await import('./settings')
|
|
getProjectSettingsMock = vi.mocked(settings.getProjectSettings)
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs()
|
|
})
|
|
|
|
it('returns a publishable-typed key from SUPABASE_PUBLISHABLE_KEY when set', async () => {
|
|
vi.stubEnv('SUPABASE_PUBLISHABLE_KEY', 'sb_publishable_abc')
|
|
|
|
const ops = getDevelopmentOperations({})
|
|
const keys = await ops.getPublishableKeys('default')
|
|
|
|
expect(keys).toEqual([
|
|
{
|
|
api_key: 'sb_publishable_abc',
|
|
name: 'publishable',
|
|
type: 'publishable',
|
|
},
|
|
])
|
|
// When the env var is set we should short-circuit and never consult project settings.
|
|
expect(getProjectSettingsMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('falls back to the anon key from project settings with type "legacy" when env var is unset', async () => {
|
|
vi.stubEnv('SUPABASE_PUBLISHABLE_KEY', '')
|
|
getProjectSettingsMock.mockReturnValue({
|
|
service_api_keys: [
|
|
{ api_key: 'service-key-value', name: 'service_role key', tags: 'service_role' },
|
|
{ api_key: 'anon-key-value', name: 'anon key', tags: 'anon' },
|
|
],
|
|
})
|
|
|
|
const ops = getDevelopmentOperations({})
|
|
const keys = await ops.getPublishableKeys('default')
|
|
|
|
expect(keys).toEqual([
|
|
{
|
|
api_key: 'anon-key-value',
|
|
name: 'anon key',
|
|
type: 'legacy',
|
|
},
|
|
])
|
|
})
|
|
|
|
it('throws when env var is unset and the anon key is missing from project settings', async () => {
|
|
vi.stubEnv('SUPABASE_PUBLISHABLE_KEY', '')
|
|
getProjectSettingsMock.mockReturnValue({
|
|
service_api_keys: [
|
|
{ api_key: 'service-key-value', name: 'service_role key', tags: 'service_role' },
|
|
],
|
|
})
|
|
|
|
const ops = getDevelopmentOperations({})
|
|
|
|
await expect(ops.getPublishableKeys('default')).rejects.toThrow(
|
|
'Anon key not found in project settings'
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('getDevelopmentOperations.generateTypescriptTypes', () => {
|
|
let generateTypescriptTypesMock: ReturnType<typeof vi.fn>
|
|
const headers = { Authorization: 'Bearer token' }
|
|
|
|
beforeEach(async () => {
|
|
const generateTypes = await import('./generate-types')
|
|
generateTypescriptTypesMock = vi.mocked(generateTypes.generateTypescriptTypes)
|
|
generateTypescriptTypesMock.mockReset()
|
|
generateTypescriptTypesMock.mockResolvedValue({ types: 'export type T = {}' })
|
|
})
|
|
|
|
it('forwards the requested schemas and the headers to the helper', async () => {
|
|
const ops = getDevelopmentOperations({ headers })
|
|
const result = await ops.generateTypescriptTypes('default', {
|
|
schemas: ['public', 'private'],
|
|
})
|
|
|
|
expect(result).toEqual({ types: 'export type T = {}' })
|
|
expect(generateTypescriptTypesMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({ headers, schemas: ['public', 'private'] })
|
|
)
|
|
})
|
|
|
|
it('calls the helper with schemas undefined when no options are given', async () => {
|
|
const ops = getDevelopmentOperations({ headers })
|
|
const result = await ops.generateTypescriptTypes('default')
|
|
|
|
expect(result).toEqual({ types: 'export type T = {}' })
|
|
const [options] = generateTypescriptTypesMock.mock.lastCall!
|
|
expect(options.headers).toBe(headers)
|
|
expect(options.schemas).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('getDebuggingOperations', () => {
|
|
let retrieveAnalyticsDataMock: ReturnType<typeof vi.fn>
|
|
|
|
beforeEach(async () => {
|
|
vi.unstubAllEnvs()
|
|
const logs = await import('./logs')
|
|
retrieveAnalyticsDataMock = vi.mocked(logs.retrieveAnalyticsData)
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs()
|
|
})
|
|
|
|
it('getLogs is not supported on self-hosted (query_logs supersedes it)', async () => {
|
|
const ops = getDebuggingOperations({})
|
|
|
|
// `get_logs` is hidden by the MCP server whenever `queryLogs` is present,
|
|
// so this method is unreachable over MCP and throws defensively.
|
|
await expect(ops.getLogs('default', { service: 'api' })).rejects.toThrow(
|
|
'get_logs is not supported on self-hosted'
|
|
)
|
|
expect(retrieveAnalyticsDataMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('queryLogs throws when logs are disabled (self-hosted default)', async () => {
|
|
vi.stubEnv('ENABLED_FEATURES_LOGS_ALL', 'false')
|
|
|
|
const ops = getDebuggingOperations({})
|
|
|
|
await expect(ops.queryLogs!('default', { sql: 'select 1' })).rejects.toThrow(
|
|
'Logs are disabled on this instance'
|
|
)
|
|
expect(retrieveAnalyticsDataMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('queryLogs passes the model-provided SQL straight through when logs are enabled', async () => {
|
|
vi.stubEnv('ENABLED_FEATURES_LOGS_ALL', 'true')
|
|
retrieveAnalyticsDataMock.mockResolvedValue({ data: ['log entry'], error: null })
|
|
|
|
const ops = getDebuggingOperations({})
|
|
await ops.queryLogs!('default', { sql: 'select * from edge_logs' })
|
|
|
|
expect(retrieveAnalyticsDataMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
params: expect.objectContaining({ sql: 'select * from edge_logs' }),
|
|
})
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('getFeedbackOperations', () => {
|
|
const DELETE_TOKEN = '6f1f2b9e-3c4d-4a5b-8c7d-9e0f1a2b3c4d'
|
|
|
|
it('submits feedback without the self-hosted project ref', async () => {
|
|
const fetch = vi.fn().mockResolvedValue(Response.json(DELETE_TOKEN))
|
|
|
|
const result = await getFeedbackOperations({ fetch }).submit({
|
|
feedback: 'list_tables is slow',
|
|
projectId: 'default',
|
|
clientInfo: { name: 'test-client', version: '1.0.0' },
|
|
})
|
|
|
|
expect(result).toEqual({ deleteToken: DELETE_TOKEN })
|
|
const [url, init] = fetch.mock.calls[0]
|
|
expect(url).toMatch(/\/rest\/v1\/rpc\/submit_interfaces_feedback$/)
|
|
const body = JSON.parse(init.body)
|
|
expect(body).toMatchObject({
|
|
feedback: 'list_tables is slow',
|
|
metadata: { source: 'mcp', client_name: 'test-client', client_version: '1.0.0' },
|
|
})
|
|
expect(body.project_ref).toBeUndefined()
|
|
expect(body.user_id).toBeUndefined()
|
|
})
|
|
|
|
it('deletes feedback by token without the self-hosted project ref', async () => {
|
|
const fetch = vi
|
|
.fn()
|
|
.mockResolvedValue(new Response(null, { headers: { 'Content-Range': '*/1' } }))
|
|
|
|
const result = await getFeedbackOperations({ fetch }).delete({
|
|
deleteToken: DELETE_TOKEN,
|
|
projectId: 'default',
|
|
})
|
|
|
|
expect(result).toEqual({ deleted: true })
|
|
const [, init] = fetch.mock.calls[0]
|
|
expect(init.method).toBe('DELETE')
|
|
expect(init.headers['x-feedback-token']).toBe(DELETE_TOKEN)
|
|
expect(init.headers).not.toHaveProperty('x-feedback-project-ref')
|
|
})
|
|
})
|
|
})
|