write_note
Create or update notes in an Obsidian vault by specifying the path and content. Choose to overwrite, append, or prepend content for flexible note management.
Instructions
Create or update a note in the vault
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | Content of the note | |
| mode | No | How to handle existing content | overwrite |
| note_path | Yes | Path to the note relative to vault root | |
| vault_path | Yes | Path to the Obsidian vault |
Implementation Reference
- src/tools/notes.ts:40-87 (handler)The main handler function implementing the write_note tool logic, which validates the vault, ensures directories exist, handles different write modes (overwrite, append, prepend) with frontmatter awareness, and writes the note file.export async function handleWriteNote( vaultManager: VaultManager, vaultPath: string, notePath: string, content: string, mode: 'overwrite' | 'append' | 'prepend' = 'overwrite' ) { await vaultManager.validateVault(vaultPath); const filePath = vaultManager.getFilePath(vaultPath, notePath); // Ensure directory exists const dir = path.dirname(filePath); await fs.mkdir(dir, { recursive: true }); let finalContent = content; if (mode !== 'overwrite') { try { const existing = await fs.readFile(filePath, 'utf-8'); if (mode === 'append') { finalContent = existing + '\n' + content; } else if (mode === 'prepend') { // If the existing content has frontmatter, insert after it const parsed = matter(existing); if (Object.keys(parsed.data).length > 0) { finalContent = parsed.matter + '\n' + content + '\n' + parsed.content; } else { finalContent = content + '\n' + existing; } } } catch (error: any) { // File doesn't exist, just write the content if (error.code !== 'ENOENT') { throw error; } } } await fs.writeFile(filePath, finalContent, 'utf-8'); return { path: notePath, success: true, mode, }; }
- src/index.ts:76-103 (schema)Defines the tool metadata and input schema for write_note, specifying required parameters and optional mode.{ name: 'write_note', description: 'Create or update a note in the vault', inputSchema: { type: 'object', properties: { vault_path: { type: 'string', description: 'Path to the Obsidian vault', }, note_path: { type: 'string', description: 'Path to the note relative to vault root', }, content: { type: 'string', description: 'Content of the note', }, mode: { type: 'string', enum: ['overwrite', 'append', 'prepend'], description: 'How to handle existing content', default: 'overwrite', }, }, required: ['vault_path', 'note_path', 'content'], }, },
- src/index.ts:190-201 (registration)Switch case in the tool execution handler that validates arguments and calls the handleWriteNote function for write_note tool invocations.case 'write_note': if (!args || typeof args !== 'object' || !('vault_path' in args) || !('note_path' in args) || !('content' in args)) { throw new McpError(ErrorCode.InvalidParams, 'Missing required parameters'); } result = await handleWriteNote( vaultManager, args.vault_path as string, args.note_path as string, args.content as string, (args.mode as 'overwrite' | 'append' | 'prepend') || 'overwrite' ); break;