Mastra
Use Humanlayer with Mastra, the TypeScript AI Framework
Overview
Mastra is a TypeScript framework for building AI applications. HumanLayer adds human oversight to your AI features.
Getting Started with Mastra
Installation
The easiest way to get started with Mastra is to use create-mastra
:
When prompted, accept the default options for the setup.
During the create-mastra
setup, you’ll be prompted to choose a model
provider. While this guide uses OpenAI models in the examples, Mastra supports
all models available through the Vercel AI SDK (e.g., OpenAI, Anthropic,
Google, etc.). Accepting the defaults will typically select OpenAI.
This command will guide you through setting up a new Mastra project, asking for a project name and other configurations.
After the setup is complete, navigate into your new project directory:
For more installation and project set up details, check Mastra’s documentation here
Next, install the necessary packages for memory management and HumanLayer integration:
Configuration
Set up your environment variables. Create a .env.development
file in your project root and add your API keys:
You can get your HumanLayer API key from the HumanLayer dashboard.
Chef Agent Example
This example demonstrates building a “Chef Agent” that can check available ingredients and then cook a meal. Checking ingredients is a simple read operation, while cooking the meal is an action that requires human approval via HumanLayer.
Tools Definition
Mastra offers flexibility by supporting both its native tool format and the Vercel AI SDK tool format. For seamless integration with HumanLayer, this guide utilizes the Vercel AI SDK format. Learn more about adding tools in Mastra here.
Agent Definition
Now that the tools are defined, we can create the agent that uses them.
Register agent
Running the Chef Agent
To run your chef agent:
-
Start your Mastra project:
-
Access the Mastra playground at
http://localhost:4111
-
Interact with your chef agent. Ask it to check ingredients (this won’t require approval). Then, ask it to cook a specific meal (e.g., “Cook chicken stir-fry”).
-
When the agent attempts to use the
cookMeal
tool, HumanLayer will require approval before proceeding. ThecheckIngredients
tool will execute without interruption.
The agent will pause execution only when attempting to use the cookMeal
tool, while waiting for human approval.
Approving Actions via HumanLayer Dashboard
When your agent uses the cookMeal
tool (wrapped with hl.requireApproval()
), you’ll need to approve the action:
-
Access the HumanLayer dashboard at:
-
View the pending approval request for
cookMealTool
. -
Review the details of the request, including:
- The tool being used (
cookMealTool
) - Parameters passed (e.g.,
mealName: "Chicken Stir-fry"
) - Context of the request
- The tool being used (
-
Choose to approve or deny the request.
If you’ve configured other notification channels (e.g., Slack), you’ll also receive approval requests through those channels.
Once approved, the agent will continue execution, receiving the confirmation message from the cookMealTool
.
Integrating HumanLayer with Mastra
HumanLayer works seamlessly with Mastra by wrapping Vercel AI SDK format tools with approval requirements.
The integration follows these simple steps:
- Create your tools using the Vercel AI SDK
tool()
function. - Initialize HumanLayer.
- Wrap the specific tools that require oversight (like
cookMealTool
) withhl.requireApproval()
. - Add both standard and approval-wrapped tools to your Mastra agents.
- Human involvement is automatically triggered only when the wrapped tools are used.