create_from_template
Generate new notes in Obsidian using predefined templates with custom variables and frontmatter for consistent document creation.
Instructions
Create a new note from a template
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| frontmatter | No | Additional frontmatter | |
| targetPath | Yes | Path for new note | |
| templatePath | Yes | Path to template file | |
| variables | No | Template variables | |
| vault | Yes | Vault name |
Implementation Reference
- src/index.ts:852-871 (handler)MCP tool handler for 'create_from_template' that retrieves the vault connector and delegates execution to TemplateService.createFromTemplate, returning the result as MCP content.case 'create_from_template': { const connector = this.connectors.get(args?.vault as string); if (!connector || !connector.vaultPath) { throw new Error(`Vault "${args?.vault}" not found or not a local vault`); } const result = await this.templateService.createFromTemplate( connector.vaultPath, args?.templatePath as string, args?.targetPath as string, { variables: args?.variables as Record<string, any> | undefined, frontmatter: args?.frontmatter as Record<string, any> | undefined, } ); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }], }; }
- src/index.ts:379-392 (registration)Registration of the 'create_from_template' tool in the MCP server's tools list, including name, description, and input schema.name: 'create_from_template', description: 'Create a new note from a template', inputSchema: { type: 'object', properties: { vault: { type: 'string', description: 'Vault name' }, templatePath: { type: 'string', description: 'Path to template file' }, targetPath: { type: 'string', description: 'Path for new note' }, variables: { type: 'object', description: 'Template variables' }, frontmatter: { type: 'object', description: 'Additional frontmatter' }, }, required: ['vault', 'templatePath', 'targetPath'], }, },
- Main helper function in TemplateService that renders a template using renderTemplate and writes the rendered content to the specified target path in the vault.async createFromTemplate( vaultPath: string, templatePath: string, targetPath: string, options?: TemplateRenderOptions ): Promise<VaultOperationResult<string>> { try { // Render template const renderResult = await this.renderTemplate(vaultPath, templatePath, { ...options, targetPath, }); if (!renderResult.success || !renderResult.data) { return { success: false, error: renderResult.error }; } // Write to target path const fullTargetPath = path.join(vaultPath, targetPath); // Ensure directory exists await fs.mkdir(path.dirname(fullTargetPath), { recursive: true }); // Write file await fs.writeFile(fullTargetPath, renderResult.data.content, 'utf-8'); return { success: true, data: targetPath }; } catch (error) { return { success: false, error: `Failed to create from template: ${error instanceof Error ? error.message : String(error)}` }; } }