PlatformFilesystem
Stores files in a Mastra Platform workspace bucket. Each Mastra Platform environment can have one bucket, and PlatformFilesystem gives agents read, write, list, delete, and move operations against it.
Related providers: S3Filesystem for direct S3 access, LocalFilesystem for local directories.
For interface details, see WorkspaceFilesystem interface.
InstallationDirect link to Installation
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/platform-workspace
pnpm add @mastra/platform-workspace
yarn add @mastra/platform-workspace
bun add @mastra/platform-workspace
Configure the platform credentials. The access token, project ID, and bucket name fall back to environment variables, so a Mastra Platform deployment can pass zero constructor options.
- .env file
- Constructor
MASTRA_PLATFORM_ACCESS_TOKEN=your-platform-access-token
MASTRA_PROJECT_ID=your-project-id
MASTRA_PLATFORM_BUCKET_NAME=your-bucket-name
new PlatformFilesystem({
accessToken: 'your-platform-access-token',
projectId: 'your-project-id',
bucketName: 'your-bucket-name',
})
On a Mastra Platform deployment, MASTRA_PLATFORM_ACCESS_TOKEN, MASTRA_PROJECT_ID, and MASTRA_PLATFORM_BUCKET_NAME are injected automatically, so the constructor can be called with no options. For local development, MASTRA_PLATFORM_ACCESS_TOKEN can contain an sk_ API token from your organization's settings page under API Tokens.
UsageDirect link to Usage
Add a PlatformFilesystem to a workspace and assign it to an agent:
import { Agent } from '@mastra/core/agent'
import { Workspace } from '@mastra/core/workspace'
import { PlatformFilesystem } from '@mastra/platform-workspace'
const workspace = new Workspace({
filesystem: new PlatformFilesystem({
// accessToken, projectId, bucketName all fall back to env vars
}),
})
const agent = new Agent({
id: 'file-agent',
name: 'File Agent',
instructions: 'You are a research assistant that reads and writes reports.',
model: 'anthropic/claude-sonnet-4-6',
workspace,
})
Reading and writing filesDirect link to Reading and writing files
Object keys are percent-encoded per segment, so filenames with ?, #, %, &, +, or spaces are preserved end-to-end:
const fs = new PlatformFilesystem()
await fs.writeFile('/analyses/repo.md', markdown)
const content = await fs.readFile('/analyses/repo.md')
const entries = await fs.readdir('/analyses')
await fs.moveFile('/analyses/repo.md', '/analyses/repo-final.md')
Read-only modeDirect link to Read-only mode
Pass readOnly: true to mount the bucket read-only. Any mutating call throws WorkspaceReadOnlyError:
const fs = new PlatformFilesystem({ readOnly: true })
await fs.readFile('/analyses/repo.md') // ok
await fs.writeFile('/analyses/repo.md', 'x') // throws WorkspaceReadOnlyError
Overwrite semanticsDirect link to Overwrite semantics
writeFile supports overwrite: false and throws FileExistsError when the destination already exists.
copyFile and moveFile always overwrite the destination. Passing overwrite: false to either method throws an error rather than silently overwriting.
Appending filesDirect link to Appending files
appendFile is a read-modify-write and isn't atomic. Concurrent appends to the same path can overwrite each other. For concurrent writers, use writeFile with distinct keys.
Constructor parametersDirect link to Constructor parameters
accessToken?:
projectId?:
bucketName?:
readOnly?:
displayName?:
description?:
icon?:
instructions?:
id?:
fetch?:
PropertiesDirect link to Properties
id:
name:
provider:
readOnly:
ErrorsDirect link to Errors
Filesystem-specific errors match the standard workspace error types:
FileNotFoundError: The path doesn't exist. Thrown byreadFile,stat, anddeleteFile(unlessforce: trueis set).FileExistsError:writeFilewas called withoverwrite: falseand the destination already exists.WorkspaceReadOnlyError: A mutating call was made on a read-only filesystem.
Other Platform API failures raise PlatformApiError. Structured { error: { message, type } } responses are parsed into .code (machine-readable kind) and .proxyMessage (human string):
import { FileNotFoundError } from '@mastra/core/workspace'
import { PlatformApiError } from '@mastra/platform-workspace'
try {
await fs.readFile('/missing.txt')
} catch (err) {
if (err instanceof FileNotFoundError) {
// handle missing file
} else if (err instanceof PlatformApiError) {
if (err.code === 'authentication_error') {
// refresh token
}
console.error(err.status, err.code, err.proxyMessage)
}
}
FileNotFoundError, FileExistsError, and WorkspaceReadOnlyError are re-exports of the standard workspace error types from @mastra/core/workspace. PlatformApiError is specific to @mastra/platform-workspace.
code and proxyMessage are undefined when the response body isn't JSON, for example an HTML 502 from a load balancer.