standardnotes-mcp 0.0.0-stage → 3.0.1
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/CHANGELOG.md +183 -0
- package/LICENSE +22 -0
- package/README.md +336 -3
- package/SECURITY.md +56 -0
- package/dist/cli/install.js +180 -0
- package/dist/cli/isMain.js +21 -0
- package/dist/cli/login.js +117 -0
- package/dist/cli/logout.js +22 -0
- package/dist/cli/setup.js +192 -0
- package/dist/config.js +60 -0
- package/dist/index.js +10 -0
- package/dist/security/errors.js +20 -0
- package/dist/security/logger.js +17 -0
- package/dist/security/redact.js +31 -0
- package/dist/security/regex.js +27 -0
- package/dist/server.js +329 -0
- package/dist/sn/cache.js +53 -0
- package/dist/sn/client.js +1999 -0
- package/dist/sn/crypto.js +63 -0
- package/dist/sn/dates.js +157 -0
- package/dist/sn/entitlement.js +75 -0
- package/dist/sn/frontmatter.js +171 -0
- package/dist/sn/governance.js +40 -0
- package/dist/sn/http.js +289 -0
- package/dist/sn/links.js +78 -0
- package/dist/sn/markdownSections.js +141 -0
- package/dist/sn/predicates.js +135 -0
- package/dist/sn/protocol004.js +401 -0
- package/dist/sn/scope.js +68 -0
- package/dist/sn/search.js +260 -0
- package/dist/sn/session.js +55 -0
- package/dist/sn/tagHierarchy.js +105 -0
- package/dist/sn/tasks.js +32 -0
- package/dist/sn/templates.js +23 -0
- package/dist/sn/types.js +12 -0
- package/dist/tools/daily.js +126 -0
- package/dist/tools/export.js +96 -0
- package/dist/tools/history.js +41 -0
- package/dist/tools/links.js +51 -0
- package/dist/tools/notes.js +404 -0
- package/dist/tools/smartviews.js +52 -0
- package/dist/tools/tags.js +82 -0
- package/dist/tools/tasks.js +43 -0
- package/dist/tools/templates.js +145 -0
- package/dist/version.js +12 -0
- package/docs/protocol-004.md +72 -0
- package/docs/self-hosted.md +120 -0
- package/docs/troubleshooting.md +89 -0
- package/package.json +86 -6
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { promises as fs } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { ToolError } from "../security/errors.js";
|
|
5
|
+
import { logger } from "../security/logger.js";
|
|
6
|
+
/**
|
|
7
|
+
* Vault export: decrypted notes to a local folder of markdown files, for
|
|
8
|
+
* backups and for pointing file-based tools at the vault. Gated behind
|
|
9
|
+
* SN_ALLOW_EXPORT (registered only when set) because it writes plaintext
|
|
10
|
+
* to disk. Protected notes are never exported; `ai: no-access` and
|
|
11
|
+
* tag-scoped notes are already invisible to the client.
|
|
12
|
+
*/
|
|
13
|
+
export const exportInput = z.object({
|
|
14
|
+
path: z
|
|
15
|
+
.string()
|
|
16
|
+
.min(1)
|
|
17
|
+
.describe("Absolute directory to export into (created if missing)"),
|
|
18
|
+
tag: z
|
|
19
|
+
.string()
|
|
20
|
+
.min(1)
|
|
21
|
+
.max(256)
|
|
22
|
+
.optional()
|
|
23
|
+
.describe("Export only this tag/folder subtree instead of the whole vault"),
|
|
24
|
+
includeTrashed: z.boolean().default(false),
|
|
25
|
+
});
|
|
26
|
+
function sanitizeFilename(title) {
|
|
27
|
+
const cleaned = title
|
|
28
|
+
// eslint-disable-next-line no-control-regex
|
|
29
|
+
.replace(/[<>:"/\\|?*\x00-\x1f]/g, "-")
|
|
30
|
+
.replace(/^[. ]+|[. ]+$/g, "")
|
|
31
|
+
.slice(0, 80)
|
|
32
|
+
.trim();
|
|
33
|
+
return cleaned === "" ? "untitled" : cleaned;
|
|
34
|
+
}
|
|
35
|
+
export function registerExportHandlers(client, exportDir) {
|
|
36
|
+
return {
|
|
37
|
+
vault_export: async (raw) => {
|
|
38
|
+
const { path: target, tag, includeTrashed } = exportInput.parse(raw);
|
|
39
|
+
if (!path.isAbsolute(target)) {
|
|
40
|
+
throw new ToolError("path_not_absolute", `Export path must be absolute, got "${target}".`, 'Pass a full path like "C:\\\\backups\\\\sn-vault" or "/home/me/sn-vault".');
|
|
41
|
+
}
|
|
42
|
+
const resolved = path.resolve(target);
|
|
43
|
+
if (exportDir !== null) {
|
|
44
|
+
const root = path.resolve(exportDir);
|
|
45
|
+
if (resolved !== root && !resolved.startsWith(root + path.sep)) {
|
|
46
|
+
throw new ToolError("path_forbidden", `Exports are confined to ${root} (SN_EXPORT_DIR).`, `Choose a directory inside ${root}.`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const notes = await client.listNotes({
|
|
50
|
+
limit: 100_000,
|
|
51
|
+
offset: 0,
|
|
52
|
+
includeTrashed,
|
|
53
|
+
tag,
|
|
54
|
+
includeDescendants: true,
|
|
55
|
+
});
|
|
56
|
+
await fs.mkdir(resolved, { recursive: true });
|
|
57
|
+
const index = [];
|
|
58
|
+
let skippedProtected = 0;
|
|
59
|
+
for (const summary of notes) {
|
|
60
|
+
if (summary.protected) {
|
|
61
|
+
skippedProtected++;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const note = await client.getNote(summary.uuid);
|
|
65
|
+
if (!note)
|
|
66
|
+
continue;
|
|
67
|
+
const ext = note.noteType === "super" ? ".json" : ".md";
|
|
68
|
+
const filename = `${sanitizeFilename(note.title)} (${note.uuid.slice(0, 8)})${ext}`;
|
|
69
|
+
await fs.writeFile(path.join(resolved, filename), note.text, "utf8");
|
|
70
|
+
index.push({
|
|
71
|
+
file: filename,
|
|
72
|
+
uuid: note.uuid,
|
|
73
|
+
title: note.title,
|
|
74
|
+
tags: note.tags,
|
|
75
|
+
noteType: note.noteType,
|
|
76
|
+
createdAt: note.createdAt,
|
|
77
|
+
updatedAt: note.updatedAt,
|
|
78
|
+
trashed: note.trashed,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
await fs.writeFile(path.join(resolved, "index.json"), JSON.stringify({ exportedAt: new Date().toISOString(), notes: index }, null, 2), "utf8");
|
|
82
|
+
logger.info("Vault export complete", {
|
|
83
|
+
path: resolved,
|
|
84
|
+
exported: index.length,
|
|
85
|
+
skippedProtected,
|
|
86
|
+
});
|
|
87
|
+
return {
|
|
88
|
+
ok: true,
|
|
89
|
+
path: resolved,
|
|
90
|
+
exported: index.length,
|
|
91
|
+
indexFile: "index.json",
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=export.js.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { uuidSchema } from "./notes.js";
|
|
3
|
+
import { isPaid } from "../sn/entitlement.js";
|
|
4
|
+
export const historyInput = z.object({
|
|
5
|
+
uuid: uuidSchema.describe("The note whose revision history to list"),
|
|
6
|
+
});
|
|
7
|
+
export const revisionGetInput = z.object({
|
|
8
|
+
uuid: uuidSchema,
|
|
9
|
+
revisionUuid: uuidSchema.describe("From notes_history"),
|
|
10
|
+
});
|
|
11
|
+
export const revisionRestoreInput = z.object({
|
|
12
|
+
uuid: uuidSchema,
|
|
13
|
+
revisionUuid: uuidSchema.describe("From notes_history"),
|
|
14
|
+
});
|
|
15
|
+
export function registerHistoryHandlers(client) {
|
|
16
|
+
return {
|
|
17
|
+
notes_history: async (raw) => {
|
|
18
|
+
const { uuid } = historyInput.parse(raw);
|
|
19
|
+
const revisions = await client.listNoteRevisions(uuid);
|
|
20
|
+
return isPaid(client.entitlement())
|
|
21
|
+
? { uuid, count: revisions.length, revisions }
|
|
22
|
+
: {
|
|
23
|
+
uuid,
|
|
24
|
+
count: revisions.length,
|
|
25
|
+
revisions,
|
|
26
|
+
notice: "Free Standard Notes plans show recent revisions only; longer note history is a paid Standard Notes feature.",
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
notes_revision_get: async (raw) => {
|
|
30
|
+
const { uuid, revisionUuid } = revisionGetInput.parse(raw);
|
|
31
|
+
return client.getNoteRevision(uuid, revisionUuid);
|
|
32
|
+
},
|
|
33
|
+
notes_revision_restore: async (raw) => {
|
|
34
|
+
const { uuid, revisionUuid } = revisionRestoreInput.parse(raw);
|
|
35
|
+
const delta = await client.restoreNoteRevision(uuid, revisionUuid);
|
|
36
|
+
await client.sync();
|
|
37
|
+
return { ok: true, uuid, revisionUuid, ...delta };
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=history.js.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { isPaid } from "../sn/entitlement.js";
|
|
3
|
+
import { refuseWrite, uuidSchema } from "./notes.js";
|
|
4
|
+
export const linksInput = z.object({
|
|
5
|
+
uuid: uuidSchema.describe("The note whose links and backlinks to list"),
|
|
6
|
+
});
|
|
7
|
+
export const linkInput = z.object({
|
|
8
|
+
uuid: uuidSchema.describe("The note to add the link to (the source)"),
|
|
9
|
+
target: uuidSchema.describe("The note being linked to"),
|
|
10
|
+
section: z
|
|
11
|
+
.string()
|
|
12
|
+
.max(500)
|
|
13
|
+
.optional()
|
|
14
|
+
.describe('Optional heading to put the [[wikilink]] under, e.g. "Related" or "Parent::Child"'),
|
|
15
|
+
});
|
|
16
|
+
// Second line of defence: the client never returns protected notes.
|
|
17
|
+
const visible = (n) => !n.protected;
|
|
18
|
+
export function registerLinkHandlers(client) {
|
|
19
|
+
return {
|
|
20
|
+
notes_links: async (raw) => {
|
|
21
|
+
const { uuid } = linksInput.parse(raw);
|
|
22
|
+
const links = await client.getLinks(uuid);
|
|
23
|
+
return {
|
|
24
|
+
...links,
|
|
25
|
+
outgoing: links.outgoing.filter(visible),
|
|
26
|
+
backlinks: links.backlinks.filter(visible),
|
|
27
|
+
ambiguous: links.ambiguous.map((a) => ({
|
|
28
|
+
...a,
|
|
29
|
+
candidates: a.candidates.filter(visible),
|
|
30
|
+
})),
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
notes_link: async (raw) => {
|
|
34
|
+
const args = linkInput.parse(raw);
|
|
35
|
+
const existing = await client.getNote(args.uuid);
|
|
36
|
+
if (existing && (existing.protected || existing.locked)) {
|
|
37
|
+
refuseWrite(existing, "link from");
|
|
38
|
+
}
|
|
39
|
+
const result = await client.linkNotes(args);
|
|
40
|
+
await client.sync();
|
|
41
|
+
const notices = [];
|
|
42
|
+
if (!isPaid(client.entitlement())) {
|
|
43
|
+
notices.push("Linked with a [[wikilink]] in the note text. Native Standard Notes note links are a paid Standard Notes feature, so the link shows as text in the app.");
|
|
44
|
+
}
|
|
45
|
+
return notices.length > 0
|
|
46
|
+
? { ok: true, ...result, notices }
|
|
47
|
+
: { ok: true, ...result };
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=links.js.map
|
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { NOTE_TYPES } from "../sn/types.js";
|
|
3
|
+
import { decideNoteType } from "../sn/entitlement.js";
|
|
4
|
+
import { ToolError } from "../security/errors.js";
|
|
5
|
+
import { parseDateInput, resolveTimeZone } from "../sn/dates.js";
|
|
6
|
+
import { parseFrontmatter } from "../sn/frontmatter.js";
|
|
7
|
+
const MAX_TEXT_BYTES = 10 * 1024 * 1024;
|
|
8
|
+
export const uuidSchema = z.string().uuid();
|
|
9
|
+
export const noteTypeSchema = z.enum(NOTE_TYPES);
|
|
10
|
+
export const listInput = z.object({
|
|
11
|
+
limit: z.number().int().min(1).max(200).default(50),
|
|
12
|
+
offset: z.number().int().min(0).default(0),
|
|
13
|
+
includeTrashed: z.boolean().default(false),
|
|
14
|
+
tag: z
|
|
15
|
+
.string()
|
|
16
|
+
.min(1)
|
|
17
|
+
.max(256)
|
|
18
|
+
.optional()
|
|
19
|
+
.describe("Filter by tag UUID or title (case-insensitive)"),
|
|
20
|
+
includeDescendants: z
|
|
21
|
+
.boolean()
|
|
22
|
+
.default(false)
|
|
23
|
+
.describe("When `tag` is set, also include notes filed under any descendant tag (SN folder behavior). Off by default."),
|
|
24
|
+
});
|
|
25
|
+
export const statsInput = z.object({});
|
|
26
|
+
export const searchInput = z.object({
|
|
27
|
+
query: z
|
|
28
|
+
.string()
|
|
29
|
+
.min(1)
|
|
30
|
+
.max(500)
|
|
31
|
+
.describe('mode "text": every space-separated word must match the title or body (small typos tolerated unless fuzzy=false); results ranked by relevance. mode "regex": a single regular expression.'),
|
|
32
|
+
mode: z.enum(["text", "regex"]).default("text"),
|
|
33
|
+
sort: z
|
|
34
|
+
.enum(["relevance", "updated"])
|
|
35
|
+
.optional()
|
|
36
|
+
.describe('Result order. Default: "relevance" in text mode (best matches first), "updated" in regex mode (newest first).'),
|
|
37
|
+
fuzzy: z
|
|
38
|
+
.boolean()
|
|
39
|
+
.default(true)
|
|
40
|
+
.describe("Text mode: also match near-miss spellings and similar titles. false = exact words only."),
|
|
41
|
+
limit: z.number().int().min(1).max(100).default(20),
|
|
42
|
+
cursor: z
|
|
43
|
+
.string()
|
|
44
|
+
.optional()
|
|
45
|
+
.describe("nextCursor value from a previous search call, for paging"),
|
|
46
|
+
contextLength: z
|
|
47
|
+
.number()
|
|
48
|
+
.int()
|
|
49
|
+
.min(20)
|
|
50
|
+
.max(500)
|
|
51
|
+
.default(100)
|
|
52
|
+
.describe("Approximate characters of context around each match snippet"),
|
|
53
|
+
maxMatchesPerHit: z.number().int().min(1).max(50).default(10),
|
|
54
|
+
tag: z
|
|
55
|
+
.string()
|
|
56
|
+
.min(1)
|
|
57
|
+
.max(256)
|
|
58
|
+
.optional()
|
|
59
|
+
.describe("Restrict to notes filed under this tag (UUID or title)"),
|
|
60
|
+
includeDescendants: z.boolean().default(false),
|
|
61
|
+
noteType: noteTypeSchema.optional(),
|
|
62
|
+
updatedAfter: z
|
|
63
|
+
.string()
|
|
64
|
+
.optional()
|
|
65
|
+
.describe('Only notes updated on/after this date. "YYYY-MM-DD" is interpreted in SN_TIMEZONE; full ISO datetimes are taken as-is.'),
|
|
66
|
+
updatedBefore: z.string().optional(),
|
|
67
|
+
includeTrashed: z.boolean().default(false),
|
|
68
|
+
});
|
|
69
|
+
export const getInput = z.object({
|
|
70
|
+
uuid: uuidSchema,
|
|
71
|
+
format: z
|
|
72
|
+
.enum(["content", "full", "outline", "section"])
|
|
73
|
+
.default("content")
|
|
74
|
+
.describe("content: uuid/title/text/noteType. full: everything incl. tags, timestamps, flags. outline: heading map only, no body. section: one heading's body (requires `section`)."),
|
|
75
|
+
section: z
|
|
76
|
+
.string()
|
|
77
|
+
.min(1)
|
|
78
|
+
.max(500)
|
|
79
|
+
.optional()
|
|
80
|
+
.describe('Heading path for format=section, e.g. "Projects::Alpha"'),
|
|
81
|
+
});
|
|
82
|
+
const tagsArraySchema = z.array(uuidSchema).max(64);
|
|
83
|
+
export const createInput = z.object({
|
|
84
|
+
title: z.string().max(500),
|
|
85
|
+
text: z
|
|
86
|
+
.string()
|
|
87
|
+
.max(MAX_TEXT_BYTES)
|
|
88
|
+
.refine((s) => Buffer.byteLength(s, "utf8") <= MAX_TEXT_BYTES, {
|
|
89
|
+
message: "text exceeds 10MB",
|
|
90
|
+
}),
|
|
91
|
+
noteType: noteTypeSchema.optional(),
|
|
92
|
+
tags: tagsArraySchema.optional(),
|
|
93
|
+
});
|
|
94
|
+
export const createManyInput = z.object({
|
|
95
|
+
notes: z.array(createInput).min(1).max(50),
|
|
96
|
+
});
|
|
97
|
+
export const updateInput = z
|
|
98
|
+
.object({
|
|
99
|
+
uuid: uuidSchema,
|
|
100
|
+
title: z.string().max(500).optional(),
|
|
101
|
+
text: z.string().max(MAX_TEXT_BYTES).optional(),
|
|
102
|
+
noteType: noteTypeSchema.optional(),
|
|
103
|
+
tags: tagsArraySchema.optional(),
|
|
104
|
+
trashed: z
|
|
105
|
+
.boolean()
|
|
106
|
+
.optional()
|
|
107
|
+
.describe("Set true to trash the note, false to restore from trash"),
|
|
108
|
+
archived: z.boolean().optional().describe("Archive / unarchive the note"),
|
|
109
|
+
pinned: z.boolean().optional().describe("Pin / unpin the note"),
|
|
110
|
+
})
|
|
111
|
+
.refine((v) => v.title !== undefined ||
|
|
112
|
+
v.text !== undefined ||
|
|
113
|
+
v.noteType !== undefined ||
|
|
114
|
+
v.tags !== undefined ||
|
|
115
|
+
v.trashed !== undefined ||
|
|
116
|
+
v.archived !== undefined ||
|
|
117
|
+
v.pinned !== undefined, {
|
|
118
|
+
message: "provide at least one of title, text, noteType, tags, trashed, archived, or pinned",
|
|
119
|
+
});
|
|
120
|
+
export const appendInput = z.object({
|
|
121
|
+
uuid: uuidSchema,
|
|
122
|
+
content: z
|
|
123
|
+
.string()
|
|
124
|
+
.min(1)
|
|
125
|
+
.max(MAX_TEXT_BYTES)
|
|
126
|
+
.describe("Text to insert"),
|
|
127
|
+
position: z.enum(["append", "prepend"]).default("append"),
|
|
128
|
+
section: z
|
|
129
|
+
.string()
|
|
130
|
+
.min(1)
|
|
131
|
+
.max(500)
|
|
132
|
+
.optional()
|
|
133
|
+
.describe('Markdown heading to target, as "Heading" or a path like "Parent::Child". Omit to insert at the start/end of the whole note.'),
|
|
134
|
+
});
|
|
135
|
+
export const replaceInInput = z.object({
|
|
136
|
+
uuid: uuidSchema,
|
|
137
|
+
replacements: z
|
|
138
|
+
.array(z.object({
|
|
139
|
+
find: z.string().min(1).max(1024),
|
|
140
|
+
replace: z.string().max(MAX_TEXT_BYTES),
|
|
141
|
+
useRegex: z.boolean().default(false),
|
|
142
|
+
caseSensitive: z.boolean().default(true),
|
|
143
|
+
replaceAll: z.boolean().default(true),
|
|
144
|
+
}))
|
|
145
|
+
.min(1)
|
|
146
|
+
.max(20)
|
|
147
|
+
.describe("Replacements run in order, each over the previous result"),
|
|
148
|
+
});
|
|
149
|
+
export const deleteInput = z.object({
|
|
150
|
+
uuid: uuidSchema,
|
|
151
|
+
permanent: z.boolean().default(false),
|
|
152
|
+
});
|
|
153
|
+
export const updateManyInput = z.object({
|
|
154
|
+
updates: z
|
|
155
|
+
.array(updateInput)
|
|
156
|
+
.min(1)
|
|
157
|
+
.max(50)
|
|
158
|
+
.describe("One patch object per note, same fields as notes_update"),
|
|
159
|
+
});
|
|
160
|
+
export const deleteManyInput = z.object({
|
|
161
|
+
uuids: z.array(uuidSchema).min(1).max(50),
|
|
162
|
+
permanent: z.boolean().default(false),
|
|
163
|
+
});
|
|
164
|
+
export const frontmatterInput = z
|
|
165
|
+
.object({
|
|
166
|
+
uuid: uuidSchema,
|
|
167
|
+
operation: z.enum(["get", "set", "delete"]),
|
|
168
|
+
key: z
|
|
169
|
+
.string()
|
|
170
|
+
.min(1)
|
|
171
|
+
.max(128)
|
|
172
|
+
.optional()
|
|
173
|
+
.describe("Frontmatter key, e.g. \"status\" or \"ai\". Omit for get to return all keys."),
|
|
174
|
+
value: z
|
|
175
|
+
.unknown()
|
|
176
|
+
.optional()
|
|
177
|
+
.describe("For set: string, number, boolean, or array of strings"),
|
|
178
|
+
})
|
|
179
|
+
.refine((v) => v.operation === "get" || v.key !== undefined, {
|
|
180
|
+
message: "set and delete require `key`",
|
|
181
|
+
})
|
|
182
|
+
.refine((v) => v.operation !== "set" || v.value !== undefined, {
|
|
183
|
+
message: "set requires `value`",
|
|
184
|
+
});
|
|
185
|
+
// `protected` (SN top-level content flag) = the user has marked the note as
|
|
186
|
+
// requiring re-auth to view. Such notes are invisible to the AI: the client
|
|
187
|
+
// already treats them as nonexistent, and these checks are a second line of
|
|
188
|
+
// defence that answers exactly like a missing note — no title, no
|
|
189
|
+
// "[Protected]" placeholder, no "this note is protected" error, nothing that
|
|
190
|
+
// confirms the note exists.
|
|
191
|
+
// `locked` (SN appData edit-lock) = read-only. Content stays visible, only
|
|
192
|
+
// writes are refused.
|
|
193
|
+
export function notProtected(n) {
|
|
194
|
+
return !n.protected;
|
|
195
|
+
}
|
|
196
|
+
export function noteNotFoundError(uuid) {
|
|
197
|
+
return new ToolError("note_not_found", `Note ${uuid} not found`, "Run notes_search or notes_list to find the note's uuid.");
|
|
198
|
+
}
|
|
199
|
+
function refuseRead(note) {
|
|
200
|
+
throw noteNotFoundError(note.uuid);
|
|
201
|
+
}
|
|
202
|
+
export function refuseWrite(note, op) {
|
|
203
|
+
if (note.protected)
|
|
204
|
+
throw noteNotFoundError(note.uuid);
|
|
205
|
+
const reason = "edit-locked";
|
|
206
|
+
throw new Error(`Note ${note.uuid} is ${reason} — cannot ${op} via MCP. ` +
|
|
207
|
+
`Unlock it in the Standard Notes app first.`);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Explains a paid-editor note type being saved as plain text on a free
|
|
211
|
+
* account (see decideNoteType). `super` never reaches here — the client
|
|
212
|
+
* refuses it before writing.
|
|
213
|
+
*/
|
|
214
|
+
function noteTypeNotice(client, requested) {
|
|
215
|
+
if (requested === undefined || requested === "super")
|
|
216
|
+
return undefined;
|
|
217
|
+
return decideNoteType(requested, client.entitlement()).notice;
|
|
218
|
+
}
|
|
219
|
+
function withNotices(result, notices) {
|
|
220
|
+
const unique = [...new Set(notices.filter((n) => !!n))];
|
|
221
|
+
return unique.length > 0 ? { ...result, notices: unique } : result;
|
|
222
|
+
}
|
|
223
|
+
export function registerNoteHandlers(client, opts = {}) {
|
|
224
|
+
return {
|
|
225
|
+
notes_list: async (raw) => {
|
|
226
|
+
const args = listInput.parse(raw);
|
|
227
|
+
const notes = await client.listNotes(args);
|
|
228
|
+
return notes.filter(notProtected);
|
|
229
|
+
},
|
|
230
|
+
notes_search: async (raw) => {
|
|
231
|
+
const args = searchInput.parse(raw);
|
|
232
|
+
const tz = resolveTimeZone();
|
|
233
|
+
const result = await client.searchNotes({
|
|
234
|
+
query: args.query,
|
|
235
|
+
mode: args.mode,
|
|
236
|
+
sort: args.sort,
|
|
237
|
+
fuzzy: args.fuzzy,
|
|
238
|
+
limit: args.limit,
|
|
239
|
+
cursor: args.cursor,
|
|
240
|
+
contextLength: args.contextLength,
|
|
241
|
+
maxMatchesPerHit: args.maxMatchesPerHit,
|
|
242
|
+
tag: args.tag,
|
|
243
|
+
includeDescendants: args.includeDescendants,
|
|
244
|
+
noteType: args.noteType,
|
|
245
|
+
updatedAfter: args.updatedAfter !== undefined
|
|
246
|
+
? parseDateInput(args.updatedAfter, tz, false)
|
|
247
|
+
: undefined,
|
|
248
|
+
updatedBefore: args.updatedBefore !== undefined
|
|
249
|
+
? parseDateInput(args.updatedBefore, tz, true)
|
|
250
|
+
: undefined,
|
|
251
|
+
includeTrashed: args.includeTrashed,
|
|
252
|
+
});
|
|
253
|
+
return {
|
|
254
|
+
...result,
|
|
255
|
+
// Second line of defence: the client never returns protected notes.
|
|
256
|
+
hits: result.hits.filter(notProtected),
|
|
257
|
+
};
|
|
258
|
+
},
|
|
259
|
+
notes_get: async (raw) => {
|
|
260
|
+
const { uuid, format, section } = getInput.parse(raw);
|
|
261
|
+
const note = await client.getNote(uuid);
|
|
262
|
+
if (!note) {
|
|
263
|
+
throw new ToolError("note_not_found", `Note ${uuid} not found`, "Run notes_search or notes_list to find the note's uuid.");
|
|
264
|
+
}
|
|
265
|
+
if (note.protected)
|
|
266
|
+
refuseRead(note);
|
|
267
|
+
if (format === "full")
|
|
268
|
+
return note;
|
|
269
|
+
if (format === "outline") {
|
|
270
|
+
const headings = await client.getOutline(uuid);
|
|
271
|
+
return { uuid: note.uuid, title: note.title, headings };
|
|
272
|
+
}
|
|
273
|
+
if (format === "section") {
|
|
274
|
+
if (section === undefined || section === "") {
|
|
275
|
+
throw new ToolError("section_required", "format=section requires the `section` parameter.", 'Pass a heading like "Log" or "Projects::Alpha". Use format=outline to list headings.');
|
|
276
|
+
}
|
|
277
|
+
const body = await client.getSection(uuid, section);
|
|
278
|
+
return { uuid: note.uuid, title: note.title, section, body };
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
uuid: note.uuid,
|
|
282
|
+
title: note.title,
|
|
283
|
+
text: note.text,
|
|
284
|
+
noteType: note.noteType,
|
|
285
|
+
};
|
|
286
|
+
},
|
|
287
|
+
notes_create: async (raw) => {
|
|
288
|
+
const args = createInput.parse(raw);
|
|
289
|
+
const uuid = await client.createNote(args);
|
|
290
|
+
await client.sync();
|
|
291
|
+
return withNotices({ uuid }, [noteTypeNotice(client, args.noteType)]);
|
|
292
|
+
},
|
|
293
|
+
notes_create_many: async (raw) => {
|
|
294
|
+
const { notes } = createManyInput.parse(raw);
|
|
295
|
+
const created = await client.createNotesBatch(notes);
|
|
296
|
+
await client.sync();
|
|
297
|
+
return withNotices({ created }, notes.map((n) => noteTypeNotice(client, n.noteType)));
|
|
298
|
+
},
|
|
299
|
+
notes_update: async (raw) => {
|
|
300
|
+
const args = updateInput.parse(raw);
|
|
301
|
+
const existing = await client.getNote(args.uuid);
|
|
302
|
+
if (existing && (existing.protected || existing.locked)) {
|
|
303
|
+
refuseWrite(existing, "update");
|
|
304
|
+
}
|
|
305
|
+
const delta = await client.updateNote(args);
|
|
306
|
+
await client.sync();
|
|
307
|
+
return withNotices({ ok: true, ...delta }, [
|
|
308
|
+
args.noteType !== undefined && args.noteType !== existing?.noteType
|
|
309
|
+
? noteTypeNotice(client, args.noteType)
|
|
310
|
+
: undefined,
|
|
311
|
+
]);
|
|
312
|
+
},
|
|
313
|
+
notes_append: async (raw) => {
|
|
314
|
+
const args = appendInput.parse(raw);
|
|
315
|
+
const existing = await client.getNote(args.uuid);
|
|
316
|
+
if (existing && (existing.protected || existing.locked)) {
|
|
317
|
+
refuseWrite(existing, "append to");
|
|
318
|
+
}
|
|
319
|
+
const delta = await client.appendNote(args);
|
|
320
|
+
await client.sync();
|
|
321
|
+
return { ok: true, ...delta };
|
|
322
|
+
},
|
|
323
|
+
notes_replace_in: async (raw) => {
|
|
324
|
+
const args = replaceInInput.parse(raw);
|
|
325
|
+
const existing = await client.getNote(args.uuid);
|
|
326
|
+
if (existing && (existing.protected || existing.locked)) {
|
|
327
|
+
refuseWrite(existing, "edit");
|
|
328
|
+
}
|
|
329
|
+
const result = await client.replaceInNote(args);
|
|
330
|
+
await client.sync();
|
|
331
|
+
return { ok: true, ...result };
|
|
332
|
+
},
|
|
333
|
+
notes_update_many: async (raw) => {
|
|
334
|
+
const { updates } = updateManyInput.parse(raw);
|
|
335
|
+
const result = await client.updateNotesBatch(updates);
|
|
336
|
+
await client.sync();
|
|
337
|
+
return withNotices({ ok: result.failed.length === 0, ...result }, updates.map((u) => noteTypeNotice(client, u.noteType)));
|
|
338
|
+
},
|
|
339
|
+
notes_delete_many: async (raw) => {
|
|
340
|
+
const { uuids, permanent } = deleteManyInput.parse(raw);
|
|
341
|
+
if (opts.confirmBatchDelete) {
|
|
342
|
+
const confirmed = await opts.confirmBatchDelete(uuids.length, permanent);
|
|
343
|
+
if (!confirmed) {
|
|
344
|
+
return { ok: false, cancelled: true };
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
const result = await client.deleteNotesBatch(uuids, permanent);
|
|
348
|
+
await client.sync();
|
|
349
|
+
return { ok: result.failed.length === 0, permanent, ...result };
|
|
350
|
+
},
|
|
351
|
+
notes_delete: async (raw) => {
|
|
352
|
+
const { uuid, permanent } = deleteInput.parse(raw);
|
|
353
|
+
const existing = await client.getNote(uuid);
|
|
354
|
+
if (existing && (existing.protected || existing.locked)) {
|
|
355
|
+
refuseWrite(existing, "delete");
|
|
356
|
+
}
|
|
357
|
+
if (opts.confirmDelete && existing) {
|
|
358
|
+
const confirmed = await opts.confirmDelete(existing);
|
|
359
|
+
if (!confirmed) {
|
|
360
|
+
return { ok: false, cancelled: true };
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
await client.deleteNote(uuid, permanent);
|
|
364
|
+
await client.sync();
|
|
365
|
+
return { ok: true, permanent };
|
|
366
|
+
},
|
|
367
|
+
notes_stats: async (raw) => {
|
|
368
|
+
statsInput.parse(raw ?? {});
|
|
369
|
+
return client.stats();
|
|
370
|
+
},
|
|
371
|
+
notes_frontmatter: async (raw) => {
|
|
372
|
+
const { uuid, operation, key, value } = frontmatterInput.parse(raw);
|
|
373
|
+
const note = await client.getNote(uuid);
|
|
374
|
+
if (!note) {
|
|
375
|
+
throw new ToolError("note_not_found", `Note ${uuid} not found`, "Run notes_search or notes_list to find the note's uuid.");
|
|
376
|
+
}
|
|
377
|
+
if (note.protected)
|
|
378
|
+
refuseRead(note);
|
|
379
|
+
if (operation === "get") {
|
|
380
|
+
const { data } = parseFrontmatter(note.text);
|
|
381
|
+
if (key !== undefined) {
|
|
382
|
+
return {
|
|
383
|
+
uuid,
|
|
384
|
+
key,
|
|
385
|
+
exists: Object.prototype.hasOwnProperty.call(data, key),
|
|
386
|
+
value: data[key] ?? null,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
return { uuid, frontmatter: data };
|
|
390
|
+
}
|
|
391
|
+
if (note.locked)
|
|
392
|
+
refuseWrite(note, "edit frontmatter of");
|
|
393
|
+
const result = await client.updateFrontmatter({
|
|
394
|
+
uuid,
|
|
395
|
+
operation,
|
|
396
|
+
key: key,
|
|
397
|
+
value,
|
|
398
|
+
});
|
|
399
|
+
await client.sync();
|
|
400
|
+
return { ok: true, uuid, key, ...result };
|
|
401
|
+
},
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
//# sourceMappingURL=notes.js.map
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { isPaid } from "../sn/entitlement.js";
|
|
3
|
+
export const smartviewsListInput = z.object({});
|
|
4
|
+
export const smartviewsNotesInput = z.object({
|
|
5
|
+
view: z
|
|
6
|
+
.string()
|
|
7
|
+
.min(1)
|
|
8
|
+
.max(256)
|
|
9
|
+
.describe("Smart view UUID or title (case-insensitive), e.g. \"Action items\""),
|
|
10
|
+
limit: z.number().int().min(1).max(200).default(50),
|
|
11
|
+
offset: z.number().int().min(0).default(0),
|
|
12
|
+
});
|
|
13
|
+
// Predicate trees are validated structurally in the client (isPredicateJson);
|
|
14
|
+
// zod just guarantees "some JSON object".
|
|
15
|
+
export const notesQueryInput = z.object({
|
|
16
|
+
predicate: z
|
|
17
|
+
.record(z.string(), z.unknown())
|
|
18
|
+
.describe('SN predicate JSON. Leaf: {"keypath":"title","operator":"startsWith","value":"Weekly"}. Compound: {"operator":"and","value":[<leaf>, ...]}. Operators: =, !=, <, >, <=, >=, startsWith, includes, matches (regex), in, not, and, or. Keypaths: title, text, trashed, archived, pinned, noteType, tagsCount, created_at_timestamp, updated_at_timestamp, and frontmatter.<key> for YAML frontmatter fields (e.g. {"keypath":"frontmatter.status","operator":"=","value":"active"}; use includes for list fields like frontmatter.tags).'),
|
|
19
|
+
limit: z.number().int().min(1).max(200).default(50),
|
|
20
|
+
offset: z.number().int().min(0).default(0),
|
|
21
|
+
});
|
|
22
|
+
// Second line of defence: the client never returns protected notes.
|
|
23
|
+
const notProtected = (n) => !n.protected;
|
|
24
|
+
export function registerSmartViewHandlers(client) {
|
|
25
|
+
return {
|
|
26
|
+
smartviews_list: async (raw) => {
|
|
27
|
+
smartviewsListInput.parse(raw ?? {});
|
|
28
|
+
const smartViews = await client.listSmartViews();
|
|
29
|
+
if (smartViews.length === 0 && !isPaid(client.entitlement())) {
|
|
30
|
+
return {
|
|
31
|
+
smartViews,
|
|
32
|
+
notice: "Smart Views are a paid Standard Notes feature, so this account has none. notes_query can run the same kind of filter on demand.",
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return { smartViews };
|
|
36
|
+
},
|
|
37
|
+
smartviews_notes: async (raw) => {
|
|
38
|
+
const { view, limit, offset } = smartviewsNotesInput.parse(raw);
|
|
39
|
+
const result = await client.runSmartView(view, { limit, offset });
|
|
40
|
+
return { ...result, notes: result.notes.filter(notProtected) };
|
|
41
|
+
},
|
|
42
|
+
notes_query: async (raw) => {
|
|
43
|
+
const { predicate, limit, offset } = notesQueryInput.parse(raw);
|
|
44
|
+
const result = await client.runPredicateQuery(predicate, {
|
|
45
|
+
limit,
|
|
46
|
+
offset,
|
|
47
|
+
});
|
|
48
|
+
return { ...result, notes: result.notes.filter(notProtected) };
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=smartviews.js.map
|