> ## Documentation Index
> Fetch the complete documentation index at: https://explore.airia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 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**:

1. **Metadata Retrieval Agent** - Scheduled agent that extracts database schema
2. **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

<img src="https://mintcdn.com/airia/FyggIi3LbVMjFjoF/integrations/Images/mysql_metadata_retrieval_agent.png?fit=max&auto=format&n=FyggIi3LbVMjFjoF&q=85&s=84ccfcfbd280d1cd637c211aa88dba86" alt="MySQL SQL Metadata agent" width="1479" height="758" data-path="integrations/Images/mysql_metadata_retrieval_agent.png" />

### Simplified Pattern (Databricks)

Databricks uses a **single-agent**:

1. **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

1. Create a new agent in **Agent Canvas**

2. Add the following components:
   * **LLM model**
   * **\[Database] Metadata Retrieval Tool** (e.g., "MySQL Metadata Retrieval Tool")
   * **Memory object**

3. Configure connections:
   * **Input** → **LLM**
   * **LLM** → **Output**
   * **Memory** → **Output**

4. Add the Metadata Retrieval Tool to the LLM

5. **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.

<img src="https://mintcdn.com/airia/FyggIi3LbVMjFjoF/integrations/Images/schedule_execution.png?fit=max&auto=format&n=FyggIi3LbVMjFjoF&q=85&s=20ab713eeefeb8675a965490b61da247" alt="Add MySQL Query tool" width="1778" height="1260" data-path="integrations/Images/schedule_execution.png" />

### Step 2: Create Query Agent

1. Create a new agent in **Agent Canvas**
2. 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**

3. Configure connections:
   * **Input** → **LLM**
   * **LLM** → **Output**
   * **Memory** → **LLM**

<img src="https://mintcdn.com/airia/FyggIi3LbVMjFjoF/integrations/Images/mysql_query_agent.png?fit=max&auto=format&n=FyggIi3LbVMjFjoF&q=85&s=ec01dfad3883001f2d1038f43303f0a1" alt="MySQL Query agent" width="1505" height="787" data-path="integrations/Images/mysql_query_agent.png" />

4. 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

1. Create a new agent in **Agent Canvas**

2. Add the following components:
   * **Databricks SQL Query Tool**
   * **LLM model**

3. Configure connections:
   * **Input** → **Databricks SQL Query Tool**
   * **Tool** → **LLM**
   * **LLM** → **Output**

4. 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:

1. Click on the **\[Database] SQL Query Tool** (e.g. Databricks SQL Query Tool)
2. Enter your SQL query with variables: `SELECT * FROM {{table_name}} WHERE date > '{{start_date}}'`
3. Save the configuration

Variables will appear as blue tags and can be set dynamically at runtime.

<img src="https://mintcdn.com/airia/ALXu7UtGeI560bwk/integrations/Images/databricks_query_parameterized.png?fit=max&auto=format&n=ALXu7UtGeI560bwk&q=85&s=808ccc6165f2d8c2e9987830a66a63c0" alt="Databricks SQL Query Action" width="1355" height="782" data-path="integrations/Images/databricks_query_parameterized.png" />

## Using Your Database Agents

### Query Examples

Ask your agents natural language questions about your data:

```
"Show me all customers from the last month"
"What are the top 10 products by sales?"
"Find all orders with status 'pending'"
"Summarize revenue by region for Q4"
```

### MongoDB-Specific Queries

```
"Find all documents in the users collection where age > 25"
"Aggregate sales data by product category"
"Show me the most recent orders from each customer"
```

### Direct SQL Queries

For SQL Tools, you can pass SQL queries directly:

```sql theme={null}
SELECT customer_id, SUM(order_value) 
FROM orders 
WHERE order_date >= '2024-01-01' 
GROUP BY customer_id
```

<img src="https://mintcdn.com/airia/ALXu7UtGeI560bwk/integrations/Images/databricks_query_results2.png?fit=max&auto=format&n=ALXu7UtGeI560bwk&q=85&s=707aba1d7fb501a7d6dfeeae8adb56e4" alt="Databricks query results" width="1081" height="856" data-path="integrations/Images/databricks_query_results2.png" />

## 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](mailto:support@airia.com) for IP whitelisting assistance.
