Mastra Factory is an open source environment for building software with coding agents. Connect your repository to turn issues into plans, implementations, and reviewed pull requests.
This template runs the Factory Server entirely on infrastructure you operate, with no Mastra platform services. Keep it separate from the repository you want agents to change.
Read the documentation or watch the Mastra Factory overview.
src/mastra/index.ts wires each service explicitly:
| Service | Provider | Configured by |
|---|---|---|
| Storage | PostgreSQL with pgvector | DATABASE_URL |
| Auth | WorkOS | WORKOS_* |
| Source code | Your own GitHub App | GITHUB_APP_* |
| Sandboxes | Local — agent commands run on this machine | — |
| Secrets | AES encryption of stored credentials | FACTORY_CREDENTIAL_ENCRYPTION_KEY |
To change a service, replace its instance in src/mastra/index.ts. See Configure your Factory.
Warning
Local sandboxes run agent commands as the server's user, with access to its files and network. Use them only on a machine dedicated to this Factory. For shared or multi-tenant hosts, replace LocalFactorySandbox with an isolated provider such as E2BFactorySandbox.
-
Install dependencies with
npm install(npm create factoryalready did this). -
Copy
.env.exampleto.env(npm create factoryalready did this). -
Generate a credential encryption key and save it as
FACTORY_CREDENTIAL_ENCRYPTION_KEY. Preserve it across restarts and deployments, and keep a protected backup.openssl rand -base64 32
-
Start the local PostgreSQL service and set
DATABASE_URL=postgres://factory:factory@localhost:54330/factory:npm run db:up
-
Create a WorkOS application. Set
WORKOS_API_KEY,WORKOS_CLIENT_ID, and a 32+ characterWORKOS_COOKIE_PASSWORD. Addhttp://localhost:4111/auth/callbackas a redirect URI. -
Create a GitHub App and fill in the
GITHUB_APP_*variables. -
Install Git and your repository's build tools on this machine, then start the server:
npm run dev
Open the local URL printed by the server and sign in. The onboarding wizard asks you to select the repository agents should change and connect a model provider.
- Open Settings → Work Intake → GitHub issues. Enable Sync GitHub issues and select your repository.
- Create a small GitHub issue, such as adding contribution guidance to the repository's README.
- Find the issue in Work → Intake, select Investigate, and open its session to follow the agent's work.
Continue with the issue-to-pull-request walkthrough.
| Configuration | What you can change |
|---|---|
| Models | Provider access, personal or organization credentials, and default model. |
| GitHub | Repository access and personal issue intake. |
| Linear | Add a LinearIntegration to integrations. |
| Jira | Add a JiraIntegration to integrations. |
| Slack | Add a SlackIntegration to integrations. |
| Auth | Replace MastraAuthWorkos with another auth provider. |
| Storage | Replace PgFactoryStorage with another Factory storage adapter. |
| Sandboxes | Replace LocalFactorySandbox, for example with E2BFactorySandbox. |
Run Factory as a persistent Node.js service on a virtual machine or in a container. Set MASTRACODE_PUBLIC_URL to the public HTTPS origin, update your WorkOS and GitHub App callback URLs to match, and supply the .env values through the deployment environment. Then build and start the server:
npm run build
npm run startSee self-hosting for runtime requirements.
The OpenCRVS Factory runs at https://factory.opencrvs.dev in the non-prod Kubernetes cluster.
Dockerfilebuilds the server image. The image also has the tools that agents use in the local sandboxes: git, the GitHub CLI, Node.js fromopencrvs-core/.nvmrc, pnpm and native build tools..github/workflows/publish-image.ymlpushes the image toghcr.io/opencrvs/factoryon each push tomain.- The Helm values, the network policy and the setup steps are in
opencrvs-testland-infrastructure/environments/non-prod/factory.
When you change the Node.js version in opencrvs-core/.nvmrc, change the FROM line in Dockerfile too.
src/mastra/index.ts reads these settings:
| Variable | Default | What it does |
|---|---|---|
MASTRACODE_DISPATCH_MAX_IN_FLIGHT |
2 |
Automated agent runs at the same time. Minimum 1. |
MASTRACODE_DISPATCH_MAX_INTERACTIVE_IN_FLIGHT |
1 |
Extra slots for replies from people in GitHub. |
FACTORY_SESSION_MAX_IDLE_DAYS |
7 |
At start, delete the session folders that had no change for this many days. 0 turns it off. |
| Script | What it does |
|---|---|
npm run dev |
Start the local Factory Server with its UI and API. |
npm run build |
Build the server and Factory UI in .mastra/output. |
npm run start |
Run the production build. |
npm run db:up / npm run db:down |
Start or stop the local PostgreSQL service. |
- Runtime: Use a Node.js version that matches
engines.nodein this project'spackage.json. - Port changes: Set
PORTand updateMASTRACODE_PUBLIC_URLto match. Update your WorkOS and GitHub App callback URLs. - Missing issues: Check repository access and your Work Intake selections. See intake troubleshooting.
See Troubleshooting or Environment variables.
Apache-2.0