Skip to main content
Glama

update_frontmatter

Modify or add YAML frontmatter properties in Obsidian notes to organize metadata, update tags, or manage document attributes.

Instructions

Update or add frontmatter (YAML) properties in a note

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to the note
propertiesYesKey-value pairs to set in frontmatter. Use null to delete a property.

Implementation Reference

  • The handler function that reads the note, parses its frontmatter, merges/updates the provided properties (deleting if null), serializes new frontmatter, and writes back to the file.
    async function handleUpdateFrontmatter(args: { path: string; properties: Record<string, unknown>; }): Promise<string> { const fullPath = resolvePath(args.path); if (!(await fileExists(fullPath))) { throw new Error(`Note not found at ${args.path}`); } const content = await fs.readFile(fullPath, "utf-8"); const { frontmatter, body } = parseFrontmatter(content); const newFrontmatter = { ...(frontmatter || {}), ...args.properties }; // Remove null values for (const [key, value] of Object.entries(newFrontmatter)) { if (value === null) { delete newFrontmatter[key]; } } const newContent = serializeFrontmatter(newFrontmatter) + body; await fs.writeFile(fullPath, newContent, "utf-8"); return `Successfully updated frontmatter in ${args.path}`; }
  • Input schema defining the parameters for the update_frontmatter tool: path to the note and properties object to update/add/delete.
    inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the note", }, properties: { type: "object", description: "Key-value pairs to set in frontmatter. Use null to delete a property.", }, }, required: ["path", "properties"], },
  • src/index.ts:930-934 (registration)
    Registration in the tool dispatch switch statement that calls the handler based on tool name.
    case "update_frontmatter": result = await handleUpdateFrontmatter( args as { path: string; properties: Record<string, unknown> } ); break;
  • src/index.ts:300-318 (registration)
    Tool registration in the tools array used for listing available tools.
    { name: "update_frontmatter", description: "Update or add frontmatter (YAML) properties in a note", inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the note", }, properties: { type: "object", description: "Key-value pairs to set in frontmatter. Use null to delete a property.", }, }, required: ["path", "properties"], }, },
  • Helper function to parse frontmatter YAML from note content into object and extract body.
    function parseFrontmatter(content: string): { frontmatter: Record<string, unknown> | null; body: string; raw: string; } { const match = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/); if (match) { try { // Simple YAML parsing for common cases const yaml: Record<string, unknown> = {}; const lines = match[1].split("\n"); for (const line of lines) { const colonIndex = line.indexOf(":"); if (colonIndex > 0) { const key = line.slice(0, colonIndex).trim(); let value: unknown = line.slice(colonIndex + 1).trim(); // Handle arrays if (value === "") { value = []; } else if ( typeof value === "string" && value.startsWith("[") && value.endsWith("]") ) { value = value .slice(1, -1) .split(",") .map((v) => v.trim()); } yaml[key] = value; } } return { frontmatter: yaml, body: match[2], raw: match[1] }; } catch { return { frontmatter: null, body: content, raw: "" }; } } return { frontmatter: null, body: content, raw: "" }; }
  • Helper function to serialize a frontmatter object back to YAML string format.
    function serializeFrontmatter(obj: Record<string, unknown>): string { const lines: string[] = []; for (const [key, value] of Object.entries(obj)) { if (value === null || value === undefined) continue; if (Array.isArray(value)) { lines.push(`${key}: [${value.join(", ")}]`); } else { lines.push(`${key}: ${value}`); } } return `---\n${lines.join("\n")}\n---\n`; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sureshsankaran/obsidian-tools-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server