@inkdropapp/mcp-server 1.4.0 → 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 +34 -27
- package/dist/api.js +6 -6
- package/dist/index.js +246 -65
- package/dist/utils.js +47 -0
- package/package.json +8 -6
- package/src/api.ts +18 -21
- package/src/index.ts +313 -92
- package/src/utils.ts +50 -0
- package/tsconfig.json +1 -1
- package/prettier.config.js +0 -12
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getNoteUri = getNoteUri;
|
|
4
|
+
exports.getFileUri = getFileUri;
|
|
5
|
+
exports.inferImageContentType = inferImageContentType;
|
|
6
|
+
exports.isViewableImageType = isViewableImageType;
|
|
7
|
+
const node_path_1 = require("node:path");
|
|
8
|
+
const inkdrop_model_1 = require("inkdrop-model");
|
|
4
9
|
/**
|
|
5
10
|
* Builds the canonical Inkdrop note URI for a note ID.
|
|
6
11
|
*
|
|
@@ -14,3 +19,45 @@ exports.getNoteUri = getNoteUri;
|
|
|
14
19
|
function getNoteUri(noteId) {
|
|
15
20
|
return `inkdrop://note/${noteId.replace(/^note:/, '')}`;
|
|
16
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Builds the Inkdrop attachment URI for a file ID.
|
|
24
|
+
*
|
|
25
|
+
* Unlike note links, attachment URIs keep the `file:` prefix and use no slash
|
|
26
|
+
* (`file:foobar` → `inkdrop://file:foobar`). This matches how the desktop app
|
|
27
|
+
* renders an attachment into a note body, so the result can be embedded as-is.
|
|
28
|
+
*
|
|
29
|
+
* @param fileId - The file document ID, with or without the `file:` prefix
|
|
30
|
+
* @returns The `inkdrop://file:<id>` URI
|
|
31
|
+
*/
|
|
32
|
+
function getFileUri(fileId) {
|
|
33
|
+
return `inkdrop://${fileId.startsWith('file:') ? fileId : `file:${fileId}`}`;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Infers the attachment content type from a file name or path extension.
|
|
37
|
+
*
|
|
38
|
+
* `SUPPORTED_IMAGE_MIME_TYPES` is keyed by MIME subtype, which doubles as the file
|
|
39
|
+
* extension for every supported type except SVG, whose subtype is `svg+xml`.
|
|
40
|
+
*
|
|
41
|
+
* @returns The matching image MIME type, or `undefined` when the extension is
|
|
42
|
+
* missing or not a supported attachment type
|
|
43
|
+
*/
|
|
44
|
+
function inferImageContentType(filePath) {
|
|
45
|
+
const ext = (0, node_path_1.extname)(filePath).toLowerCase().replace(/^\./, '');
|
|
46
|
+
return inkdrop_model_1.SUPPORTED_IMAGE_MIME_TYPES[ext === 'svg' ? 'svg+xml' : ext];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Content types the MCP `image` content block can carry. Inkdrop also accepts
|
|
50
|
+
* SVG and HEIC/HEIF attachments, which have to be written to disk instead.
|
|
51
|
+
*/
|
|
52
|
+
const VIEWABLE_IMAGE_TYPES = new Set([
|
|
53
|
+
'image/png',
|
|
54
|
+
'image/jpeg',
|
|
55
|
+
'image/jpg',
|
|
56
|
+
'image/gif'
|
|
57
|
+
]);
|
|
58
|
+
/**
|
|
59
|
+
* Whether an attachment can be returned as an MCP `image` content block.
|
|
60
|
+
*/
|
|
61
|
+
function isViewableImageType(contentType) {
|
|
62
|
+
return contentType !== undefined && VIEWABLE_IMAGE_TYPES.has(contentType);
|
|
63
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inkdropapp/mcp-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Inkdrop Model Context Protocol Server",
|
|
5
5
|
"bin": {
|
|
6
6
|
"inkdrop-mcp-server": "dist/index.js"
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
"build": "tsc && chmod +x dist/*.js",
|
|
11
11
|
"prepublishOnly": "npm run build",
|
|
12
12
|
"watch": "tsc --watch",
|
|
13
|
+
"format": "oxfmt",
|
|
14
|
+
"format:check": "oxfmt --check",
|
|
13
15
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
14
16
|
},
|
|
15
17
|
"repository": {
|
|
@@ -24,14 +26,14 @@
|
|
|
24
26
|
"author": "Takuya Matsuyama<contact@inkdrop.app>",
|
|
25
27
|
"license": "Apache-2.0",
|
|
26
28
|
"dependencies": {
|
|
27
|
-
"@modelcontextprotocol/sdk": "^1.
|
|
29
|
+
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
28
30
|
"ajv": "^8.20.0",
|
|
29
|
-
"inkdrop-model": "^2.13.
|
|
30
|
-
"zod": "^4.4
|
|
31
|
+
"inkdrop-model": "^2.13.1",
|
|
32
|
+
"zod": "^4.5.4"
|
|
31
33
|
},
|
|
32
34
|
"devDependencies": {
|
|
33
|
-
"@types/node": "^26.
|
|
34
|
-
"
|
|
35
|
+
"@types/node": "^26.4.1",
|
|
36
|
+
"oxfmt": "^0.66.0",
|
|
35
37
|
"typescript": "^7.0.2"
|
|
36
38
|
}
|
|
37
39
|
}
|
package/src/api.ts
CHANGED
|
@@ -1,48 +1,45 @@
|
|
|
1
1
|
const config = {
|
|
2
2
|
baseUrl: process.env.INKDROP_LOCAL_SERVER_URL,
|
|
3
3
|
username: process.env.INKDROP_LOCAL_USERNAME,
|
|
4
|
-
password: process.env.INKDROP_LOCAL_PASSWORD
|
|
5
|
-
}
|
|
4
|
+
password: process.env.INKDROP_LOCAL_PASSWORD
|
|
5
|
+
}
|
|
6
6
|
|
|
7
7
|
function getAuthHeaders() {
|
|
8
|
-
const token = Buffer.from(`${config.username}:${config.password}`).toString(
|
|
9
|
-
"base64"
|
|
10
|
-
);
|
|
8
|
+
const token = Buffer.from(`${config.username}:${config.password}`).toString('base64')
|
|
11
9
|
return {
|
|
12
10
|
Authorization: `Basic ${token}`
|
|
13
|
-
}
|
|
11
|
+
}
|
|
14
12
|
}
|
|
15
13
|
|
|
16
14
|
export function buildUrl(path: string, qs: Record<string, any> = {}): string {
|
|
17
|
-
const url = new URL(path, config.baseUrl)
|
|
15
|
+
const url = new URL(path, config.baseUrl)
|
|
18
16
|
for (const [k, v] of Object.entries(qs)) {
|
|
19
|
-
if (v !== undefined && v !== null) url.searchParams.set(k, String(v))
|
|
17
|
+
if (v !== undefined && v !== null) url.searchParams.set(k, String(v))
|
|
20
18
|
}
|
|
21
|
-
return url.toString()
|
|
19
|
+
return url.toString()
|
|
22
20
|
}
|
|
23
21
|
|
|
24
22
|
export async function fetchJSON<T = any>(path: string, qs: Record<string, any> = {}): Promise<T> {
|
|
25
|
-
const url = buildUrl(path, qs)
|
|
26
|
-
const resp = await fetch(url, { headers: getAuthHeaders() })
|
|
23
|
+
const url = buildUrl(path, qs)
|
|
24
|
+
const resp = await fetch(url, { headers: getAuthHeaders() })
|
|
27
25
|
if (!resp.ok) {
|
|
28
|
-
throw new Error(`Fetch error [${resp.status}] ${resp.statusText}`)
|
|
26
|
+
throw new Error(`Fetch error [${resp.status}] ${resp.statusText}`)
|
|
29
27
|
}
|
|
30
|
-
return await resp.json() as T
|
|
28
|
+
return (await resp.json()) as T
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
export async function postJSON(path: string, body: unknown) {
|
|
34
|
-
const url = buildUrl(path)
|
|
31
|
+
export async function postJSON<T = any>(path: string, body: unknown): Promise<T> {
|
|
32
|
+
const url = buildUrl(path)
|
|
35
33
|
const resp = await fetch(url, {
|
|
36
|
-
method:
|
|
34
|
+
method: 'POST',
|
|
37
35
|
headers: {
|
|
38
36
|
...getAuthHeaders(),
|
|
39
|
-
|
|
37
|
+
'Content-Type': 'application/json'
|
|
40
38
|
},
|
|
41
39
|
body: JSON.stringify(body)
|
|
42
|
-
})
|
|
40
|
+
})
|
|
43
41
|
if (!resp.ok) {
|
|
44
|
-
throw new Error(`Fetch error [${resp.status}] ${resp.statusText}`)
|
|
42
|
+
throw new Error(`Fetch error [${resp.status}] ${resp.statusText}`)
|
|
45
43
|
}
|
|
46
|
-
return await resp.json()
|
|
44
|
+
return (await resp.json()) as T
|
|
47
45
|
}
|
|
48
|
-
|
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
|
}
|
|
@@ -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 }) => {
|
|
@@ -326,10 +321,7 @@ server.registerTool(
|
|
|
326
321
|
|
|
327
322
|
title: z.string().max(128).describe('The note title'),
|
|
328
323
|
|
|
329
|
-
body: z
|
|
330
|
-
.string()
|
|
331
|
-
.max(1048576)
|
|
332
|
-
.describe('The content of the note represented with Markdown'),
|
|
324
|
+
body: z.string().max(1048576).describe('The content of the note represented with Markdown'),
|
|
333
325
|
|
|
334
326
|
status: z
|
|
335
327
|
.enum(['none', 'active', 'onHold', 'completed', 'dropped'])
|
|
@@ -495,9 +487,7 @@ server.registerTool(
|
|
|
495
487
|
}
|
|
496
488
|
|
|
497
489
|
const patched =
|
|
498
|
-
body.slice(0, firstIndex) +
|
|
499
|
-
new_string +
|
|
500
|
-
body.slice(firstIndex + old_string.length)
|
|
490
|
+
body.slice(0, firstIndex) + new_string + body.slice(firstIndex + old_string.length)
|
|
501
491
|
|
|
502
492
|
const res = await postJSON('/notes', {
|
|
503
493
|
...existingNote,
|
|
@@ -515,21 +505,17 @@ server.registerTool(
|
|
|
515
505
|
}
|
|
516
506
|
)
|
|
517
507
|
|
|
518
|
-
server.registerTool(
|
|
519
|
-
'
|
|
520
|
-
{
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
text: JSON.stringify(tags, null, 2)
|
|
528
|
-
}
|
|
529
|
-
]
|
|
530
|
-
}
|
|
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
|
+
]
|
|
531
517
|
}
|
|
532
|
-
)
|
|
518
|
+
})
|
|
533
519
|
|
|
534
520
|
server.registerTool(
|
|
535
521
|
'read-tag',
|
|
@@ -564,10 +550,7 @@ server.registerTool(
|
|
|
564
550
|
{
|
|
565
551
|
description: 'Create a new tag in the database',
|
|
566
552
|
inputSchema: {
|
|
567
|
-
color: z
|
|
568
|
-
.enum(TagColors)
|
|
569
|
-
.default('default')
|
|
570
|
-
.describe('The color type of the tag'),
|
|
553
|
+
color: z.enum(TagColors).default('default').describe('The color type of the tag'),
|
|
571
554
|
|
|
572
555
|
name: z.string().max(64).describe('The name of the tag')
|
|
573
556
|
}
|
|
@@ -589,7 +572,7 @@ server.registerTool(
|
|
|
589
572
|
'update-tag',
|
|
590
573
|
{
|
|
591
574
|
description:
|
|
592
|
-
'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.',
|
|
593
576
|
inputSchema: {
|
|
594
577
|
_id: z
|
|
595
578
|
.string()
|
|
@@ -599,23 +582,18 @@ server.registerTool(
|
|
|
599
582
|
.describe(
|
|
600
583
|
'The unique document ID which should start with `tag:` and the remains are randomly generated string'
|
|
601
584
|
),
|
|
602
|
-
|
|
603
585
|
_rev: z
|
|
604
586
|
.string()
|
|
587
|
+
.optional()
|
|
605
588
|
.describe(
|
|
606
|
-
'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.'
|
|
607
590
|
),
|
|
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')
|
|
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')
|
|
615
593
|
}
|
|
616
594
|
},
|
|
617
|
-
async
|
|
618
|
-
const res = await postJSON(
|
|
595
|
+
async ({ _id, ...updateFields }) => {
|
|
596
|
+
const res = await postJSON(`/${_id}`, updateFields)
|
|
619
597
|
return {
|
|
620
598
|
content: [
|
|
621
599
|
{
|
|
@@ -669,49 +647,292 @@ server.registerTool(
|
|
|
669
647
|
}
|
|
670
648
|
)
|
|
671
649
|
|
|
672
|
-
server.
|
|
673
|
-
'
|
|
674
|
-
{
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
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
|
+
{
|
|
680
673
|
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
|
-
\`\`\`
|
|
674
|
+
text: JSON.stringify(res, null, 2)
|
|
675
|
+
}
|
|
676
|
+
]
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
)
|
|
702
680
|
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
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
|
+
)
|
|
706
729
|
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
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
|
|
711
794
|
}
|
|
712
795
|
}
|
|
713
|
-
|
|
714
|
-
|
|
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
|
+
}
|
|
715
936
|
)
|
|
716
937
|
|
|
717
938
|
async function runServer() {
|