MastraFactory class
MastraFactory assembles the storage domains, integrations, rules, routes, agent controller, tools, and background workers used by a Mastra Factory server. Its prepare() method returns configuration for a Mastra instance, and finalize() starts the runtime after that instance exists.
Install the package:
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/factorypnpm add @mastra/factoryyarn add @mastra/factorybun add @mastra/factoryQuickstart
Create the Factory, pass the result of prepare() to a literal new Mastra() call, then call finalize():
import { Mastra } from '@mastra/core/mastra'
import { MastraFactory } from '@mastra/factory'
import { LibSQLFactoryStorage } from '@mastra/libsql'
const storage = new LibSQLFactoryStorage({
id: 'factory-storage',
url: 'file:./factory.db',
})
export const factory = new MastraFactory({
storage,
auth: null,
})
const prepared = await factory.prepare()
export const mastra = new Mastra({
...prepared,
bundler: {
transpilePackages: ['@mastra/factory'],
},
})
await factory.finalize()
Constructor parameters
MastraFactorySandboxConfig
Methods
prepare()
Prepares the Factory runtime and returns the configuration used to construct Mastra.
class MastraFactory {
prepare(): Promise<MastraArgs>
}
prepare() performs the one-time setup required before constructing the host Mastra instance. It:
- Registers the Factory storage domains and initializes the storage backend.
- Initializes the authentication provider when it supports initialization.
- Registers the tenant credential resolver when authentication is enabled.
- Adds Platform GitHub and Linear integrations for missing provider IDs when
MASTRA_PLATFORM_SECRET_KEYis set. - Initializes configured and defaulted integrations with scoped storage, project storage, and route authentication.
- Creates the sandbox fleet when
sandboxis configured. - Validates integration identifiers, rules, and OAuth state-signing requirements.
- Builds the Mastra Code agent controller, integration tools, API routes, middleware, and workers.
Call prepare() once. A second or concurrent call throws an error.
Returns
Promise<MastraArgs> containing the constructor properties to spread directly into new Mastra(). The returned object includes the prepared agent controller, storage, server configuration, and integration workers when present.
finalize()
Starts the prepared controller, workers, thread reconciliation, and Factory decision dispatcher.
class MastraFactory {
finalize(): Promise<void>
}
Call finalize() after constructing the Mastra instance. Calling it before prepare() throws an error.
shutdown()
Stops background dispatch owned by the Factory runtime.
class MastraFactory {
shutdown(): Promise<void>
}
Call shutdown() during host process shutdown. It stops the Factory decision dispatcher. The host remains responsible for shutting down the Mastra instance and its workers.
Lifecycle
The required lifecycle order is:
- Create
MastraFactorywith explicit storage and optional capabilities. - Call
factory.prepare()once. - Construct and export
Mastrafrom the returned properties. - Call
factory.finalize()after theMastrainstance exists. - Call
factory.shutdown()before the host process exits.
Integrations
Pass integrations through the integrations constructor property. Each integration implements FactoryIntegration and can contribute HTTP routes, source capabilities, agent tools, session tools, tool observers, diagnostics, audit export, and background workers.
Included integrations
Import the included integrations from the @mastra/factory/integrations subpaths.
| Integration | Configuration |
|---|---|
/github/integration | GithubIntegration accepts GitHub App credentials and an optional webhook secret. |
/linear/integration | LinearIntegration accepts a Linear OAuth client ID and client secret. |
/workos/integration | WorkOSAuditIntegration accepts a WorkOS client and return URL. |
/platform/github/integration | PlatformGithubIntegration reads its Mastra Platform API configuration from the environment. |
/platform/linear/integration | PlatformLinearIntegration reads its Mastra Platform API configuration from the environment. |
When MASTRA_PLATFORM_SECRET_KEY is set, prepare() automatically creates the Platform GitHub and Linear integrations unless the integrations array already contains an integration with the corresponding github or linear ID.
The package exposes integration contracts and included integrations through these subpaths. Other wildcard-importable files under @mastra/factory/*, such as route assembly and storage-domain implementations, are internal runtime wiring rather than supported extension points.
Rules
Import the default rules and rule types from @mastra/factory/rules:
import {
DEFAULT_FACTORY_RULE_VERSION,
defaultFactoryRules,
type FactoryRules,
} from '@mastra/factory/rules'
const rules: FactoryRules = defaultFactoryRules({
version: DEFAULT_FACTORY_RULE_VERSION,
overrides: {},
})
Pass the returned rules object to the MastraFactory constructor. FactoryRules contains handlers for the work and review boards, tool results, GitHub events, and Linear events. defaultFactoryRules() merges the supplied overrides with the built-in rules and validates the result.