The CLI will guide you through setup. Choose the “Agent” template when prompted.
2
Set up environment variables
Create a .env file in your project root and add your OpenAI API key:
.env
OPENAI_API_KEY=sk-your-api-key-here
Never commit your .env file to version control. Add it to .gitignore.
3
Create a tool
Create a file at src/mastra/tools/stock-price.ts:
src/mastra/tools/stock-price.ts
import { createTool } from '@mastra/core/tools';import { z } from 'zod';export const stockPrices = createTool({ id: 'stock-prices', description: 'Get current stock price for a given symbol', inputSchema: z.object({ symbol: z.string().describe('Stock symbol (e.g., AAPL, GOOGL)'), }), outputSchema: z.object({ symbol: z.string(), currentPrice: z.number(), change: z.number(), }), execute: async ({ symbol }) => { // This is a mock implementation // In production, you'd call a real stock API const mockPrices: Record<string, any> = { AAPL: { currentPrice: 178.72, change: 2.34 }, GOOGL: { currentPrice: 142.56, change: -1.23 }, MSFT: { currentPrice: 378.91, change: 5.67 }, }; const data = mockPrices[symbol.toUpperCase()]; if (!data) { throw new Error(`Stock symbol ${symbol} not found`); } return { symbol: symbol.toUpperCase(), ...data, }; },});
Tools define the actions your agent can perform. Each tool has an input schema, output schema, and an execute function.
4
Create an agent
Create a file at src/mastra/agents/index.ts:
src/mastra/agents/index.ts
import { openai } from '@ai-sdk/openai';import { Agent } from '@mastra/core/agent';import { stockPrices } from '../tools/stock-price';export const stockAgent = new Agent({ id: 'stock-agent', name: 'Stock Agent', instructions: 'You are a helpful assistant that provides current stock prices. ' + 'When asked about a stock, use the stock price tool to fetch the current price.', model: openai('gpt-4o'), tools: { stockPrices, },});
5
Configure the Mastra instance
Create a file at src/mastra/index.ts:
src/mastra/index.ts
import { Mastra } from '@mastra/core/mastra';import { stockAgent } from './agents';export const mastra = new Mastra({ agents: { stockAgent },});
The Mastra instance is the central configuration hub where you register all your agents, workflows, and other components.
6
Run your agent
Create a file at src/index.ts:
src/index.ts
import { mastra } from './mastra';async function main() { const stockAgent = mastra.getAgent('stockAgent'); const response = await stockAgent.generate( 'What is the current stock price of Apple (AAPL)?' ); console.log('Agent response:', response.text); // Access tool results const toolCall: any = response.toolResults.find( (result: any) => result.toolName === 'stockPrices' ); if (toolCall) { console.log('Tool result:', toolCall.result); }}main();
Run your agent:
npx tsx src/index.ts
7
See the output
You should see output similar to:
Agent response: The current stock price of Apple (AAPL) is $178.72, with a change of +$2.34.Tool result: { symbol: 'AAPL', currentPrice: 178.72, change: 2.34 }
Congratulations! You’ve successfully created your first Mastra agent.