What You’ll Build
You’ll create a stock price agent that can fetch real-time stock information and respond to user queries.Prerequisites
- Node.js >= 22.13.0 installed
- An OpenAI API key (get one at platform.openai.com)
Create a new project
Run the Mastra CLI to scaffold a new project:The CLI will guide you through setup. Choose the “Agent” template when prompted.
Set up environment variables
Create a
.env file in your project root and add your OpenAI API key:.env
Configure the Mastra instance
Create a file at The Mastra instance is the central configuration hub where you register all your agents, workflows, and other components.
src/mastra/index.ts:src/mastra/index.ts
How It Works
- Tool Definition: You defined a
stockPricestool that fetches stock data - Agent Creation: You created an agent with instructions and gave it access to the tool
- Mastra Registration: You registered the agent with the Mastra instance
- Execution: When you called
agent.generate(), the LLM decided to use the tool, executed it, and formulated a response
Next Steps
Add Memory
Give your agent conversation history
Create Workflows
Build multi-step orchestrated processes
Add More Tools
Expand your agent’s capabilities
Deploy
Take your agent to production