Skip to main content

Database Tool (Read/Write)

Empower your agents to execute SQL queries directly against your database to fetch data, update records, or manage schemas.

This tool allows for full read and write access. If you only need to fetch data, consider using the Database Read-Only tool instead.

💡 Core Concepts

To use this tool effectively, it helps to understand how SVAHNAR interacts with your database.

1. How does the Database Tool work?

The agent uses your connection string (db_uri) to connect to your database. It can execute any valid SQL query you authorize.

  • Read Operations: For SELECT queries, it returns the results as structured JSON data.
  • Write Operations: For INSERT, UPDATE, DELETE, etc., it automatically commits the transaction and returns the number of rows affected.

⚙️ Configuration Steps

Follow these steps to configure your agent for database access.

Prepare Your Connection String

You need a valid SQLAlchemy connection string (URI) for your database.

Format: dialect+driver://username:password@host:port/database

Examples:

  • PostgreSQL: postgresql+psycopg2://user:pass@localhost/mydatabase
  • MySQL: mysql+pymysql://user:pass@localhost/mydatabase
  • SQLite: sqlite:///example.db

Configure the Agent

In your agent configuration YAML, you will assign the DatabaseTool and provide the db_uri in the config block.

Security Note

This tool permits data modification (UPDATE/DELETE). Ensure the database user provided in the URI has appropriate permissions. If possible, restrict the user's privileges at the database level to only the tables necessary.


📚 Practical Recipes

Recipe 1: Full Access Database Agent

Use Case: An admin assistant agent that can look up user details and update their subscription status.

create_agent_network:
agent-1:
agent_name: db_admin_agent
LLM_config:
params:
model: gpt-4o
tools:
tool_assigned:
- name: DatabaseTool
config:
# Your database connection string
db_uri: "postgresql+psycopg2://admin:securepass@db-prod.svahnar.com:5432/users_db"
agent_function:
- "You are a database admin. You can search for users by email and update their subscription_level."
- "If a query fails, analyze the schema and retry for once"
incoming_edge:
- Start
outgoing_edge: []


🚑 Troubleshooting

  • "Missing required field: 'db_uri'"

    • Ensure the db_uri is correctly indented under the config: block in your YAML file.
  • Connection Refused / Timeout

    • Verify that the SVAHNAR agent has network access to your database host.
    • Check firewall rules and whitelist the agent's IP if necessary.
    • For dedicated IP addresses, please contact SVAHNAR support to get a list of IPs to whitelist in your firewall (Only for Teams and Enterprise plans).
  • Agent gets stuck in a loop

    • If the DB schema is too large for the LLM's context window, try to reduce the number of tables the agent has access to (if possible).