Workspace Class
Added in: @mastra/core@1.1.0
The Workspace class combines a filesystem and sandbox to provide agents with file storage and command execution capabilities. It also supports BM25 and vector search for indexed content.
Usage ExampleDirect link to Usage Example
import { Workspace, LocalFilesystem, LocalSandbox } from '@mastra/core/workspace'
const workspace = new Workspace({
id: 'my-workspace',
name: 'My Workspace',
filesystem: new LocalFilesystem({
basePath: './workspace',
}),
sandbox: new LocalSandbox({
workingDirectory: './workspace',
}),
bm25: true,
autoIndexPaths: ['/docs'],
})
Constructor parametersDirect link to Constructor parameters
id?:
name?:
filesystem?:
sandbox?:
bm25?:
vectorStore?:
embedder?:
autoIndexPaths?:
skills?:
skillSource?:
onMount?:
searchIndexName?:
tools?:
enabled?:
requireApproval?:
name?:
requireReadBeforeWrite?:
maxOutputTokens?:
operationTimeout?:
Tool configurationDirect link to Tool configuration
The tools option accepts a WorkspaceToolsConfig object that controls which workspace tools are enabled and their safety settings.
import { Workspace } from '@mastra/core/workspace'
import { WORKSPACE_TOOLS } from '@mastra/core/workspace'
const workspace = new Workspace({
id: 'my-workspace',
name: 'My Workspace',
tools: {
// Global defaults (apply to all tools)
enabled: true,
requireApproval: false,
// Per-tool overrides using WORKSPACE_TOOLS constants
[WORKSPACE_TOOLS.FILESYSTEM.WRITE_FILE]: {
requireApproval: true,
},
},
})
The config object has two parts:
- Global defaults (
enabled,requireApproval) - Apply to all tools unless overridden - Per-tool overrides - Use
WORKSPACE_TOOLSconstants as keys to configure individual tools
See workspace overview for more examples.
Tool name remappingDirect link to Tool name remapping
Rename workspace tools by setting the name property on individual tool configs. The config key remains the original constant — only the name exposed to the agent changes.
import { Workspace } from '@mastra/core/workspace'
import { WORKSPACE_TOOLS } from '@mastra/core/workspace'
const workspace = new Workspace({
id: 'my-workspace',
name: 'My Workspace',
tools: {
[WORKSPACE_TOOLS.FILESYSTEM.READ_FILE]: { name: 'view' },
[WORKSPACE_TOOLS.FILESYSTEM.GREP]: { name: 'search_content' },
},
})
Tool names must be unique across all workspace tools. Setting a custom name that conflicts with another tool's default or custom name throws an error.
PropertiesDirect link to Properties
id:
name:
status:
filesystem:
sandbox:
skills:
canBM25:
canVector:
canHybrid:
MethodsDirect link to Methods
LifecycleDirect link to Lifecycle
init()Direct link to init
Initialize the workspace and prepare resources.
await workspace.init()
Calling init() is optional in most cases:
- Sandbox: Auto-starts on first
executeCommand()call. Useinit()to avoid first-command latency. - Filesystem: Creates the base directory and runs any provider-specific setup. Some providers auto-create the directory on first operation.
- Search: Required only if using
autoIndexPathsfor auto-indexing.
Initialization performs:
- Starts the filesystem provider (creates base directory if needed)
- Starts the sandbox provider (creates working directory, sets up isolation if configured)
- Indexes files from
autoIndexPathsfor search
destroy()Direct link to destroy
Destroy the workspace and clean up resources.
await workspace.destroy()
Search operationsDirect link to Search operations
index(path, content, options?)Direct link to indexpath-content-options
Index content for search.
await workspace.index('/docs/guide.md', 'Guide content...')
search(query, options?)Direct link to searchquery-options
Search indexed content.
const results = await workspace.search('password reset', {
topK: 10,
mode: 'hybrid',
})
UtilityDirect link to Utility
getInfo()Direct link to getinfo
Get workspace information.
const info = await workspace.getInfo()
// { id, name, status, createdAt, lastAccessedAt, filesystem?, sandbox? }
getInstructions(opts?)Direct link to getinstructionsopts
Returns combined instructions from the filesystem and sandbox providers. Injected into the agent's system message to help it understand the execution context.
const instructions = workspace.getInstructions()
Pass requestContext to enable per-request customization when a provider's instructions option is a function:
const instructions = workspace.getInstructions({ requestContext })
Parameters:
opts.requestContext?:
Returns: string
To override the default output, pass an instructions option to LocalFilesystem or LocalSandbox.
getToolsConfig()Direct link to gettoolsconfig
Get the current tools configuration.
const config = workspace.getToolsConfig()
Returns: WorkspaceToolsConfig | undefined
setToolsConfig(config?)Direct link to settoolsconfigconfig
Replace the per-tool configuration at runtime. This performs a full replacement — it doesn't merge with the previous config. Pass undefined to reset to defaults. Changes take effect on the next agent interaction (the next createWorkspaceTools() call).
import { WORKSPACE_TOOLS } from '@mastra/core/workspace'
// Disable write tools for read-only mode
workspace.setToolsConfig({
[WORKSPACE_TOOLS.FILESYSTEM.WRITE_FILE]: { enabled: false },
[WORKSPACE_TOOLS.FILESYSTEM.EDIT_FILE]: { enabled: false },
})
// Reset to defaults
workspace.setToolsConfig(undefined)
Parameters:
config?:
Agent toolsDirect link to Agent tools
A workspace provides tools to agents based on what's configured.
Filesystem toolsDirect link to Filesystem tools
Added when a filesystem is configured:
| Tool | Description |
|---|---|
mastra_workspace_read_file | Read file contents. Supports optional line range for reading specific portions of large files. |
mastra_workspace_write_file | Create or overwrite a file with new content. Creates parent directories automatically. |
mastra_workspace_edit_file | Edit an existing file by finding and replacing text. Useful for targeted changes without rewriting the entire file. |
mastra_workspace_list_files | List directory contents as a tree structure. Supports recursive listing with depth limits, glob patterns, and .gitignore filtering (enabled by default). |
mastra_workspace_delete | Delete a file or directory. Supports recursive deletion for directories. |
mastra_workspace_file_stat | Get metadata about a file or directory including size, type, and modification time. |
mastra_workspace_mkdir | Create a directory. Creates parent directories automatically if they don't exist. |
mastra_workspace_grep | Search file contents using regex patterns. Supports glob filtering, context lines, and case-insensitive search. |
Write tools (write_file, edit_file, delete, mkdir) are excluded when the filesystem is in read-only mode.
Sandbox toolsDirect link to Sandbox tools
Added when a sandbox is configured:
| Tool | Description |
|---|---|
mastra_workspace_execute_command | Execute a shell command. Returns stdout, stderr, and exit code. When the sandbox has a process manager, accepts background: true to spawn a long-running process and return a PID. |
mastra_workspace_get_process_output | Get stdout, stderr, and status of a background process by PID. Accepts tail to limit output lines and wait: true to block until exit. Only available when the sandbox has a process manager. |
mastra_workspace_kill_process | Kill a background process by PID. Returns the last 50 lines of output. Only available when the sandbox has a process manager. |
The execute_command tool accepts a backgroundProcesses option for lifecycle callbacks on background processes:
backgroundProcesses?:
onStdout?:
onStderr?:
onExit?:
abortSignal?:
See Background process callbacks for usage examples.
Search toolsDirect link to Search tools
Added when BM25 or vector search is configured:
| Tool | Description |
|---|---|
mastra_workspace_search | Search indexed content using keyword (BM25), semantic (vector), or hybrid search. Returns ranked results with scores. |
mastra_workspace_index | Index content for search. Associates content with a path for later retrieval. |
The index tool is excluded when the filesystem is in read-only mode.