@inkdropapp/mcp-server 1.8.0 → 1.10.0-beta.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/.github/workflows/actions.lock +10 -0
- package/.github/workflows/ci.yml +65 -0
- package/.github/workflows/publish.yml +10 -1
- package/.oxfmtrc.json +2 -1
- package/README.md +16 -6
- package/dist/client/agent-identity.js +45 -0
- package/dist/client/agent-session.js +66 -0
- package/dist/client/local-server.js +64 -0
- package/dist/features/files/image-type.js +31 -0
- package/dist/features/files/tools.js +191 -0
- package/dist/features/notebooks/tools.js +95 -0
- package/dist/features/notes/note-anchor.js +58 -0
- package/dist/features/notes/note-schemas.js +38 -0
- package/dist/features/notes/note-update.js +16 -0
- package/dist/features/notes/resource.js +26 -0
- package/dist/features/notes/text-edits.js +36 -0
- package/dist/features/notes/tools.js +405 -0
- package/dist/features/tags/tools.js +82 -0
- package/dist/index.js +19 -826
- package/dist/utils/document-fields.js +18 -0
- package/mise.toml +5 -0
- package/oxlint.config.ts +50 -0
- package/package.json +22 -14
- package/pnpm-workspace.yaml +6 -0
- package/src/client/agent-identity.ts +70 -0
- package/src/client/agent-session.ts +75 -0
- package/src/client/local-server.ts +73 -0
- package/src/features/files/image-type.ts +35 -0
- package/src/features/files/tools.ts +230 -0
- package/src/features/notebooks/tools.ts +133 -0
- package/src/features/notes/note-anchor.ts +85 -0
- package/src/features/notes/note-schemas.ts +60 -0
- package/src/features/notes/note-update.ts +27 -0
- package/src/features/notes/resource.ts +34 -0
- package/src/features/notes/text-edits.ts +48 -0
- package/src/features/notes/tools.ts +499 -0
- package/src/features/tags/tools.ts +109 -0
- package/src/index.ts +17 -1049
- package/src/utils/document-fields.ts +21 -0
- package/tests/client/agent-identity.test.ts +98 -0
- package/tests/features/notes/note-anchor.test.ts +125 -0
- package/tests/features/notes/note-schemas.test.ts +27 -0
- package/tests/utils/document-fields.test.ts +41 -0
- package/tsconfig.json +1 -1
- package/dist/api.js +0 -47
- package/dist/utils.js +0 -100
- package/src/api.ts +0 -45
- package/src/utils.ts +0 -112
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import { getNoteUri } from '@inkdropapp/protocol-utils'
|
|
2
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
3
|
+
import type { Note } from 'inkdrop-model'
|
|
4
|
+
import { z } from 'zod'
|
|
5
|
+
|
|
6
|
+
import { fetchJSON, postJSON } from '../../client/local-server.js'
|
|
7
|
+
import { describeNoteLines, extractNoteLines, parseNoteReference } from './note-anchor.js'
|
|
8
|
+
import {
|
|
9
|
+
noteBodySchema,
|
|
10
|
+
noteBookIdSchema,
|
|
11
|
+
noteCreatedAtSchema,
|
|
12
|
+
noteIdSchema,
|
|
13
|
+
noteRevSchema,
|
|
14
|
+
noteStatusSchema,
|
|
15
|
+
noteTagsSchema,
|
|
16
|
+
noteTitleSchema,
|
|
17
|
+
noteUpdatedAtSchema,
|
|
18
|
+
sourceTemplateIdSchema
|
|
19
|
+
} from './note-schemas.js'
|
|
20
|
+
import { updateNote } from './note-update.js'
|
|
21
|
+
import { applyTextEdits } from './text-edits.js'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Registers the tools that read, search, create and update notes and templates.
|
|
25
|
+
*
|
|
26
|
+
* @param server - The MCP server to register on
|
|
27
|
+
*/
|
|
28
|
+
export function registerNoteTools(server: McpServer): void {
|
|
29
|
+
server.registerTool(
|
|
30
|
+
'read-note',
|
|
31
|
+
{
|
|
32
|
+
description:
|
|
33
|
+
'Retrieve the complete contents of the note by its ID or its note link from the database. ' +
|
|
34
|
+
'A note link can carry a line anchor (`inkdrop://note/<id>#L5` or `#L5-L8`); ' +
|
|
35
|
+
'the returned `body` then holds only those lines. ' +
|
|
36
|
+
'A note created from a template has a `sourceTemplateId`; when present, the response ' +
|
|
37
|
+
'includes a resource link to that source template, whose body holds the fill-out ' +
|
|
38
|
+
'instructions and examples. Read the template (or pass `includeTemplate: true`) before ' +
|
|
39
|
+
'completing the note.',
|
|
40
|
+
inputSchema: {
|
|
41
|
+
noteId: z
|
|
42
|
+
.string()
|
|
43
|
+
.describe(
|
|
44
|
+
'ID of the note to retrieve, or a note link. The ID can be found as `_id` in the note docs and always starts with `note:`. ' +
|
|
45
|
+
'A note link looks like `inkdrop://note/<id>`, optionally followed by `#L5` (one line) or `#L5-L8` (a line range) to read only those lines.'
|
|
46
|
+
),
|
|
47
|
+
includeTemplate: z
|
|
48
|
+
.boolean()
|
|
49
|
+
.optional()
|
|
50
|
+
.default(false)
|
|
51
|
+
.describe(
|
|
52
|
+
'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.'
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
async ({ noteId: noteIdOrUri, includeTemplate }) => {
|
|
57
|
+
const { noteId, anchor } = parseNoteReference(noteIdOrUri)
|
|
58
|
+
const storedNote = await fetchJSON<Note>(`/${noteId}`, {})
|
|
59
|
+
const anchoredLines =
|
|
60
|
+
anchor?.type === 'lines' ? extractNoteLines(storedNote.body, anchor) : undefined
|
|
61
|
+
const note = anchoredLines ? { ...storedNote, body: anchoredLines.text } : storedNote
|
|
62
|
+
|
|
63
|
+
let templateBlockText: string | null = null
|
|
64
|
+
if (includeTemplate && note.sourceTemplateId) {
|
|
65
|
+
try {
|
|
66
|
+
const template = await fetchJSON<Note>(`/${note.sourceTemplateId}`, {})
|
|
67
|
+
templateBlockText = `# Source template: ${template.title}\n\n${template.body}`
|
|
68
|
+
} catch {
|
|
69
|
+
// Template unavailable (e.g. deleted → 404). Skip the embed; the note read still succeeds.
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
content: [
|
|
75
|
+
{
|
|
76
|
+
type: 'text' as const,
|
|
77
|
+
text: JSON.stringify(note, null, 2)
|
|
78
|
+
},
|
|
79
|
+
...(anchoredLines
|
|
80
|
+
? [{ type: 'text' as const, text: describeNoteLines(anchoredLines) }]
|
|
81
|
+
: []),
|
|
82
|
+
...(anchor?.type === 'heading'
|
|
83
|
+
? [
|
|
84
|
+
{
|
|
85
|
+
type: 'text' as const,
|
|
86
|
+
text: `The link points to the heading whose slug is \`${anchor.slug}\`. Heading anchors are not resolved by this tool, so \`body\` holds the whole note.`
|
|
87
|
+
}
|
|
88
|
+
]
|
|
89
|
+
: []),
|
|
90
|
+
...(note.sourceTemplateId
|
|
91
|
+
? [
|
|
92
|
+
{
|
|
93
|
+
type: 'resource_link' as const,
|
|
94
|
+
uri: getNoteUri(note.sourceTemplateId),
|
|
95
|
+
name: 'Source template',
|
|
96
|
+
mimeType: 'application/json',
|
|
97
|
+
description:
|
|
98
|
+
'The template this note was created from. Read it to get the fill-out instructions and examples before completing this note.'
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
: []),
|
|
102
|
+
...(templateBlockText ? [{ type: 'text' as const, text: templateBlockText }] : [])
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
server.registerTool(
|
|
109
|
+
'read-backlinks',
|
|
110
|
+
{
|
|
111
|
+
description: `Retrieve backlinks for a note — the notes that link to it.
|
|
112
|
+
Each result is returned as a resource link (\`inkdrop://note/<id>\`) pointing to the referring note.
|
|
113
|
+
Backlinks are found by searching all note bodies for the note's link URI.`,
|
|
114
|
+
inputSchema: {
|
|
115
|
+
noteId: z
|
|
116
|
+
.string()
|
|
117
|
+
.describe(
|
|
118
|
+
'ID of the note to find backlinks for. It can be found as `_id` in the note docs. It always starts with `note:`.'
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
async ({ noteId }) => {
|
|
123
|
+
if (!noteId.startsWith('note:')) {
|
|
124
|
+
noteId = `note:${noteId}`
|
|
125
|
+
}
|
|
126
|
+
const noteLink = getNoteUri(noteId)
|
|
127
|
+
const backlinks: Array<Note> = await fetchJSON('/notes', {
|
|
128
|
+
keyword: noteLink,
|
|
129
|
+
limit: 100
|
|
130
|
+
})
|
|
131
|
+
if (backlinks.length === 0) {
|
|
132
|
+
return {
|
|
133
|
+
content: [
|
|
134
|
+
{
|
|
135
|
+
type: 'text',
|
|
136
|
+
text: `No backlinks found for ${noteLink}.`
|
|
137
|
+
}
|
|
138
|
+
]
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
content: backlinks.map(note => ({
|
|
143
|
+
type: 'resource_link' as const,
|
|
144
|
+
uri: getNoteUri(note._id),
|
|
145
|
+
name: note.title || 'Untitled note',
|
|
146
|
+
mimeType: 'application/json'
|
|
147
|
+
}))
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
server.registerTool(
|
|
153
|
+
'search-notes',
|
|
154
|
+
{
|
|
155
|
+
description: `List all notes that contain a given keyword.
|
|
156
|
+
The result does not include entire note bodies as they are truncated in 200 characters.
|
|
157
|
+
You have to retrieve the full note content by calling \`read-note\`.
|
|
158
|
+
Here are tips to specify keywords effectively:
|
|
159
|
+
|
|
160
|
+
## Use special qualifiers to narrow down results
|
|
161
|
+
|
|
162
|
+
You can use special qualifiers to get more accurate results. See the qualifiers and their usage examples:
|
|
163
|
+
|
|
164
|
+
- **book**
|
|
165
|
+
\`book:Blog\`: Searches for notes in the 'Blog' notebook.
|
|
166
|
+
\`book:"Desktop App"\`: Searches for notes in the 'Desktop App' notebook.
|
|
167
|
+
\`book:kGlLniaV\`: Searches for notes in the notebook with the ID 'book:kGlLniaV'.
|
|
168
|
+
- **tag**
|
|
169
|
+
\`tag:JavaScript\`: Searches for all notes having the 'JavaScript' tag. Read more about [tags](https://docs.inkdrop.app/manual/write-notes#tag-notes).
|
|
170
|
+
\`tag:pQ7xYt2R\`: Searches for all notes having the tag with the ID 'tag:pQ7xYt2R'.
|
|
171
|
+
- **status**
|
|
172
|
+
\`status:onHold\`: Searches for all notes with the 'On hold' status. Read more about [statuses](/reference/note-statuses).
|
|
173
|
+
- **title**
|
|
174
|
+
\`title:"JavaScript setTimeout"\`: Searches for the note with the specified title.
|
|
175
|
+
- **body**
|
|
176
|
+
\`body:KEYWORD\`: Searches for a specific word in all notes. Equivalent to a [global search](#search-for-notes-across-all-notebooks).
|
|
177
|
+
|
|
178
|
+
### Combine qualifiers
|
|
179
|
+
|
|
180
|
+
You can combine the filter qualifiers to refine data even more.
|
|
181
|
+
|
|
182
|
+
**Find notes that contain the word 'Hello' and have the 'Issue' tag.**
|
|
183
|
+
|
|
184
|
+
\`\`\`text
|
|
185
|
+
Hello tag:Issue
|
|
186
|
+
\`\`\`
|
|
187
|
+
|
|
188
|
+
**Find notes that contain the word 'Typescript,' have the 'Contribution' tag, and the 'Completed' status**
|
|
189
|
+
|
|
190
|
+
\`\`\`text
|
|
191
|
+
Typescript tag:Contribution status:Completed
|
|
192
|
+
\`\`\`
|
|
193
|
+
|
|
194
|
+
## Search for text with spaces
|
|
195
|
+
|
|
196
|
+
To find the text that includes spaces, put the text into the double quotation marks ("):
|
|
197
|
+
|
|
198
|
+
\`\`\`text
|
|
199
|
+
"database associations"
|
|
200
|
+
\`\`\`
|
|
201
|
+
|
|
202
|
+
## Exclude text from search
|
|
203
|
+
|
|
204
|
+
To exclude text from the search results or ignore a specific qualifier, put the minus sign (-) before it. You can also combine the exclusions. See the examples:
|
|
205
|
+
|
|
206
|
+
- \`-book:Backend "closure functions"\`: Ignores the 'Backend' notebook while searching for the 'closure functions' phrase.
|
|
207
|
+
- \`-tag:JavaScript\`: Ignores all notes having the 'JavaScript' tag.
|
|
208
|
+
- \`-book:Typescript tag:work "Data types"\`: Ignores the 'Typescript' notebook and the 'work' tag while searching for the 'Data types' phrase.
|
|
209
|
+
- \`-status:dropped title:"Sprint 10.0" debounce\`: Ignores notes with the 'Dropped' status while searching for the 'debounce' word in the note with the 'Sprint 10.0' title.
|
|
210
|
+
- \`-"Phrase to ignore" "in the rest of a sentence"\`: Ignores the 'Phrase to ignore' part while searching for 'in the rest of a sentence'.
|
|
211
|
+
|
|
212
|
+
Note that you can't specify excluding modifiers only without including conditions.
|
|
213
|
+
|
|
214
|
+
**WARNING**: Make sure to enter a text to search for after the exclusion modifier.
|
|
215
|
+
|
|
216
|
+
- ✅ Will work
|
|
217
|
+
\`-book:Backend "closure functions"\`
|
|
218
|
+
|
|
219
|
+
- ⛔️ Won't work
|
|
220
|
+
\`-book:Backend\`. There's no query. Inkdrop doesn't understand what to search for.
|
|
221
|
+
`,
|
|
222
|
+
inputSchema: {
|
|
223
|
+
keyword: z.string().describe(`Keyword to search for.`)
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
async ({ keyword }) => {
|
|
227
|
+
const notes: Array<Note> = await fetchJSON('/notes', { keyword, limit: 10 })
|
|
228
|
+
const summaries = notes.map(note => {
|
|
229
|
+
return {
|
|
230
|
+
...note,
|
|
231
|
+
body: note.body.slice(0, 200)
|
|
232
|
+
}
|
|
233
|
+
})
|
|
234
|
+
return {
|
|
235
|
+
content: [
|
|
236
|
+
{
|
|
237
|
+
type: 'text',
|
|
238
|
+
text: JSON.stringify(summaries, null, 2)
|
|
239
|
+
}
|
|
240
|
+
]
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
server.registerTool(
|
|
246
|
+
'list-notes',
|
|
247
|
+
{
|
|
248
|
+
description: `List all notes with specified conditions.
|
|
249
|
+
The result does not include entire note bodies as they are truncated in 200 characters.
|
|
250
|
+
You have to retrieve the full note content by calling \`read-note\`.
|
|
251
|
+
`,
|
|
252
|
+
inputSchema: {
|
|
253
|
+
bookId: z
|
|
254
|
+
.string()
|
|
255
|
+
.optional()
|
|
256
|
+
.describe(
|
|
257
|
+
`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`
|
|
258
|
+
),
|
|
259
|
+
tagIds: z
|
|
260
|
+
.array(z.string())
|
|
261
|
+
.optional()
|
|
262
|
+
.default([])
|
|
263
|
+
.describe(
|
|
264
|
+
`An array of tag IDs to filter. It always starts with 'tag:'. You can retrieve a list of available tags from \`list-tags\`.`
|
|
265
|
+
),
|
|
266
|
+
keyword: z.string().optional().describe(`Keyword to filter notes`),
|
|
267
|
+
sort: z
|
|
268
|
+
.enum(['updatedAt', 'createdAt', 'title'])
|
|
269
|
+
.optional()
|
|
270
|
+
.default('updatedAt')
|
|
271
|
+
.describe(`Sort the documents by the specified field`),
|
|
272
|
+
descending: z
|
|
273
|
+
.boolean()
|
|
274
|
+
.optional()
|
|
275
|
+
.default(true)
|
|
276
|
+
.describe(`Reverse the order of the output documents`),
|
|
277
|
+
limit: z.number().optional().default(100).describe(`Limit the number of results returned`)
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
async ({ bookId, tagIds, keyword, sort, descending, limit }) => {
|
|
281
|
+
const bookFilter = bookId ?? ''
|
|
282
|
+
const tagFilter = tagIds?.join(' ') ?? ''
|
|
283
|
+
keyword = `${bookFilter} ${tagFilter} ${keyword || ''}`.trim()
|
|
284
|
+
const notes: Array<Note> = await fetchJSON('/notes', {
|
|
285
|
+
keyword,
|
|
286
|
+
sort,
|
|
287
|
+
descending,
|
|
288
|
+
limit
|
|
289
|
+
})
|
|
290
|
+
const summaries = notes.map(note => {
|
|
291
|
+
return {
|
|
292
|
+
...note,
|
|
293
|
+
body: note.body.slice(0, 200)
|
|
294
|
+
}
|
|
295
|
+
})
|
|
296
|
+
return {
|
|
297
|
+
content: [
|
|
298
|
+
{
|
|
299
|
+
type: 'text',
|
|
300
|
+
text: JSON.stringify(summaries, null, 2)
|
|
301
|
+
}
|
|
302
|
+
]
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
server.registerTool(
|
|
308
|
+
'list-templates',
|
|
309
|
+
{
|
|
310
|
+
description: `List note templates: your custom templates, followed by the official ones bundled with Inkdrop.
|
|
311
|
+
Templates are notes whose \`bookId\` is \`template\`. Official templates are read-only.
|
|
312
|
+
The result does not include entire template bodies as they are truncated in 200 characters.
|
|
313
|
+
You have to retrieve the full template content by calling \`read-note\`.
|
|
314
|
+
When you create a note from a template, pass the template's \`_id\` as \`sourceTemplateId\` to \`create-note\`.
|
|
315
|
+
`,
|
|
316
|
+
inputSchema: {
|
|
317
|
+
keyword: z
|
|
318
|
+
.string()
|
|
319
|
+
.optional()
|
|
320
|
+
.describe(
|
|
321
|
+
'Filter templates by fuzzy-matching their titles. Search qualifiers such as `book:` or `tag:` are not supported.'
|
|
322
|
+
)
|
|
323
|
+
}
|
|
324
|
+
},
|
|
325
|
+
async ({ keyword }) => {
|
|
326
|
+
const templates: Array<Note> = await fetchJSON('/templates', { keyword })
|
|
327
|
+
const summaries = templates.map(template => {
|
|
328
|
+
return {
|
|
329
|
+
...template,
|
|
330
|
+
body: template.body.slice(0, 200)
|
|
331
|
+
}
|
|
332
|
+
})
|
|
333
|
+
return {
|
|
334
|
+
content: [
|
|
335
|
+
{
|
|
336
|
+
type: 'text',
|
|
337
|
+
text: JSON.stringify(summaries, null, 2)
|
|
338
|
+
}
|
|
339
|
+
]
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
server.registerTool(
|
|
345
|
+
'create-note',
|
|
346
|
+
{
|
|
347
|
+
description: 'Create a new note in the database',
|
|
348
|
+
inputSchema: {
|
|
349
|
+
bookId: noteBookIdSchema.describe(
|
|
350
|
+
'The notebook ID. Specify `template` to create a custom note template'
|
|
351
|
+
),
|
|
352
|
+
title: noteTitleSchema,
|
|
353
|
+
body: noteBodySchema.describe('The content of the note represented with Markdown'),
|
|
354
|
+
status: noteStatusSchema.optional(),
|
|
355
|
+
tags: noteTagsSchema.optional(),
|
|
356
|
+
sourceTemplateId: sourceTemplateIdSchema
|
|
357
|
+
.optional()
|
|
358
|
+
.describe(
|
|
359
|
+
'The ID of the template the note is created from, as returned by `list-templates`. It always starts with `note:`. Pass it when you create a note from a template, so that `read-note` can link the note back to its fill-out instructions.'
|
|
360
|
+
)
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
async noteData => {
|
|
364
|
+
const res = await postJSON(`/notes`, noteData)
|
|
365
|
+
return {
|
|
366
|
+
content: [
|
|
367
|
+
{
|
|
368
|
+
type: 'text',
|
|
369
|
+
text: JSON.stringify(res, null, 2)
|
|
370
|
+
}
|
|
371
|
+
]
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
server.registerTool(
|
|
377
|
+
'update-note',
|
|
378
|
+
{
|
|
379
|
+
description:
|
|
380
|
+
'Update one or more existing notes in the database. Only the fields you provide will be updated; omitted fields remain unchanged. Each note is saved independently, so a failed update does not roll back the others; the result reports the outcome of every note.',
|
|
381
|
+
inputSchema: {
|
|
382
|
+
notes: z
|
|
383
|
+
.array(
|
|
384
|
+
z.object({
|
|
385
|
+
_id: noteIdSchema,
|
|
386
|
+
_rev: noteRevSchema,
|
|
387
|
+
bookId: noteBookIdSchema
|
|
388
|
+
.optional()
|
|
389
|
+
.describe(
|
|
390
|
+
'The notebook ID. Specify `template` to turn the note into a custom note template'
|
|
391
|
+
),
|
|
392
|
+
title: noteTitleSchema.optional(),
|
|
393
|
+
body: noteBodySchema
|
|
394
|
+
.optional()
|
|
395
|
+
.describe(
|
|
396
|
+
'The content of the note in Markdown. NOTE: Do not escape special characters like `\\n`.'
|
|
397
|
+
),
|
|
398
|
+
status: noteStatusSchema.optional(),
|
|
399
|
+
tags: noteTagsSchema.optional(),
|
|
400
|
+
sourceTemplateId: sourceTemplateIdSchema
|
|
401
|
+
.nullable()
|
|
402
|
+
.optional()
|
|
403
|
+
.describe(
|
|
404
|
+
'The ID of the template the note was created from, as returned by `list-templates`. It always starts with `note:`. Pass `null` to remove the link to the template. Omit it to keep the current value.'
|
|
405
|
+
),
|
|
406
|
+
createdAt: noteCreatedAtSchema.optional(),
|
|
407
|
+
updatedAt: noteUpdatedAtSchema.optional()
|
|
408
|
+
})
|
|
409
|
+
)
|
|
410
|
+
.min(1)
|
|
411
|
+
.describe('The notes to update.')
|
|
412
|
+
}
|
|
413
|
+
},
|
|
414
|
+
async ({ notes }) => {
|
|
415
|
+
const settledUpdates = await Promise.allSettled(notes.map(updateNote))
|
|
416
|
+
const results = settledUpdates.map((settledUpdate, noteIndex) =>
|
|
417
|
+
settledUpdate.status === 'fulfilled'
|
|
418
|
+
? settledUpdate.value
|
|
419
|
+
: {
|
|
420
|
+
ok: false,
|
|
421
|
+
id: notes[noteIndex]._id,
|
|
422
|
+
error: (settledUpdate.reason as Error).message
|
|
423
|
+
}
|
|
424
|
+
)
|
|
425
|
+
return {
|
|
426
|
+
content: [
|
|
427
|
+
{
|
|
428
|
+
type: 'text',
|
|
429
|
+
text: JSON.stringify(results, null, 2)
|
|
430
|
+
}
|
|
431
|
+
],
|
|
432
|
+
isError: settledUpdates.some(settledUpdate => settledUpdate.status === 'rejected')
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
server.registerTool(
|
|
438
|
+
'patch-note',
|
|
439
|
+
{
|
|
440
|
+
description:
|
|
441
|
+
'Update the body of the existing note by performing one or more exact string replacements. Use this tool to make partial updates to the note body without replacing the entire content. You must first read the note with `read-note` to get the current body, then specify the exact substrings to replace. Edits are applied in order, each against the result of the previous one, and the note is only saved if all of them succeed.',
|
|
442
|
+
inputSchema: {
|
|
443
|
+
_id: noteIdSchema,
|
|
444
|
+
_rev: noteRevSchema,
|
|
445
|
+
edits: z
|
|
446
|
+
.array(
|
|
447
|
+
z.object({
|
|
448
|
+
old_string: z
|
|
449
|
+
.string()
|
|
450
|
+
.min(1)
|
|
451
|
+
.describe(
|
|
452
|
+
'The exact text to find in the note body. Must match exactly one occurrence. Include enough surrounding context to ensure a unique match.'
|
|
453
|
+
),
|
|
454
|
+
new_string: z
|
|
455
|
+
.string()
|
|
456
|
+
.describe(
|
|
457
|
+
'The text to replace `old_string` with. Use an empty string to delete the matched text.'
|
|
458
|
+
)
|
|
459
|
+
})
|
|
460
|
+
)
|
|
461
|
+
.min(1)
|
|
462
|
+
.describe('The replacements to perform, applied in order.'),
|
|
463
|
+
createdAt: noteCreatedAtSchema.optional(),
|
|
464
|
+
updatedAt: noteUpdatedAtSchema.optional()
|
|
465
|
+
}
|
|
466
|
+
},
|
|
467
|
+
async ({ _id, _rev, edits, createdAt, updatedAt }) => {
|
|
468
|
+
const existingNote = await fetchJSON<Note>(`/${_id}`, { rev: _rev })
|
|
469
|
+
|
|
470
|
+
const patchResult = applyTextEdits(existingNote.body, edits)
|
|
471
|
+
if (!patchResult.ok) {
|
|
472
|
+
return {
|
|
473
|
+
content: [
|
|
474
|
+
{
|
|
475
|
+
type: 'text',
|
|
476
|
+
text: `Failed to patch: ${patchResult.error} No edits were saved.`
|
|
477
|
+
}
|
|
478
|
+
],
|
|
479
|
+
isError: true
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const res = await postJSON('/notes', {
|
|
484
|
+
...existingNote,
|
|
485
|
+
body: patchResult.text,
|
|
486
|
+
createdAt: createdAt ?? existingNote.createdAt,
|
|
487
|
+
updatedAt: updatedAt ?? +new Date()
|
|
488
|
+
})
|
|
489
|
+
return {
|
|
490
|
+
content: [
|
|
491
|
+
{
|
|
492
|
+
type: 'text',
|
|
493
|
+
text: JSON.stringify(res, null, 2)
|
|
494
|
+
}
|
|
495
|
+
]
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
)
|
|
499
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
2
|
+
import { TAG_COLOR, TAG_DOCID_PREFIX, type Tag } from 'inkdrop-model'
|
|
3
|
+
import { z } from 'zod'
|
|
4
|
+
|
|
5
|
+
import { fetchJSON, postJSON } from '../../client/local-server.js'
|
|
6
|
+
import { documentIdSchema, revisionSchema } from '../../utils/document-fields.js'
|
|
7
|
+
|
|
8
|
+
const TagColors = Object.values(TAG_COLOR) as [string, ...Array<string>]
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Registers the tools that read, create and update tags.
|
|
12
|
+
*
|
|
13
|
+
* @param server - The MCP server to register on
|
|
14
|
+
*/
|
|
15
|
+
export function registerTagTools(server: McpServer): void {
|
|
16
|
+
server.registerTool('list-tags', { description: 'Retrieve a list of all tags' }, async () => {
|
|
17
|
+
const tags: Array<Tag> = await fetchJSON('/tags')
|
|
18
|
+
return {
|
|
19
|
+
content: [
|
|
20
|
+
{
|
|
21
|
+
type: 'text',
|
|
22
|
+
text: JSON.stringify(tags, null, 2)
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
server.registerTool(
|
|
29
|
+
'read-tag',
|
|
30
|
+
{
|
|
31
|
+
description: 'Retrieve a single tag',
|
|
32
|
+
inputSchema: {
|
|
33
|
+
tagId: z
|
|
34
|
+
.string()
|
|
35
|
+
.describe(
|
|
36
|
+
'ID of the tag to retrieve. It can be found as `_id` in the tag docs. It always starts with `tag:`.'
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
async ({ tagId }) => {
|
|
41
|
+
if (!tagId.startsWith('tag:')) {
|
|
42
|
+
tagId = `tag:${tagId}`
|
|
43
|
+
}
|
|
44
|
+
const tag: Tag = await fetchJSON(`/${tagId}`, {})
|
|
45
|
+
return {
|
|
46
|
+
content: [
|
|
47
|
+
{
|
|
48
|
+
type: 'text',
|
|
49
|
+
text: JSON.stringify(tag, null, 2)
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
server.registerTool(
|
|
57
|
+
'create-tag',
|
|
58
|
+
{
|
|
59
|
+
description: 'Create a new tag in the database',
|
|
60
|
+
inputSchema: {
|
|
61
|
+
color: z.enum(TagColors).default('default').describe('The color type of the tag'),
|
|
62
|
+
|
|
63
|
+
name: z.string().max(64).describe('The name of the tag')
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
async tagData => {
|
|
67
|
+
const res = await postJSON(`/tags`, tagData)
|
|
68
|
+
return {
|
|
69
|
+
content: [
|
|
70
|
+
{
|
|
71
|
+
type: 'text',
|
|
72
|
+
text: JSON.stringify(res, null, 2)
|
|
73
|
+
}
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
server.registerTool(
|
|
80
|
+
'update-tag',
|
|
81
|
+
{
|
|
82
|
+
description:
|
|
83
|
+
'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.',
|
|
84
|
+
inputSchema: {
|
|
85
|
+
_id: documentIdSchema(TAG_DOCID_PREFIX).describe(
|
|
86
|
+
'The unique document ID which should start with `tag:` and the remains are randomly generated string'
|
|
87
|
+
),
|
|
88
|
+
_rev: revisionSchema
|
|
89
|
+
.optional()
|
|
90
|
+
.describe(
|
|
91
|
+
'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.'
|
|
92
|
+
),
|
|
93
|
+
color: z.enum(TagColors).optional().describe('The color type of the tag'),
|
|
94
|
+
name: z.string().max(64).optional().describe('The name of the tag')
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
async ({ _id, ...updateFields }) => {
|
|
98
|
+
const res = await postJSON(`/${_id}`, updateFields)
|
|
99
|
+
return {
|
|
100
|
+
content: [
|
|
101
|
+
{
|
|
102
|
+
type: 'text',
|
|
103
|
+
text: JSON.stringify(res, null, 2)
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
)
|
|
109
|
+
}
|