create_obsidian_note
Create Obsidian notes programmatically by specifying path, title, content, and tags, with optional template support for structured frontmatter.
Instructions
Create a new Obsidian note with optional frontmatter and tags
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path for the new note (relative to vault root, e.g., "folder/note.md") | |
| title | Yes | Title of the note | |
| content | Yes | Content of the note | |
| tags | No | Tags to add to the note | |
| template | No | Template to use for the note (optional) |
Implementation Reference
- src/index.ts:176-200 (handler)The handleCreateNote method in ObsidianMCPServer class that orchestrates the tool execution - extracts args, calls notesTool.createNote(), and formats the response.
/** * ノート作成の処理 */ private async handleCreateNote(args: any) { const { path, title, content, tags, template } = args; if (!path || !title || !content) { throw new McpError(ErrorCode.InvalidParams, 'Path, title, and content are required'); } const result = await this.notesTool.createNote(path, title, content, tags, template); return { content: [ { type: 'text', text: `✅ ノートが作成されました\\n\\n` + `📁 パス: ${result.path}\\n` + `📝 タイトル: ${result.title}\\n` + `🏷️ タグ: ${result.tags.join(', ') || 'なし'}\\n` + `📅 作成日時: ${result.created.toISOString()}` } ] }; } - src/tools/notes.ts:106-169 (handler)The createNote method in NotesTool class that executes the core logic: checks file existence, appends .md extension, builds frontmatter with title/dates/tags, applies optional template, writes file via fileSystem.writeFile, and returns NoteMetadata.
/** * 新しいノートを作成 * @param path ノートのパス * @param title ノートのタイトル * @param content ノートの内容 * @param tags タグ(オプション) * @param template テンプレート(オプション) * @returns 作成されたノートの情報 */ async createNote( path: string, title: string, content: string, tags: string[] = [], template?: string ): Promise<NoteMetadata> { try { // ファイルが既に存在するかチェック if (await this.fileSystem.exists(path)) { throw new Error(`${ErrorCode.FILE_NOT_FOUND}: File '${path}' already exists`); } // .mdの拡張子を確認 if (!path.endsWith('.md')) { path += '.md'; } // Frontmatterを作成 const frontmatter: any = { title, created: new Date().toISOString(), modified: new Date().toISOString() }; if (tags.length > 0) { frontmatter.tags = tags; } // テンプレートが指定されている場合は適用 let finalContent = content; if (template) { finalContent = template.replace('{{content}}', content); } // Frontmatterとコンテンツを結合 const noteContent = matter.stringify(finalContent, frontmatter); // ファイルを作成 await this.fileSystem.writeFile(path, noteContent); return { title, path, tags, lastModified: new Date(), created: new Date() }; } catch (error) { if (error instanceof Error) { throw error; } throw new Error(`${ErrorCode.PERMISSION_DENIED}: Failed to create note '${path}'`); } } - src/tools/notes.ts:17-48 (schema)The getCreateNoteToolDefinition static method that defines the tool's name ('create_obsidian_note'), description, and inputSchema with required fields (path, title, content) and optional fields (tags, template).
static getCreateNoteToolDefinition(): Tool { return { name: 'create_obsidian_note', description: 'Create a new Obsidian note with optional frontmatter and tags', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path for the new note (relative to vault root, e.g., "folder/note.md")' }, title: { type: 'string', description: 'Title of the note' }, content: { type: 'string', description: 'Content of the note' }, tags: { type: 'array', items: { type: 'string' }, description: 'Tags to add to the note' }, template: { type: 'string', description: 'Template to use for the note (optional)' } }, required: ['path', 'title', 'content'] } }; - src/index.ts:87-99 (registration)The setupRequestHandlers method where the tool is registered - NotesTool.getCreateNoteToolDefinition() is listed in the ListToolsRequestSchema handler, and the 'create_obsidian_note' case in the CallToolRequestSchema switch dispatches to handleCreateNote.
private setupRequestHandlers(): void { // ツール一覧の取得 this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ TranslateTool.getToolDefinition(), NotesTool.getCreateNoteToolDefinition(), NotesTool.getReadNoteToolDefinition(), NotesTool.getUpdateNoteToolDefinition(), SearchTool.getSearchToolDefinition(), SearchTool.getSearchByTagsToolDefinition(), ], }; - src/utils/file-system.ts:51-67 (helper)The writeFile method in FileSystemHelper that handles the actual file writing with recursive directory creation, used by createNote to persist the note to disk.
/** * ファイルを書き込み * @param filePath ファイルパス * @param content ファイル内容 */ async writeFile(filePath: string, content: string): Promise<void> { try { const absolutePath = this.getAbsolutePath(filePath); // ディレクトリが存在しない場合は作成 await fs.mkdir(dirname(absolutePath), { recursive: true }); await fs.writeFile(absolutePath, content, 'utf-8'); } catch (error) { throw new Error(`${ErrorCode.PERMISSION_DENIED}: Cannot write file '${filePath}': ${error}`); } }