Files
supabase/apps/studio/lib/api/self-hosted/mcp.test.ts
Pedro Rodrigues 47595f8ac7 feat(self-hosted): implement queryLogs for the MCP debugging tools (#48900)
> [!IMPORTANT]  
>
> Only merge this when (https://github.com/supabase/platform/pull/36804)
is merged, as the AI assistant will not have access to the `query_logs`
tool for the remote MCP server

## 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 (self-hosted / CLI Studio MCP server).

## What is the current behavior?

Self-hosted `getDebuggingOperations`
(`apps/studio/lib/api/self-hosted/mcp.ts`) implements only `getLogs`, so
the MCP `debugging` group exposes `get_logs` — a fixed per-service log
dump built by `getLogQuery`. Logs are served by Logflare, which speaks
BigQuery SQL.

## What is the new behavior?

Bumps `@supabase/mcp-server-supabase` to `^0.10.0` (adds `query_logs` +
`logsDialect`, and hides `get_logs` wherever a platform declares
`queryLogs`) and moves logs over to it.

- **Self-hosted `query_logs`:** declares `logsDialect: 'bigquery'` and
implements `queryLogs`, passing the model's SQL straight through to the
same Logflare `logs.all` endpoint (arbitrary `sql` param) — no new
endpoint, no dialect translation.
- **Drops `get_logs` from self-hosted:** `getLogs` throws (the server
hides it once `queryLogs` exists) and the per-service `getLogQuery`
builder is deleted; the model now writes its own BigQuery SQL, guided by
the dialect schema hint.
- **Honors no-logs mode:** `query_logs` throws when `logs:all` is
disabled — the self-hosted default, enabled via the
`docker-compose.logs.yml` override.
- **Assistant:** switches the dashboard assistant from `get_logs` to
`query_logs` (allowlist, drift guard, prompt, mocks, evals).

Refs AI-1046


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
  * AI debugging can query recent project logs using read-only SQL.
* Log queries support optional time-range filters, filtering,
aggregation, and joins.
* Self-hosted debugging checks whether logging is enabled before running
queries.

* **Bug Fixes**
* Updated debugging workflows and validation to consistently use the new
log-query capability.
* Removed reliance on legacy service-specific log filtering and query
behavior.

* **Documentation**
* Updated MCP debugging tool guidance to describe SQL-based log queries.

* **Tests**
* Expanded coverage for enabled, disabled, and unsupported logging
scenarios.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-12 13:11:23 +01:00

141 lines
4.4 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { getDebuggingOperations, getDevelopmentOperations } 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.clearAllMocks()
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('getDebuggingOperations', () => {
let retrieveAnalyticsDataMock: ReturnType<typeof vi.fn>
beforeEach(async () => {
vi.clearAllMocks()
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' }),
})
)
})
})
})