-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathaccount-tools.ts
More file actions
420 lines (389 loc) · 12.6 KB
/
Copy pathaccount-tools.ts
File metadata and controls
420 lines (389 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import {
inputRequired,
type RequestStateCodec,
type ServerContext,
} from '@modelcontextprotocol/server';
import { tool } from '@supabase/mcp-utils';
import { z } from 'zod/v4';
import type { ToolDefs } from './util.js';
import {
actionOnlyElicitationSchema,
checkConfirmationState,
isFormCapable,
projectCostStateSchema,
type ConfirmationState,
} from './confirmation.js';
import type { AccountOperations } from '../platform/types.js';
import { organizationSchema, projectSchema } from '../platform/types.js';
import { getBranchCost, getNextProjectCost } from '../pricing.js';
import { AWS_REGION_CODES } from '../regions.js';
import { hashObject } from '../util.js';
type AccountToolsOptions = {
account: AccountOperations;
readOnly?: boolean;
/**
* Enables confirmation via elicitation inside `create_project` for clients
* that declare per-request form capability (see `isFormCapable`). Absent,
* `create_project` keeps requiring `confirm_cost_id` from `confirm_cost`
* unchanged.
*/
confirmation?: {
codec: RequestStateCodec<ConfirmationState>;
};
};
const listOrganizationsInputSchema = z.object({});
const listOrganizationsOutputSchema = z.object({
organizations: z.array(
z.object({
id: z.string(),
slug: z.string(),
name: z.string(),
})
),
});
const getOrganizationInputSchema = z.object({
id: z.string().describe('The organization ID'),
});
const getOrganizationOutputSchema = organizationSchema;
const listProjectsInputSchema = z.object({});
const listProjectsOutputSchema = z.object({
projects: z.array(projectSchema),
});
const getProjectInputSchema = z.object({
id: z.string().describe('The project ID'),
});
const getProjectOutputSchema = projectSchema;
const getCostInputSchema = z.object({
type: z.enum(['project', 'branch']),
organization_id: z
.string()
.describe('The organization ID. Always ask the user.'),
});
const getCostOutputSchema = z.object({
type: z.enum(['project', 'branch']),
amount: z.number().describe('Cost in USD'),
recurrence: z.enum(['hourly', 'monthly']),
});
const confirmCostInputSchema = z.object({
type: z.enum(['project', 'branch']),
recurrence: z.enum(['hourly', 'monthly']),
amount: z.number(),
});
const confirmCostOutputSchema = z.object({
confirmation_id: z.string(),
});
const createProjectInputSchema = z.object({
name: z.string().describe('The name of the project'),
region: z
.enum(AWS_REGION_CODES)
.describe('The region to create the project in.'),
organization_id: z.string(),
confirm_cost_id: z
.string({
error: (issue) =>
issue.input === undefined
? 'User must confirm understanding of costs before creating a project.'
: undefined,
})
.describe('The cost confirmation ID. Call `confirm_cost` first.'),
});
const createProjectOutputSchema = projectSchema;
const createProjectInputSchemaWithElicitation = createProjectInputSchema.extend(
{
confirm_cost_id: z
.string()
.optional()
.describe(
'The cost confirmation ID. Only required for clients without per-request form-elicitation capability; those clients must call `confirm_cost` first. Form-capable clients are asked to confirm the cost inline when creating the project.'
),
}
);
const pauseProjectInputSchema = z.object({
project_id: z.string(),
});
const pauseProjectOutputSchema = z.object({
success: z.boolean(),
});
const restoreProjectInputSchema = z.object({
project_id: z.string(),
});
const restoreProjectOutputSchema = z.object({
success: z.boolean(),
});
export const accountToolDefs = {
list_organizations: {
description: 'Lists all organizations that the user is a member of.',
parameters: listOrganizationsInputSchema,
outputSchema: listOrganizationsOutputSchema,
annotations: {
title: 'List organizations',
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
},
get_organization: {
description:
'Gets details for an organization. Includes subscription plan.',
parameters: getOrganizationInputSchema,
outputSchema: getOrganizationOutputSchema,
annotations: {
title: 'Get organization details',
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
},
list_projects: {
description:
'Lists all Supabase projects for the user. Use this to help discover the project ID of the project that the user is working on.',
parameters: listProjectsInputSchema,
outputSchema: listProjectsOutputSchema,
annotations: {
title: 'List projects',
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
},
get_project: {
description: 'Gets details for a Supabase project.',
parameters: getProjectInputSchema,
outputSchema: getProjectOutputSchema,
annotations: {
title: 'Get project details',
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
},
get_cost: {
description:
'Gets the cost of creating a new project or branch. Never assume organization as costs can be different for each. Always repeat the cost to the user and confirm their understanding before proceeding.',
parameters: getCostInputSchema,
outputSchema: getCostOutputSchema,
annotations: {
title: 'Get cost of new resources',
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
},
confirm_cost: {
description:
'Ask the user to confirm their understanding of the cost of creating a new project or branch. Call `get_cost` first. Returns a unique ID for this confirmation which should be passed to `create_project` or `create_branch`.',
parameters: confirmCostInputSchema,
outputSchema: confirmCostOutputSchema,
annotations: {
title: 'Confirm cost understanding',
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
},
create_project: {
description:
'Creates a new Supabase project. Always ask the user which organization to create the project in. The project can take a few minutes to initialize - use `get_project` to check the status.',
parameters: createProjectInputSchema,
outputSchema: createProjectOutputSchema,
annotations: {
title: 'Create project',
readOnlyHint: false,
destructiveHint: false,
idempotentHint: false,
openWorldHint: false,
},
},
pause_project: {
description: 'Pauses a Supabase project.',
parameters: pauseProjectInputSchema,
outputSchema: pauseProjectOutputSchema,
annotations: {
title: 'Pause project',
readOnlyHint: false,
destructiveHint: false,
idempotentHint: false,
openWorldHint: false,
},
},
restore_project: {
description: 'Restores a Supabase project.',
parameters: restoreProjectInputSchema,
outputSchema: restoreProjectOutputSchema,
annotations: {
title: 'Restore project',
readOnlyHint: false,
destructiveHint: false,
idempotentHint: false,
openWorldHint: false,
},
},
} as const satisfies ToolDefs;
export function getAccountTools({
account,
readOnly,
confirmation,
}: AccountToolsOptions) {
return {
list_organizations: tool({
...accountToolDefs.list_organizations,
execute: async () => {
return { organizations: await account.listOrganizations() };
},
}),
get_organization: tool({
...accountToolDefs.get_organization,
execute: async ({ id: organizationId }) => {
return await account.getOrganization(organizationId);
},
}),
list_projects: tool({
...accountToolDefs.list_projects,
execute: async () => {
return { projects: await account.listProjects() };
},
}),
get_project: tool({
...accountToolDefs.get_project,
execute: async ({ id }) => {
return await account.getProject(id);
},
}),
get_cost: tool({
...accountToolDefs.get_cost,
execute: async ({ type, organization_id }) => {
switch (type) {
case 'project':
return await getNextProjectCost(account, organization_id);
case 'branch':
return getBranchCost();
default:
throw new Error(`Unknown cost type: ${type}`);
}
},
}),
confirm_cost: tool({
...accountToolDefs.confirm_cost,
execute: async (cost) => {
return { confirmation_id: await hashObject(cost) };
},
}),
create_project: tool({
...accountToolDefs.create_project,
parameters: confirmation
? createProjectInputSchemaWithElicitation
: createProjectInputSchema,
execute: async (
{
name,
region,
organization_id,
confirm_cost_id,
}: z.infer<typeof createProjectInputSchemaWithElicitation>,
ctx: ServerContext
) => {
if (readOnly) {
throw new Error('Cannot create a project in read-only mode.');
}
if (confirmation && isFormCapable(ctx)) {
const { codec } = confirmation;
const cost = await getNextProjectCost(account, organization_id);
const state = ctx.mcpReq.requestState<unknown>();
if (!state && cost.amount === 0) {
return await account.createProject({
name,
region,
organization_id,
});
}
const costSuffix = cost.recurrence === 'monthly' ? '/month' : '/hr';
const askForConfirmation = async () =>
inputRequired({
inputRequests: {
confirm_cost: inputRequired.elicit({
mode: 'form',
message: [
`Project: $${cost.amount}${costSuffix} until deleted.`,
'Billed hourly while running; paused projects are not billed.',
'Standard rate, before plan allowances or exemptions.',
].join('\n'),
requestedSchema: actionOnlyElicitationSchema,
}),
},
requestState: await codec.mint(
{ tool: 'create_project', name, region, organization_id, cost },
ctx
),
});
const confirmationState = await checkConfirmationState({
ctx,
tool: 'create_project',
schema: projectCostStateSchema,
requestKey: 'confirm_cost',
askForConfirmation,
argsMatch: (state) =>
state.name === name &&
state.region === region &&
state.organization_id === organization_id,
payloadMatch: (state) =>
cost.amount === 0 ||
(state.cost.type === cost.type &&
state.cost.recurrence === cost.recurrence &&
state.cost.amount === cost.amount),
declinedText: 'Project creation was declined.',
cancelledText: 'Project creation was cancelled.',
});
switch (confirmationState.kind) {
case 'reprompt':
case 'terminal':
return confirmationState.result;
case 'proceed':
return await account.createProject({
name: confirmationState.state.name,
region: confirmationState.state.region,
organization_id: confirmationState.state.organization_id,
});
}
}
const cost = await getNextProjectCost(account, organization_id);
const costHash = await hashObject(cost);
if (costHash !== confirm_cost_id) {
throw new Error(
'Cost confirmation ID does not match the expected cost of creating a project.'
);
}
return await account.createProject({
name,
region,
organization_id,
});
},
}),
pause_project: tool({
...accountToolDefs.pause_project,
execute: async ({ project_id }) => {
if (readOnly) {
throw new Error('Cannot pause a project in read-only mode.');
}
await account.pauseProject(project_id);
return { success: true };
},
}),
restore_project: tool({
...accountToolDefs.restore_project,
execute: async ({ project_id }) => {
if (readOnly) {
throw new Error('Cannot restore a project in read-only mode.');
}
await account.restoreProject(project_id);
return { success: true };
},
}),
};
}