Deployment Guides
Relevant source files
The following files were used as context for generating this wiki page:The Mastra framework features a modular, plugin-based deployment system designed for targeting various hosting platforms. The core of this system is an abstract Deployer class which defines a standard interface for bundling and deploying Mastra applications. Platform-specific logic is encapsulated in subclasses that extend this base Deployer.
This architecture allows for consistent deployment workflows while accommodating the unique configurations and requirements of platforms like Vercel, Netlify, Cloudflare, and Mastra's own Cloud environment. Each deployer is responsible for generating platform-specific configuration files, creating the correct server entry point, and managing dependencies.
Core Architecture
The deployment system is built around the base Deployer class, which provides the foundational structure and methods for all platform-specific deployers.
Sources: packages/deployer/src/index.ts
Deployer Base Class
The Deployer class is an abstract class that establishes the contract for all deployment adapters. It includes methods for bundling the application, linting, and deploying.
The following diagram illustrates the class hierarchy of the deployment system.
Key Methods
| Method | Description | Source File |
|---|---|---|
bundle() | Orchestrates the application bundling process. It changes to the project directory, finds the Mastra entry file, discovers tool paths, and calls the internal _bundle method. | deployers/cloud/src/index.ts:46-66 |
deploy() | An abstract method intended for platform-specific deployment logic. In some implementations, it's a no-op, deferring to platform CLIs. | deployers/cloudflare/src/index.ts:133-135 |
lint() | A placeholder for linting operations. | deployers/cloud/src/index.ts:38-38 |
writePackageJson() | Writes a package.json file to the output directory, merging project dependencies with deployer-specific ones. | deployers/cloud/src/index.ts:29-37 |
_bundle() | The core protected method that handles the Rollup-based bundling logic, using plugins for aliasing, JSON handling, and more. | packages/deployer/src/index.ts |
Bundling Process
The bundle method is a central part of the deployment workflow. It prepares the application code for deployment by creating a single, optimized file.
The following flowchart shows the general bundling process initiated by the bundle() method.
The bundling process relies on several dependencies, including Rollup and its plugins, to handle module resolution, aliasing, and code transformation.
Sources: packages/deployer/package.json:84-96
Platform-Specific Deployers
Cloudflare Deployer
The CloudflareDeployer is designed to deploy Mastra applications as Cloudflare Workers. It generates a wrangler.json configuration file and creates a worker entry point.
Sources: deployers/cloudflare/src/index.ts
Configuration
The constructor accepts Cloudflare-specific settings.
| Option | Type | Description |
|---|---|---|
projectName | string | The name of the Cloudflare worker. Defaults to 'mastra'. |
routes | CFRoute[] | An array of route patterns to trigger the worker. |
workerNamespace | string | The namespace for the worker. |
d1Databases | D1DatabaseBinding[] | Bindings for Cloudflare D1 databases. |
kvNamespaces | KVNamespaceBinding[] | Bindings for Cloudflare KV namespaces. |
Sources: deployers/cloudflare/src/index.ts:27-44
writeFiles() Method
This method generates the wrangler.json file, which is essential for deploying to Cloudflare Workers. It populates the configuration with the project name, entry point, compatibility settings, and any D1 or KV bindings.
Sources: deployers/cloudflare/src/index.ts:69-95
getEntry() Method
The Cloudflare entry point is structured as a default export with an async fetch handler, which is the standard for Cloudflare Workers. It dynamically imports the Mastra instance and tools, creates a Hono server, and returns the server's fetch handler.
// Source: deployers/cloudflare/src/index.ts:97-116
export default {
fetch: async (request, env, context) => {
const { mastra } = await import('#mastra');
const { tools } = await import('#tools');
const {createHonoServer, getToolExports} = await import('#server');
const _mastra = mastra();
if (_mastra.getStorage()) {
_mastra.__registerInternalWorkflow(scoreTracesWorkflow);
}
const app = await createHonoServer(_mastra, { tools: getToolExports(tools) });
return app.fetch(request, env, context);
}
}
Netlify Deployer
The NetlifyDeployer prepares a Mastra application for deployment on Netlify, typically as a serverless function. It generates a netlify.toml file.
Sources: deployers/netlify/src/index.ts
Configuration
| Option | Type | Description |
|---|---|---|
functions | NetlifyFunction[] | Configuration for Netlify functions. |
redirects | NetlifyRedirect[] | Configuration for URL redirects. |
Sources: deployers/netlify/src/index.ts:16-24
writeFiles() Method
This method creates the netlify.toml file, defining the build command and the functions directory.
Sources: deployers/netlify/src/index.ts:47-59
getEntry() Method
The Netlify entry point exports a handler function compatible with Netlify Functions, which uses the @hono/node-server adapter to process requests.
// Source: deployers/netlify/src/index.ts:26-45
import { handle } from '@hono/node-server/netlify';
import { scoreTracesWorkflow } from '@mastra/core/evals/scoreTraces';
const { mastra } = await import('#mastra');
const { tools } = await import('#tools');
const { createHonoServer, getToolExports } = await import('#server');
if (mastra.getStorage()) {
mastra.__registerInternalWorkflow(scoreTracesWorkflow);
}
const app = await createHonoServer(mastra, { tools: getToolExports(tools) });
export const handler = handle(app);
Mastra Cloud Deployer
The CloudDeployer is tailored for deploying to Mastra's managed cloud environment. It has specialized logic for dependency management and server initialization, including logging and storage setup.
Sources: deployers/cloud/src/index.ts, deployers/cloud/src/integration.test.ts
Dependency Management
The writePackageJson method is overridden to inject specific dependencies required for the Mastra Cloud environment. It reads versions from a versions.json file and adds packages like @mastra/loggers, @mastra/libsql, and @mastra/cloud.
Sources: deployers/cloud/src/index.ts:29-37, deployers/cloud/src/integration.test.ts:58-65
getEntry() Method
The entry point for the Cloud Deployer is the most complex, setting up a robust server environment:
- Readiness Logging: It logs structured JSON messages to indicate server startup progress (
Server starting,Server started,Runner Initialized). - Logger Initialization: It configures a
PinoLoggerwith anHttpTransportto send logs to a remote endpoint, specified byprocess.env.BUSINESS_API_RUNNER_LOGS_ENDPOINT. - Storage Setup: It initializes
LibSQLStoreandLibSQLVectorfor data and vector persistence using credentials from environment variables (MASTRA_STORAGE_URL,MASTRA_STORAGE_AUTH_TOKEN). - Server Creation: It calls
createNodeServerto start the Hono-based API server.
Sources: deployers/cloud/src/index.ts:70-159
Summary
Mastra's deployment system is a powerful example of the framework's modularity. By defining a common Deployer interface and providing platform-specific implementations, it simplifies the process of deploying complex AI applications to a variety of modern hosting environments. Each deployer encapsulates the unique requirements of its target platform, from configuration files (wrangler.json, netlify.toml) to server entry points, ensuring a smooth and consistent developer experience across different deployment targets.