@inkdropapp/mcp-server 1.3.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +1 -3
- package/.github/workflows/claude-code-review.yml +9 -10
- package/.github/workflows/claude.yml +6 -7
- package/.oxfmtrc.json +12 -0
- package/README.md +48 -38
- package/Session.vim +122 -0
- package/dist/api.js +6 -6
- package/dist/index.js +320 -67
- package/dist/utils.js +63 -0
- package/package.json +10 -8
- package/pnpm-workspace.yaml +3 -0
- package/src/api.ts +18 -21
- package/src/index.ts +397 -94
- package/src/utils.ts +63 -0
- package/tsconfig.json +5 -11
- package/prettier.config.js +0 -12
package/src/index.ts
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { createHash } from 'node:crypto'
|
|
4
|
+
import { readFile, writeFile } from 'node:fs/promises'
|
|
5
|
+
import { basename } from 'node:path'
|
|
6
|
+
|
|
7
|
+
import { McpServer, ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
7
8
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
|
|
8
9
|
import {
|
|
9
10
|
Note,
|
|
10
|
-
NoteSchema,
|
|
11
11
|
Book,
|
|
12
|
-
BookSchema,
|
|
13
|
-
TagSchema,
|
|
14
12
|
TAG_COLOR,
|
|
15
|
-
Tag
|
|
13
|
+
Tag,
|
|
14
|
+
File as IDFile,
|
|
15
|
+
maxAttachmentFileSize,
|
|
16
|
+
supportedImageFileTypes
|
|
16
17
|
} from 'inkdrop-model'
|
|
17
18
|
import { z } from 'zod'
|
|
18
19
|
|
|
19
20
|
import { fetchJSON, postJSON } from './api'
|
|
21
|
+
import { getFileUri, getNoteUri, inferImageContentType, isViewableImageType } from './utils'
|
|
20
22
|
|
|
21
23
|
const server = new McpServer({
|
|
22
24
|
name: 'Inkdrop',
|
|
@@ -31,7 +33,9 @@ server.registerResource(
|
|
|
31
33
|
mimeType: 'application/json'
|
|
32
34
|
},
|
|
33
35
|
async (uri, { noteId }) => {
|
|
34
|
-
const
|
|
36
|
+
const id = Array.isArray(noteId) ? noteId[0] : noteId
|
|
37
|
+
const normalizedId = id.startsWith('note:') ? id : `note:${id}`
|
|
38
|
+
const note: Note[] = await fetchJSON(`/${normalizedId}`, {})
|
|
35
39
|
return {
|
|
36
40
|
contents: [
|
|
37
41
|
{
|
|
@@ -48,29 +52,106 @@ server.registerTool(
|
|
|
48
52
|
'read-note',
|
|
49
53
|
{
|
|
50
54
|
description:
|
|
51
|
-
'Retrieve the complete contents of the note by its ID from the database.'
|
|
55
|
+
'Retrieve the complete contents of the note by its ID from the database. ' +
|
|
56
|
+
'A note created from a template has a `sourceTemplateId`; when present, the response ' +
|
|
57
|
+
'includes a resource link to that source template, whose body holds the fill-out ' +
|
|
58
|
+
'instructions and examples. Read the template (or pass `includeTemplate: true`) before ' +
|
|
59
|
+
'completing the note.',
|
|
52
60
|
inputSchema: {
|
|
53
61
|
noteId: z
|
|
54
62
|
.string()
|
|
55
63
|
.describe(
|
|
56
64
|
'ID of the note to retrieve. It can be found as `_id` in the note docs. It always starts with \`note:\`.'
|
|
65
|
+
),
|
|
66
|
+
includeTemplate: z
|
|
67
|
+
.boolean()
|
|
68
|
+
.optional()
|
|
69
|
+
.default(false)
|
|
70
|
+
.describe(
|
|
71
|
+
'When true and the note has a `sourceTemplateId`, also fetch that template and embed its body (the fill-out instructions and examples) as an extra text block, so you get the note and its template in one call.'
|
|
57
72
|
)
|
|
58
73
|
}
|
|
59
74
|
},
|
|
60
|
-
async ({ noteId }) => {
|
|
75
|
+
async ({ noteId, includeTemplate }) => {
|
|
61
76
|
if (!noteId.startsWith('note:')) noteId = `note:${noteId}`
|
|
62
|
-
const note
|
|
77
|
+
const note = await fetchJSON<Note>(`/${noteId}`, {})
|
|
78
|
+
|
|
79
|
+
let templateBlockText: string | null = null
|
|
80
|
+
if (includeTemplate && note.sourceTemplateId) {
|
|
81
|
+
try {
|
|
82
|
+
const template = await fetchJSON<Note>(`/${note.sourceTemplateId}`, {})
|
|
83
|
+
templateBlockText = `# Source template: ${template.title}\n\n${template.body}`
|
|
84
|
+
} catch {
|
|
85
|
+
// Template unavailable (e.g. deleted → 404). Skip the embed; the note read still succeeds.
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
63
89
|
return {
|
|
64
90
|
content: [
|
|
65
91
|
{
|
|
66
|
-
type: 'text',
|
|
92
|
+
type: 'text' as const,
|
|
67
93
|
text: JSON.stringify(note, null, 2)
|
|
68
|
-
}
|
|
94
|
+
},
|
|
95
|
+
...(note.sourceTemplateId
|
|
96
|
+
? [
|
|
97
|
+
{
|
|
98
|
+
type: 'resource_link' as const,
|
|
99
|
+
uri: getNoteUri(note.sourceTemplateId),
|
|
100
|
+
name: 'Source template',
|
|
101
|
+
mimeType: 'application/json',
|
|
102
|
+
description:
|
|
103
|
+
'The template this note was created from. Read it to get the fill-out instructions and examples before completing this note.'
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
: []),
|
|
107
|
+
...(templateBlockText ? [{ type: 'text' as const, text: templateBlockText }] : [])
|
|
69
108
|
]
|
|
70
109
|
}
|
|
71
110
|
}
|
|
72
111
|
)
|
|
73
112
|
|
|
113
|
+
server.registerTool(
|
|
114
|
+
'read-backlinks',
|
|
115
|
+
{
|
|
116
|
+
description: `Retrieve backlinks for a note — the notes that link to it.
|
|
117
|
+
Each result is returned as a resource link (\`inkdrop://note/<id>\`) pointing to the referring note.
|
|
118
|
+
Backlinks are found by searching all note bodies for the note's link URI.`,
|
|
119
|
+
inputSchema: {
|
|
120
|
+
noteId: z
|
|
121
|
+
.string()
|
|
122
|
+
.describe(
|
|
123
|
+
'ID of the note to find backlinks for. It can be found as `_id` in the note docs. It always starts with \`note:\`.'
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
async ({ noteId }) => {
|
|
128
|
+
if (!noteId.startsWith('note:')) noteId = `note:${noteId}`
|
|
129
|
+
const noteLink = getNoteUri(noteId)
|
|
130
|
+
const backlinks: Note[] = await fetchJSON('/notes', {
|
|
131
|
+
keyword: noteLink,
|
|
132
|
+
limit: 100
|
|
133
|
+
})
|
|
134
|
+
if (backlinks.length === 0) {
|
|
135
|
+
return {
|
|
136
|
+
content: [
|
|
137
|
+
{
|
|
138
|
+
type: 'text',
|
|
139
|
+
text: `No backlinks found for ${noteLink}.`
|
|
140
|
+
}
|
|
141
|
+
]
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
content: backlinks.map(note => ({
|
|
146
|
+
type: 'resource_link' as const,
|
|
147
|
+
uri: getNoteUri(note._id),
|
|
148
|
+
name: note.title || 'Untitled note',
|
|
149
|
+
mimeType: 'application/json'
|
|
150
|
+
}))
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
)
|
|
154
|
+
|
|
74
155
|
server.registerTool(
|
|
75
156
|
'search-notes',
|
|
76
157
|
{
|
|
@@ -196,11 +277,7 @@ You have to retrieve the full note content by calling \`read-note\`.
|
|
|
196
277
|
.optional()
|
|
197
278
|
.default(true)
|
|
198
279
|
.describe(`Reverse the order of the output documents`),
|
|
199
|
-
limit: z
|
|
200
|
-
.number()
|
|
201
|
-
.optional()
|
|
202
|
-
.default(100)
|
|
203
|
-
.describe(`Limit the number of results returned`)
|
|
280
|
+
limit: z.number().optional().default(100).describe(`Limit the number of results returned`)
|
|
204
281
|
}
|
|
205
282
|
},
|
|
206
283
|
async ({ bookId, tagIds, keyword, sort, descending, limit }) => {
|
|
@@ -244,10 +321,7 @@ server.registerTool(
|
|
|
244
321
|
|
|
245
322
|
title: z.string().max(128).describe('The note title'),
|
|
246
323
|
|
|
247
|
-
body: z
|
|
248
|
-
.string()
|
|
249
|
-
.max(1048576)
|
|
250
|
-
.describe('The content of the note represented with Markdown'),
|
|
324
|
+
body: z.string().max(1048576).describe('The content of the note represented with Markdown'),
|
|
251
325
|
|
|
252
326
|
status: z
|
|
253
327
|
.enum(['none', 'active', 'onHold', 'completed', 'dropped'])
|
|
@@ -413,9 +487,7 @@ server.registerTool(
|
|
|
413
487
|
}
|
|
414
488
|
|
|
415
489
|
const patched =
|
|
416
|
-
body.slice(0, firstIndex) +
|
|
417
|
-
new_string +
|
|
418
|
-
body.slice(firstIndex + old_string.length)
|
|
490
|
+
body.slice(0, firstIndex) + new_string + body.slice(firstIndex + old_string.length)
|
|
419
491
|
|
|
420
492
|
const res = await postJSON('/notes', {
|
|
421
493
|
...existingNote,
|
|
@@ -433,21 +505,17 @@ server.registerTool(
|
|
|
433
505
|
}
|
|
434
506
|
)
|
|
435
507
|
|
|
436
|
-
server.registerTool(
|
|
437
|
-
'
|
|
438
|
-
{
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
text: JSON.stringify(tags, null, 2)
|
|
446
|
-
}
|
|
447
|
-
]
|
|
448
|
-
}
|
|
508
|
+
server.registerTool('list-tags', { description: 'Retrieve a list of all tags' }, async () => {
|
|
509
|
+
const tags: Tag[] = await fetchJSON('/tags')
|
|
510
|
+
return {
|
|
511
|
+
content: [
|
|
512
|
+
{
|
|
513
|
+
type: 'text',
|
|
514
|
+
text: JSON.stringify(tags, null, 2)
|
|
515
|
+
}
|
|
516
|
+
]
|
|
449
517
|
}
|
|
450
|
-
)
|
|
518
|
+
})
|
|
451
519
|
|
|
452
520
|
server.registerTool(
|
|
453
521
|
'read-tag',
|
|
@@ -482,10 +550,7 @@ server.registerTool(
|
|
|
482
550
|
{
|
|
483
551
|
description: 'Create a new tag in the database',
|
|
484
552
|
inputSchema: {
|
|
485
|
-
color: z
|
|
486
|
-
.enum(TagColors)
|
|
487
|
-
.default('default')
|
|
488
|
-
.describe('The color type of the tag'),
|
|
553
|
+
color: z.enum(TagColors).default('default').describe('The color type of the tag'),
|
|
489
554
|
|
|
490
555
|
name: z.string().max(64).describe('The name of the tag')
|
|
491
556
|
}
|
|
@@ -507,7 +572,7 @@ server.registerTool(
|
|
|
507
572
|
'update-tag',
|
|
508
573
|
{
|
|
509
574
|
description:
|
|
510
|
-
'Update the existing tag in the database.
|
|
575
|
+
'Update the existing tag in the database. Only the fields you provide will be updated; omitted fields remain unchanged, so you do not need to read the tag first. Call `list-tags` or `read-tag` to find the tag ID.',
|
|
511
576
|
inputSchema: {
|
|
512
577
|
_id: z
|
|
513
578
|
.string()
|
|
@@ -517,23 +582,18 @@ server.registerTool(
|
|
|
517
582
|
.describe(
|
|
518
583
|
'The unique document ID which should start with `tag:` and the remains are randomly generated string'
|
|
519
584
|
),
|
|
520
|
-
|
|
521
585
|
_rev: z
|
|
522
586
|
.string()
|
|
587
|
+
.optional()
|
|
523
588
|
.describe(
|
|
524
|
-
'This is a CouchDB specific field. The current MVCC-token/revision of this document
|
|
589
|
+
'This is a CouchDB specific field. The current MVCC-token/revision of this document. Optional: the server merges your fields into the stored tag, so it is only needed as an optimistic-concurrency guard. Pass it when you have read the tag first and want the update to fail on a conflicting concurrent edit.'
|
|
525
590
|
),
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
.enum(TagColors)
|
|
529
|
-
.default('default')
|
|
530
|
-
.describe('The color type of the tag'),
|
|
531
|
-
|
|
532
|
-
name: z.string().max(64).describe('The name of the tag')
|
|
591
|
+
color: z.enum(TagColors).optional().describe('The color type of the tag'),
|
|
592
|
+
name: z.string().max(64).optional().describe('The name of the tag')
|
|
533
593
|
}
|
|
534
594
|
},
|
|
535
|
-
async
|
|
536
|
-
const res = await postJSON(
|
|
595
|
+
async ({ _id, ...updateFields }) => {
|
|
596
|
+
const res = await postJSON(`/${_id}`, updateFields)
|
|
537
597
|
return {
|
|
538
598
|
content: [
|
|
539
599
|
{
|
|
@@ -587,49 +647,292 @@ server.registerTool(
|
|
|
587
647
|
}
|
|
588
648
|
)
|
|
589
649
|
|
|
590
|
-
server.
|
|
591
|
-
'
|
|
592
|
-
{
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
650
|
+
server.registerTool(
|
|
651
|
+
'create-book',
|
|
652
|
+
{
|
|
653
|
+
description:
|
|
654
|
+
'Create a new notebook in the database. To create a nested notebook, pass the parent notebook ID as `parentBookId`. Call `list-notebooks` first to find it.',
|
|
655
|
+
inputSchema: {
|
|
656
|
+
name: z.string().min(1).max(64).describe('The notebook name'),
|
|
657
|
+
parentBookId: z
|
|
658
|
+
.string()
|
|
659
|
+
.min(6)
|
|
660
|
+
.max(128)
|
|
661
|
+
.regex(/^book:/)
|
|
662
|
+
.optional()
|
|
663
|
+
.describe(
|
|
664
|
+
'The ID of the parent notebook. Omit it to create the notebook at the root level.'
|
|
665
|
+
)
|
|
666
|
+
}
|
|
667
|
+
},
|
|
668
|
+
async bookData => {
|
|
669
|
+
const res = await postJSON(`/books`, bookData)
|
|
670
|
+
return {
|
|
671
|
+
content: [
|
|
672
|
+
{
|
|
598
673
|
type: 'text',
|
|
599
|
-
text:
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
- Get a list of all notebooks
|
|
606
|
-
- Create a new note
|
|
607
|
-
- Update an existing note
|
|
608
|
-
|
|
609
|
-
Best practices:
|
|
610
|
-
- When searching:
|
|
611
|
-
- Use specific, targeted queries for better results (e.g., "auth mobile app" rather than just "auth")
|
|
612
|
-
- Apply relevant filters when asked or when you can infer the appropriate filters to narrow results
|
|
613
|
-
- Use \`read-note\` to get the full note content
|
|
614
|
-
|
|
615
|
-
Model schemas:
|
|
616
|
-
|
|
617
|
-
\`\`\`json
|
|
618
|
-
${JSON.stringify(NoteSchema, null, 2)}
|
|
619
|
-
\`\`\`
|
|
674
|
+
text: JSON.stringify(res, null, 2)
|
|
675
|
+
}
|
|
676
|
+
]
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
)
|
|
620
680
|
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
681
|
+
server.registerTool(
|
|
682
|
+
'update-book',
|
|
683
|
+
{
|
|
684
|
+
description:
|
|
685
|
+
'Update the existing notebook in the database. Only the fields you provide will be updated; omitted fields remain unchanged, so you do not need to read the notebook first. Call `list-notebooks` or `read-book` to find the notebook ID.',
|
|
686
|
+
inputSchema: {
|
|
687
|
+
_id: z
|
|
688
|
+
.string()
|
|
689
|
+
.min(6)
|
|
690
|
+
.max(128)
|
|
691
|
+
.regex(/^book:/)
|
|
692
|
+
.describe(
|
|
693
|
+
'The unique document ID which should start with `book:` and the remains are randomly generated string'
|
|
694
|
+
),
|
|
695
|
+
_rev: z
|
|
696
|
+
.string()
|
|
697
|
+
.optional()
|
|
698
|
+
.describe(
|
|
699
|
+
'This is a CouchDB specific field. The current MVCC-token/revision of this document. Optional: the server merges your fields into the stored notebook, so it is only needed as an optimistic-concurrency guard. Pass it when you have read the notebook first and want the update to fail on a conflicting concurrent edit.'
|
|
700
|
+
),
|
|
701
|
+
name: z.string().min(1).max(64).optional().describe('The notebook name'),
|
|
702
|
+
parentBookId: z
|
|
703
|
+
.string()
|
|
704
|
+
.min(6)
|
|
705
|
+
.max(128)
|
|
706
|
+
.regex(/^book:/)
|
|
707
|
+
.nullable()
|
|
708
|
+
.optional()
|
|
709
|
+
.describe(
|
|
710
|
+
'The ID of the parent notebook. Pass `null` to move the notebook to the root level.'
|
|
711
|
+
)
|
|
712
|
+
}
|
|
713
|
+
},
|
|
714
|
+
async ({ _id, ...updateFields }) => {
|
|
715
|
+
const res = await postJSON(`/${_id}`, {
|
|
716
|
+
...updateFields,
|
|
717
|
+
updatedAt: +new Date()
|
|
718
|
+
})
|
|
719
|
+
return {
|
|
720
|
+
content: [
|
|
721
|
+
{
|
|
722
|
+
type: 'text',
|
|
723
|
+
text: JSON.stringify(res, null, 2)
|
|
724
|
+
}
|
|
725
|
+
]
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
)
|
|
624
729
|
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
730
|
+
const ImageFileTypes = [...supportedImageFileTypes] as [string, ...string[]]
|
|
731
|
+
|
|
732
|
+
server.registerTool(
|
|
733
|
+
'create-file',
|
|
734
|
+
{
|
|
735
|
+
description:
|
|
736
|
+
'Create a new attachment file in the database from a local image file or from base64 image data. ' +
|
|
737
|
+
'Returns the Markdown to embed the attachment in a note body. ' +
|
|
738
|
+
'Prefer `filePath` over `data`: it keeps the image bytes out of the conversation.',
|
|
739
|
+
inputSchema: {
|
|
740
|
+
filePath: z
|
|
741
|
+
.string()
|
|
742
|
+
.optional()
|
|
743
|
+
.describe(
|
|
744
|
+
'Absolute path to a local image file to attach. Either this or `data` must be given.'
|
|
745
|
+
),
|
|
746
|
+
data: z
|
|
747
|
+
.string()
|
|
748
|
+
.optional()
|
|
749
|
+
.describe(
|
|
750
|
+
'Base64-encoded image data, without a `data:` URI prefix. Use `filePath` instead whenever the image is already on disk.'
|
|
751
|
+
),
|
|
752
|
+
name: z
|
|
753
|
+
.string()
|
|
754
|
+
.min(1)
|
|
755
|
+
.max(128)
|
|
756
|
+
.optional()
|
|
757
|
+
.describe(
|
|
758
|
+
'The file name. Defaults to the file name of `filePath`, or `image.<ext>` when `data` is given.'
|
|
759
|
+
),
|
|
760
|
+
contentType: z
|
|
761
|
+
.enum(ImageFileTypes)
|
|
762
|
+
.optional()
|
|
763
|
+
.describe(
|
|
764
|
+
'The MIME type of the image. Inferred from the `filePath` extension when omitted, so it is required with `data`.'
|
|
765
|
+
)
|
|
766
|
+
}
|
|
767
|
+
},
|
|
768
|
+
async ({ filePath, data, name, contentType }) => {
|
|
769
|
+
if ((filePath === undefined) === (data === undefined)) {
|
|
770
|
+
return {
|
|
771
|
+
content: [
|
|
772
|
+
{
|
|
773
|
+
type: 'text',
|
|
774
|
+
text: 'Failed to create the attachment: provide exactly one of `filePath` or `data`.'
|
|
775
|
+
}
|
|
776
|
+
],
|
|
777
|
+
isError: true
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
let buffer: Buffer
|
|
782
|
+
if (filePath !== undefined) {
|
|
783
|
+
try {
|
|
784
|
+
buffer = await readFile(filePath)
|
|
785
|
+
} catch (e) {
|
|
786
|
+
return {
|
|
787
|
+
content: [
|
|
788
|
+
{
|
|
789
|
+
type: 'text',
|
|
790
|
+
text: `Failed to read the file at ${filePath}: ${(e as Error).message}`
|
|
791
|
+
}
|
|
792
|
+
],
|
|
793
|
+
isError: true
|
|
629
794
|
}
|
|
630
795
|
}
|
|
631
|
-
|
|
632
|
-
|
|
796
|
+
contentType ??= inferImageContentType(filePath)
|
|
797
|
+
name ??= basename(filePath)
|
|
798
|
+
} else {
|
|
799
|
+
buffer = Buffer.from(data!, 'base64')
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
if (!contentType) {
|
|
803
|
+
return {
|
|
804
|
+
content: [
|
|
805
|
+
{
|
|
806
|
+
type: 'text',
|
|
807
|
+
text: `Failed to create the attachment: could not infer the content type${
|
|
808
|
+
filePath ? ` from ${filePath}` : ''
|
|
809
|
+
}. Pass \`contentType\` explicitly. Supported types: ${supportedImageFileTypes.join(', ')}.`
|
|
810
|
+
}
|
|
811
|
+
],
|
|
812
|
+
isError: true
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
const base64 = buffer.toString('base64')
|
|
817
|
+
const contentLength = Math.floor((base64.length * 3) / 4)
|
|
818
|
+
if (contentLength > maxAttachmentFileSize) {
|
|
819
|
+
return {
|
|
820
|
+
content: [
|
|
821
|
+
{
|
|
822
|
+
type: 'text',
|
|
823
|
+
text: `Failed to create the attachment: the file is ${contentLength} bytes, over the ${maxAttachmentFileSize} byte limit.`
|
|
824
|
+
}
|
|
825
|
+
],
|
|
826
|
+
isError: true
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
const res = await postJSON<{ id: string; ok: boolean; rev: string }>('/files', {
|
|
831
|
+
name: name ?? `image.${contentType.replace(/^image\//, '')}`,
|
|
832
|
+
contentType,
|
|
833
|
+
contentLength,
|
|
834
|
+
publicIn: [],
|
|
835
|
+
md5digest: createHash('md5').update(buffer).digest('hex'),
|
|
836
|
+
_attachments: {
|
|
837
|
+
index: {
|
|
838
|
+
content_type: contentType,
|
|
839
|
+
data: base64
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
})
|
|
843
|
+
|
|
844
|
+
return {
|
|
845
|
+
content: [
|
|
846
|
+
{
|
|
847
|
+
type: 'text',
|
|
848
|
+
text: JSON.stringify(res, null, 2)
|
|
849
|
+
},
|
|
850
|
+
{
|
|
851
|
+
type: 'text',
|
|
852
|
+
text: `Markdown to embed this attachment in a note body:\n\n})`
|
|
853
|
+
}
|
|
854
|
+
]
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
)
|
|
858
|
+
|
|
859
|
+
server.registerTool(
|
|
860
|
+
'read-file',
|
|
861
|
+
{
|
|
862
|
+
description:
|
|
863
|
+
'Retrieve an attachment file by its ID. Returns the file metadata, and the image itself when it is a PNG, JPEG or GIF. ' +
|
|
864
|
+
'Pass `outputPath` to write the attachment to disk instead — required to get at SVG, HEIC and HEIF attachments, which cannot be displayed inline.',
|
|
865
|
+
inputSchema: {
|
|
866
|
+
fileId: z
|
|
867
|
+
.string()
|
|
868
|
+
.describe(
|
|
869
|
+
'ID of the attachment to retrieve. It can be found as `_id` in the file docs. It always starts with \`file:\`.'
|
|
870
|
+
),
|
|
871
|
+
outputPath: z
|
|
872
|
+
.string()
|
|
873
|
+
.optional()
|
|
874
|
+
.describe(
|
|
875
|
+
'Absolute path to write the attachment to. When given, the file is saved there and its bytes are not returned inline.'
|
|
876
|
+
),
|
|
877
|
+
includeImage: z
|
|
878
|
+
.boolean()
|
|
879
|
+
.optional()
|
|
880
|
+
.default(true)
|
|
881
|
+
.describe(
|
|
882
|
+
'Whether to return the image itself alongside the metadata. Set to false to fetch only the metadata of a large attachment.'
|
|
883
|
+
)
|
|
884
|
+
}
|
|
885
|
+
},
|
|
886
|
+
async ({ fileId, outputPath, includeImage }) => {
|
|
887
|
+
if (!fileId.startsWith('file:')) fileId = `file:${fileId}`
|
|
888
|
+
const needsData = includeImage || outputPath !== undefined
|
|
889
|
+
const file = await fetchJSON<IDFile>(`/${fileId}`, {
|
|
890
|
+
attachments: needsData ? true : undefined
|
|
891
|
+
})
|
|
892
|
+
|
|
893
|
+
const { _attachments, ...metadata } = file
|
|
894
|
+
const base64 =
|
|
895
|
+
typeof _attachments?.index?.data === 'string' ? _attachments.index.data : undefined
|
|
896
|
+
|
|
897
|
+
const attachmentBlock = async () => {
|
|
898
|
+
if (!needsData) return []
|
|
899
|
+
if (!base64) {
|
|
900
|
+
return [
|
|
901
|
+
{
|
|
902
|
+
type: 'text' as const,
|
|
903
|
+
text: 'The attachment data could not be read from the document.'
|
|
904
|
+
}
|
|
905
|
+
]
|
|
906
|
+
}
|
|
907
|
+
if (outputPath !== undefined) {
|
|
908
|
+
await writeFile(outputPath, Buffer.from(base64, 'base64'))
|
|
909
|
+
return [{ type: 'text' as const, text: `Attachment written to ${outputPath}` }]
|
|
910
|
+
}
|
|
911
|
+
if (!isViewableImageType(file.contentType)) {
|
|
912
|
+
return [
|
|
913
|
+
{
|
|
914
|
+
type: 'text' as const,
|
|
915
|
+
text: `The attachment is a ${file.contentType} file, which cannot be displayed inline. Pass \`outputPath\` to write it to disk.`
|
|
916
|
+
}
|
|
917
|
+
]
|
|
918
|
+
}
|
|
919
|
+
return [
|
|
920
|
+
{
|
|
921
|
+
type: 'image' as const,
|
|
922
|
+
data: base64,
|
|
923
|
+
// `image/jpg` is not a real MIME type; the image block expects `image/jpeg`.
|
|
924
|
+
mimeType: file.contentType === 'image/jpg' ? 'image/jpeg' : file.contentType
|
|
925
|
+
}
|
|
926
|
+
]
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
return {
|
|
930
|
+
content: [
|
|
931
|
+
{ type: 'text' as const, text: JSON.stringify(metadata, null, 2) },
|
|
932
|
+
...(await attachmentBlock())
|
|
933
|
+
]
|
|
934
|
+
}
|
|
935
|
+
}
|
|
633
936
|
)
|
|
634
937
|
|
|
635
938
|
async function runServer() {
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { extname } from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { SUPPORTED_IMAGE_MIME_TYPES, type ImageFileType } from 'inkdrop-model'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Builds the canonical Inkdrop note URI for a note ID.
|
|
7
|
+
*
|
|
8
|
+
* The `note:` prefix is stripped so the result matches how note links are stored
|
|
9
|
+
* in note bodies (`note:foobar` → `inkdrop://note/foobar`). Backlink searches rely
|
|
10
|
+
* on this exact form to find notes that link to a given note.
|
|
11
|
+
*
|
|
12
|
+
* @param noteId - The note document ID, with or without the `note:` prefix
|
|
13
|
+
* @returns The `inkdrop://note/<id>` URI
|
|
14
|
+
*/
|
|
15
|
+
export function getNoteUri(noteId: string): string {
|
|
16
|
+
return `inkdrop://note/${noteId.replace(/^note:/, '')}`
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Builds the Inkdrop attachment URI for a file ID.
|
|
21
|
+
*
|
|
22
|
+
* Unlike note links, attachment URIs keep the `file:` prefix and use no slash
|
|
23
|
+
* (`file:foobar` → `inkdrop://file:foobar`). This matches how the desktop app
|
|
24
|
+
* renders an attachment into a note body, so the result can be embedded as-is.
|
|
25
|
+
*
|
|
26
|
+
* @param fileId - The file document ID, with or without the `file:` prefix
|
|
27
|
+
* @returns The `inkdrop://file:<id>` URI
|
|
28
|
+
*/
|
|
29
|
+
export function getFileUri(fileId: string): string {
|
|
30
|
+
return `inkdrop://${fileId.startsWith('file:') ? fileId : `file:${fileId}`}`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Infers the attachment content type from a file name or path extension.
|
|
35
|
+
*
|
|
36
|
+
* `SUPPORTED_IMAGE_MIME_TYPES` is keyed by MIME subtype, which doubles as the file
|
|
37
|
+
* extension for every supported type except SVG, whose subtype is `svg+xml`.
|
|
38
|
+
*
|
|
39
|
+
* @returns The matching image MIME type, or `undefined` when the extension is
|
|
40
|
+
* missing or not a supported attachment type
|
|
41
|
+
*/
|
|
42
|
+
export function inferImageContentType(filePath: string): ImageFileType | undefined {
|
|
43
|
+
const ext = extname(filePath).toLowerCase().replace(/^\./, '')
|
|
44
|
+
return SUPPORTED_IMAGE_MIME_TYPES[ext === 'svg' ? 'svg+xml' : ext]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Content types the MCP `image` content block can carry. Inkdrop also accepts
|
|
49
|
+
* SVG and HEIC/HEIF attachments, which have to be written to disk instead.
|
|
50
|
+
*/
|
|
51
|
+
const VIEWABLE_IMAGE_TYPES = new Set<ImageFileType>([
|
|
52
|
+
'image/png',
|
|
53
|
+
'image/jpeg',
|
|
54
|
+
'image/jpg',
|
|
55
|
+
'image/gif'
|
|
56
|
+
])
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Whether an attachment can be returned as an MCP `image` content block.
|
|
60
|
+
*/
|
|
61
|
+
export function isViewableImageType(contentType: ImageFileType | undefined): boolean {
|
|
62
|
+
return contentType !== undefined && VIEWABLE_IMAGE_TYPES.has(contentType)
|
|
63
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"outDir": "./dist",
|
|
4
4
|
"rootDir": "./src",
|
|
5
|
-
"lib": [
|
|
6
|
-
|
|
7
|
-
],
|
|
5
|
+
"lib": ["es2024"],
|
|
6
|
+
"types": ["node"],
|
|
8
7
|
"stripInternal": true,
|
|
9
|
-
"noUnusedLocals":
|
|
8
|
+
"noUnusedLocals": true,
|
|
10
9
|
"strict": true,
|
|
11
10
|
"target": "es2024",
|
|
12
11
|
"module": "nodenext",
|
|
@@ -17,11 +16,6 @@
|
|
|
17
16
|
"declaration": false,
|
|
18
17
|
"skipLibCheck": true
|
|
19
18
|
},
|
|
20
|
-
"include": [
|
|
21
|
-
|
|
22
|
-
],
|
|
23
|
-
"exclude": [
|
|
24
|
-
"node_modules",
|
|
25
|
-
"build"
|
|
26
|
-
]
|
|
19
|
+
"include": ["src/*.ts"],
|
|
20
|
+
"exclude": ["node_modules", "build"]
|
|
27
21
|
}
|