-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
697 lines (653 loc) · 20.1 KB
/
Copy pathindex.js
File metadata and controls
697 lines (653 loc) · 20.1 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { exec } from 'child_process';
import { promisify } from 'util';
import { readFile, writeFile, access, mkdir } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
const execAsync = promisify(exec);
const __dirname = dirname(fileURLToPath(import.meta.url));
class ClaspEnhancedServer {
constructor() {
this.server = new Server(
{
name: 'clasp-enhanced',
version: '1.0.0',
},
{
capabilities: {
tools: {},
},
}
);
}
async execClaspCommand(command, options = {}) {
try {
const { stdout, stderr } = await execAsync(command, {
cwd: options.cwd || process.cwd(),
env: { ...process.env, ...options.env },
});
if (stderr && !stderr.includes('Warning')) {
throw new Error(stderr);
}
return stdout.trim();
} catch (error) {
throw new Error(`Clasp command failed: ${error.message}`);
}
}
setupToolHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: 'clasp_login',
description: 'Login to Google account for clasp',
inputSchema: {
type: 'object',
properties: {
creds: {
type: 'string',
description: 'Optional path to credentials file',
},
global: {
type: 'boolean',
description: 'Save credentials globally',
default: true,
},
},
},
},
{
name: 'clasp_logout',
description: 'Logout from Google account',
inputSchema: {
type: 'object',
properties: {},
},
},
{
name: 'clasp_create',
description: 'Create a new Google Apps Script project',
inputSchema: {
type: 'object',
properties: {
title: {
type: 'string',
description: 'Title of the project',
},
type: {
type: 'string',
enum: ['standalone', 'docs', 'sheets', 'slides', 'forms', 'webapp', 'api'],
description: 'Type of project',
default: 'standalone',
},
rootDir: {
type: 'string',
description: 'Root directory for the project',
default: '.',
},
parentId: {
type: 'string',
description: 'Drive folder ID for the project',
},
},
required: ['title'],
},
},
{
name: 'clasp_clone',
description: 'Clone an existing Google Apps Script project',
inputSchema: {
type: 'object',
properties: {
scriptId: {
type: 'string',
description: 'Script ID to clone',
},
versionNumber: {
type: 'number',
description: 'Specific version to clone (optional)',
},
rootDir: {
type: 'string',
description: 'Directory to clone into',
default: '.',
},
},
required: ['scriptId'],
},
},
{
name: 'clasp_pull',
description: 'Pull changes from Google Apps Script',
inputSchema: {
type: 'object',
properties: {
versionNumber: {
type: 'number',
description: 'Specific version to pull',
},
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
{
name: 'clasp_push',
description: 'Push changes to Google Apps Script',
inputSchema: {
type: 'object',
properties: {
watch: {
type: 'boolean',
description: 'Watch for changes',
default: false,
},
force: {
type: 'boolean',
description: 'Force push without confirmation',
default: false,
},
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
{
name: 'clasp_status',
description: 'Check clasp project status',
inputSchema: {
type: 'object',
properties: {
json: {
type: 'boolean',
description: 'Output as JSON',
default: false,
},
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
{
name: 'clasp_open',
description: 'Open the project in the Apps Script editor',
inputSchema: {
type: 'object',
properties: {
webapp: {
type: 'boolean',
description: 'Open web app URL',
default: false,
},
deploymentId: {
type: 'string',
description: 'Deployment ID to open',
},
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
{
name: 'clasp_deployments',
description: 'List deployments',
inputSchema: {
type: 'object',
properties: {
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
{
name: 'clasp_deploy',
description: 'Create a new deployment',
inputSchema: {
type: 'object',
properties: {
versionNumber: {
type: 'number',
description: 'Version to deploy',
},
description: {
type: 'string',
description: 'Deployment description',
},
deploymentId: {
type: 'string',
description: 'ID to update existing deployment',
},
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
{
name: 'clasp_undeploy',
description: 'Remove a deployment',
inputSchema: {
type: 'object',
properties: {
deploymentId: {
type: 'string',
description: 'Deployment ID to remove',
},
all: {
type: 'boolean',
description: 'Remove all deployments',
default: false,
},
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
{
name: 'clasp_version',
description: 'Create a new version',
inputSchema: {
type: 'object',
properties: {
description: {
type: 'string',
description: 'Version description',
},
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
{
name: 'clasp_versions',
description: 'List versions',
inputSchema: {
type: 'object',
properties: {
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
{
name: 'clasp_list',
description: 'List your Google Apps Script projects',
inputSchema: {
type: 'object',
properties: {},
},
},
{
name: 'clasp_logs',
description: 'View project logs',
inputSchema: {
type: 'object',
properties: {
json: {
type: 'boolean',
description: 'Output as JSON',
default: false,
},
open: {
type: 'boolean',
description: 'Open logs in browser',
default: false,
},
setup: {
type: 'boolean',
description: 'Setup logs',
default: false,
},
watch: {
type: 'boolean',
description: 'Watch for new logs',
default: false,
},
simplified: {
type: 'boolean',
description: 'Simplified output',
default: false,
},
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
{
name: 'clasp_run',
description: 'Run a function in the Apps Script project',
inputSchema: {
type: 'object',
properties: {
functionName: {
type: 'string',
description: 'Function name to run',
},
nondev: {
type: 'boolean',
description: 'Run with production (non-development) deployment',
default: false,
},
params: {
type: 'string',
description: 'Parameters as JSON string',
},
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
required: ['functionName'],
},
},
{
name: 'clasp_apis',
description: 'List or enable/disable APIs',
inputSchema: {
type: 'object',
properties: {
list: {
type: 'boolean',
description: 'List enabled APIs',
default: false,
},
enable: {
type: 'string',
description: 'API to enable',
},
disable: {
type: 'string',
description: 'API to disable',
},
open: {
type: 'boolean',
description: 'Open API console',
default: false,
},
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
{
name: 'clasp_setting',
description: 'Manage project settings',
inputSchema: {
type: 'object',
properties: {
key: {
type: 'string',
description: 'Setting key (e.g., scriptId, rootDir)',
},
value: {
type: 'string',
description: 'Setting value',
},
rootDir: {
type: 'string',
description: 'Root directory of the project',
default: '.',
},
},
},
},
],
}));
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
switch (name) {
case 'clasp_login':
return await this.handleLogin(args);
case 'clasp_logout':
return await this.handleLogout();
case 'clasp_create':
return await this.handleCreate(args);
case 'clasp_clone':
return await this.handleClone(args);
case 'clasp_pull':
return await this.handlePull(args);
case 'clasp_push':
return await this.handlePush(args);
case 'clasp_status':
return await this.handleStatus(args);
case 'clasp_open':
return await this.handleOpen(args);
case 'clasp_deployments':
return await this.handleDeployments(args);
case 'clasp_deploy':
return await this.handleDeploy(args);
case 'clasp_undeploy':
return await this.handleUndeploy(args);
case 'clasp_version':
return await this.handleVersion(args);
case 'clasp_versions':
return await this.handleVersions(args);
case 'clasp_list':
return await this.handleList();
case 'clasp_logs':
return await this.handleLogs(args);
case 'clasp_run':
return await this.handleRun(args);
case 'clasp_apis':
return await this.handleApis(args);
case 'clasp_setting':
return await this.handleSetting(args);
default:
throw new Error(`Unknown tool: ${name}`);
}
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error: ${error.message}`,
},
],
isError: true,
};
}
});
}
async handleLogin(args) {
let command = 'clasp login';
if (args.creds) command += ` --creds ${args.creds}`;
if (!args.global) command += ' --no-localhost';
const output = await this.execClaspCommand(command);
return {
content: [{ type: 'text', text: output || 'Login initiated. Follow the browser prompt.' }],
};
}
async handleLogout() {
const output = await this.execClaspCommand('clasp logout');
return {
content: [{ type: 'text', text: output || 'Logged out successfully.' }],
};
}
async handleCreate(args) {
let command = `clasp create --title "${args.title}"`;
if (args.type) command += ` --type ${args.type}`;
if (args.parentId) command += ` --parentId ${args.parentId}`;
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handleClone(args) {
let command = `clasp clone ${args.scriptId}`;
if (args.versionNumber) command += ` ${args.versionNumber}`;
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handlePull(args) {
let command = 'clasp pull';
if (args.versionNumber) command += ` --versionNumber ${args.versionNumber}`;
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handlePush(args) {
let command = 'clasp push';
if (args.watch) command += ' --watch';
if (args.force) command += ' --force';
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handleStatus(args) {
let command = 'clasp status';
if (args.json) command += ' --json';
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handleOpen(args) {
let command = 'clasp open';
if (args.webapp) command += ' --webapp';
if (args.deploymentId) command += ` --deploymentId ${args.deploymentId}`;
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output || 'Opening in browser...' }],
};
}
async handleDeployments(args) {
const output = await this.execClaspCommand('clasp deployments', { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handleDeploy(args) {
let command = 'clasp deploy';
if (args.versionNumber) command += ` --versionNumber ${args.versionNumber}`;
if (args.description) command += ` --description "${args.description}"`;
if (args.deploymentId) command += ` --deploymentId ${args.deploymentId}`;
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handleUndeploy(args) {
let command = 'clasp undeploy';
if (args.all) {
command += ' --all';
} else if (args.deploymentId) {
command += ` ${args.deploymentId}`;
}
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handleVersion(args) {
let command = 'clasp version';
if (args.description) command += ` "${args.description}"`;
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handleVersions(args) {
const output = await this.execClaspCommand('clasp versions', { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handleList() {
const output = await this.execClaspCommand('clasp list');
return {
content: [{ type: 'text', text: output }],
};
}
async handleLogs(args) {
let command = 'clasp logs';
if (args.json) command += ' --json';
if (args.open) command += ' --open';
if (args.setup) command += ' --setup';
if (args.watch) command += ' --watch';
if (args.simplified) command += ' --simplified';
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handleRun(args) {
let command = `clasp run ${args.functionName}`;
if (args.nondev) command += ' --nondev';
if (args.params) command += ` --params '${args.params}'`;
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handleApis(args) {
let command = 'clasp apis';
if (args.list) command += ' list';
if (args.enable) command += ` enable ${args.enable}`;
if (args.disable) command += ` disable ${args.disable}`;
if (args.open) command += ' --open';
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async handleSetting(args) {
let command = 'clasp setting';
if (args.key && args.value) {
command += ` ${args.key} ${args.value}`;
} else if (args.key) {
command += ` ${args.key}`;
}
const output = await this.execClaspCommand(command, { cwd: args.rootDir });
return {
content: [{ type: 'text', text: output }],
};
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
// Setup handlers after connection is established
this.setupToolHandlers();
console.error('Clasp Enhanced MCP server running on stdio');
}
}
const server = new ClaspEnhancedServer();
server.run().catch(console.error);