prepend_to_note
Add content to the beginning of an Obsidian note while preserving frontmatter. Use this tool to insert new information at the top of your notes.
Instructions
Prepend content to a note, after frontmatter if present
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Relative path to the note | |
| content | Yes | Content to prepend |
Implementation Reference
- src/lib/vault.ts:96-118 (handler)The implementation of the prepend_to_note logic which reads the file, detects frontmatter, and prepends the new content either after the frontmatter or at the start of the file.
export async function prependToNote( vaultPath: string, relativePath: string, content: string, ): Promise<void> { const fullPath = resolveVaultPath(vaultPath, relativePath); const existing = await fs.readFile(fullPath, "utf-8"); // Detect frontmatter block (starts with --- on first line) const frontmatterMatch = existing.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n?/); let result: string; if (frontmatterMatch) { const frontmatter = frontmatterMatch[0]; const rest = existing.slice(frontmatter.length); const separator = frontmatter.endsWith("\n") ? "" : "\n"; result = frontmatter + separator + content + "\n" + rest; } else { result = content + "\n" + existing; } await fs.writeFile(fullPath, result, "utf-8"); } - src/tools/write.ts:129-149 (registration)The MCP tool registration for 'prepend_to_note', which validates inputs and calls the prependToNote handler.
// 3. prepend_to_note server.registerTool( "prepend_to_note", { description: "Prepend content to a note, after frontmatter if present", inputSchema: { path: z.string().min(1).describe("Relative path to the note"), content: z.string().describe("Content to prepend"), }, }, async ({ path: notePath, content }) => { try { const resolvedPath = ensureMdExtension(notePath); await prependToNote(vaultPath, resolvedPath, content); return textResult(`Prepended content to '${resolvedPath}'.`); } catch (err) { console.error("prepend_to_note error:", err); return errorResult(`Error prepending to note: ${err instanceof Error ? err.message : String(err)}`); } }, );