Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs-v1.agno.com/llms.txt

Use this file to discover all available pages before exploring further.

SQLTools enable an Agent to run SQL queries and interact with databases.

Prerequisites

The following example requires the sqlalchemy library and a database URL.
pip install -U sqlalchemy
You will also need to install the appropriate Python adapter for the specific database you intend to use.

PostgreSQL

For PostgreSQL, you can install the psycopg2-binary adapter:
pip install -U psycopg2-binary

MySQL

For MySQL, you can install the mysqlclient adapter:
pip install -U mysqlclient
The mysqlclient adapter may have additional system-level dependencies. Please consult the official installation guide for more details. You will also need a database. The following example uses a Postgres database running in a Docker container.
 docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v pgvolume:/var/lib/postgresql/data \
  -p 5532:5432 \
  --name pgvector \
  agno/pgvector:16

Example

The following agent will run a SQL query to list all tables in the database and describe the contents of one of the tables.
cookbook/tools/sql_tools.py
from agno.agent import Agent
from agno.tools.sql import SQLTools

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

agent = Agent(tools=[SQLTools(db_url=db_url)])
agent.print_response("List the tables in the database. Tell me about contents of one of the tables", markdown=True)

Toolkit Params

ParameterTypeDefaultDescription
db_urlstr-The URL for connecting to the database.
db_engineEngine-The database engine used for connections and operations.
userstr-The username for database authentication.
passwordstr-The password for database authentication.
hoststr-The hostname or IP address of the database server.
portint-The port number on which the database server is listening.
schemastr-The specific schema within the database to use.
dialectstr-The SQL dialect used by the database.
tablesDict[str, Any]-A dictionary mapping table names to their respective metadata or structure.
list_tablesboolTrueEnables the functionality to list all tables in the database.
describe_tableboolTrueEnables the functionality to describe the schema of a specific table.
run_sql_queryboolTrueEnables the functionality to execute SQL queries directly.

Toolkit Functions

FunctionDescription
list_tablesLists all tables in the database.
describe_tableDescribes the schema of a specific table.
run_sql_queryExecutes SQL queries directly.

Developer Resources