-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
executable file
·834 lines (791 loc) · 22.6 KB
/
index.ts
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
#!/usr/bin/env node
import "dotenv/config";
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
ListToolsRequestSchema,
CallToolRequestSchema,
ListResourceTemplatesRequestSchema,
ReadResourceRequestSchema,
ResourceTemplate,
Tool,
ListPromptsRequestSchema,
GetPromptRequestSchema,
Prompt,
} from "@modelcontextprotocol/sdk/types.js";
import { readFileSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import {
ApiError,
ConfigService,
DocCreate,
DocService,
DocUpdate,
TaskCreate,
TaskService,
TaskUpdate,
} from "dart-tools";
const ID_REGEX = /^[a-zA-Z0-9]{12}$/;
const token = process.env.DART_TOKEN;
if (!token) {
console.error("DART_TOKEN environment variable is required");
process.exit(1);
}
const filename = fileURLToPath(import.meta.url);
const packageJson = JSON.parse(
readFileSync(join(dirname(filename), "..", "package.json"), "utf-8"),
);
const getIdValidated = (strMaybe: any): string => {
if (typeof strMaybe !== "string" && !(strMaybe instanceof String)) {
throw new Error("ID must be a string");
}
const id = strMaybe.toString();
if (!ID_REGEX.test(id)) {
throw new Error(`ID must be 12 alphanumeric characters`);
}
return id;
};
// Prompts
const CREATE_TASK_PROMPT_NAME = "Create task";
const CREATE_TASK_PROMPT: Prompt = {
name: CREATE_TASK_PROMPT_NAME,
description: "Create a new task in Dart",
arguments: [
{
name: "title",
description: "Title of the task",
required: true,
},
{
name: "description",
description: "Description of the task",
required: false,
},
{
name: "status",
description: "Status of the task",
required: false,
},
{
name: "priority",
description: "Priority of the task",
required: false,
},
{
name: "assignee",
description: "Email of the assignee",
required: false,
},
],
};
const CREATE_DOC_PROMPT_NAME = "Create doc";
const CREATE_DOC_PROMPT: Prompt = {
name: CREATE_DOC_PROMPT_NAME,
description: "Create a new document in Dart",
arguments: [
{
name: "title",
description: "Title of the document",
required: true,
},
{
name: "text",
description: "Content of the document",
required: false,
},
{
name: "folder",
description: "Folder to place the document in",
required: false,
},
],
};
const SUMMARIZE_TASKS_PROMPT_NAME = "Summarize tasks";
const SUMMARIZE_TASKS_PROMPT: Prompt = {
name: SUMMARIZE_TASKS_PROMPT_NAME,
description: "Get a summary of tasks with optional filtering",
arguments: [
{
name: "status",
description: "Filter by status (e.g., 'In Progress', 'Done')",
required: false,
},
{
name: "assignee",
description: "Filter by assignee email",
required: false,
},
],
};
// Resources
const CONFIG_RESOURCE_TEMPLATE: ResourceTemplate = {
uriTemplate: "dart-config:",
name: "Dart config",
description:
"Information about the authenticated user associated with the API key, including their role, teams, and settings.",
parameters: {},
examples: ["dart-config:"],
};
const TASK_RESOURCE_TEMPLATE: ResourceTemplate = {
uriTemplate: "dart-task:///{taskId}",
name: "Dart task",
description:
"A Dart task with its title, description, status, priority, dates, and more. Use this to fetch detailed information about a specific task.",
parameters: {
taskId: {
type: "string",
description: "The unique identifier of the Dart task",
},
},
examples: ["dart-task:///9q5qtB8n2Qn6"],
};
const DOC_RESOURCE_TEMPLATE: ResourceTemplate = {
uriTemplate: "dart-doc:///{docId}",
name: "Dart doc",
description:
"A Dart doc with its title, text content, and folder. Use this to fetch detailed information about a specific doc.",
parameters: {
docId: {
type: "string",
description: "The unique identifier of the Dart doc",
},
},
examples: ["dart-doc:///9q5qtB8n2Qn6"],
};
// Tools
const GET_CONFIG_TOOL: Tool = {
name: "get_config",
description:
"Get information about the user's space, including all of the possible values that can be provided to other endpoints. This includes available assignees, dartboards, folders, statuses, tags, priorities, and sizes.",
inputSchema: {
type: "object",
properties: {},
required: [],
},
};
const LIST_TASKS_TOOL: Tool = {
name: "list_tasks",
description:
"List tasks from Dart with optional filtering parameters. You can filter by assignee, status, dartboard, priority, due date, and more.",
inputSchema: {
type: "object",
properties: {
assignee: {
type: "string",
description: "Filter by assignee name or email",
},
assignee_duid: {
type: "string",
description: "Filter by assignee ID",
},
dartboard: {
type: "string",
description: "Filter by dartboard title",
},
dartboard_duid: {
type: "string",
description: "Filter by dartboard ID",
},
description: {
type: "string",
description: "Filter by description content",
},
due_at_before: {
type: "string",
description: "Filter by due date before (ISO format)",
},
due_at_after: {
type: "string",
description: "Filter by due date after (ISO format)",
},
duids: { type: "string", description: "Filter by IDs" },
in_trash: { type: "boolean", description: "Filter by trash status" },
is_draft: { type: "boolean", description: "Filter by draft status" },
kind: { type: "string", description: "Filter by task kind" },
limit: { type: "number", description: "Number of results per page" },
offset: {
type: "number",
description: "Initial index for pagination",
},
priority: { type: "string", description: "Filter by priority" },
size: { type: "number", description: "Filter by task size" },
start_at_before: {
type: "string",
description: "Filter by start date before (ISO format)",
},
start_at_after: {
type: "string",
description: "Filter by start date after (ISO format)",
},
status: { type: "string", description: "Filter by status" },
status_duid: { type: "string", description: "Filter by status ID" },
subscriber_duid: {
type: "string",
description: "Filter by subscriber ID",
},
tag: { type: "string", description: "Filter by tag" },
title: { type: "string", description: "Filter by title" },
},
required: [],
},
};
const CREATE_TASK_TOOL: Tool = {
name: "create_task",
description:
"Create a new task in Dart. You can specify title, description, status, priority, size, dates, dartboard, assignees, tags, and parent task.",
inputSchema: {
type: "object",
properties: {
title: {
type: "string",
description: "The title of the task (required)",
},
description: {
type: "string",
description:
"A longer description of the task, which can include markdown formatting",
},
status: {
type: "string",
description: "The status from the list of available statuses",
},
priority: {
type: "string",
description: "The priority (Critical, High, Medium, or Low)",
},
size: {
type: "number",
description: "A number that represents the amount of work needed",
},
startAt: {
type: "string",
description:
"The start date in ISO format (should be at 9:00am in user's timezone)",
},
dueAt: {
type: "string",
description:
"The due date in ISO format (should be at 9:00am in user's timezone)",
},
dartboard: {
type: "string",
description: "The title of the dartboard (project or list of tasks)",
},
assignees: {
type: "array",
items: { type: "string" },
description:
"Array of assignee names or emails (if workspace allows multiple assignees)",
},
assignee: {
type: "string",
description:
"Single assignee name or email (if workspace doesn't allow multiple assignees)",
},
tags: {
type: "array",
items: { type: "string" },
description: "Array of tags to apply to the task",
},
parentId: {
type: "string",
description: "The ID of the parent task",
},
},
required: ["title"],
},
};
const GET_TASK_TOOL: Tool = {
name: "get_task",
description:
"Retrieve an existing task by its ID. Returns the task's information including title, description, status, priority, dates, and more.",
inputSchema: {
type: "object",
properties: {
id: {
type: "string",
description: "The 12-character alphanumeric ID of the task",
pattern: "^[a-zA-Z0-9]{12}$",
},
},
required: ["id"],
},
};
const UPDATE_TASK_TOOL: Tool = {
name: "update_task",
description:
"Update an existing task. You can modify any of its properties including title, description, status, priority, dates, assignees, and more.",
inputSchema: {
type: "object",
properties: {
id: {
type: "string",
description: "The 12-character alphanumeric ID of the task",
pattern: "^[a-zA-Z0-9]{12}$",
},
title: {
type: "string",
description: "The title of the task",
},
description: {
type: "string",
description:
"A longer description of the task, which can include markdown formatting",
},
status: {
type: "string",
description: "The status from the list of available statuses",
},
priority: {
type: "string",
description: "The priority (Critical, High, Medium, or Low)",
},
size: {
type: "number",
description: "A number that represents the amount of work needed",
},
startAt: {
type: "string",
description:
"The start date in ISO format (should be at 9:00am in user's timezone)",
},
dueAt: {
type: "string",
description:
"The due date in ISO format (should be at 9:00am in user's timezone)",
},
dartboard: {
type: "string",
description: "The title of the dartboard (project or list of tasks)",
},
assignees: {
type: "array",
items: { type: "string" },
description:
"Array of assignee names or emails (if workspace allows multiple assignees)",
},
assignee: {
type: "string",
description:
"Single assignee name or email (if workspace doesn't allow multiple assignees)",
},
tags: {
type: "array",
items: { type: "string" },
description: "Array of tags to apply to the task",
},
parentId: {
type: "string",
description: "The ID of the parent task",
},
},
required: ["id"],
},
};
const DELETE_TASK_TOOL: Tool = {
name: "delete_task",
description:
"Move an existing task to the trash, where it can be recovered if needed. Nothing else about the task will be changed.",
inputSchema: {
type: "object",
properties: {
id: {
type: "string",
description: "The 12-character alphanumeric ID of the task",
pattern: "^[a-zA-Z0-9]{12}$",
},
},
required: ["id"],
},
};
const LIST_DOCS_TOOL: Tool = {
name: "list_docs",
description:
"List docs from Dart with optional filtering parameters. You can filter by folder, title, text content, and more.",
inputSchema: {
type: "object",
properties: {
folder: {
type: "string",
description: "Filter by folder title",
},
folder_duid: {
type: "string",
description: "Filter by folder ID",
},
duids: {
type: "string",
description: "Filter by IDs",
},
in_trash: {
type: "boolean",
description: "Filter by trash status",
},
is_draft: {
type: "boolean",
description: "Filter by draft status",
},
limit: {
type: "number",
description: "Number of results per page",
},
offset: {
type: "number",
description: "Initial index for pagination",
},
s: {
type: "string",
description: "Search by title, text, or folder title",
},
text: {
type: "string",
description: "Filter by text content",
},
title: {
type: "string",
description: "Filter by title",
},
},
required: [],
},
};
const CREATE_DOC_TOOL: Tool = {
name: "create_doc",
description:
"Create a new doc in Dart. You can specify title, text content, and folder.",
inputSchema: {
type: "object",
properties: {
title: {
type: "string",
description: "The title of the doc (required)",
},
text: {
type: "string",
description:
"The text content of the doc, which can include markdown formatting",
},
folder: {
type: "string",
description: "The title of the folder to place the doc in",
},
},
required: ["title"],
},
};
const GET_DOC_TOOL: Tool = {
name: "get_doc",
description:
"Retrieve an existing doc by its ID. Returns the doc's information including title, text content, folder, and more.",
inputSchema: {
type: "object",
properties: {
id: {
type: "string",
description: "The 12-character alphanumeric ID of the doc",
pattern: "^[a-zA-Z0-9]{12}$",
},
},
required: ["id"],
},
};
const UPDATE_DOC_TOOL: Tool = {
name: "update_doc",
description:
"Update an existing doc. You can modify its title, text content, and folder.",
inputSchema: {
type: "object",
properties: {
id: {
type: "string",
description: "The 12-character alphanumeric ID of the doc",
pattern: "^[a-zA-Z0-9]{12}$",
},
title: {
type: "string",
description: "The title of the doc",
},
text: {
type: "string",
description:
"The text content of the doc, which can include markdown formatting",
},
folder: {
type: "string",
description: "The title of the folder to place the doc in",
},
},
required: ["id"],
},
};
const DELETE_DOC_TOOL: Tool = {
name: "delete_doc",
description:
"Move an existing doc to the trash, where it can be recovered if needed. Nothing else about the doc will be changed.",
inputSchema: {
type: "object",
properties: {
id: {
type: "string",
description: "The 12-character alphanumeric ID of the doc",
pattern: "^[a-zA-Z0-9]{12}$",
},
},
required: ["id"],
},
};
// Server
const server = new Server(
{
name: "dart-mcp",
version: packageJson.version,
},
{
capabilities: {
prompts: {},
resources: {},
tools: {},
},
},
);
server.setRequestHandler(ListPromptsRequestSchema, async () => ({
prompts: [CREATE_TASK_PROMPT, CREATE_DOC_PROMPT, SUMMARIZE_TASKS_PROMPT],
}));
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
const promptName = request.params.name;
if (promptName === CREATE_TASK_PROMPT_NAME) {
const title = request.params.arguments?.title || "(no title)";
const description = request.params.arguments?.description || "";
const status = request.params.arguments?.status || "";
const priority = request.params.arguments?.priority || "";
const assignee = request.params.arguments?.assignee || "";
return {
description: "Create a new task in Dart",
messages: [
{
role: "user",
content: {
type: "text",
text: `Create a new task in Dart with the following details:
Title: ${title}
${description ? `Description: ${description}` : ""}
${status ? `Status: ${status}` : ""}
${priority ? `Priority: ${priority}` : ""}
${assignee ? `Assignee: ${assignee}` : ""}`,
},
},
],
};
}
if (promptName === CREATE_DOC_PROMPT_NAME) {
const title = request.params.arguments?.title || "(no title)";
const text = request.params.arguments?.text || "";
const folder = request.params.arguments?.folder || "";
return {
description: "Create a new document in Dart",
messages: [
{
role: "user",
content: {
type: "text",
text: `Create a new document in Dart with the following details:
Title: ${title}
${text ? `Content: ${text}` : ""}
${folder ? `Folder: ${folder}` : ""}`,
},
},
],
};
}
if (promptName === SUMMARIZE_TASKS_PROMPT_NAME) {
const status = request.params.arguments?.status || "";
const assignee = request.params.arguments?.assignee || "";
return {
description: "Get a summary of tasks with optional filtering",
messages: [
{
role: "user",
content: {
type: "text",
text: `Summarize the tasks in Dart${status ? ` with status "${status}"` : ""}${assignee ? ` assigned to ${assignee}` : ""}.
Please include the total count, group by status, and list any high priority items.`,
},
},
],
};
}
throw new Error(`Unknown prompt: ${promptName}`);
});
server.setRequestHandler(ListResourceTemplatesRequestSchema, async () => ({
resourceTemplates: [
CONFIG_RESOURCE_TEMPLATE,
TASK_RESOURCE_TEMPLATE,
DOC_RESOURCE_TEMPLATE,
],
}));
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
const { uri } = request.params;
const url = new URL(uri);
const path = url.pathname.replace(/^\//, "");
const { protocol } = url;
if (protocol === "dart-config") {
const config = await ConfigService.getConfig();
return {
contents: [
{
uri,
mimeType: "application/json",
text: JSON.stringify(config, null, 2),
},
],
};
}
if (protocol === "dart-task:") {
const task = await TaskService.retrieveTask(path);
return {
contents: [
{
uri,
mimeType: "application/json",
text: JSON.stringify(task, null, 2),
},
],
};
}
if (protocol === "dart-doc:") {
const doc = await DocService.retrieveDoc(path);
return {
contents: [
{
uri,
mimeType: "application/json",
text: JSON.stringify(doc, null, 2),
},
],
};
}
throw new Error(`Unknown resource: ${uri}`);
});
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
GET_CONFIG_TOOL,
LIST_TASKS_TOOL,
CREATE_TASK_TOOL,
GET_TASK_TOOL,
UPDATE_TASK_TOOL,
DELETE_TASK_TOOL,
LIST_DOCS_TOOL,
CREATE_DOC_TOOL,
GET_DOC_TOOL,
UPDATE_DOC_TOOL,
DELETE_DOC_TOOL,
],
}));
server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
if (!request.params.arguments) {
throw new Error("Arguments are required");
}
switch (request.params.name) {
case "get_config": {
const config = await ConfigService.getConfig();
return {
content: [{ type: "text", text: JSON.stringify(config, null, 2) }],
};
}
case "list_tasks": {
const tasks = await TaskService.listTasks(request.params.arguments);
return {
content: [{ type: "text", text: JSON.stringify(tasks, null, 2) }],
};
}
case "create_task": {
const taskData = request.params.arguments as TaskCreate;
const task = await TaskService.createTask({ item: taskData });
return {
content: [{ type: "text", text: JSON.stringify(task, null, 2) }],
};
}
case "get_task": {
const id = getIdValidated(request.params.arguments.id);
const task = await TaskService.retrieveTask(id);
return {
content: [{ type: "text", text: JSON.stringify(task, null, 2) }],
};
}
case "update_task": {
const id = getIdValidated(request.params.arguments.id);
const taskData = request.params.arguments as TaskUpdate;
const task = await TaskService.updateTask(id, {
item: taskData,
});
return {
content: [{ type: "text", text: JSON.stringify(task, null, 2) }],
};
}
case "delete_task": {
const id = getIdValidated(request.params.arguments.id);
const task = await TaskService.deleteTask(id);
return {
content: [{ type: "text", text: JSON.stringify(task, null, 2) }],
};
}
case "list_docs": {
const docs = await DocService.listDocs(request.params.arguments);
return {
content: [{ type: "text", text: JSON.stringify(docs, null, 2) }],
};
}
case "create_doc": {
const docData = request.params.arguments as DocCreate;
const doc = await DocService.createDoc({
item: docData,
});
return {
content: [{ type: "text", text: JSON.stringify(doc, null, 2) }],
};
}
case "get_doc": {
const id = getIdValidated(request.params.arguments.id);
const doc = await DocService.retrieveDoc(id);
return {
content: [{ type: "text", text: JSON.stringify(doc, null, 2) }],
};
}
case "update_doc": {
const id = getIdValidated(request.params.arguments.id);
const docData = request.params.arguments as DocUpdate;
const doc = await DocService.updateDoc(id, { item: docData });
return {
content: [{ type: "text", text: JSON.stringify(doc, null, 2) }],
};
}
case "delete_doc": {
const id = getIdValidated(request.params.arguments.id);
const doc = await DocService.deleteDoc(id);
return {
content: [{ type: "text", text: JSON.stringify(doc, null, 2) }],
};
}
default:
throw new Error(`Unknown tool: ${request.params.name}`);
}
} catch (error) {
if (error instanceof ApiError) {
throw new Error(
`API error: ${error.status} ${JSON.stringify(error.body) || error.message || "(unknown error)"}`,
);
}
throw error;
}
});
async function runServer() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Dart MCP Server running on stdio");
}
runServer().catch((error) => {
console.error("Unhandled error:", error);
process.exit(1);
});