@inkdropapp/mcp-server 1.4.0 → 1.6.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/.oxfmtrc.json +12 -0
- package/README.md +47 -36
- package/dist/api.js +6 -6
- package/dist/index.js +280 -70
- package/dist/utils.js +47 -0
- package/package.json +8 -6
- package/src/api.ts +18 -21
- package/src/index.ts +356 -97
- package/src/utils.ts +50 -0
- package/tsconfig.json +1 -1
- package/.github/workflows/claude-code-review.yml +0 -78
- package/.github/workflows/claude.yml +0 -64
- package/prettier.config.js +0 -12
package/src/index.ts
CHANGED
|
@@ -1,23 +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'
|
|
20
|
-
import { getNoteUri } from './utils'
|
|
21
|
+
import { getFileUri, getNoteUri, inferImageContentType, isViewableImageType } from './utils'
|
|
21
22
|
|
|
22
23
|
const server = new McpServer({
|
|
23
24
|
name: 'Inkdrop',
|
|
@@ -103,9 +104,7 @@ server.registerTool(
|
|
|
103
104
|
}
|
|
104
105
|
]
|
|
105
106
|
: []),
|
|
106
|
-
...(templateBlockText
|
|
107
|
-
? [{ type: 'text' as const, text: templateBlockText }]
|
|
108
|
-
: [])
|
|
107
|
+
...(templateBlockText ? [{ type: 'text' as const, text: templateBlockText }] : [])
|
|
109
108
|
]
|
|
110
109
|
}
|
|
111
110
|
}
|
|
@@ -258,7 +257,7 @@ You have to retrieve the full note content by calling \`read-note\`.
|
|
|
258
257
|
.string()
|
|
259
258
|
.optional()
|
|
260
259
|
.describe(
|
|
261
|
-
`ID of the notebook. It always starts with 'book:'. You can retrieve a list of notebooks with \`list-notebooks
|
|
260
|
+
`ID of the notebook. It always starts with 'book:'. You can retrieve a list of notebooks with \`list-notebooks\`. The pseudo-notebooks 'trash' and 'template' are not supported here; use \`list-templates\` for templates`
|
|
262
261
|
),
|
|
263
262
|
tagIds: z
|
|
264
263
|
.array(z.string())
|
|
@@ -278,11 +277,7 @@ You have to retrieve the full note content by calling \`read-note\`.
|
|
|
278
277
|
.optional()
|
|
279
278
|
.default(true)
|
|
280
279
|
.describe(`Reverse the order of the output documents`),
|
|
281
|
-
limit: z
|
|
282
|
-
.number()
|
|
283
|
-
.optional()
|
|
284
|
-
.default(100)
|
|
285
|
-
.describe(`Limit the number of results returned`)
|
|
280
|
+
limit: z.number().optional().default(100).describe(`Limit the number of results returned`)
|
|
286
281
|
}
|
|
287
282
|
},
|
|
288
283
|
async ({ bookId, tagIds, keyword, sort, descending, limit }) => {
|
|
@@ -312,6 +307,42 @@ You have to retrieve the full note content by calling \`read-note\`.
|
|
|
312
307
|
}
|
|
313
308
|
)
|
|
314
309
|
|
|
310
|
+
server.registerTool(
|
|
311
|
+
'list-templates',
|
|
312
|
+
{
|
|
313
|
+
description: `List note templates: your custom templates, followed by the official ones bundled with Inkdrop.
|
|
314
|
+
Templates are notes whose \`bookId\` is \`template\`. Official templates are read-only.
|
|
315
|
+
The result does not include entire template bodies as they are truncated in 200 characters.
|
|
316
|
+
You have to retrieve the full template content by calling \`read-note\`.
|
|
317
|
+
`,
|
|
318
|
+
inputSchema: {
|
|
319
|
+
keyword: z
|
|
320
|
+
.string()
|
|
321
|
+
.optional()
|
|
322
|
+
.describe(
|
|
323
|
+
'Filter templates by fuzzy-matching their titles. Search qualifiers such as `book:` or `tag:` are not supported.'
|
|
324
|
+
)
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
async ({ keyword }) => {
|
|
328
|
+
const templates: Note[] = await fetchJSON('/templates', { keyword })
|
|
329
|
+
const summaries = templates.map(template => {
|
|
330
|
+
return {
|
|
331
|
+
...template,
|
|
332
|
+
body: template.body.substring(0, 200)
|
|
333
|
+
}
|
|
334
|
+
})
|
|
335
|
+
return {
|
|
336
|
+
content: [
|
|
337
|
+
{
|
|
338
|
+
type: 'text',
|
|
339
|
+
text: JSON.stringify(summaries, null, 2)
|
|
340
|
+
}
|
|
341
|
+
]
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
)
|
|
345
|
+
|
|
315
346
|
server.registerTool(
|
|
316
347
|
'create-note',
|
|
317
348
|
{
|
|
@@ -321,15 +352,12 @@ server.registerTool(
|
|
|
321
352
|
.string()
|
|
322
353
|
.min(5)
|
|
323
354
|
.max(128)
|
|
324
|
-
.regex(/^(book:|trash$)/)
|
|
325
|
-
.describe('The notebook ID'),
|
|
355
|
+
.regex(/^(book:|trash$|template$)/)
|
|
356
|
+
.describe('The notebook ID. Specify `template` to create a custom note template'),
|
|
326
357
|
|
|
327
358
|
title: z.string().max(128).describe('The note title'),
|
|
328
359
|
|
|
329
|
-
body: z
|
|
330
|
-
.string()
|
|
331
|
-
.max(1048576)
|
|
332
|
-
.describe('The content of the note represented with Markdown'),
|
|
360
|
+
body: z.string().max(1048576).describe('The content of the note represented with Markdown'),
|
|
333
361
|
|
|
334
362
|
status: z
|
|
335
363
|
.enum(['none', 'active', 'onHold', 'completed', 'dropped'])
|
|
@@ -382,9 +410,11 @@ server.registerTool(
|
|
|
382
410
|
.string()
|
|
383
411
|
.min(5)
|
|
384
412
|
.max(128)
|
|
385
|
-
.regex(/^(book:|trash$)/)
|
|
413
|
+
.regex(/^(book:|trash$|template$)/)
|
|
386
414
|
.optional()
|
|
387
|
-
.describe(
|
|
415
|
+
.describe(
|
|
416
|
+
'The notebook ID. Specify `template` to turn the note into a custom note template'
|
|
417
|
+
),
|
|
388
418
|
|
|
389
419
|
title: z.string().max(128).optional().describe('The note title'),
|
|
390
420
|
|
|
@@ -495,9 +525,7 @@ server.registerTool(
|
|
|
495
525
|
}
|
|
496
526
|
|
|
497
527
|
const patched =
|
|
498
|
-
body.slice(0, firstIndex) +
|
|
499
|
-
new_string +
|
|
500
|
-
body.slice(firstIndex + old_string.length)
|
|
528
|
+
body.slice(0, firstIndex) + new_string + body.slice(firstIndex + old_string.length)
|
|
501
529
|
|
|
502
530
|
const res = await postJSON('/notes', {
|
|
503
531
|
...existingNote,
|
|
@@ -515,21 +543,17 @@ server.registerTool(
|
|
|
515
543
|
}
|
|
516
544
|
)
|
|
517
545
|
|
|
518
|
-
server.registerTool(
|
|
519
|
-
'
|
|
520
|
-
{
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
text: JSON.stringify(tags, null, 2)
|
|
528
|
-
}
|
|
529
|
-
]
|
|
530
|
-
}
|
|
546
|
+
server.registerTool('list-tags', { description: 'Retrieve a list of all tags' }, async () => {
|
|
547
|
+
const tags: Tag[] = await fetchJSON('/tags')
|
|
548
|
+
return {
|
|
549
|
+
content: [
|
|
550
|
+
{
|
|
551
|
+
type: 'text',
|
|
552
|
+
text: JSON.stringify(tags, null, 2)
|
|
553
|
+
}
|
|
554
|
+
]
|
|
531
555
|
}
|
|
532
|
-
)
|
|
556
|
+
})
|
|
533
557
|
|
|
534
558
|
server.registerTool(
|
|
535
559
|
'read-tag',
|
|
@@ -564,10 +588,7 @@ server.registerTool(
|
|
|
564
588
|
{
|
|
565
589
|
description: 'Create a new tag in the database',
|
|
566
590
|
inputSchema: {
|
|
567
|
-
color: z
|
|
568
|
-
.enum(TagColors)
|
|
569
|
-
.default('default')
|
|
570
|
-
.describe('The color type of the tag'),
|
|
591
|
+
color: z.enum(TagColors).default('default').describe('The color type of the tag'),
|
|
571
592
|
|
|
572
593
|
name: z.string().max(64).describe('The name of the tag')
|
|
573
594
|
}
|
|
@@ -589,7 +610,7 @@ server.registerTool(
|
|
|
589
610
|
'update-tag',
|
|
590
611
|
{
|
|
591
612
|
description:
|
|
592
|
-
'Update the existing tag in the database.
|
|
613
|
+
'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.',
|
|
593
614
|
inputSchema: {
|
|
594
615
|
_id: z
|
|
595
616
|
.string()
|
|
@@ -599,23 +620,18 @@ server.registerTool(
|
|
|
599
620
|
.describe(
|
|
600
621
|
'The unique document ID which should start with `tag:` and the remains are randomly generated string'
|
|
601
622
|
),
|
|
602
|
-
|
|
603
623
|
_rev: z
|
|
604
624
|
.string()
|
|
625
|
+
.optional()
|
|
605
626
|
.describe(
|
|
606
|
-
'This is a CouchDB specific field. The current MVCC-token/revision of this document
|
|
627
|
+
'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.'
|
|
607
628
|
),
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
.enum(TagColors)
|
|
611
|
-
.default('default')
|
|
612
|
-
.describe('The color type of the tag'),
|
|
613
|
-
|
|
614
|
-
name: z.string().max(64).describe('The name of the tag')
|
|
629
|
+
color: z.enum(TagColors).optional().describe('The color type of the tag'),
|
|
630
|
+
name: z.string().max(64).optional().describe('The name of the tag')
|
|
615
631
|
}
|
|
616
632
|
},
|
|
617
|
-
async
|
|
618
|
-
const res = await postJSON(
|
|
633
|
+
async ({ _id, ...updateFields }) => {
|
|
634
|
+
const res = await postJSON(`/${_id}`, updateFields)
|
|
619
635
|
return {
|
|
620
636
|
content: [
|
|
621
637
|
{
|
|
@@ -669,49 +685,292 @@ server.registerTool(
|
|
|
669
685
|
}
|
|
670
686
|
)
|
|
671
687
|
|
|
672
|
-
server.
|
|
673
|
-
'
|
|
674
|
-
{
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
688
|
+
server.registerTool(
|
|
689
|
+
'create-book',
|
|
690
|
+
{
|
|
691
|
+
description:
|
|
692
|
+
'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.',
|
|
693
|
+
inputSchema: {
|
|
694
|
+
name: z.string().min(1).max(64).describe('The notebook name'),
|
|
695
|
+
parentBookId: z
|
|
696
|
+
.string()
|
|
697
|
+
.min(6)
|
|
698
|
+
.max(128)
|
|
699
|
+
.regex(/^book:/)
|
|
700
|
+
.optional()
|
|
701
|
+
.describe(
|
|
702
|
+
'The ID of the parent notebook. Omit it to create the notebook at the root level.'
|
|
703
|
+
)
|
|
704
|
+
}
|
|
705
|
+
},
|
|
706
|
+
async bookData => {
|
|
707
|
+
const res = await postJSON(`/books`, bookData)
|
|
708
|
+
return {
|
|
709
|
+
content: [
|
|
710
|
+
{
|
|
680
711
|
type: 'text',
|
|
681
|
-
text:
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
- Get a list of all notebooks
|
|
688
|
-
- Create a new note
|
|
689
|
-
- Update an existing note
|
|
690
|
-
|
|
691
|
-
Best practices:
|
|
692
|
-
- When searching:
|
|
693
|
-
- Use specific, targeted queries for better results (e.g., "auth mobile app" rather than just "auth")
|
|
694
|
-
- Apply relevant filters when asked or when you can infer the appropriate filters to narrow results
|
|
695
|
-
- Use \`read-note\` to get the full note content
|
|
696
|
-
|
|
697
|
-
Model schemas:
|
|
698
|
-
|
|
699
|
-
\`\`\`json
|
|
700
|
-
${JSON.stringify(NoteSchema, null, 2)}
|
|
701
|
-
\`\`\`
|
|
712
|
+
text: JSON.stringify(res, null, 2)
|
|
713
|
+
}
|
|
714
|
+
]
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
)
|
|
702
718
|
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
719
|
+
server.registerTool(
|
|
720
|
+
'update-book',
|
|
721
|
+
{
|
|
722
|
+
description:
|
|
723
|
+
'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.',
|
|
724
|
+
inputSchema: {
|
|
725
|
+
_id: z
|
|
726
|
+
.string()
|
|
727
|
+
.min(6)
|
|
728
|
+
.max(128)
|
|
729
|
+
.regex(/^book:/)
|
|
730
|
+
.describe(
|
|
731
|
+
'The unique document ID which should start with `book:` and the remains are randomly generated string'
|
|
732
|
+
),
|
|
733
|
+
_rev: z
|
|
734
|
+
.string()
|
|
735
|
+
.optional()
|
|
736
|
+
.describe(
|
|
737
|
+
'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.'
|
|
738
|
+
),
|
|
739
|
+
name: z.string().min(1).max(64).optional().describe('The notebook name'),
|
|
740
|
+
parentBookId: z
|
|
741
|
+
.string()
|
|
742
|
+
.min(6)
|
|
743
|
+
.max(128)
|
|
744
|
+
.regex(/^book:/)
|
|
745
|
+
.nullable()
|
|
746
|
+
.optional()
|
|
747
|
+
.describe(
|
|
748
|
+
'The ID of the parent notebook. Pass `null` to move the notebook to the root level.'
|
|
749
|
+
)
|
|
750
|
+
}
|
|
751
|
+
},
|
|
752
|
+
async ({ _id, ...updateFields }) => {
|
|
753
|
+
const res = await postJSON(`/${_id}`, {
|
|
754
|
+
...updateFields,
|
|
755
|
+
updatedAt: +new Date()
|
|
756
|
+
})
|
|
757
|
+
return {
|
|
758
|
+
content: [
|
|
759
|
+
{
|
|
760
|
+
type: 'text',
|
|
761
|
+
text: JSON.stringify(res, null, 2)
|
|
762
|
+
}
|
|
763
|
+
]
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
)
|
|
706
767
|
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
768
|
+
const ImageFileTypes = [...supportedImageFileTypes] as [string, ...string[]]
|
|
769
|
+
|
|
770
|
+
server.registerTool(
|
|
771
|
+
'create-file',
|
|
772
|
+
{
|
|
773
|
+
description:
|
|
774
|
+
'Create a new attachment file in the database from a local image file or from base64 image data. ' +
|
|
775
|
+
'Returns the Markdown to embed the attachment in a note body. ' +
|
|
776
|
+
'Prefer `filePath` over `data`: it keeps the image bytes out of the conversation.',
|
|
777
|
+
inputSchema: {
|
|
778
|
+
filePath: z
|
|
779
|
+
.string()
|
|
780
|
+
.optional()
|
|
781
|
+
.describe(
|
|
782
|
+
'Absolute path to a local image file to attach. Either this or `data` must be given.'
|
|
783
|
+
),
|
|
784
|
+
data: z
|
|
785
|
+
.string()
|
|
786
|
+
.optional()
|
|
787
|
+
.describe(
|
|
788
|
+
'Base64-encoded image data, without a `data:` URI prefix. Use `filePath` instead whenever the image is already on disk.'
|
|
789
|
+
),
|
|
790
|
+
name: z
|
|
791
|
+
.string()
|
|
792
|
+
.min(1)
|
|
793
|
+
.max(128)
|
|
794
|
+
.optional()
|
|
795
|
+
.describe(
|
|
796
|
+
'The file name. Defaults to the file name of `filePath`, or `image.<ext>` when `data` is given.'
|
|
797
|
+
),
|
|
798
|
+
contentType: z
|
|
799
|
+
.enum(ImageFileTypes)
|
|
800
|
+
.optional()
|
|
801
|
+
.describe(
|
|
802
|
+
'The MIME type of the image. Inferred from the `filePath` extension when omitted, so it is required with `data`.'
|
|
803
|
+
)
|
|
804
|
+
}
|
|
805
|
+
},
|
|
806
|
+
async ({ filePath, data, name, contentType }) => {
|
|
807
|
+
if ((filePath === undefined) === (data === undefined)) {
|
|
808
|
+
return {
|
|
809
|
+
content: [
|
|
810
|
+
{
|
|
811
|
+
type: 'text',
|
|
812
|
+
text: 'Failed to create the attachment: provide exactly one of `filePath` or `data`.'
|
|
813
|
+
}
|
|
814
|
+
],
|
|
815
|
+
isError: true
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
let buffer: Buffer
|
|
820
|
+
if (filePath !== undefined) {
|
|
821
|
+
try {
|
|
822
|
+
buffer = await readFile(filePath)
|
|
823
|
+
} catch (e) {
|
|
824
|
+
return {
|
|
825
|
+
content: [
|
|
826
|
+
{
|
|
827
|
+
type: 'text',
|
|
828
|
+
text: `Failed to read the file at ${filePath}: ${(e as Error).message}`
|
|
829
|
+
}
|
|
830
|
+
],
|
|
831
|
+
isError: true
|
|
711
832
|
}
|
|
712
833
|
}
|
|
713
|
-
|
|
714
|
-
|
|
834
|
+
contentType ??= inferImageContentType(filePath)
|
|
835
|
+
name ??= basename(filePath)
|
|
836
|
+
} else {
|
|
837
|
+
buffer = Buffer.from(data!, 'base64')
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
if (!contentType) {
|
|
841
|
+
return {
|
|
842
|
+
content: [
|
|
843
|
+
{
|
|
844
|
+
type: 'text',
|
|
845
|
+
text: `Failed to create the attachment: could not infer the content type${
|
|
846
|
+
filePath ? ` from ${filePath}` : ''
|
|
847
|
+
}. Pass \`contentType\` explicitly. Supported types: ${supportedImageFileTypes.join(', ')}.`
|
|
848
|
+
}
|
|
849
|
+
],
|
|
850
|
+
isError: true
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
const base64 = buffer.toString('base64')
|
|
855
|
+
const contentLength = Math.floor((base64.length * 3) / 4)
|
|
856
|
+
if (contentLength > maxAttachmentFileSize) {
|
|
857
|
+
return {
|
|
858
|
+
content: [
|
|
859
|
+
{
|
|
860
|
+
type: 'text',
|
|
861
|
+
text: `Failed to create the attachment: the file is ${contentLength} bytes, over the ${maxAttachmentFileSize} byte limit.`
|
|
862
|
+
}
|
|
863
|
+
],
|
|
864
|
+
isError: true
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
const res = await postJSON<{ id: string; ok: boolean; rev: string }>('/files', {
|
|
869
|
+
name: name ?? `image.${contentType.replace(/^image\//, '')}`,
|
|
870
|
+
contentType,
|
|
871
|
+
contentLength,
|
|
872
|
+
publicIn: [],
|
|
873
|
+
md5digest: createHash('md5').update(buffer).digest('hex'),
|
|
874
|
+
_attachments: {
|
|
875
|
+
index: {
|
|
876
|
+
content_type: contentType,
|
|
877
|
+
data: base64
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
})
|
|
881
|
+
|
|
882
|
+
return {
|
|
883
|
+
content: [
|
|
884
|
+
{
|
|
885
|
+
type: 'text',
|
|
886
|
+
text: JSON.stringify(res, null, 2)
|
|
887
|
+
},
|
|
888
|
+
{
|
|
889
|
+
type: 'text',
|
|
890
|
+
text: `Markdown to embed this attachment in a note body:\n\n})`
|
|
891
|
+
}
|
|
892
|
+
]
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
)
|
|
896
|
+
|
|
897
|
+
server.registerTool(
|
|
898
|
+
'read-file',
|
|
899
|
+
{
|
|
900
|
+
description:
|
|
901
|
+
'Retrieve an attachment file by its ID. Returns the file metadata, and the image itself when it is a PNG, JPEG or GIF. ' +
|
|
902
|
+
'Pass `outputPath` to write the attachment to disk instead — required to get at SVG, HEIC and HEIF attachments, which cannot be displayed inline.',
|
|
903
|
+
inputSchema: {
|
|
904
|
+
fileId: z
|
|
905
|
+
.string()
|
|
906
|
+
.describe(
|
|
907
|
+
'ID of the attachment to retrieve. It can be found as `_id` in the file docs. It always starts with \`file:\`.'
|
|
908
|
+
),
|
|
909
|
+
outputPath: z
|
|
910
|
+
.string()
|
|
911
|
+
.optional()
|
|
912
|
+
.describe(
|
|
913
|
+
'Absolute path to write the attachment to. When given, the file is saved there and its bytes are not returned inline.'
|
|
914
|
+
),
|
|
915
|
+
includeImage: z
|
|
916
|
+
.boolean()
|
|
917
|
+
.optional()
|
|
918
|
+
.default(true)
|
|
919
|
+
.describe(
|
|
920
|
+
'Whether to return the image itself alongside the metadata. Set to false to fetch only the metadata of a large attachment.'
|
|
921
|
+
)
|
|
922
|
+
}
|
|
923
|
+
},
|
|
924
|
+
async ({ fileId, outputPath, includeImage }) => {
|
|
925
|
+
if (!fileId.startsWith('file:')) fileId = `file:${fileId}`
|
|
926
|
+
const needsData = includeImage || outputPath !== undefined
|
|
927
|
+
const file = await fetchJSON<IDFile>(`/${fileId}`, {
|
|
928
|
+
attachments: needsData ? true : undefined
|
|
929
|
+
})
|
|
930
|
+
|
|
931
|
+
const { _attachments, ...metadata } = file
|
|
932
|
+
const base64 =
|
|
933
|
+
typeof _attachments?.index?.data === 'string' ? _attachments.index.data : undefined
|
|
934
|
+
|
|
935
|
+
const attachmentBlock = async () => {
|
|
936
|
+
if (!needsData) return []
|
|
937
|
+
if (!base64) {
|
|
938
|
+
return [
|
|
939
|
+
{
|
|
940
|
+
type: 'text' as const,
|
|
941
|
+
text: 'The attachment data could not be read from the document.'
|
|
942
|
+
}
|
|
943
|
+
]
|
|
944
|
+
}
|
|
945
|
+
if (outputPath !== undefined) {
|
|
946
|
+
await writeFile(outputPath, Buffer.from(base64, 'base64'))
|
|
947
|
+
return [{ type: 'text' as const, text: `Attachment written to ${outputPath}` }]
|
|
948
|
+
}
|
|
949
|
+
if (!isViewableImageType(file.contentType)) {
|
|
950
|
+
return [
|
|
951
|
+
{
|
|
952
|
+
type: 'text' as const,
|
|
953
|
+
text: `The attachment is a ${file.contentType} file, which cannot be displayed inline. Pass \`outputPath\` to write it to disk.`
|
|
954
|
+
}
|
|
955
|
+
]
|
|
956
|
+
}
|
|
957
|
+
return [
|
|
958
|
+
{
|
|
959
|
+
type: 'image' as const,
|
|
960
|
+
data: base64,
|
|
961
|
+
// `image/jpg` is not a real MIME type; the image block expects `image/jpeg`.
|
|
962
|
+
mimeType: file.contentType === 'image/jpg' ? 'image/jpeg' : file.contentType
|
|
963
|
+
}
|
|
964
|
+
]
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
return {
|
|
968
|
+
content: [
|
|
969
|
+
{ type: 'text' as const, text: JSON.stringify(metadata, null, 2) },
|
|
970
|
+
...(await attachmentBlock())
|
|
971
|
+
]
|
|
972
|
+
}
|
|
973
|
+
}
|
|
715
974
|
)
|
|
716
975
|
|
|
717
976
|
async function runServer() {
|
package/src/utils.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { extname } from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { SUPPORTED_IMAGE_MIME_TYPES, type ImageFileType } from 'inkdrop-model'
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* Builds the canonical Inkdrop note URI for a note ID.
|
|
3
7
|
*
|
|
@@ -11,3 +15,49 @@
|
|
|
11
15
|
export function getNoteUri(noteId: string): string {
|
|
12
16
|
return `inkdrop://note/${noteId.replace(/^note:/, '')}`
|
|
13
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
|
+
}
|