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 usecreate-mastra
:
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.For more installation and project set up details, check Mastra’s documentation
here
Configuration
Set up your environment variables. Create a.env.development
file in your project root and add your API keys:
.env.development
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.
src/mastra/tools/kitchenTools.ts
Agent Definition
Now that the tools are defined, we can create the agent that uses them.src/mastra/agents/chefAgent.ts
Register agent
src/mastra/index.ts
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 thecookMeal
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.
cookMealTool
.
Integrating HumanLayer with Mastra
HumanLayer works seamlessly with Mastra by wrapping Vercel AI SDK format tools
with approval requirements.
- 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.