@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,133 @@
|
|
|
1
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
2
|
+
import { BOOK_DOCID_PREFIX, type Book } from 'inkdrop-model'
|
|
3
|
+
import { z } from 'zod'
|
|
4
|
+
|
|
5
|
+
import { fetchJSON, postJSON } from '../../client/local-server.js'
|
|
6
|
+
import { documentIdSchema, revisionSchema, timestampSchema } from '../../utils/document-fields.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Registers the tools that read, create and update notebooks.
|
|
10
|
+
*
|
|
11
|
+
* @param server - The MCP server to register on
|
|
12
|
+
*/
|
|
13
|
+
export function registerNotebookTools(server: McpServer): void {
|
|
14
|
+
server.registerTool(
|
|
15
|
+
'list-notebooks',
|
|
16
|
+
{ description: 'Retrieve a list of all notebooks' },
|
|
17
|
+
async () => {
|
|
18
|
+
const books: Array<Book> = await fetchJSON('/books')
|
|
19
|
+
return {
|
|
20
|
+
content: [
|
|
21
|
+
{
|
|
22
|
+
type: 'text',
|
|
23
|
+
text: JSON.stringify(books, null, 2)
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
server.registerTool(
|
|
31
|
+
'read-book',
|
|
32
|
+
{
|
|
33
|
+
description: 'Retrieve a single book',
|
|
34
|
+
inputSchema: {
|
|
35
|
+
bookId: z
|
|
36
|
+
.string()
|
|
37
|
+
.describe(
|
|
38
|
+
'ID of the book to retrieve. It can be found as `_id` in the book docs. It always starts with `book:`.'
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
async ({ bookId }) => {
|
|
43
|
+
if (!bookId.startsWith('book:')) {
|
|
44
|
+
bookId = `book:${bookId}`
|
|
45
|
+
}
|
|
46
|
+
const book: Book = await fetchJSON(`/${bookId}`, {})
|
|
47
|
+
return {
|
|
48
|
+
content: [
|
|
49
|
+
{
|
|
50
|
+
type: 'text',
|
|
51
|
+
text: JSON.stringify(book, null, 2)
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
server.registerTool(
|
|
59
|
+
'create-book',
|
|
60
|
+
{
|
|
61
|
+
description:
|
|
62
|
+
'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.',
|
|
63
|
+
inputSchema: {
|
|
64
|
+
name: z.string().min(1).max(64).describe('The notebook name'),
|
|
65
|
+
parentBookId: documentIdSchema(BOOK_DOCID_PREFIX)
|
|
66
|
+
.optional()
|
|
67
|
+
.describe(
|
|
68
|
+
'The ID of the parent notebook. Omit it to create the notebook at the root level.'
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
async bookData => {
|
|
73
|
+
const res = await postJSON(`/books`, bookData)
|
|
74
|
+
return {
|
|
75
|
+
content: [
|
|
76
|
+
{
|
|
77
|
+
type: 'text',
|
|
78
|
+
text: JSON.stringify(res, null, 2)
|
|
79
|
+
}
|
|
80
|
+
]
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
server.registerTool(
|
|
86
|
+
'update-book',
|
|
87
|
+
{
|
|
88
|
+
description:
|
|
89
|
+
'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.',
|
|
90
|
+
inputSchema: {
|
|
91
|
+
_id: documentIdSchema(BOOK_DOCID_PREFIX).describe(
|
|
92
|
+
'The unique document ID which should start with `book:` and the remains are randomly generated string'
|
|
93
|
+
),
|
|
94
|
+
_rev: revisionSchema
|
|
95
|
+
.optional()
|
|
96
|
+
.describe(
|
|
97
|
+
'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.'
|
|
98
|
+
),
|
|
99
|
+
name: z.string().min(1).max(64).optional().describe('The notebook name'),
|
|
100
|
+
parentBookId: documentIdSchema(BOOK_DOCID_PREFIX)
|
|
101
|
+
.nullable()
|
|
102
|
+
.optional()
|
|
103
|
+
.describe(
|
|
104
|
+
'The ID of the parent notebook. Pass `null` to move the notebook to the root level.'
|
|
105
|
+
),
|
|
106
|
+
createdAt: timestampSchema
|
|
107
|
+
.optional()
|
|
108
|
+
.describe(
|
|
109
|
+
'The date time when the notebook was created, represented with Unix timestamps in milliseconds. Omit it to keep the current value.'
|
|
110
|
+
),
|
|
111
|
+
updatedAt: timestampSchema
|
|
112
|
+
.optional()
|
|
113
|
+
.describe(
|
|
114
|
+
'The date time when the notebook was last updated, represented with Unix timestamps in milliseconds. Defaults to the current time.'
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
async ({ _id, ...updateFields }) => {
|
|
119
|
+
const res = await postJSON(`/${_id}`, {
|
|
120
|
+
...updateFields,
|
|
121
|
+
updatedAt: updateFields.updatedAt ?? +new Date()
|
|
122
|
+
})
|
|
123
|
+
return {
|
|
124
|
+
content: [
|
|
125
|
+
{
|
|
126
|
+
type: 'text',
|
|
127
|
+
text: JSON.stringify(res, null, 2)
|
|
128
|
+
}
|
|
129
|
+
]
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
)
|
|
133
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { parseInkdropUri, type NoteAnchor } from '@inkdropapp/protocol-utils'
|
|
2
|
+
|
|
3
|
+
/** A note, and optionally a location inside it. */
|
|
4
|
+
export type NoteReference = {
|
|
5
|
+
/** The note document ID, with the `note:` prefix. */
|
|
6
|
+
noteId: string
|
|
7
|
+
anchor?: NoteAnchor
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** A 1-based, inclusive line range. */
|
|
11
|
+
export type NoteLineRange = {
|
|
12
|
+
startLine: number
|
|
13
|
+
endLine: number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type NoteLines = NoteLineRange & {
|
|
17
|
+
/** The requested lines, joined with `\n`. */
|
|
18
|
+
text: string
|
|
19
|
+
totalLines: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const INKDROP_URI_PREFIX = 'inkdrop://'
|
|
23
|
+
const LINE_BREAK_PATTERN = /\r\n?|\n/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Parses what a tool received as a note ID: a document ID with or without the
|
|
27
|
+
* `note:` prefix, or a note link such as `inkdrop://note/abc123#L5-L8`.
|
|
28
|
+
*
|
|
29
|
+
* @param noteIdOrUri - The note ID or note URI
|
|
30
|
+
* @returns The note ID, plus the anchor when the URI has one
|
|
31
|
+
* @throws {Error} If the input is an Inkdrop URI that doesn't point to a note
|
|
32
|
+
*/
|
|
33
|
+
export function parseNoteReference(noteIdOrUri: string): NoteReference {
|
|
34
|
+
if (!noteIdOrUri.startsWith(INKDROP_URI_PREFIX)) {
|
|
35
|
+
return { noteId: noteIdOrUri.startsWith('note:') ? noteIdOrUri : `note:${noteIdOrUri}` }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const parsedUri = parseInkdropUri(noteIdOrUri)
|
|
39
|
+
if (parsedUri?.resourceType !== 'note') {
|
|
40
|
+
throw new Error(`\`${noteIdOrUri}\` is not a note URI. Expected \`inkdrop://note/<id>\`.`)
|
|
41
|
+
}
|
|
42
|
+
return parsedUri.anchor
|
|
43
|
+
? { noteId: parsedUri.resourceId, anchor: parsedUri.anchor }
|
|
44
|
+
: { noteId: parsedUri.resourceId }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Picks a line range out of a note body.
|
|
49
|
+
*
|
|
50
|
+
* The range is clamped to the body the same way the editor clamps a line anchor,
|
|
51
|
+
* so a link to lines that no longer exist lands on the last line.
|
|
52
|
+
*
|
|
53
|
+
* @param body - The note body
|
|
54
|
+
* @param lineRange - The lines to pick
|
|
55
|
+
* @returns The lines that were picked, their actual range, and the body's line count
|
|
56
|
+
*/
|
|
57
|
+
export function extractNoteLines(body: string, lineRange: NoteLineRange): NoteLines {
|
|
58
|
+
const lines = body.split(LINE_BREAK_PATTERN)
|
|
59
|
+
const totalLines = lines.length
|
|
60
|
+
const clampLine = (line: number) => Math.min(Math.max(line, 1), totalLines)
|
|
61
|
+
const startLine = clampLine(lineRange.startLine)
|
|
62
|
+
const endLine = clampLine(lineRange.endLine)
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
text: lines.slice(startLine - 1, endLine).join('\n'),
|
|
66
|
+
startLine,
|
|
67
|
+
endLine,
|
|
68
|
+
totalLines
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Words the notice `read-note` adds when `body` is an excerpt, so the excerpt
|
|
74
|
+
* isn't mistaken for the whole note.
|
|
75
|
+
*
|
|
76
|
+
* @param noteLines - The lines that were picked out of the note body
|
|
77
|
+
* @returns The notice text
|
|
78
|
+
*/
|
|
79
|
+
export function describeNoteLines({ startLine, endLine, totalLines }: NoteLines): string {
|
|
80
|
+
const lineLabel = startLine === endLine ? `line ${startLine}` : `lines ${startLine}-${endLine}`
|
|
81
|
+
return (
|
|
82
|
+
`\`body\` holds only ${lineLabel} of this note's ${totalLines} lines, as the link's anchor asks for. ` +
|
|
83
|
+
'Call `read-note` with the note ID alone to get the whole body.'
|
|
84
|
+
)
|
|
85
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { NOTE_DOCID_PREFIX, NOTE_TITLE_MAX_LENGTH, TAG_DOCID_PREFIX } from 'inkdrop-model'
|
|
2
|
+
import { z } from 'zod'
|
|
3
|
+
|
|
4
|
+
import { documentIdSchema, revisionSchema, timestampSchema } from '../../utils/document-fields.js'
|
|
5
|
+
|
|
6
|
+
const noteIdFormatSchema = documentIdSchema(NOTE_DOCID_PREFIX)
|
|
7
|
+
|
|
8
|
+
/** The ID of a note document. */
|
|
9
|
+
export const noteIdSchema = noteIdFormatSchema.describe(
|
|
10
|
+
'The unique document ID which should start with `note:` and the remains are randomly generated string'
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
/** The revision a write must be based on. */
|
|
14
|
+
export const noteRevSchema = revisionSchema.describe(
|
|
15
|
+
'This is a CouchDB specific field. The current MVCC-token/revision of this document (mandatory and immutable).'
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A notebook ID, or one of the `trash` and `template` pseudo-notebooks.
|
|
20
|
+
* It has no description because each tool explains what `template` does there.
|
|
21
|
+
*/
|
|
22
|
+
export const noteBookIdSchema = z
|
|
23
|
+
.string()
|
|
24
|
+
.min(5)
|
|
25
|
+
.max(128)
|
|
26
|
+
.regex(/^(book:|trash$|template$)/)
|
|
27
|
+
|
|
28
|
+
/** The title of a note. */
|
|
29
|
+
export const noteTitleSchema = z.string().max(NOTE_TITLE_MAX_LENGTH).describe('The note title')
|
|
30
|
+
|
|
31
|
+
/** The Markdown body of a note. It has no description because each tool words it differently. */
|
|
32
|
+
export const noteBodySchema = z.string().max(1_048_576)
|
|
33
|
+
|
|
34
|
+
/** The task status of a note. */
|
|
35
|
+
export const noteStatusSchema = z
|
|
36
|
+
.enum(['none', 'active', 'onHold', 'completed', 'dropped'])
|
|
37
|
+
.describe('The status of the note')
|
|
38
|
+
|
|
39
|
+
/** The IDs of the tags assigned to a note. */
|
|
40
|
+
export const noteTagsSchema = z
|
|
41
|
+
.array(documentIdSchema(TAG_DOCID_PREFIX))
|
|
42
|
+
.describe(
|
|
43
|
+
'An array of tag IDs to assign to the note. Call `list-tags` or `read-tag` to retrieve available tags. You can create a new tag with `create-tag` tool if necessary.'
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
/** The creation time of a note, which a write keeps unless it is given. */
|
|
47
|
+
export const noteCreatedAtSchema = timestampSchema.describe(
|
|
48
|
+
'The date time when the note was created, represented with Unix timestamps in milliseconds. Omit it to keep the current value.'
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
/** The last update time of a note, which a write sets to now unless it is given. */
|
|
52
|
+
export const noteUpdatedAtSchema = timestampSchema.describe(
|
|
53
|
+
'The date time when the note was last updated, represented with Unix timestamps in milliseconds. Defaults to the current time.'
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The ID of the template a note was created from.
|
|
58
|
+
* It has no description because only `update-note` can clear it, so each tool words it differently.
|
|
59
|
+
*/
|
|
60
|
+
export const sourceTemplateIdSchema = noteIdFormatSchema
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Note } from 'inkdrop-model'
|
|
2
|
+
|
|
3
|
+
import { fetchJSON, postJSON } from '../../client/local-server.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The fields to change on a note, with the ID and current revision that identify it.
|
|
7
|
+
* A `null` `sourceTemplateId` removes the note's link to its template.
|
|
8
|
+
*/
|
|
9
|
+
export type NoteUpdate = Partial<Omit<Note, 'sourceTemplateId'>> &
|
|
10
|
+
Pick<Note, '_id' | '_rev'> & { sourceTemplateId?: string | null }
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Merges the given fields into the stored note and saves it.
|
|
14
|
+
*
|
|
15
|
+
* @param noteUpdate - The note ID, its current revision, and the fields to change
|
|
16
|
+
* @returns The response of the local server for the saved note
|
|
17
|
+
* @throws If the note can't be fetched at that revision or the save is rejected
|
|
18
|
+
*/
|
|
19
|
+
export async function updateNote(noteUpdate: NoteUpdate) {
|
|
20
|
+
const existingNote = await fetchJSON<Note>(`/${noteUpdate._id}`, { rev: noteUpdate._rev })
|
|
21
|
+
|
|
22
|
+
return postJSON<{ ok: boolean; id: string; rev: string }>('/notes', {
|
|
23
|
+
...existingNote,
|
|
24
|
+
...noteUpdate,
|
|
25
|
+
updatedAt: noteUpdate.updatedAt ?? +new Date()
|
|
26
|
+
})
|
|
27
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ResourceTemplate, type McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
2
|
+
import type { Note } from 'inkdrop-model'
|
|
3
|
+
|
|
4
|
+
import { fetchJSON } from '../../client/local-server.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Registers the `inkdrop://note/{noteId}` resource.
|
|
8
|
+
*
|
|
9
|
+
* @param server - The MCP server to register on
|
|
10
|
+
*/
|
|
11
|
+
export function registerNoteResource(server: McpServer): void {
|
|
12
|
+
server.registerResource(
|
|
13
|
+
'note',
|
|
14
|
+
new ResourceTemplate('inkdrop://note/{noteId}', { list: undefined }),
|
|
15
|
+
{
|
|
16
|
+
description: 'A note data',
|
|
17
|
+
mimeType: 'application/json'
|
|
18
|
+
},
|
|
19
|
+
async (uri, { noteId }) => {
|
|
20
|
+
const id = Array.isArray(noteId) ? noteId[0] : noteId
|
|
21
|
+
const normalizedId = id.startsWith('note:') ? id : `note:${id}`
|
|
22
|
+
const note: Array<Note> = await fetchJSON(`/${normalizedId}`, {})
|
|
23
|
+
return {
|
|
24
|
+
contents: [
|
|
25
|
+
{
|
|
26
|
+
uri: uri.href,
|
|
27
|
+
text: JSON.stringify(note, null, 2),
|
|
28
|
+
mimeType: 'application/json'
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
)
|
|
34
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export type TextEdit = {
|
|
2
|
+
old_string: string
|
|
3
|
+
new_string: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type ApplyTextEditsResult = { ok: true; text: string } | { ok: false; error: string }
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Applies exact string replacements in order, each against the result of the
|
|
10
|
+
* previous one, so a later edit can match text an earlier edit produced.
|
|
11
|
+
*
|
|
12
|
+
* Every `old_string` must match exactly one location at the time it is applied.
|
|
13
|
+
* Processing stops at the first edit that does not, so callers never see a
|
|
14
|
+
* partially patched text.
|
|
15
|
+
*
|
|
16
|
+
* @param text - The text to patch
|
|
17
|
+
* @param edits - The replacements to perform
|
|
18
|
+
* @returns The patched text, or an error naming the 1-based position of the failing edit
|
|
19
|
+
*/
|
|
20
|
+
export function applyTextEdits(text: string, edits: Array<TextEdit>): ApplyTextEditsResult {
|
|
21
|
+
let patchedText = text
|
|
22
|
+
|
|
23
|
+
for (const [editIndex, { old_string, new_string }] of edits.entries()) {
|
|
24
|
+
const editLabel = `edit ${editIndex + 1} of ${edits.length}`
|
|
25
|
+
|
|
26
|
+
const firstMatchIndex = patchedText.indexOf(old_string)
|
|
27
|
+
if (firstMatchIndex === -1) {
|
|
28
|
+
return {
|
|
29
|
+
ok: false,
|
|
30
|
+
error: `\`old_string\` of ${editLabel} was not found in the note body.`
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (patchedText.indexOf(old_string, firstMatchIndex + 1) !== -1) {
|
|
35
|
+
return {
|
|
36
|
+
ok: false,
|
|
37
|
+
error: `\`old_string\` of ${editLabel} matches multiple locations in the note body. Provide more surrounding context to make it unique.`
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
patchedText =
|
|
42
|
+
patchedText.slice(0, firstMatchIndex) +
|
|
43
|
+
new_string +
|
|
44
|
+
patchedText.slice(firstMatchIndex + old_string.length)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { ok: true, text: patchedText }
|
|
48
|
+
}
|