mountAgentControllerOnMastra()
The mountAgentControllerOnMastra() function builds the Mastra Code agent controller (the coding agent behind Mastra Code, with its modes, tools, memory, and thread management) and registers it on a server-owned Mastra instance. Use it to serve the Mastra Code agent to your own UI (web app, editor, bot): each client creates or resumes its own isolated session through the returned AgentController.
To construct the Mastra instance yourself (for example in a deployable entry file), use prepareAgentControllerMount() from the same package, which returns the constructor args plus a finalize() callback.
Usage exampleUsage example的直接链接
import { mountAgentControllerOnMastra } from '@mastra/code-sdk'
const { mastra, controller } = await mountAgentControllerOnMastra({
cwd: process.cwd(),
})
// Each client drives its own session
const session = await controller.createSession({ resourceId: 'user-123' })
Commit attributionCommit attribution的直接链接
Use coAuthor to set the Co-Authored-By trailer in coding-agent commit guidance:
const { mastra, controller } = await mountAgentControllerOnMastra({
cwd: process.cwd(),
coAuthor: {
name: 'my-coding-app',
email: 'my-coding-app@example.com',
},
})
Both fields are optional. An omitted field uses the SDK default: mastra-platform[bot] for the name and 284800079+mastra-platform[bot]@users.noreply.github.com for the email.
The mastracode terminal app sets its name to mastracode, and Factory sets its name to mastra-platform[bot]. Both use the same default email, so GitHub resolves their trailers to the same bot account while the raw commit message retains the app name.
Pass an existing mastra to mount the controller onto a Mastra instance that already hosts other primitives:
import { Mastra } from '@mastra/core/mastra'
import { mountAgentControllerOnMastra } from '@mastra/code-sdk'
const mastra = new Mastra({/* ... */})
const { controller } = await mountAgentControllerOnMastra({ mastra })
ParametersParameters的直接链接
cwd?:
coAuthor?:
mastra?:
controllerId?:
client.getAgentController().modes?:
subagents?:
extraTools?:
postToolObserver?:
inputProcessors?:
disabledTools?:
disableBuiltinTools?:
storage?:
workspace?:
configDir?:
mcpServers?:
buildApiRoutes?:
mastra is provided.buildServerConfig?:
mastra is provided.ReturnsReturns的直接链接
mastra:
controller:
controller.createSession().authStorage:
Nested controllers in pluginsNested controllers in plugins的直接链接
A plugin that boots another controller in the same process must borrow the host's storage instead of opening the same database files with its own native SQLite library.
context.getStorage()contextgetstorage的直接链接
The optional accessor on MastraCodePluginContext returns the host's storage (MastraCompositeStore), storageBackend ('libsql' | 'pg'), and optional vector (MastraVector). Call it when the tool runs, then pass the returned object to bootLocalAgentController():
import { bootLocalAgentController } from '@mastra/code-sdk'
import type { MastraCodePluginContext } from '@mastra/code-sdk/plugin'
async function bootNestedController(context: MastraCodePluginContext) {
const sharedStorage = context.getStorage?.()
if (!sharedStorage) {
throw new Error('This plugin requires host-provided shared storage.')
}
return bootLocalAgentController({ cwd: context.cwd, ...sharedStorage })
}
The host owns these instances. Stop the nested controller's workers and destroy its controller when finished, but don't close the borrowed stores or invoke its storage-maintenance methods. If the accessor is unavailable on an older host, don't fall back to opening the same database files.