obsidian-sync-mcp 0.1.2 → 0.2.2
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/README.md +145 -176
- package/dist/chunk-TFJDIQK4.js +83 -0
- package/dist/chunk-TFJDIQK4.js.map +1 -0
- package/dist/main.js +204 -187
- package/dist/main.js.map +1 -1
- package/dist/{vault-IGTFVPKN.js → vault-J2V2D3HA.js} +4 -25
- package/dist/{vault-IGTFVPKN.js.map → vault-J2V2D3HA.js.map} +1 -1
- package/dist/{vault-local-65IVVIHQ.js → vault-local-3C2JVBRO.js} +3 -20
- package/dist/vault-local-3C2JVBRO.js.map +1 -0
- package/package.json +3 -3
- package/dist/chunk-26MFONZB.js +0 -20
- package/dist/chunk-26MFONZB.js.map +0 -1
- package/dist/chunk-ZJJW3NJ5.js +0 -43
- package/dist/chunk-ZJJW3NJ5.js.map +0 -1
- package/dist/vault-local-65IVVIHQ.js.map +0 -1
package/dist/main.js
CHANGED
|
@@ -1,23 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
// Node polyfills for livesync-commonlib browser globals
|
|
2
3
|
if(!("navigator" in globalThis)){globalThis.navigator={language:"en"};}
|
|
3
|
-
import
|
|
4
|
+
import {
|
|
5
|
+
extractSnippet
|
|
6
|
+
} from "./chunk-TFJDIQK4.js";
|
|
4
7
|
|
|
5
8
|
// src/main.ts
|
|
6
9
|
import { FastMCP } from "fastmcp";
|
|
7
|
-
import { z } from "zod";
|
|
8
10
|
import { join } from "path";
|
|
9
11
|
import { timingSafeEqual as timingSafeEqual2, createHash as createHash2 } from "crypto";
|
|
10
12
|
import { watch } from "fs";
|
|
11
13
|
import { readFile as readFile3 } from "fs/promises";
|
|
12
14
|
|
|
13
|
-
// src/deeplink.ts
|
|
14
|
-
function makeDeepLink(vaultName, notePath) {
|
|
15
|
-
const cleanPath = notePath.replace(/\.md$/, "");
|
|
16
|
-
const encodedVault = encodeURIComponent(vaultName);
|
|
17
|
-
const encodedPath = encodeURIComponent(cleanPath);
|
|
18
|
-
return `obsidian://open?vault=${encodedVault}&file=${encodedPath}`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
15
|
// src/auth.ts
|
|
22
16
|
import { randomUUID, randomBytes, createHash, timingSafeEqual } from "crypto";
|
|
23
17
|
import { readFile, writeFile, mkdir, chmod } from "fs/promises";
|
|
@@ -76,13 +70,21 @@ function mountPasswordAuth(app, baseUrl, password, persistPath) {
|
|
|
76
70
|
return c.json({ error: "too_many_clients" }, 429);
|
|
77
71
|
}
|
|
78
72
|
const body = await c.req.json();
|
|
73
|
+
const redirectUris = Array.isArray(body.redirect_uris) ? body.redirect_uris.slice(0, 5) : [];
|
|
74
|
+
if (redirectUris.length === 0) {
|
|
75
|
+
return c.json({ error: "invalid_client_metadata", error_description: "redirect_uris required" }, 400);
|
|
76
|
+
}
|
|
77
|
+
if (redirectUris.some((u) => typeof u !== "string" || u.length > 2048)) {
|
|
78
|
+
return c.json({ error: "invalid_client_metadata", error_description: "invalid redirect_uri" }, 400);
|
|
79
|
+
}
|
|
79
80
|
const clientId = randomUUID();
|
|
80
81
|
const clientSecret = randomBytes(32).toString("hex");
|
|
82
|
+
const clientName = typeof body.client_name === "string" ? body.client_name.slice(0, 256) : void 0;
|
|
81
83
|
const client = {
|
|
82
84
|
clientId,
|
|
83
85
|
clientSecret,
|
|
84
|
-
redirectUris
|
|
85
|
-
clientName
|
|
86
|
+
redirectUris,
|
|
87
|
+
clientName
|
|
86
88
|
};
|
|
87
89
|
clients.set(clientId, client);
|
|
88
90
|
return c.json({
|
|
@@ -103,7 +105,7 @@ function mountPasswordAuth(app, baseUrl, password, persistPath) {
|
|
|
103
105
|
if (!client) {
|
|
104
106
|
return c.text("Unknown client", 400);
|
|
105
107
|
}
|
|
106
|
-
if (
|
|
108
|
+
if (!client.redirectUris.includes(redirectUri)) {
|
|
107
109
|
return c.text("Invalid redirect URI", 400);
|
|
108
110
|
}
|
|
109
111
|
if (codeChallengeMethod !== "S256" || !codeChallenge) {
|
|
@@ -180,7 +182,8 @@ function mountPasswordAuth(app, baseUrl, password, persistPath) {
|
|
|
180
182
|
const codeVerifier = body["code_verifier"];
|
|
181
183
|
const redirectUri = body["redirect_uri"];
|
|
182
184
|
const pending = pendingAuths.get(code);
|
|
183
|
-
if (!pending) {
|
|
185
|
+
if (!pending || Date.now() - pending.createdAt > PENDING_TTL_MS) {
|
|
186
|
+
if (pending) pendingAuths.delete(code);
|
|
184
187
|
return c.json({ error: "invalid_grant" }, 400);
|
|
185
188
|
}
|
|
186
189
|
if (redirectUri !== pending.redirectUri) {
|
|
@@ -262,9 +265,12 @@ function mountPasswordAuth(app, baseUrl, password, persistPath) {
|
|
|
262
265
|
if (!persistPath) return;
|
|
263
266
|
try {
|
|
264
267
|
await mkdir(dirname(persistPath), { recursive: true });
|
|
268
|
+
const now = Date.now();
|
|
269
|
+
const activeTokens = [...tokens.entries()].filter(([_, r]) => r.expiresAt > now);
|
|
270
|
+
const activeRefresh = [...refreshTokens.entries()].filter(([_, r]) => r.refreshExpiresAt > now);
|
|
265
271
|
const data = JSON.stringify({
|
|
266
|
-
tokens: Object.fromEntries(
|
|
267
|
-
refreshTokens: Object.fromEntries(
|
|
272
|
+
tokens: Object.fromEntries(activeTokens),
|
|
273
|
+
refreshTokens: Object.fromEntries(activeRefresh),
|
|
268
274
|
clients: Object.fromEntries(clients)
|
|
269
275
|
});
|
|
270
276
|
await writeFile(persistPath, data, { encoding: "utf-8", mode: 384 });
|
|
@@ -279,11 +285,14 @@ function mountPasswordAuth(app, baseUrl, password, persistPath) {
|
|
|
279
285
|
try {
|
|
280
286
|
const raw = await readFile(persistPath, "utf-8");
|
|
281
287
|
const data = JSON.parse(raw);
|
|
288
|
+
const now = Date.now();
|
|
282
289
|
for (const [k, v] of Object.entries(data.tokens ?? {})) {
|
|
283
|
-
|
|
290
|
+
const record = v;
|
|
291
|
+
if (record.expiresAt > now) tokens.set(k, record);
|
|
284
292
|
}
|
|
285
293
|
for (const [k, v] of Object.entries(data.refreshTokens ?? {})) {
|
|
286
|
-
|
|
294
|
+
const record = v;
|
|
295
|
+
if (record.refreshExpiresAt > now) refreshTokens.set(k, record);
|
|
287
296
|
}
|
|
288
297
|
for (const [k, v] of Object.entries(data.clients ?? {})) {
|
|
289
298
|
clients.set(k, v);
|
|
@@ -309,7 +318,7 @@ function renderPasswordPage(code, csrf, error) {
|
|
|
309
318
|
</style></head>
|
|
310
319
|
<body>
|
|
311
320
|
<h1>Obsidian Sync MCP</h1>
|
|
312
|
-
${error ? `<p class="error">${error}</p>` : "<p>Enter the server password to authorize access to your vault.</p>"}
|
|
321
|
+
${error ? `<p class="error">${error.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")}</p>` : "<p>Enter the server password to authorize access to your vault.</p>"}
|
|
313
322
|
<form method="POST" action="/oauth/approve" autocomplete="on">
|
|
314
323
|
<input type="hidden" name="code" value="${code}">
|
|
315
324
|
<input type="hidden" name="csrf" value="${csrf}">
|
|
@@ -395,20 +404,11 @@ var SearchIndex = class {
|
|
|
395
404
|
}
|
|
396
405
|
}
|
|
397
406
|
const output = [];
|
|
398
|
-
const lowerQuery = query.toLowerCase();
|
|
399
407
|
for (const path of paths) {
|
|
400
408
|
if (output.length >= MAX_RESULTS) break;
|
|
401
409
|
const content = this.contents.get(path);
|
|
402
410
|
if (!content) continue;
|
|
403
|
-
|
|
404
|
-
if (idx !== -1) {
|
|
405
|
-
const start = Math.max(0, idx - 80);
|
|
406
|
-
const end = Math.min(content.length, idx + query.length + 80);
|
|
407
|
-
const snippet = (start > 0 ? "..." : "") + content.slice(start, end) + (end < content.length ? "..." : "");
|
|
408
|
-
output.push({ path, snippet });
|
|
409
|
-
} else {
|
|
410
|
-
output.push({ path, snippet: content.slice(0, 160) + (content.length > 160 ? "..." : "") });
|
|
411
|
-
}
|
|
411
|
+
output.push({ path, snippet: extractSnippet(content, query) });
|
|
412
412
|
}
|
|
413
413
|
return output;
|
|
414
414
|
}
|
|
@@ -425,6 +425,176 @@ var SearchIndex = class {
|
|
|
425
425
|
}
|
|
426
426
|
};
|
|
427
427
|
|
|
428
|
+
// src/tools.ts
|
|
429
|
+
import { z } from "zod";
|
|
430
|
+
|
|
431
|
+
// src/deeplink.ts
|
|
432
|
+
function makeDeepLink(vaultName, notePath) {
|
|
433
|
+
const cleanPath = notePath.replace(/\.md$/, "");
|
|
434
|
+
const encodedVault = encodeURIComponent(vaultName);
|
|
435
|
+
const encodedPath = encodeURIComponent(cleanPath);
|
|
436
|
+
return `obsidian://open?vault=${encodedVault}&file=${encodedPath}`;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// src/tools.ts
|
|
440
|
+
function registerTools(server2, vault2, searchIndex2, vaultName) {
|
|
441
|
+
server2.addTool({
|
|
442
|
+
name: "read_note",
|
|
443
|
+
description: "Read the content of a note from the Obsidian vault. Returns the markdown content and a deep link to open it in Obsidian.",
|
|
444
|
+
parameters: z.object({
|
|
445
|
+
path: z.string().describe("Vault-relative path to the note, e.g. 'daily/2026-03-23.md'")
|
|
446
|
+
}),
|
|
447
|
+
execute: async ({ path }) => {
|
|
448
|
+
const content = await vault2.readNote(path);
|
|
449
|
+
if (content === null) {
|
|
450
|
+
return `Note not found: ${path}`;
|
|
451
|
+
}
|
|
452
|
+
const deepLink = makeDeepLink(vaultName, path);
|
|
453
|
+
return `[Open in Obsidian](${deepLink})
|
|
454
|
+
|
|
455
|
+
---
|
|
456
|
+
|
|
457
|
+
${content}`;
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
server2.addTool({
|
|
461
|
+
name: "write_note",
|
|
462
|
+
description: "Write or update a note in the Obsidian vault. Creates the note if it doesn't exist, overwrites if it does.",
|
|
463
|
+
parameters: z.object({
|
|
464
|
+
path: z.string().describe("Vault-relative path to the note, e.g. 'daily/2026-03-23.md'"),
|
|
465
|
+
content: z.string().describe("Full markdown content for the note")
|
|
466
|
+
}),
|
|
467
|
+
execute: async ({ path, content }) => {
|
|
468
|
+
const ok = await vault2.writeNote(path, content);
|
|
469
|
+
if (!ok) {
|
|
470
|
+
return `Failed to write note: ${path}`;
|
|
471
|
+
}
|
|
472
|
+
searchIndex2.update(path, content);
|
|
473
|
+
const deepLink = makeDeepLink(vaultName, path);
|
|
474
|
+
return `Note saved: ${path}
|
|
475
|
+
[Open in Obsidian](${deepLink})`;
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
server2.addTool({
|
|
479
|
+
name: "list_notes",
|
|
480
|
+
description: "List all markdown notes in the vault, optionally filtered to a folder.",
|
|
481
|
+
parameters: z.object({
|
|
482
|
+
folder: z.string().optional().describe("Folder to filter by, e.g. 'daily/' or 'projects/'. Omit for all notes.")
|
|
483
|
+
}),
|
|
484
|
+
execute: async ({ folder }) => {
|
|
485
|
+
let notes = searchIndex2.listPaths(folder);
|
|
486
|
+
if (notes.length === 0) {
|
|
487
|
+
notes = await vault2.listNotes(folder);
|
|
488
|
+
}
|
|
489
|
+
if (notes.length === 0) {
|
|
490
|
+
return folder ? `No notes found in folder: ${folder}` : "Vault is empty.";
|
|
491
|
+
}
|
|
492
|
+
const total = notes.length;
|
|
493
|
+
const capped = notes.slice(0, 500);
|
|
494
|
+
const lines = capped.map((p) => {
|
|
495
|
+
const deepLink = makeDeepLink(vaultName, p);
|
|
496
|
+
return `- [${p}](${deepLink})`;
|
|
497
|
+
});
|
|
498
|
+
if (total > 500) {
|
|
499
|
+
lines.push(`
|
|
500
|
+
... and ${total - 500} more. Use a folder filter to narrow results.`);
|
|
501
|
+
}
|
|
502
|
+
return lines.join("\n");
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
server2.addTool({
|
|
506
|
+
name: "search_vault",
|
|
507
|
+
description: "Search for a text query across all notes in the vault. Returns matching notes with snippets.",
|
|
508
|
+
parameters: z.object({
|
|
509
|
+
query: z.string().describe("Text to search for (case-insensitive)")
|
|
510
|
+
}),
|
|
511
|
+
execute: async ({ query }) => {
|
|
512
|
+
const results = searchIndex2.search(query);
|
|
513
|
+
if (results.length === 0) {
|
|
514
|
+
return `No results for: ${query}`;
|
|
515
|
+
}
|
|
516
|
+
const lines = results.map((r) => {
|
|
517
|
+
const deepLink = makeDeepLink(vaultName, r.path);
|
|
518
|
+
return `### [${r.path}](${deepLink})
|
|
519
|
+
\`\`\`
|
|
520
|
+
${r.snippet}
|
|
521
|
+
\`\`\``;
|
|
522
|
+
});
|
|
523
|
+
return lines.join("\n\n");
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
server2.addTool({
|
|
527
|
+
name: "delete_note",
|
|
528
|
+
description: "Delete a note from the Obsidian vault.",
|
|
529
|
+
parameters: z.object({
|
|
530
|
+
path: z.string().describe("Vault-relative path to the note to delete")
|
|
531
|
+
}),
|
|
532
|
+
execute: async ({ path }) => {
|
|
533
|
+
const ok = await vault2.deleteNote(path);
|
|
534
|
+
if (ok) searchIndex2.remove(path);
|
|
535
|
+
return ok ? `Deleted: ${path}` : `Failed to delete: ${path}`;
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
server2.addTool({
|
|
539
|
+
name: "move_note",
|
|
540
|
+
description: "Move or rename a note. Use this to rename a note within the same folder, move it to a different folder, or both at once. Creates destination folders automatically.",
|
|
541
|
+
parameters: z.object({
|
|
542
|
+
from: z.string().describe("Current path, e.g. 'daily/old-name.md'"),
|
|
543
|
+
to: z.string().describe("New path, e.g. 'projects/new-name.md'")
|
|
544
|
+
}),
|
|
545
|
+
execute: async ({ from, to }) => {
|
|
546
|
+
const content = await vault2.readNote(from);
|
|
547
|
+
const ok = await vault2.moveNote(from, to);
|
|
548
|
+
if (!ok) {
|
|
549
|
+
return `Failed to move: ${from} \u2192 ${to}`;
|
|
550
|
+
}
|
|
551
|
+
searchIndex2.remove(from);
|
|
552
|
+
if (content) searchIndex2.update(to, content);
|
|
553
|
+
const deepLink = makeDeepLink(vaultName, to);
|
|
554
|
+
return `Moved: ${from} \u2192 ${to}
|
|
555
|
+
[Open in Obsidian](${deepLink})`;
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
server2.addTool({
|
|
559
|
+
name: "get_note_metadata",
|
|
560
|
+
description: "Get metadata about a note without reading its full content. Returns frontmatter properties, tags (both frontmatter and inline #tags), internal links ([[wikilinks]] and markdown links), file size, and timestamps.",
|
|
561
|
+
parameters: z.object({
|
|
562
|
+
path: z.string().describe("Vault-relative path to the note, e.g. 'projects/my-project.md'")
|
|
563
|
+
}),
|
|
564
|
+
execute: async ({ path }) => {
|
|
565
|
+
const meta = await vault2.getMetadata(path);
|
|
566
|
+
if (!meta) {
|
|
567
|
+
return `Note not found: ${path}`;
|
|
568
|
+
}
|
|
569
|
+
const deepLink = makeDeepLink(vaultName, path);
|
|
570
|
+
const lines = [
|
|
571
|
+
`**${path}**`,
|
|
572
|
+
`Size: ${meta.size} bytes`,
|
|
573
|
+
`Created: ${new Date(meta.ctime).toISOString()}`,
|
|
574
|
+
`Modified: ${new Date(meta.mtime).toISOString()}`
|
|
575
|
+
];
|
|
576
|
+
if (Object.keys(meta.frontmatter).length > 0) {
|
|
577
|
+
lines.push(`
|
|
578
|
+
Frontmatter:`);
|
|
579
|
+
for (const [k, v] of Object.entries(meta.frontmatter)) {
|
|
580
|
+
lines.push(` ${k}: ${v}`);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (meta.tags.length > 0) {
|
|
584
|
+
lines.push(`
|
|
585
|
+
Tags: ${meta.tags.map((t) => `#${t}`).join(", ")}`);
|
|
586
|
+
}
|
|
587
|
+
if (meta.links.length > 0) {
|
|
588
|
+
lines.push(`
|
|
589
|
+
Links: ${meta.links.join(", ")}`);
|
|
590
|
+
}
|
|
591
|
+
lines.push(`
|
|
592
|
+
[Open in Obsidian](${deepLink})`);
|
|
593
|
+
return lines.join("\n");
|
|
594
|
+
}
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
|
|
428
598
|
// src/main.ts
|
|
429
599
|
var VAULT_PATH = process.env.VAULT_PATH;
|
|
430
600
|
var COUCHDB_URL = process.env.COUCHDB_URL;
|
|
@@ -438,11 +608,11 @@ var BASE_URL = process.env.BASE_URL ?? `http://localhost:${PORT}`;
|
|
|
438
608
|
var AUTH_TOKEN = process.env.MCP_AUTH_TOKEN;
|
|
439
609
|
var vault;
|
|
440
610
|
if (VAULT_PATH) {
|
|
441
|
-
const { LocalVault } = await import("./vault-local-
|
|
611
|
+
const { LocalVault } = await import("./vault-local-3C2JVBRO.js");
|
|
442
612
|
vault = new LocalVault(VAULT_PATH);
|
|
443
613
|
console.log(`Local mode: ${VAULT_PATH}`);
|
|
444
614
|
} else if (COUCHDB_URL) {
|
|
445
|
-
const { Vault } = await import("./vault-
|
|
615
|
+
const { Vault } = await import("./vault-J2V2D3HA.js");
|
|
446
616
|
vault = new Vault({
|
|
447
617
|
couchdbUrl: COUCHDB_URL,
|
|
448
618
|
couchdbUser: COUCHDB_USER,
|
|
@@ -489,7 +659,7 @@ if (VAULT_PATH) {
|
|
|
489
659
|
}
|
|
490
660
|
});
|
|
491
661
|
console.log("Watching vault for external changes.");
|
|
492
|
-
} else if (COUCHDB_URL &&
|
|
662
|
+
} else if (COUCHDB_URL && vault.watchChanges) {
|
|
493
663
|
vault.watchChanges((path, content) => {
|
|
494
664
|
if (content) {
|
|
495
665
|
searchIndex.update(path, content);
|
|
@@ -501,7 +671,7 @@ if (VAULT_PATH) {
|
|
|
501
671
|
}
|
|
502
672
|
var serverOptions = {
|
|
503
673
|
name: "obsidian-sync-mcp",
|
|
504
|
-
version: "0.
|
|
674
|
+
version: "0.2.2",
|
|
505
675
|
instructions: "Access and manage an Obsidian vault. You can read, write, list, search, and delete markdown notes. Every response includes a deep link to open the note directly in Obsidian."
|
|
506
676
|
};
|
|
507
677
|
var auth = null;
|
|
@@ -527,160 +697,7 @@ if (AUTH_TOKEN) {
|
|
|
527
697
|
auth = mountPasswordAuth(server.getApp(), BASE_URL, AUTH_TOKEN, tokenPath);
|
|
528
698
|
await auth.loadTokens();
|
|
529
699
|
}
|
|
530
|
-
server
|
|
531
|
-
name: "read_note",
|
|
532
|
-
description: "Read the content of a note from the Obsidian vault. Returns the markdown content and a deep link to open it in Obsidian.",
|
|
533
|
-
parameters: z.object({
|
|
534
|
-
path: z.string().describe("Vault-relative path to the note, e.g. 'daily/2026-03-23.md'")
|
|
535
|
-
}),
|
|
536
|
-
execute: async ({ path }) => {
|
|
537
|
-
const content = await vault.readNote(path);
|
|
538
|
-
if (content === null) {
|
|
539
|
-
return `Note not found: ${path}`;
|
|
540
|
-
}
|
|
541
|
-
const deepLink = makeDeepLink(VAULT_NAME, path);
|
|
542
|
-
return `${content}
|
|
543
|
-
|
|
544
|
-
---
|
|
545
|
-
[Open in Obsidian](${deepLink})`;
|
|
546
|
-
}
|
|
547
|
-
});
|
|
548
|
-
server.addTool({
|
|
549
|
-
name: "write_note",
|
|
550
|
-
description: "Write or update a note in the Obsidian vault. Creates the note if it doesn't exist, overwrites if it does.",
|
|
551
|
-
parameters: z.object({
|
|
552
|
-
path: z.string().describe("Vault-relative path to the note, e.g. 'daily/2026-03-23.md'"),
|
|
553
|
-
content: z.string().describe("Full markdown content for the note")
|
|
554
|
-
}),
|
|
555
|
-
execute: async ({ path, content }) => {
|
|
556
|
-
const ok = await vault.writeNote(path, content);
|
|
557
|
-
if (!ok) {
|
|
558
|
-
return `Failed to write note: ${path}`;
|
|
559
|
-
}
|
|
560
|
-
searchIndex.update(path, content);
|
|
561
|
-
const deepLink = makeDeepLink(VAULT_NAME, path);
|
|
562
|
-
return `Note saved: ${path}
|
|
563
|
-
[Open in Obsidian](${deepLink})`;
|
|
564
|
-
}
|
|
565
|
-
});
|
|
566
|
-
server.addTool({
|
|
567
|
-
name: "list_notes",
|
|
568
|
-
description: "List all markdown notes in the vault, optionally filtered to a folder.",
|
|
569
|
-
parameters: z.object({
|
|
570
|
-
folder: z.string().optional().describe("Folder to filter by, e.g. 'daily/' or 'projects/'. Omit for all notes.")
|
|
571
|
-
}),
|
|
572
|
-
execute: async ({ folder }) => {
|
|
573
|
-
let notes = searchIndex.listPaths(folder);
|
|
574
|
-
if (notes.length === 0) {
|
|
575
|
-
notes = await vault.listNotes(folder);
|
|
576
|
-
}
|
|
577
|
-
if (notes.length === 0) {
|
|
578
|
-
return folder ? `No notes found in folder: ${folder}` : "Vault is empty.";
|
|
579
|
-
}
|
|
580
|
-
const total = notes.length;
|
|
581
|
-
const capped = notes.slice(0, 500);
|
|
582
|
-
const lines = capped.map((p) => {
|
|
583
|
-
const deepLink = makeDeepLink(VAULT_NAME, p);
|
|
584
|
-
return `- [${p}](${deepLink})`;
|
|
585
|
-
});
|
|
586
|
-
if (total > 500) {
|
|
587
|
-
lines.push(`
|
|
588
|
-
... and ${total - 500} more. Use a folder filter to narrow results.`);
|
|
589
|
-
}
|
|
590
|
-
return lines.join("\n");
|
|
591
|
-
}
|
|
592
|
-
});
|
|
593
|
-
server.addTool({
|
|
594
|
-
name: "search_vault",
|
|
595
|
-
description: "Search for a text query across all notes in the vault. Returns matching notes with snippets.",
|
|
596
|
-
parameters: z.object({
|
|
597
|
-
query: z.string().describe("Text to search for (case-insensitive)")
|
|
598
|
-
}),
|
|
599
|
-
execute: async ({ query }) => {
|
|
600
|
-
const results = searchIndex.search(query);
|
|
601
|
-
if (results.length === 0) {
|
|
602
|
-
return `No results for: ${query}`;
|
|
603
|
-
}
|
|
604
|
-
const lines = results.map((r) => {
|
|
605
|
-
const deepLink = makeDeepLink(VAULT_NAME, r.path);
|
|
606
|
-
return `### [${r.path}](${deepLink})
|
|
607
|
-
\`\`\`
|
|
608
|
-
${r.snippet}
|
|
609
|
-
\`\`\``;
|
|
610
|
-
});
|
|
611
|
-
return lines.join("\n\n");
|
|
612
|
-
}
|
|
613
|
-
});
|
|
614
|
-
server.addTool({
|
|
615
|
-
name: "delete_note",
|
|
616
|
-
description: "Delete a note from the Obsidian vault.",
|
|
617
|
-
parameters: z.object({
|
|
618
|
-
path: z.string().describe("Vault-relative path to the note to delete")
|
|
619
|
-
}),
|
|
620
|
-
execute: async ({ path }) => {
|
|
621
|
-
const ok = await vault.deleteNote(path);
|
|
622
|
-
if (ok) searchIndex.remove(path);
|
|
623
|
-
return ok ? `Deleted: ${path}` : `Failed to delete: ${path}`;
|
|
624
|
-
}
|
|
625
|
-
});
|
|
626
|
-
server.addTool({
|
|
627
|
-
name: "move_note",
|
|
628
|
-
description: "Move or rename a note. Use this to rename a note within the same folder, move it to a different folder, or both at once. Creates destination folders automatically.",
|
|
629
|
-
parameters: z.object({
|
|
630
|
-
from: z.string().describe("Current path, e.g. 'daily/old-name.md'"),
|
|
631
|
-
to: z.string().describe("New path, e.g. 'projects/new-name.md'")
|
|
632
|
-
}),
|
|
633
|
-
execute: async ({ from, to }) => {
|
|
634
|
-
const content = await vault.readNote(from);
|
|
635
|
-
const ok = await vault.moveNote(from, to);
|
|
636
|
-
if (!ok) {
|
|
637
|
-
return `Failed to move: ${from} \u2192 ${to}`;
|
|
638
|
-
}
|
|
639
|
-
searchIndex.remove(from);
|
|
640
|
-
if (content) searchIndex.update(to, content);
|
|
641
|
-
const deepLink = makeDeepLink(VAULT_NAME, to);
|
|
642
|
-
return `Moved: ${from} \u2192 ${to}
|
|
643
|
-
[Open in Obsidian](${deepLink})`;
|
|
644
|
-
}
|
|
645
|
-
});
|
|
646
|
-
server.addTool({
|
|
647
|
-
name: "get_note_metadata",
|
|
648
|
-
description: "Get metadata about a note without reading its full content. Returns frontmatter properties, tags (both frontmatter and inline #tags), internal links ([[wikilinks]] and markdown links), file size, and timestamps.",
|
|
649
|
-
parameters: z.object({
|
|
650
|
-
path: z.string().describe("Vault-relative path to the note, e.g. 'projects/my-project.md'")
|
|
651
|
-
}),
|
|
652
|
-
execute: async ({ path }) => {
|
|
653
|
-
const meta = await vault.getMetadata(path);
|
|
654
|
-
if (!meta) {
|
|
655
|
-
return `Note not found: ${path}`;
|
|
656
|
-
}
|
|
657
|
-
const deepLink = makeDeepLink(VAULT_NAME, path);
|
|
658
|
-
const lines = [
|
|
659
|
-
`**${path}**`,
|
|
660
|
-
`Size: ${meta.size} bytes`,
|
|
661
|
-
`Created: ${new Date(meta.ctime).toISOString()}`,
|
|
662
|
-
`Modified: ${new Date(meta.mtime).toISOString()}`
|
|
663
|
-
];
|
|
664
|
-
if (Object.keys(meta.frontmatter).length > 0) {
|
|
665
|
-
lines.push(`
|
|
666
|
-
Frontmatter:`);
|
|
667
|
-
for (const [k, v] of Object.entries(meta.frontmatter)) {
|
|
668
|
-
lines.push(` ${k}: ${v}`);
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
if (meta.tags.length > 0) {
|
|
672
|
-
lines.push(`
|
|
673
|
-
Tags: ${meta.tags.map((t) => `#${t}`).join(", ")}`);
|
|
674
|
-
}
|
|
675
|
-
if (meta.links.length > 0) {
|
|
676
|
-
lines.push(`
|
|
677
|
-
Links: ${meta.links.join(", ")}`);
|
|
678
|
-
}
|
|
679
|
-
lines.push(`
|
|
680
|
-
[Open in Obsidian](${deepLink})`);
|
|
681
|
-
return lines.join("\n");
|
|
682
|
-
}
|
|
683
|
-
});
|
|
700
|
+
registerTools(server, vault, searchIndex, VAULT_NAME);
|
|
684
701
|
async function shutdown() {
|
|
685
702
|
console.log("Shutting down...");
|
|
686
703
|
if (fsWatcher) fsWatcher.close();
|