Skip to content

Commit 143e5d6

Browse files
authored
feat: get_logs platform-specific sql and 24 hour logs (#155)
* feat: support platform-specific sql in `getLogs` * feat: open `get_logs` to the past 24 hours * fix: specify `iso_timestamp_end` Specifying both start and end timestamps is needed to bypass 1-minute default
1 parent 5417461 commit 143e5d6

File tree

4 files changed

+29
-37
lines changed

4 files changed

+29
-37
lines changed

packages/mcp-server-supabase/src/logs.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import { stripIndent } from 'common-tags';
2+
import type { LogsService } from './platform/types.js';
23

3-
export function getLogQuery(
4-
service:
5-
| 'api'
6-
| 'branch-action'
7-
| 'postgres'
8-
| 'edge-function'
9-
| 'auth'
10-
| 'storage'
11-
| 'realtime',
12-
limit: number = 100
13-
) {
4+
export function getLogQuery(service: LogsService, limit: number = 100) {
145
switch (service) {
156
case 'api':
167
return stripIndent`

packages/mcp-server-supabase/src/platform/api-platform.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import {
33
parseMultipartStream,
44
} from '@mjackson/multipart-parser';
55
import type { InitData } from '@supabase/mcp-utils';
6-
import { relative } from 'node:path/posix';
76
import { fileURLToPath } from 'node:url';
87
import packageJson from '../../package.json' with { type: 'json' };
98
import { getDeploymentId, normalizeFilename } from '../edge-function.js';
9+
import { getLogQuery } from '../logs.js';
1010
import {
1111
assertSuccess,
1212
createManagementApiClient,
@@ -232,9 +232,11 @@ export function createSupabaseApiPlatform(
232232

233233
const debugging: DebuggingOperations = {
234234
async getLogs(projectId: string, options: GetLogsOptions) {
235-
const { sql, iso_timestamp_start, iso_timestamp_end } =
235+
const { service, iso_timestamp_start, iso_timestamp_end } =
236236
getLogsOptionsSchema.parse(options);
237237

238+
const sql = getLogQuery(service);
239+
238240
const response = await managementApiClient.GET(
239241
'/v1/projects/{ref}/analytics/endpoints/logs.all',
240242
{

packages/mcp-server-supabase/src/platform/types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,18 @@ export const migrationSchema = z.object({
123123
name: z.string().optional(),
124124
});
125125

126+
export const logsServiceSchema = z.enum([
127+
'api',
128+
'branch-action',
129+
'postgres',
130+
'edge-function',
131+
'auth',
132+
'storage',
133+
'realtime',
134+
]);
135+
126136
export const getLogsOptionsSchema = z.object({
127-
sql: z.string(),
137+
service: logsServiceSchema,
128138
iso_timestamp_start: z.string().optional(),
129139
iso_timestamp_end: z.string().optional(),
130140
});
@@ -151,6 +161,7 @@ export type ApplyMigrationOptions = z.infer<typeof applyMigrationOptionsSchema>;
151161
export type Migration = z.infer<typeof migrationSchema>;
152162
export type ListMigrationsResult = z.infer<typeof migrationSchema>;
153163

164+
export type LogsService = z.infer<typeof logsServiceSchema>;
154165
export type GetLogsOptions = z.infer<typeof getLogsOptionsSchema>;
155166
export type GenerateTypescriptTypesResult = z.infer<
156167
typeof generateTypescriptTypesResultSchema

packages/mcp-server-supabase/src/tools/debugging-tools.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { z } from 'zod';
2-
import { getLogQuery } from '../logs.js';
3-
import type { DebuggingOperations } from '../platform/types.js';
2+
import {
3+
logsServiceSchema,
4+
type DebuggingOperations,
5+
} from '../platform/types.js';
46
import { injectableTool } from './util.js';
57

68
export type DebuggingToolsOptions = {
@@ -17,7 +19,7 @@ export function getDebuggingTools({
1719
return {
1820
get_logs: injectableTool({
1921
description:
20-
'Gets logs for a Supabase project by service type. Use this to help debug problems with your app. This will only return logs within the last minute. If the logs you are looking for are older than 1 minute, re-run your test to reproduce them.',
22+
'Gets logs for a Supabase project by service type. Use this to help debug problems with your app. This will return logs within the last 24 hours.',
2123
annotations: {
2224
title: 'Get project logs',
2325
readOnlyHint: true,
@@ -27,31 +29,17 @@ export function getDebuggingTools({
2729
},
2830
parameters: z.object({
2931
project_id: z.string(),
30-
service: z
31-
.enum([
32-
'api',
33-
'branch-action',
34-
'postgres',
35-
'edge-function',
36-
'auth',
37-
'storage',
38-
'realtime',
39-
])
40-
.describe('The service to fetch logs for'),
32+
service: logsServiceSchema.describe('The service to fetch logs for'),
4133
}),
4234
inject: { project_id },
4335
execute: async ({ project_id, service }) => {
44-
// Omitting start and end time defaults to the last minute.
45-
// But since branch actions are async, we need to wait longer
46-
// for jobs to be scheduled and run to completion.
47-
const startTimestamp =
48-
service === 'branch-action'
49-
? new Date(Date.now() - 5 * 60 * 1000)
50-
: undefined;
36+
const startTimestamp = new Date(Date.now() - 24 * 60 * 60 * 1000); // Last 24 hours
37+
const endTimestamp = new Date();
5138

5239
return debugging.getLogs(project_id, {
53-
sql: getLogQuery(service),
54-
iso_timestamp_start: startTimestamp?.toISOString(),
40+
service,
41+
iso_timestamp_start: startTimestamp.toISOString(),
42+
iso_timestamp_end: endTimestamp.toISOString(),
5543
});
5644
},
5745
}),

0 commit comments

Comments
 (0)