What You’ll Build
A data analysis agent that:- Adapts its capabilities based on user roles (admin vs regular user)
- Changes behavior based on environment (development vs production)
- Uses conditional tool access for security
- Implements role-based access control
Prerequisites
Complete Example
Step-by-Step Walkthrough
Define Role and Environment Parameters
Create a function that accepts user role and environment:This allows you to create different agent configurations dynamically.
Add Core Tools
Add tools that are available to all users:These tools are always included regardless of role.
Add Conditional Tools
Use
withToolIf() to add role-based tools:The
delete_records tool is ONLY added when isAdmin is true, providing true role-based access control.Add Environment-Based Constraints
Use Different constraints apply based on the environment.
withConstraintIf() for different environments:Add Role-Based Constraints
Apply constraints based on user permissions:This prevents non-authorized users from modifying data.
Expected Output
When you run this example, you’ll see different configurations for each role:Key Concepts
Conditional Tools - Use
withToolIf() to add tools based on conditions. This is perfect for role-based access control where different users need different capabilities.Conditional Constraints - Use
withConstraintIf() to apply different rules based on environment or user role. This ensures appropriate behavior in development vs production.Dynamic Configuration - Create a factory function that builds different agent configurations based on runtime conditions. This is essential for multi-tenant applications.
Configuration Inspection - Use
getSummary() and validate() to verify that your conditional logic produced the expected configuration.Real-World Use Cases
This pattern is perfect for:- Multi-tenant SaaS - Different features for different subscription tiers
- Enterprise Applications - Role-based access control (admin, manager, employee)
- Development Workflows - Different behavior in dev, staging, and production
- Compliance - Enforcing different rules based on data sensitivity
- A/B Testing - Creating variants of agents for experimentation
Advanced Patterns
Feature Flags
Permission Checking
Environment Variables
Next Steps
Validation Guide
Learn how to validate agent configurations before deployment
Multi-Tool Agent
See how to combine multiple tools effectively