examples/agent/README.md
This directory contains example agents demonstrating various Mastra features.
Install dependencies (from the repository root):
pnpm install
Set up environment variables:
cp .env.example .env
# Edit .env with your API keys
Run the example:
pnpm start
pnpm start - Run the agent examplespnpm mastra:dev - Start Mastra development serverpnpm mastra:studio - Open Mastra Studio UIpnpm mastra:build - Build for productionpnpm mastra:start - Start production serverThis example includes a demonstration of Mastra's Request Context Presets feature, which allows you to define named configurations that can be easily switched between in the Studio UI.
Request context presets are predefined JSON configurations that can be selected from a dropdown in the Mastra Studio Playground. This feature enables you to quickly switch between different environments, user roles, or scenarios without manually editing JSON each time.
mastra dev --request-context-presets ./request-context-presets.json
mastra studio --request-context-presets ./request-context-presets.json
Once you start Mastra with the --request-context-presets flag:
The dropdown includes a "Custom" option that is automatically selected when you manually edit the JSON.
This example includes the following presets in request-context-presets.json:
development: Development environment with debug logging
https://dev-api.example.comproduction: Production environment with minimal logging
https://api.example.comstaging: Staging environment for testing
https://staging-api.example.comadmin-user: Full administrative access
guest-user: Limited read-only access
The requestContextDemoAgent demonstrates how agents can dynamically adapt their behavior based on the request context:
Instructions: Vary by environment and role
Model Selection: Automatically chooses appropriate model
gpt-4o (higher quality)gpt-4o-mini (faster iteration)Tool Availability: Conditionally provides tools based on permissions
apiRequestTool: Available to all users, uses environment-specific endpointsadminActionTool: Only available with "manage" permissionanalyticsTool: Only available when features.analytics is enabledTry switching between presets to see how the agent behaves differently:
Select the development preset and ask: "Make an API request to fetch user data"
Select the admin-user preset and ask: "Show me analytics for the platform"
Select the guest-user preset and ask the same question
You can create custom presets for your own use cases:
{
"preset-name": {
"customField": "value",
"nested": {
"data": "example"
}
},
"another-preset": {
"userId": "user-123",
"tenant": "acme-corp"
}
}
Requirements: