跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Vercel Deployer

Use @mastra/deployer-vercel to deploy your Mastra server as serverless functions on Vercel. The deployer bundles your code and generates a .vercel/output directory conforming to Vercel's Build Output API, ready to deploy with no additional configuration.

备注

This guide covers deploying the Mastra server. If you're using a server adapter or web framework, deploy the way you normally would for that framework.

Before you begin
Before you begin的直接链接

You'll need a Mastra application and a Vercel account.

注意

Vercel Functions use an ephemeral filesystem, so any storage you configure (including observability storage) must be hosted externally. If you're using LibSQLStore with a file URL, switch to a remotely hosted database.

Installation
Installation的直接链接

Add the @mastra/deployer-vercel package to your project:

npm install @mastra/deployer-vercel@latest

Import VercelDeployer and set it as the deployer in your Mastra configuration:

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { VercelDeployer } from '@mastra/deployer-vercel'

export const mastra = new Mastra({
deployer: new VercelDeployer(),
})

Deploy
Deploy的直接链接

  1. Push your project to a remote Git provider (e.g. GitHub) and connect your repository to Vercel.

    By default, Vercel runs npm run build, which triggers mastra build. If you don't have a build script, add "build": "mastra build" to your package.json.

    备注

    Remember to set your environment variables needed to run your application (e.g. your model provider API key).

  2. Once you're ready, click Deploy and wait for the first deployment to complete.

  3. Verify your deployment at https://<your-project>.vercel.app/api/agents, which should return a JSON list of your agents.

    Since the Mastra server prefixes every endpoint with /api, you have to add it to your URLs when making requests.

  4. You can now call your Mastra endpoints over HTTP.

    注意

    Set up authentication before exposing your endpoints publicly.

Studio
Studio的直接链接

You can deploy Studio alongside your API by enabling the studio option. Studio is deployed as static assets served from Vercel's Edge CDN, so it doesn't consume function invocations.

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { VercelDeployer } from '@mastra/deployer-vercel'

export const mastra = new Mastra({
deployer: new VercelDeployer({
studio: true,
}),
})

After deploying, Studio is available at the root URL (https://<your-project>.vercel.app/) and the API remains at /api/*. Studio automatically connects to the API on the same origin, so you don't need additional environment variables.

Studio serves its own pages from the CDN, and every other path goes to your server, so routes you add with registerApiRoute() stay reachable at their own paths. Avoid giving a custom route a path that Studio already uses (for example /agents or /workflows), since Studio claims those first.

注意

Once Studio is connected to your Mastra server, it has full access to your agents, workflows, and tools. Be sure to secure it properly in production (e.g. behind authentication, VPN, etc.) to prevent unauthorized access.

Optional overrides
Optional overrides的直接链接

The Vercel deployer supports configuration options that are written to the Vercel Output API function config. See the VercelDeployer reference for available options like maxDuration, memory, and regions.

Observability
Observability的直接链接

Serverless functions can terminate immediately after returning a response. Any pending async work - like sending telemetry - may get killed before completing. Awaiting flush() ensures all traces are sent before the function exits.

api/chat.ts
import type { VercelRequest, VercelResponse } from '@vercel/node'
import { mastra } from '../src/mastra'

export default async function handler(req: VercelRequest, res: VercelResponse) {
const { message } = req.body
const agent = mastra.getAgent('myAgent')
const result = await agent.generate([{ role: 'user', content: message }])

await mastra.observability.flush()

return res.json(result)
}
注意

The Vercel deployer doesn't include flush calls. If you need this, you'll need to wrap the handler yourself to add the logic before returning the response. Alternatively, deploy to a long-running server like a virtual machine where this isn't an issue.