obsidian-sync-mcp 0.1.1 → 0.2.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/README.md +149 -174
- package/dist/chunk-F7WFTFMH.js +82 -0
- package/dist/chunk-F7WFTFMH.js.map +1 -0
- package/dist/main.js +207 -191
- package/dist/main.js.map +1 -1
- package/dist/{vault-IGTFVPKN.js → vault-GDCDM6GS.js} +3 -25
- package/dist/{vault-IGTFVPKN.js.map → vault-GDCDM6GS.js.map} +1 -1
- package/dist/{vault-local-65IVVIHQ.js → vault-local-WRHVJLBZ.js} +2 -20
- package/dist/vault-local-WRHVJLBZ.js.map +1 -0
- package/package.json +1 -1
- 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,16 @@
|
|
|
1
1
|
// Node polyfills for livesync-commonlib browser globals
|
|
2
2
|
if(!("navigator" in globalThis)){globalThis.navigator={language:"en"};}
|
|
3
|
-
import
|
|
3
|
+
import {
|
|
4
|
+
extractSnippet
|
|
5
|
+
} from "./chunk-F7WFTFMH.js";
|
|
4
6
|
|
|
5
7
|
// src/main.ts
|
|
6
8
|
import { FastMCP } from "fastmcp";
|
|
7
|
-
import { z } from "zod";
|
|
8
9
|
import { join } from "path";
|
|
9
10
|
import { timingSafeEqual as timingSafeEqual2, createHash as createHash2 } from "crypto";
|
|
10
11
|
import { watch } from "fs";
|
|
11
12
|
import { readFile as readFile3 } from "fs/promises";
|
|
12
13
|
|
|
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
14
|
// src/auth.ts
|
|
22
15
|
import { randomUUID, randomBytes, createHash, timingSafeEqual } from "crypto";
|
|
23
16
|
import { readFile, writeFile, mkdir, chmod } from "fs/promises";
|
|
@@ -76,13 +69,21 @@ function mountPasswordAuth(app, baseUrl, password, persistPath) {
|
|
|
76
69
|
return c.json({ error: "too_many_clients" }, 429);
|
|
77
70
|
}
|
|
78
71
|
const body = await c.req.json();
|
|
72
|
+
const redirectUris = Array.isArray(body.redirect_uris) ? body.redirect_uris.slice(0, 5) : [];
|
|
73
|
+
if (redirectUris.length === 0) {
|
|
74
|
+
return c.json({ error: "invalid_client_metadata", error_description: "redirect_uris required" }, 400);
|
|
75
|
+
}
|
|
76
|
+
if (redirectUris.some((u) => typeof u !== "string" || u.length > 2048)) {
|
|
77
|
+
return c.json({ error: "invalid_client_metadata", error_description: "invalid redirect_uri" }, 400);
|
|
78
|
+
}
|
|
79
79
|
const clientId = randomUUID();
|
|
80
80
|
const clientSecret = randomBytes(32).toString("hex");
|
|
81
|
+
const clientName = typeof body.client_name === "string" ? body.client_name.slice(0, 256) : void 0;
|
|
81
82
|
const client = {
|
|
82
83
|
clientId,
|
|
83
84
|
clientSecret,
|
|
84
|
-
redirectUris
|
|
85
|
-
clientName
|
|
85
|
+
redirectUris,
|
|
86
|
+
clientName
|
|
86
87
|
};
|
|
87
88
|
clients.set(clientId, client);
|
|
88
89
|
return c.json({
|
|
@@ -103,7 +104,7 @@ function mountPasswordAuth(app, baseUrl, password, persistPath) {
|
|
|
103
104
|
if (!client) {
|
|
104
105
|
return c.text("Unknown client", 400);
|
|
105
106
|
}
|
|
106
|
-
if (
|
|
107
|
+
if (!client.redirectUris.includes(redirectUri)) {
|
|
107
108
|
return c.text("Invalid redirect URI", 400);
|
|
108
109
|
}
|
|
109
110
|
if (codeChallengeMethod !== "S256" || !codeChallenge) {
|
|
@@ -180,7 +181,8 @@ function mountPasswordAuth(app, baseUrl, password, persistPath) {
|
|
|
180
181
|
const codeVerifier = body["code_verifier"];
|
|
181
182
|
const redirectUri = body["redirect_uri"];
|
|
182
183
|
const pending = pendingAuths.get(code);
|
|
183
|
-
if (!pending) {
|
|
184
|
+
if (!pending || Date.now() - pending.createdAt > PENDING_TTL_MS) {
|
|
185
|
+
if (pending) pendingAuths.delete(code);
|
|
184
186
|
return c.json({ error: "invalid_grant" }, 400);
|
|
185
187
|
}
|
|
186
188
|
if (redirectUri !== pending.redirectUri) {
|
|
@@ -262,9 +264,12 @@ function mountPasswordAuth(app, baseUrl, password, persistPath) {
|
|
|
262
264
|
if (!persistPath) return;
|
|
263
265
|
try {
|
|
264
266
|
await mkdir(dirname(persistPath), { recursive: true });
|
|
267
|
+
const now = Date.now();
|
|
268
|
+
const activeTokens = [...tokens.entries()].filter(([_, r]) => r.expiresAt > now);
|
|
269
|
+
const activeRefresh = [...refreshTokens.entries()].filter(([_, r]) => r.refreshExpiresAt > now);
|
|
265
270
|
const data = JSON.stringify({
|
|
266
|
-
tokens: Object.fromEntries(
|
|
267
|
-
refreshTokens: Object.fromEntries(
|
|
271
|
+
tokens: Object.fromEntries(activeTokens),
|
|
272
|
+
refreshTokens: Object.fromEntries(activeRefresh),
|
|
268
273
|
clients: Object.fromEntries(clients)
|
|
269
274
|
});
|
|
270
275
|
await writeFile(persistPath, data, { encoding: "utf-8", mode: 384 });
|
|
@@ -279,11 +284,14 @@ function mountPasswordAuth(app, baseUrl, password, persistPath) {
|
|
|
279
284
|
try {
|
|
280
285
|
const raw = await readFile(persistPath, "utf-8");
|
|
281
286
|
const data = JSON.parse(raw);
|
|
287
|
+
const now = Date.now();
|
|
282
288
|
for (const [k, v] of Object.entries(data.tokens ?? {})) {
|
|
283
|
-
|
|
289
|
+
const record = v;
|
|
290
|
+
if (record.expiresAt > now) tokens.set(k, record);
|
|
284
291
|
}
|
|
285
292
|
for (const [k, v] of Object.entries(data.refreshTokens ?? {})) {
|
|
286
|
-
|
|
293
|
+
const record = v;
|
|
294
|
+
if (record.refreshExpiresAt > now) refreshTokens.set(k, record);
|
|
287
295
|
}
|
|
288
296
|
for (const [k, v] of Object.entries(data.clients ?? {})) {
|
|
289
297
|
clients.set(k, v);
|
|
@@ -299,7 +307,7 @@ function mountPasswordAuth(app, baseUrl, password, persistPath) {
|
|
|
299
307
|
function renderPasswordPage(code, csrf, error) {
|
|
300
308
|
return `<!DOCTYPE html>
|
|
301
309
|
<html><head><title>Obsidian Sync MCP - Authorize</title>
|
|
302
|
-
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'
|
|
310
|
+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'">
|
|
303
311
|
<style>
|
|
304
312
|
body { font-family: system-ui; max-width: 400px; margin: 80px auto; padding: 0 20px; }
|
|
305
313
|
h1 { font-size: 1.3em; }
|
|
@@ -309,7 +317,7 @@ function renderPasswordPage(code, csrf, error) {
|
|
|
309
317
|
</style></head>
|
|
310
318
|
<body>
|
|
311
319
|
<h1>Obsidian Sync MCP</h1>
|
|
312
|
-
${error ? `<p class="error">${error}</p>` : "<p>Enter the server password to authorize access to your vault.</p>"}
|
|
320
|
+
${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
321
|
<form method="POST" action="/oauth/approve" autocomplete="on">
|
|
314
322
|
<input type="hidden" name="code" value="${code}">
|
|
315
323
|
<input type="hidden" name="csrf" value="${csrf}">
|
|
@@ -395,20 +403,11 @@ var SearchIndex = class {
|
|
|
395
403
|
}
|
|
396
404
|
}
|
|
397
405
|
const output = [];
|
|
398
|
-
const lowerQuery = query.toLowerCase();
|
|
399
406
|
for (const path of paths) {
|
|
400
407
|
if (output.length >= MAX_RESULTS) break;
|
|
401
408
|
const content = this.contents.get(path);
|
|
402
409
|
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
|
-
}
|
|
410
|
+
output.push({ path, snippet: extractSnippet(content, query) });
|
|
412
411
|
}
|
|
413
412
|
return output;
|
|
414
413
|
}
|
|
@@ -425,6 +424,176 @@ var SearchIndex = class {
|
|
|
425
424
|
}
|
|
426
425
|
};
|
|
427
426
|
|
|
427
|
+
// src/tools.ts
|
|
428
|
+
import { z } from "zod";
|
|
429
|
+
|
|
430
|
+
// src/deeplink.ts
|
|
431
|
+
function makeDeepLink(vaultName, notePath) {
|
|
432
|
+
const cleanPath = notePath.replace(/\.md$/, "");
|
|
433
|
+
const encodedVault = encodeURIComponent(vaultName);
|
|
434
|
+
const encodedPath = encodeURIComponent(cleanPath);
|
|
435
|
+
return `obsidian://open?vault=${encodedVault}&file=${encodedPath}`;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// src/tools.ts
|
|
439
|
+
function registerTools(server2, vault2, searchIndex2, vaultName) {
|
|
440
|
+
server2.addTool({
|
|
441
|
+
name: "read_note",
|
|
442
|
+
description: "Read the content of a note from the Obsidian vault. Returns the markdown content and a deep link to open it in Obsidian.",
|
|
443
|
+
parameters: z.object({
|
|
444
|
+
path: z.string().describe("Vault-relative path to the note, e.g. 'daily/2026-03-23.md'")
|
|
445
|
+
}),
|
|
446
|
+
execute: async ({ path }) => {
|
|
447
|
+
const content = await vault2.readNote(path);
|
|
448
|
+
if (content === null) {
|
|
449
|
+
return `Note not found: ${path}`;
|
|
450
|
+
}
|
|
451
|
+
const deepLink = makeDeepLink(vaultName, path);
|
|
452
|
+
return `[Open in Obsidian](${deepLink})
|
|
453
|
+
|
|
454
|
+
---
|
|
455
|
+
|
|
456
|
+
${content}`;
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
server2.addTool({
|
|
460
|
+
name: "write_note",
|
|
461
|
+
description: "Write or update a note in the Obsidian vault. Creates the note if it doesn't exist, overwrites if it does.",
|
|
462
|
+
parameters: z.object({
|
|
463
|
+
path: z.string().describe("Vault-relative path to the note, e.g. 'daily/2026-03-23.md'"),
|
|
464
|
+
content: z.string().describe("Full markdown content for the note")
|
|
465
|
+
}),
|
|
466
|
+
execute: async ({ path, content }) => {
|
|
467
|
+
const ok = await vault2.writeNote(path, content);
|
|
468
|
+
if (!ok) {
|
|
469
|
+
return `Failed to write note: ${path}`;
|
|
470
|
+
}
|
|
471
|
+
searchIndex2.update(path, content);
|
|
472
|
+
const deepLink = makeDeepLink(vaultName, path);
|
|
473
|
+
return `Note saved: ${path}
|
|
474
|
+
[Open in Obsidian](${deepLink})`;
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
server2.addTool({
|
|
478
|
+
name: "list_notes",
|
|
479
|
+
description: "List all markdown notes in the vault, optionally filtered to a folder.",
|
|
480
|
+
parameters: z.object({
|
|
481
|
+
folder: z.string().optional().describe("Folder to filter by, e.g. 'daily/' or 'projects/'. Omit for all notes.")
|
|
482
|
+
}),
|
|
483
|
+
execute: async ({ folder }) => {
|
|
484
|
+
let notes = searchIndex2.listPaths(folder);
|
|
485
|
+
if (notes.length === 0) {
|
|
486
|
+
notes = await vault2.listNotes(folder);
|
|
487
|
+
}
|
|
488
|
+
if (notes.length === 0) {
|
|
489
|
+
return folder ? `No notes found in folder: ${folder}` : "Vault is empty.";
|
|
490
|
+
}
|
|
491
|
+
const total = notes.length;
|
|
492
|
+
const capped = notes.slice(0, 500);
|
|
493
|
+
const lines = capped.map((p) => {
|
|
494
|
+
const deepLink = makeDeepLink(vaultName, p);
|
|
495
|
+
return `- [${p}](${deepLink})`;
|
|
496
|
+
});
|
|
497
|
+
if (total > 500) {
|
|
498
|
+
lines.push(`
|
|
499
|
+
... and ${total - 500} more. Use a folder filter to narrow results.`);
|
|
500
|
+
}
|
|
501
|
+
return lines.join("\n");
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
server2.addTool({
|
|
505
|
+
name: "search_vault",
|
|
506
|
+
description: "Search for a text query across all notes in the vault. Returns matching notes with snippets.",
|
|
507
|
+
parameters: z.object({
|
|
508
|
+
query: z.string().describe("Text to search for (case-insensitive)")
|
|
509
|
+
}),
|
|
510
|
+
execute: async ({ query }) => {
|
|
511
|
+
const results = searchIndex2.search(query);
|
|
512
|
+
if (results.length === 0) {
|
|
513
|
+
return `No results for: ${query}`;
|
|
514
|
+
}
|
|
515
|
+
const lines = results.map((r) => {
|
|
516
|
+
const deepLink = makeDeepLink(vaultName, r.path);
|
|
517
|
+
return `### [${r.path}](${deepLink})
|
|
518
|
+
\`\`\`
|
|
519
|
+
${r.snippet}
|
|
520
|
+
\`\`\``;
|
|
521
|
+
});
|
|
522
|
+
return lines.join("\n\n");
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
server2.addTool({
|
|
526
|
+
name: "delete_note",
|
|
527
|
+
description: "Delete a note from the Obsidian vault.",
|
|
528
|
+
parameters: z.object({
|
|
529
|
+
path: z.string().describe("Vault-relative path to the note to delete")
|
|
530
|
+
}),
|
|
531
|
+
execute: async ({ path }) => {
|
|
532
|
+
const ok = await vault2.deleteNote(path);
|
|
533
|
+
if (ok) searchIndex2.remove(path);
|
|
534
|
+
return ok ? `Deleted: ${path}` : `Failed to delete: ${path}`;
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
server2.addTool({
|
|
538
|
+
name: "move_note",
|
|
539
|
+
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.",
|
|
540
|
+
parameters: z.object({
|
|
541
|
+
from: z.string().describe("Current path, e.g. 'daily/old-name.md'"),
|
|
542
|
+
to: z.string().describe("New path, e.g. 'projects/new-name.md'")
|
|
543
|
+
}),
|
|
544
|
+
execute: async ({ from, to }) => {
|
|
545
|
+
const content = await vault2.readNote(from);
|
|
546
|
+
const ok = await vault2.moveNote(from, to);
|
|
547
|
+
if (!ok) {
|
|
548
|
+
return `Failed to move: ${from} \u2192 ${to}`;
|
|
549
|
+
}
|
|
550
|
+
searchIndex2.remove(from);
|
|
551
|
+
if (content) searchIndex2.update(to, content);
|
|
552
|
+
const deepLink = makeDeepLink(vaultName, to);
|
|
553
|
+
return `Moved: ${from} \u2192 ${to}
|
|
554
|
+
[Open in Obsidian](${deepLink})`;
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
server2.addTool({
|
|
558
|
+
name: "get_note_metadata",
|
|
559
|
+
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.",
|
|
560
|
+
parameters: z.object({
|
|
561
|
+
path: z.string().describe("Vault-relative path to the note, e.g. 'projects/my-project.md'")
|
|
562
|
+
}),
|
|
563
|
+
execute: async ({ path }) => {
|
|
564
|
+
const meta = await vault2.getMetadata(path);
|
|
565
|
+
if (!meta) {
|
|
566
|
+
return `Note not found: ${path}`;
|
|
567
|
+
}
|
|
568
|
+
const deepLink = makeDeepLink(vaultName, path);
|
|
569
|
+
const lines = [
|
|
570
|
+
`**${path}**`,
|
|
571
|
+
`Size: ${meta.size} bytes`,
|
|
572
|
+
`Created: ${new Date(meta.ctime).toISOString()}`,
|
|
573
|
+
`Modified: ${new Date(meta.mtime).toISOString()}`
|
|
574
|
+
];
|
|
575
|
+
if (Object.keys(meta.frontmatter).length > 0) {
|
|
576
|
+
lines.push(`
|
|
577
|
+
Frontmatter:`);
|
|
578
|
+
for (const [k, v] of Object.entries(meta.frontmatter)) {
|
|
579
|
+
lines.push(` ${k}: ${v}`);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
if (meta.tags.length > 0) {
|
|
583
|
+
lines.push(`
|
|
584
|
+
Tags: ${meta.tags.map((t) => `#${t}`).join(", ")}`);
|
|
585
|
+
}
|
|
586
|
+
if (meta.links.length > 0) {
|
|
587
|
+
lines.push(`
|
|
588
|
+
Links: ${meta.links.join(", ")}`);
|
|
589
|
+
}
|
|
590
|
+
lines.push(`
|
|
591
|
+
[Open in Obsidian](${deepLink})`);
|
|
592
|
+
return lines.join("\n");
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
|
|
428
597
|
// src/main.ts
|
|
429
598
|
var VAULT_PATH = process.env.VAULT_PATH;
|
|
430
599
|
var COUCHDB_URL = process.env.COUCHDB_URL;
|
|
@@ -438,11 +607,11 @@ var BASE_URL = process.env.BASE_URL ?? `http://localhost:${PORT}`;
|
|
|
438
607
|
var AUTH_TOKEN = process.env.MCP_AUTH_TOKEN;
|
|
439
608
|
var vault;
|
|
440
609
|
if (VAULT_PATH) {
|
|
441
|
-
const { LocalVault } = await import("./vault-local-
|
|
610
|
+
const { LocalVault } = await import("./vault-local-WRHVJLBZ.js");
|
|
442
611
|
vault = new LocalVault(VAULT_PATH);
|
|
443
612
|
console.log(`Local mode: ${VAULT_PATH}`);
|
|
444
613
|
} else if (COUCHDB_URL) {
|
|
445
|
-
const { Vault } = await import("./vault-
|
|
614
|
+
const { Vault } = await import("./vault-GDCDM6GS.js");
|
|
446
615
|
vault = new Vault({
|
|
447
616
|
couchdbUrl: COUCHDB_URL,
|
|
448
617
|
couchdbUser: COUCHDB_USER,
|
|
@@ -457,9 +626,9 @@ if (VAULT_PATH) {
|
|
|
457
626
|
}
|
|
458
627
|
await vault.init();
|
|
459
628
|
console.log("Vault ready.");
|
|
460
|
-
var
|
|
629
|
+
var baseDataDir = process.env.DATA_DIR ?? join(process.env.HOME ?? process.env.USERPROFILE ?? "/tmp", ".obsidian-mcp");
|
|
461
630
|
var vaultId = createHash2("sha256").update(VAULT_PATH ?? COUCHDB_URL ?? "default").digest("hex").slice(0, 12);
|
|
462
|
-
var dataDir = join(
|
|
631
|
+
var dataDir = join(baseDataDir, vaultId);
|
|
463
632
|
var indexPath = join(dataDir, "search-index.json");
|
|
464
633
|
var searchIndex = new SearchIndex(indexPath);
|
|
465
634
|
var loaded = await searchIndex.loadFromDisk();
|
|
@@ -489,7 +658,7 @@ if (VAULT_PATH) {
|
|
|
489
658
|
}
|
|
490
659
|
});
|
|
491
660
|
console.log("Watching vault for external changes.");
|
|
492
|
-
} else if (COUCHDB_URL &&
|
|
661
|
+
} else if (COUCHDB_URL && vault.watchChanges) {
|
|
493
662
|
vault.watchChanges((path, content) => {
|
|
494
663
|
if (content) {
|
|
495
664
|
searchIndex.update(path, content);
|
|
@@ -501,7 +670,7 @@ if (VAULT_PATH) {
|
|
|
501
670
|
}
|
|
502
671
|
var serverOptions = {
|
|
503
672
|
name: "obsidian-sync-mcp",
|
|
504
|
-
version: "0.
|
|
673
|
+
version: "0.2.0",
|
|
505
674
|
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
675
|
};
|
|
507
676
|
var auth = null;
|
|
@@ -527,160 +696,7 @@ if (AUTH_TOKEN) {
|
|
|
527
696
|
auth = mountPasswordAuth(server.getApp(), BASE_URL, AUTH_TOKEN, tokenPath);
|
|
528
697
|
await auth.loadTokens();
|
|
529
698
|
}
|
|
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
|
-
});
|
|
699
|
+
registerTools(server, vault, searchIndex, VAULT_NAME);
|
|
684
700
|
async function shutdown() {
|
|
685
701
|
console.log("Shutting down...");
|
|
686
702
|
if (fsWatcher) fsWatcher.close();
|
|
@@ -697,7 +713,7 @@ setInterval(async () => {
|
|
|
697
713
|
}, 5 * 60 * 1e3).unref();
|
|
698
714
|
server.start({
|
|
699
715
|
transportType: "httpStream",
|
|
700
|
-
httpStream: { port: PORT, endpoint: "/mcp" }
|
|
716
|
+
httpStream: { port: PORT, endpoint: "/mcp", host: process.env.HOST ?? "0.0.0.0" }
|
|
701
717
|
});
|
|
702
718
|
console.log(`MCP server listening on port ${PORT}`);
|
|
703
719
|
//# sourceMappingURL=main.js.map
|