mountAgentControllerOnMastra()
The @mastra/code-sdk package is experimental and subject to breaking changes in minor versions.
The mountAgentControllerOnMastra() function builds the Mastra Code agent controller — the coding agent behind the mastracode CLI, 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 exampleDirect link to Usage 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' })
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 })
ParametersDirect link to Parameters
cwd?:
mastra?:
controllerId?:
modes?:
subagents?:
extraTools?:
disabledTools?:
storage?:
workspace?:
configDir?:
mcpServers?:
buildApiRoutes?:
mastra is provided.buildServerConfig?:
mastra is provided.ReturnsDirect link to Returns
mastra:
controller:
controller.createSession().