Skip to main content
Mastra 1.0 is available 🎉 Read announcement

WorkspaceFilesystem Interface

The WorkspaceFilesystem interface defines how workspaces interact with file storage.

Methods
Direct link to Methods

readFile(path, options?)
Direct link to readfilepath-options

Read file contents.

const content = await filesystem.readFile('/docs/guide.md');
const buffer = await filesystem.readFile('/image.png', { encoding: 'binary' });

Parameters:

path:

string
File path relative to basePath

options.encoding?:

'utf-8' | 'binary'
= 'utf-8'
Text or binary encoding

Returns: Promise<string | Buffer>

writeFile(path, content, options?)
Direct link to writefilepath-content-options

Write file contents.

await filesystem.writeFile('/docs/new.md', '# New Document');
await filesystem.writeFile('/nested/path/file.md', content, { recursive: true });

Parameters:

path:

string
File path relative to basePath

content:

string | Buffer
File content

options.recursive?:

boolean
= false
Create parent directories if they don't exist

options.overwrite?:

boolean
= true
Overwrite existing file

deleteFile(path, options?)
Direct link to deletefilepath-options

Delete a file.

await filesystem.deleteFile('/docs/old.md');
await filesystem.deleteFile('/docs/maybe.md', { force: true }); // Don't throw if missing

Parameters:

path:

string
File path

options.force?:

boolean
= false
Don't throw error if file doesn't exist

readdir(path, options?)
Direct link to readdirpath-options

List directory contents.

const entries = await filesystem.readdir('/docs');
// [{ name: 'guide.md', type: 'file' }, { name: 'api', type: 'directory' }]

Returns: Promise<FileEntry[]>

interface FileEntry {
name: string;
type: 'file' | 'directory';
size?: number;
modifiedAt?: Date;
}

mkdir(path, options?)
Direct link to mkdirpath-options

Create a directory.

await filesystem.mkdir('/docs/api');
await filesystem.mkdir('/deeply/nested/path', { recursive: true });

Parameters:

path:

string
Directory path

options.recursive?:

boolean
= false
Create parent directories

rmdir(path, options?)
Direct link to rmdirpath-options

Remove a directory.

await filesystem.rmdir('/docs/old');
await filesystem.rmdir('/docs/nested', { recursive: true });

Parameters:

path:

string
Directory path

options.recursive?:

boolean
= false
Remove contents recursively

options.force?:

boolean
= false
Don't throw if directory doesn't exist

exists(path)
Direct link to existspath

Check if a path exists.

const exists = await filesystem.exists('/docs/guide.md');

Returns: Promise<boolean>

stat(path)
Direct link to statpath

Get file or directory metadata.

const stat = await filesystem.stat('/docs/guide.md');
// { type: 'file', size: 1234, modifiedAt: Date, createdAt: Date, path: '/docs/guide.md' }

Returns: Promise<FileStat>

interface FileStat {
type: 'file' | 'directory';
size: number;
modifiedAt: Date;
createdAt: Date;
path: string;
}

Optional Methods
Direct link to Optional Methods

init()
Direct link to init

Initialize the filesystem. Called by workspace.init().

await filesystem.init?.();

destroy()
Direct link to destroy

Clean up resources. Called by workspace.destroy().

await filesystem.destroy?.();

getInfo()
Direct link to getinfo

Get filesystem metadata.

const info = await filesystem.getInfo?.();
// { id, name, provider, basePath, readOnly, status, storage? }

Returns: Promise<FilesystemInfo>

interface FilesystemInfo {
id: string;
name: string;
provider: string;
basePath?: string;
readOnly?: boolean;
status?: string;
storage?: {
totalBytes?: number;
usedBytes?: number;
availableBytes?: number;
};
}

getInstructions()
Direct link to getinstructions

Get a description of how this filesystem works. Used in tool descriptions to help agents understand path semantics.

const instructions = filesystem.getInstructions?.();
// 'Local filesystem at "/workspace". Files at workspace path "/foo" are stored at "/workspace/foo" on disk.'

Returns: string