Using Database tools in an Agent
Configure agents to query and interact with your database using SQL queries and metadata retrieval.
Prerequisites
- Database tools already configured for your database type
- Project with agent building permissions
- Database server accessible via public IP
π‘ Setup Required: Ensure youβve configured your database tools first by following the setup guide for your specific database type.
Supported Database Types
- MySQL - Standard metadata + query pattern
- PostgreSQL - Standard metadata + query pattern
- MS SQL Server - Standard metadata + query pattern
- Snowflake - Standard metadata + query pattern
- MongoDB - Standard pattern with Find + Aggregate tools
- Databricks - Simplified query-only pattern
Agent Patterns
Agent Pattern (MySQL, PostgreSQL, MS SQL, Snowflake, MongoDB)
Most database tools use a two-agent architecture:
- Metadata Retrieval Agent - Scheduled agent that extracts database schema
- Query Agent - Interactive agent that answers user questions
Required Components:
- Database-specific SQL Query Tool
- Database-specific Metadata Retrieval Tool
- Memory object (for metadata storage)
- LLM model
Simplified Pattern (Databricks)
Databricks uses a single-agent:
- Query Agent - Direct SQL query execution with LLM processing
Required Components:
- Databricks SQL Query Tool
- LLM model
Setting Up Standard Database Agents
Step 1: Create Metadata Retrieval Agent
-
Create a new agent in Agent Canvas
-
Add the following components:
- LLM model
- [Database] Metadata Retrieval Tool (e.g., βMySQL Metadata Retrieval Toolβ)
- Memory object
-
Configure connections:
- Input β LLM
- LLM β Output
- Memory β Output
-
Add the Metadata Retrieval Tool to the LLM
-
Schedule the agent:
- Click the gear icon on the Input block
- Toggle Schedule Execution ON
- Set repeat interval based on how often your database schema changes
π‘ Scheduling Tip: Run metadata retrieval daily for frequently changing databases, or weekly for stable schemas.
Step 2: Create Query Agent
- Create a new agent in Agent Canvas
- Add the following components:
- LLM model
- [Database] SQL Query Tool (e.g., βMySQL SQL Query Toolβ)
- Memory object (same as metadata agent)
For MongoDB: Add both MongoDB Find Tool and MongoDB Aggregate Tool
- Configure connections:
- Input β LLM
- LLM β Output
- Memory β LLM
- Add the SQL Query Tool(s) to the LLM
β οΈ Important: Run the Metadata Retrieval Agent at least once before using the Query Agent to populate database schema information.
Setting Up Databricks Agents
Single Query Agent Setup
-
Create a new agent in Agent Canvas
-
Add the following components:
- Databricks SQL Query Tool
- LLM model
-
Configure connections:
- Input β Databricks SQL Query Tool
- Tool β LLM
- LLM β Output
-
Configure the SQL Query Tool:
- Click on the Databricks SQL Query Tool
- Set it to receive value from Input Step Result
Using Parameterized Queries
For dynamic queries, use Agent Variables in your SQL statements:
- Click on the [Database] SQL Query Tool (e.g. Databricks SQL Query Tool)
- Enter your SQL query with variables:
SELECT * FROM {{table_name}} WHERE date > '{{start_date}}'
- Save the configuration
Variables will appear as blue tags and can be set dynamically at runtime.
Using Your Database Agents
Query Examples
Ask your agents natural language questions about your data:
MongoDB-Specific Queries
Direct SQL Queries
For SQL Tools, you can pass SQL queries directly:
Best Practices
π‘ Tip: Add custom prompts to your LLM with:
- Database schema context
- Common query patterns for your domain
- Output formatting preferences
- Business logic explanations
π‘ Memory Management: Use the same Memory object across both agents to ensure consistent metadata access.
β οΈ Connection Requirements: All database servers must be accessible via public IP. Contact support@airia.com for IP whitelisting assistance.