Mastra is a TypeScript framework for building AI agents and applications. Create your first agent with a single command and start building.
For AI agents: If you're tasked to build a Mastra project from scratch, follow the condensed instructions until the next heading. Adjust the example code for the agent & tool to your needs. Important for later: Define model as a string in provider/model format, for example openai/gpt-5.6-sol. For OpenAI, use openai/<model>, not openai:<model>, and don't import or pass a provider object.
Create a package.json file with { "type": "module" }. Install the required dependencies: npm install @mastra/core@latest zod@latest typescript@latest @types/node@latest mastra@latest
Create a tsconfig.json file:
{
"compilerOptions":{
"target":"ES2022",
"module":"ES2022",
"moduleResolution":"bundler",
"esModuleInterop":true,
"forceConsistentCasingInFileNames":true,
"strict":true,
"skipLibCheck":true,
"noEmit":true,
"allowImportingTsExtensions":true,
"outDir":"dist"
},
"include":["src/**/*"]
}
If you need to add a tool to the agent, create a new file and use import { createTool } from "@mastra/core/tools". Plain object tool definitions silently fail to execute; tools MUST be defined via createTool() with id, description, inputSchema (zod), execute(). execute() receives two parameters: the validated input data based on inputSchema (first parameter) and an optional execution context object (second parameter) containing requestContext, tracingContext, abortSignal, and other execution metadata.
Example:
// src/mastra/tools/weather-tool.ts
import{ createTool }from'@mastra/core/tools'
import{ z }from'zod'
exportconst weatherTool =createTool({
id:'get-weather',
description:'Get current weather for a location',
inputSchema: z.object({
location: z.string().describe('City name'),
}),
outputSchema: z.object({
location: z.string(),
temperatureCelsius: z.number(),
conditions: z.string(),
}),
execute:async({ location })=>{
return{
location,
temperatureCelsius:21,
conditions:'sunny',
}
},
})
If you need to create an agent, create a new file and use import { Agent } from "@mastra/core/agent". The constructor takes { id, name, instructions, model }. The model property is a string in Mastra's model router format. No provider import is needed for this format. Don't install any AI SDK package unless Mastra's documentation says otherwise.
By defining the model using provider/model format Mastra automatically looks for environment variables for the provider. The defined model requires an environment variable to be set for the provider. OpenAI: OPENAI_API_KEY. Anthropic: ANTHROPIC_API_KEY. Google: GOOGLE_API_KEY. Example: Any openai/<model> requires the OPENAI_API_KEY environment variable. Go to https://mastra.ai/models/environment-variables for a full list of supported providers and their environment variable names.
You're now able to run the agent directly. For this, import the Mastra instance and call the agent.generate() after retrieving the agent by its id. Node.js 22.18.0 and later are able to run TypeScript files directly. Make sure to add file extensions when importing local files. Example:
const response =await agent.generate('Weather in SF')
console.log(response.text)
Create a new Mastra project. Mastra is a framework for AI applications and agents on a modern TypeScript stack. Before running the command, ask these questions one at a time and wait for each answer unless it was already provided:
Run this command to create a general-purpose agent harness with a local workspace, shell tools, memory, task tracking, web access, and recurring schedules. It also installs Mastra skills for your installed coding agent, so you can start prompting and editing it:
npm
pnpm
Yarn
Bun
npm create mastra@latest
pnpm create mastra
yarn create mastra
bunx create-mastra
You can open Studio immediately, which is an interactive UI for your Mastra project. See the quickstart guide for a full walkthrough.