The Vault Operations layer (vault-operations/) provides the filesystem I/O foundation for reading, writing, and surgically transforming Obsidian vault content. It bridges the gap between pure Markdown parsing and the physical storage layer, ensuring that all file manipulations maintain vault integrity and follow Obsidian-compatible conventions. All functions in this layer follow a consistent (params, logger) two-argument pattern src/vault-mcp/vault-operations/vault-filesystem.ts71-73
The vaultFs object in vault-filesystem.ts provides the primary interface for interacting with .md files and binary assets. It handles path resolution, atomic writes, and structured data extraction.
All operations enforce strict path safety to prevent directory traversal attacks and ensure files remain within the vault boundaries.
resolveSafePath: Resolves a vault-relative path to an absolute path, throwing an error if the result escapes the vault root src/vault-mcp/vault-operations/vault-filesystem.ts55-65toVaultRelativePath: Canonicalizes paths by normalizing separators and collapsing traversal segments to prevent evasion of protected-path checks src/vault-mcp/vault-operations/vault-filesystem.ts51-52deleteNote check against protectedPaths (configured via MEMORY_DIR and DAILY_NOTES_FOLDER) after normalization to prevent accidental deletion of core configuration or memory files src/vault-mcp/vault-operations/vault-filesystem.ts400-410assertPathHasExtension to ensure operations only target .md files src/vault-mcp/vault-operations/vault-filesystem.ts184-188To prevent file corruption and partial writes (especially critical when syncing via Obsidian Sync), Vault Cortex uses atomic write patterns:
atomicWriteFile: Writes content to a unique .tmp file and then performs a rename over the target. This ensures readers see either the old or new content, never a truncated or 0-byte file src/vault-mcp/vault-operations/vault-filesystem.ts117-135atomicWriteFileExclusive: A "no-clobber" write that fails with EEXIST if the target exists. On supported filesystems, it uses hard link for atomicity; on Windows bind-mounts (where WINDOWS_MODE is active), it uses an O_EXCL (wx flag) placeholder reservation followed by a rename to maintain TOCTOU safety src/vault-mcp/vault-operations/vault-filesystem.ts156-182Beyond raw file reading, vaultFs and asset-operations.ts provide structured access:
readNoteOutline: Returns frontmatter and a list of all headings in the note src/vault-mcp/vault-operations/vault-filesystem.ts257-273readNoteSection: Extracts the content of a specific heading section, including its sub-headings src/vault-mcp/vault-operations/vault-filesystem.ts275-305readAssetContent: Dispatches files to their most useful representation: images fitted to a byte budget, canvases linearized, or PDFs rendered as images/structured text src/vault-mcp/vault-operations/asset-operations.ts21-31Sources: src/vault-mcp/vault-operations/vault-filesystem.ts src/vault-mcp/vault-operations/asset-operations.ts src/vault-mcp/config.ts
The vaultPatcher implements high-precision edits that target specific parts of a note, preserving frontmatter and formatting without full-file rewrites where possible.
The patchNote function supports operations relative to a specific heading src/vault-mcp/vault-operations/vault-patcher.ts186-196
| Operation | Behavior |
|---|---|
append | Adds content at the end of the section's body src/vault-mcp/vault-operations/vault-patcher.ts107-112 |
prepend | Adds content at the start of the section's body src/vault-mcp/vault-operations/vault-patcher.ts113-118 |
replace | Overwrites the entire body of the section src/vault-mcp/vault-operations/vault-patcher.ts119-124 |
insert_before | Places content immediately before the targeted heading src/vault-mcp/vault-operations/vault-patcher.ts125-130 |
replaceInNote: Performs targeted text replacement with an optional replace_all flag src/vault-mcp/vault-operations/vault-patcher.ts306-344deleteSpan: Removes a range of text defined by a startAnchor and an endAnchor src/vault-mcp/vault-operations/vault-patcher.ts346-386Diagram: Patcher Execution Flow
Sources: src/vault-mcp/vault-operations/vault-patcher.ts src/vault-mcp/vault-operations/vault-filesystem.ts
The note-mover.ts module handles moving notes while maintaining vault-wide referential integrity by updating all links pointing to the moved file src/vault-mcp/vault-operations/note-mover.ts1-19
The moveNote function follows a "preflight then commit" pattern src/vault-mcp/vault-operations/note-mover.ts271-354:
atomicWriteFileExclusive src/vault-mcp/vault-operations/note-mover.ts322mapWithConcurrency src/vault-mcp/vault-operations/note-mover.ts330-338pruneEmptyParents src/vault-mcp/vault-operations/note-mover.ts347-350The mover re-resolves links to determine the best format for the new location.
classifyLinkForm: Detects if the original link was a basename, absolute, or relative path src/vault-mcp/vault-operations/note-mover.ts94-116buildReplacementTarget: Constructs the new link. If a short "basename" link would become ambiguous at the new location, it upgrades it to a vault-absolute path src/vault-mcp/vault-operations/note-mover.ts123-155Diagram: Link Rewriting Logic
Sources: src/vault-mcp/vault-operations/note-mover.ts src/vault-mcp/obsidian-markdown/links.ts
The asset-operations.ts module provides a grouped read/browse surface for non-markdown files, handling media processing and format conversion src/vault-mcp/vault-operations/asset-operations.ts17-31
fitImageToByteBudget to ensure they fit within MCP response limits src/vault-mcp/vault-operations/asset-operations.ts35-36renderPdfPages src/vault-mcp/vault-operations/asset-operations.ts115-122.canvas files are linearized into a readable Markdown outline using linearizeCanvas src/vault-mcp/vault-operations/asset-operations.ts221-224MAX_TEXT_OUTPUT_BYTES (100 KiB) src/vault-mcp/vault-operations/asset-operations.ts40-54Sources: src/vault-mcp/vault-operations/asset-operations.ts src/vault-mcp/mcp-core/tools/asset-tools.ts
The memory-store.ts module manages the About Me/ directory using heading-aware logic for dated bullet entries src/vault-mcp/vault-operations/memory-store.ts1-15
SHRINK_FLOOR_BYTES of 1250) to prevent accidental clobbering src/vault-mcp/vault-operations/memory-store.ts28-43The daily-notes.ts module reads .obsidian/daily-notes.json to resolve paths for specific dates using Luxon, respecting user-defined formats and folders src/vault-mcp/vault-operations/daily-notes.ts41-65
Sources: src/vault-mcp/vault-operations/memory-store.ts src/vault-mcp/vault-operations/daily-notes.ts
The system uses file-write-lock.ts to prevent race conditions during concurrent tool calls.
withFileLock: Serializing. Queues operations. Used by memory-store.ts src/utils/file-write-lock.ts68-78withExclusiveFileLock: Fail-fast. Rejects if a write is in progress. Used by vault-patcher.ts src/vault-mcp/vault-operations/vault-patcher.ts9withExclusiveMultiFileLock: Multi-file Fail-fast. Atomic acquisition for multiple files. Used by note-mover.ts src/vault-mcp/vault-operations/note-mover.ts33Sources: src/utils/file-write-lock.ts src/vault-mcp/vault-operations/vault-patcher.ts src/vault-mcp/vault-operations/note-mover.ts
Refresh this wiki
This wiki was recently refreshed. Please wait 7 days to refresh again.