#!/usr/bin/env node /** * obsidian — MCP-Server fuer Self-hosted LiveSync (CouchDB) oben auf * https://vpn.your-domain.com/obsidiannotes (Konto jarvis). * * Der Tresor ist Jarvis' Haupt-Dokumentationsbereich (Obsidian/Markdown). Der * Server liest und schreibt direkt im CouchDB-Datenbankschema von * Self-hosted LiveSync 1.0 (Protocol v2): * - Eine Datei = ein Metadata-Dokument { path, ctime, mtime, size, eden, * children, type: "plain" | "newnote" }, _id = klein-gefalteter Pfad. * - Ihr Inhalt = ein oder mehrere Chunk-Dokumente { type: "leaf", data }. * Bei type "plain" ist data literal (decode = direkt), bei "newnote" * Base64 (decode = base64). children referenziert die Chunks in * Reihenfolge; Konkatenieren ergibt den Dateiinhalt. * * Sicherheitsmodell: * - Nur der dedizierte CouchDB-User jarvis (kein Admin). Zugangsdaten aus * der Datei OBSIDIAN_PASSFILE (600, uid 1000) — nie im Config-Text. * - TLS-Verifizierung wird bewusst NICHT deaktiviert (Zertifikat von * vpn.your-domain.com wird normal geprueft; falls das Zert an Stelle der * Einrichtung abweicht: OBSIDIAN_TLS_INSECURE=1 im Server-Env setzen). * - create/update/delete nur als beabsichtigte Schreibzugriffe. Lesungen * liefern keine geheimen Werte ausser Notizinhalt. * - Session-/Zugangsdaten werden nie geloggt. */ 'use strict'; const https = require('https'); const fs = require('fs'); const { pathToFileURL } = require('url'); const URL = (process.env.OBSIDIAN_COUCH_URL || 'https://vpn.your-domain.com').replace(/\/$/, ''); const DB = process.env.OBSIDIAN_COUCH_DB || 'obsidiannotes'; const PASSFILE = process.env.OBSIDIAN_PASSFILE || '/home/node/.openclaw/mcp/.couchpass'; const COMMONLIB = process.env.OBSIDIAN_COMMONLIB || '/home/node/.openclaw/obsidian-commonlib/node_modules/@vrtmrz/livesync-commonlib/dist/index.js'; const TLS_INSECURE = String(process.env.OBSIDIAN_TLS_INSECURE || '0') === '1'; const TIMEOUT_MS = 20000; // stdout belongs exclusively to MCP JSON-RPC; Commonlib emits diagnostics via console. console.log = () => {}; console.info = () => {}; console.debug = () => {}; let cachedAuth = null; let manipulatorPromise = null; function readAuth() { if (cachedAuth) return cachedAuth; let st; try { st = fs.statSync(PASSFILE); } catch (e) { throw new Error(`Passwortdatei nicht lesbar: ${PASSFILE}`); } if ((st.mode & 0o077) !== 0) throw new Error(`Passwortdatei nicht restriktiv (Modus ${st.mode & 0o777}).`); if (st.uid !== 1000) throw new Error(`Passwortdatei gehoert uid ${st.uid}, erwartet 1000.`); const t = fs.readFileSync(PASSFILE, 'utf8').trim(); const idx = t.indexOf(':'); if (idx <= 0) throw new Error('Passwortdatei hat ungueltiges Format (user:pass).'); cachedAuth = { user: t.slice(0, idx), pass: t.slice(idx + 1) }; return cachedAuth; } function request(method, path, body) { return new Promise((resolve, reject) => { const auth = readAuth(); const data = body === undefined ? undefined : JSON.stringify(body); const timeout = setTimeout(() => { r.destroy(new Error('Timeout')); }, TIMEOUT_MS); const r = https.request({ hostname: URL.replace(/^https:\/\//, ''), port: 443, path, method, rejectUnauthorized: !TLS_INSECURE, headers: { Authorization: 'Basic ' + Buffer.from(`${auth.user}:${auth.pass}`).toString('base64'), Accept: 'application/json', ...(data ? { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(data) } : {}), }, }, (res) => { let s = ''; res.on('data', (c) => { s += c; }); res.on('end', () => { clearTimeout(timeout); let j = null; try { j = JSON.parse(s); } catch (e) { /* non-JSON */ } resolve({ status: res.statusCode, json: j }); }); }); r.on('error', (e) => { clearTimeout(timeout); reject(e); }); if (data) r.write(data); r.end(); }); } function enc(p) { return encodeURIComponent(p); } async function getDoc(id) { const r = await request('GET', `/${DB}/${enc(id)}`); if (r.status === 404) return null; if (r.status < 200 || r.status >= 300) throw new Error(`GET ${id}: ${r.status}`); return r.json; } async function allDocs() { const r = await request('GET', `/${DB}/_all_docs?include_docs=true`); const rows = r.json && r.json.rows ? r.json.rows.map((x) => x.doc) : []; return rows.filter((d) => d && d._id && !d._deleted); } function isFileMeta(d) { return Boolean(d && d.path && !d._deleted && (d.type === 'plain' || d.type === 'newnote') && typeof d.children === 'object'); } async function findMeta(path) { const docs = await allDocs(); return docs.find((d) => isFileMeta(d) && d.path === path) || docs.find((d) => isFileMeta(d) && String(d.path).toLowerCase() === String(path).toLowerCase()); } async function getManipulator() { if (manipulatorPromise) return manipulatorPromise; manipulatorPromise = (async () => { const milestone = await request('GET', `/${DB}/${enc('_local/obsydian_livesync_milestone')}`); const preferred = milestone.json && milestone.json.tweak_values && milestone.json.tweak_values.PREFERRED; if (!preferred) throw new Error('LiveSync-PREFERRED-Einstellungen fehlen im Milestone-Dokument.'); if (preferred.encrypt || preferred.usePathObfuscation || preferred.enableCompression) { throw new Error('Verschluesselung, Pfadverschleierung oder Kompression ist aktiv; Schreiben wird sicherheitshalber verweigert.'); } const { DirectFileManipulator } = await import(pathToFileURL(COMMONLIB).href); const auth = readAuth(); const client = new DirectFileManipulator({ url: URL, username: auth.user, password: auth.pass, database: DB, passphrase: undefined, obfuscatePassphrase: undefined, idDerivationVersion: preferred.idDerivationVersion ?? 0, encryptInternalMetadata: preferred.encryptInternalMetadata ?? false, useDynamicIterationCount: preferred.useDynamicIterationCount ?? false, customChunkSize: preferred.customChunkSize ?? 0, minimumChunkSize: preferred.minimumChunkSize ?? 20, hashAlg: preferred.hashAlg || 'xxhash64', useEden: preferred.useEden ?? false, maxChunksInEden: preferred.maxChunksInEden ?? 10, maxTotalLengthInEden: preferred.maxTotalLengthInEden ?? 1024, maxAgeInEden: preferred.maxAgeInEden ?? 10, enableCompression: false, handleFilenameCaseSensitive: preferred.handleFilenameCaseSensitive ?? false, chunkSplitterVersion: preferred.chunkSplitterVersion || 'v3-rabin-karp', E2EEAlgorithm: preferred.E2EEAlgorithm || 'v2', }); await client.ready.promise; return client; })(); return manipulatorPromise; } function fmtTime(ms) { if (!ms) return '-'; return new Date(ms).toISOString().replace('T', ' ').replace('Z', 'UTC').slice(0, 19); } function fmtBytes(n) { if (n >= 1024 * 1024) return (n / 1048576).toFixed(2) + ' MiB'; if (n >= 1024) return (n / 1024).toFixed(1) + ' KiB'; return n + ' B'; } /* ---------- Werkzeuge ---------- */ async function status() { const u = await request('GET', '/'); const db = await request('GET', `/${DB}`); return { error: false, text: [ '## Obsidian / LiveSync (CouchDB)', '', `- Server: ${URL} (CouchDB ${u.json && u.json.version ? u.json.version : '?'})`, `- Datenbank: ${DB} (Dokumente: ${db.json ? db.json.doc_count : '?'})`, `- Konto: ${readAuth().user}`, '', 'Haupt-Dokumentationsbereich (Jarvis) — Markdown-Notizen, syncen per LiveSync zu allen Obsidian-Geraeten.', ].join('\n'), }; } async function listNotes(args) { const docs = await allDocs(); const files = docs.filter(isFileMeta) .map((d) => ({ path: d.path, size: d.size || 0, mtime: d.mtime || 0, type: d.type === 'newnote' ? 'binar' : 'text', chunks: Array.isArray(d.children) ? d.children.length : 0 })) .sort((a, b) => a.path.localeCompare(b.path)); const prefix = args && args.prefix ? String(args.prefix) : ''; const filtered = prefix ? files.filter((f) => f.path.toLowerCase().startsWith(prefix.toLowerCase())) : files; const limit = Math.min(1000, Math.max(1, Number((args && args.limit) || 200))); const shown = filtered.slice(0, limit); const totalBytes = filtered.reduce((s, f) => s + (f.size || 0), 0); const lines = [ '## Obsidian-Notizen', '', `Dateien: ${filtered.length} (${fmtBytes(totalBytes)})${filtered.length > shown.length ? ', angezeigt ' + shown.length : ''}`, '', '| Pfad | Groesse | Geaendert | Chunks |', '|---|---|---|---|', ]; for (const f of shown) { lines.push(`| ${f.path.replace(/\|/g, '\\|')} | ${fmtBytes(f.size)} | ${fmtTime(f.mtime)} | ${f.chunks} |`); } if (!shown.length) lines.push('| (keine Notizen) |'); return { error: false, text: lines.join('\n') }; } async function readNote(args) { const path = String((args && args.path) || '').trim(); if (!path) return { error: true, text: 'Pfad fehlt.' }; const meta = await findMeta(path); if (!meta) return { error: true, text: `Notiz nicht gefunden: ${path}` }; const chunks = []; for (const id of meta.children || []) { const c = await getDoc(id); if (!c) throw new Error(`Chunk ${id} von ${path} fehlt.`); chunks.push(c.data || ''); } let content = chunks.join(''); if (meta.type === 'newnote') content = Buffer.from(content, 'base64').toString('utf8'); if (content.length > 60000) content = content.slice(0, 60000) + '\n\n… (gekuerzt)'; return { error: false, text: [ `## ${path}`, '', `_${fmtTime(meta.mtime)}, ${fmtBytes(meta.size)}_`, '', content, ].join('\n'), }; } async function writeNote(args) { const path = String((args && args.path) || '').trim(); const content = String((args && args.content) === undefined ? '' : args.content); if (!path) return { error: true, text: 'Pfad fehlt (z. B. "Doku/Projekt.md").' }; if (!path.toLowerCase().endsWith('.md')) return { error: true, text: 'Schreiben ist auf Markdown-Notizen (.md) begrenzt.' }; const existing = await findMeta(path); const now = Date.now(); const size = Buffer.byteLength(content, 'utf8'); const client = await getManipulator(); const ok = await client.put(path, new Blob([content], { type: 'text/plain' }), { ctime: existing ? existing.ctime : now, mtime: now, size, }, 'plain'); if (!ok) throw new Error(`LiveSync-Commonlib konnte ${path} nicht speichern.`); const saved = await findMeta(path); if (!saved) throw new Error(`Gespeicherte Metadata fuer ${path} nicht gefunden.`); return { error: false, text: `## Gespeichert: ${path}\n\n- Dokument: ${saved._id}\n- Groesse: ${fmtBytes(size)} (${saved.children.length} Chunk(s))\n- Zeit: ${fmtTime(now)}\n- Writer: LiveSync Commonlib 0.1.36\n\n${content.length > 400 ? content.slice(0, 400) + '\n…' : content}`, }; } async function deleteNote(args) { const path = String((args && args.path) || '').trim(); if (!path) return { error: true, text: 'Pfad fehlt.' }; const meta = await findMeta(path); if (!meta) return { error: true, text: `Notiz nicht gefunden: ${path}` }; const client = await getManipulator(); const ok = await client.delete(path); if (!ok) throw new Error(`LiveSync-Commonlib konnte ${path} nicht loeschen.`); return { error: false, text: `## Geloescht: ${path}\n\n(LiveSync-konforme Loeschung ueber Commonlib; Chunks bleiben fuer Sync/GC erhalten.)` }; } async function searchNotes(args) { const text = String((args && args.text) || '').trim().toLowerCase(); const limit = Math.min(50, Math.max(1, Number((args && args.limit) || 10))); if (!text) return { error: true, text: 'Suchtext fehlt.' }; const docs = await allDocs(); const files = docs.filter(isFileMeta); const hits = []; outer: for (const meta of files) { if (hits.length >= limit) break; const chunks = []; for (const id of meta.children || []) { const c = await getDoc(id); if (c) chunks.push(c.data || ''); } let content = chunks.join(''); if (meta.type === 'newnote') content = Buffer.from(content, 'base64').toString('utf8'); const idx = content.toLowerCase().indexOf(text); if (idx >= 0) { const start = Math.max(0, idx - 60); const snippet = (start > 0 ? '…' : '') + content.slice(start, idx + text.length + 120).replace(/\n+/g, ' ').replace(/\|/g, '\\|'); hits.push({ path: meta.path, snippet }); } } const lines = [ `## Suche: ${(args && args.text)}`, '', `Treffer: ${hits.length}${hits.length >= limit ? ' (Limit)' : ''}`, '', ...hits.map((h) => `- **${h.path}**: ${h.snippet}`), ]; if (!hits.length) lines.push('(keine Treffer)'); return { error: false, text: lines.join('\n') }; } const TOOLS = [ { name: 'obsidian_status', description: 'Zeigt Server- und Vaultzustand (CouchDB-Version, Datenbank, Dokumentanzahl, Konto). Lesend.', inputSchema: { type: 'object', properties: {}, additionalProperties: false }, }, { name: 'obsidian_list_notes', description: 'Listet Obsidian-Notizen (Pfad, Groesse, Aenderungszeit, Chunk-Anzahl), optional gefiltert nach Pfad-Praefix. Lesend.', inputSchema: { type: 'object', properties: { prefix: { type: 'string', description: 'Nur Notizen unter diesem Pfad-Praefix, z. B. "Doku/".' }, limit: { type: 'integer', description: 'Max. Zeilen (1-1000, Standard 200).' }, }, additionalProperties: false, }, }, { name: 'obsidian_read_note', description: 'Liest eine Obsidian-Notiz vollstaendig (Markdown) anhand ihres Pfads, z. B. "Projekt/X.md". Lesend.', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Vault-Pfad der Notiz (inkl. .md).' }, }, required: ['path'], additionalProperties: false, }, }, { name: 'obsidian_search_notes', description: 'Durchsucht Notizinhalte nach einem Text (Gross-/Kleinschreibung egal) und liefert Pfad + Trefferschnipsel. Lesend.', inputSchema: { type: 'object', properties: { text: { type: 'string', description: 'Suchtext.' }, limit: { type: 'integer', description: 'Max. Treffer (1-50, Standard 10).' }, }, required: ['text'], additionalProperties: false, }, }, { name: 'obsidian_write_note', description: 'Legt eine Notiz an oder ueberschreibt sie (Markdown) unter dem angegebenen Pfad — Jarvis\' Haupt-Dokumentation. Inhalt wird als Chunk-Dokumente schema-konform im LiveSync-Vault gespeichert. SCHREIBEND.', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Vault-Pfad, z. B. "Doku/Projekt.md".' }, content: { type: 'string', description: 'Markdown-Inhalt (ueberschreibt die Notiz).' }, }, required: ['path', 'content'], additionalProperties: false, }, }, { name: 'obsidian_delete_note', description: 'Loescht eine Obsidian-Notiz (Pfad). Vorher mit obsidian_read_note bestaetigen. SCHREIBEND, destruktiv.', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Vault-Pfad der zu loeschenden Notiz.' }, }, required: ['path'], additionalProperties: false, }, }, ]; /* ---------- MCP-Rahmen (wie bitwarden.js) ---------- */ function reply(id, result) { process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id, result }) + '\n'); } function replyError(id, code, message) { process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id, error: { code, message } }) + '\n'); } let buf = ''; process.stdin.setEncoding('utf8'); process.stdin.on('data', (chunk) => { buf += chunk; let idx; while ((idx = buf.indexOf('\n')) >= 0) { const line = buf.slice(0, idx).trim(); buf = buf.slice(idx + 1); if (!line) continue; handle(line); } }); function handle(line) { let msg; try { msg = JSON.parse(line); } catch (e) { replyError(null, -32700, 'Parse error'); return; } const { id, method, params } = msg; if (method === 'notifications/initialized' || (id === undefined && method)) return; if (method === 'initialize') { reply(id, { protocolVersion: (params && params.protocolVersion) || '2024-11-05', capabilities: { tools: {} }, serverInfo: { name: 'obsidian', version: '1.0.0' }, }); return; } if (method === 'ping') { reply(id, {}); return; } if (method === 'tools/list') { reply(id, { tools: TOOLS.map(({ name, description, inputSchema }) => ({ name, description, inputSchema, annotations: { title: name.replace(/_/g, ' '), readOnlyHint: /^obsidian_(status|list_notes|read_note|search_notes)$/.test(name), destructiveHint: name === 'obsidian_delete_note', idempotentHint: name !== 'obsidian_delete_note', openWorldHint: true, }, })), }); return; } if (method === 'tools/call') { const name = params && params.name; const args = (params && params.arguments) || {}; const dispatch = { obsidian_status: () => status(), obsidian_list_notes: () => listNotes(args), obsidian_read_note: () => readNote(args), obsidian_search_notes: () => searchNotes(args), obsidian_write_note: () => writeNote(args), obsidian_delete_note: () => deleteNote(args), }; const fn = dispatch[name]; if (!fn) { reply(id, { content: [{ type: 'text', text: `Unbekanntes oder nicht freigegebenes Werkzeug: ${name}` }], isError: true }); return; } fn() .then((r) => { if (r.error) reply(id, { content: [{ type: 'text', text: r.text }], isError: true }); else reply(id, { content: [{ type: 'text', text: r.text }], isError: false }); }) .catch((e) => reply(id, { content: [{ type: 'text', text: `Interner Fehler: ${e.message || e}` }], isError: true })); return; } replyError(id, -32601, 'Method not found'); }