General-purpose SQL tool for Strands agents — supports PostgreSQL, MySQL, and SQLite.
Project description
strands-sql
A general-purpose SQL tool for Strands Agents — supports PostgreSQL, MySQL, and SQLite via SQLAlchemy.
Installation
# SQLite (no extra driver needed)
pip install strands-sql
# PostgreSQL
pip install "strands-sql[postgres]"
# MySQL
pip install "strands-sql[mysql]"
strands-sqlrequiressqlglotfor SQL parsing — it is installed automatically as a dependency.
Quick Start
from strands_sql import StrandsSQL
db = StrandsSQL("sqlite:///./local.db")
print(db.list_tables())
print(db.schema_summary())
print(db.describe_table("users"))
print(db.query("SELECT * FROM orders WHERE amount > 100"))
# Write data (disabled by default — pass read_only=False to enable)
db_write = StrandsSQL("sqlite:///./local.db", read_only=False)
db_write.execute("INSERT INTO users (name, age) VALUES ('Eve', 22)")
Use with a Strands Agent
from strands import Agent
from strands_sql import StrandsSQL
db = StrandsSQL("sqlite:///./local.db")
# Use db.as_tool() to preserve your connection and settings
agent = Agent(tools=[db.as_tool()])
agent("How many users are there?")
agent("Show me all orders above 100")
agent("What tables exist in this database?")
⚠️ Note
Always usedb.as_tool()rather than passingsql_databasedirectly.as_tool()binds your connection string,read_onlyflag, table access rules, and other settings to the tool — passingsql_databasedirectly means the agent must supply all of these itself on every call.
Configuration
Connection String
Pass it to StrandsSQL() directly, or set the DATABASE_URL environment variable:
export DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
db = StrandsSQL("postgresql://user:password@localhost:5432/mydb") # explicit
db = StrandsSQL() # reads DATABASE_URL automatically
Options
db = StrandsSQL(
"sqlite:///./local.db",
read_only=True,
max_rows=500,
timeout=30,
output_format="markdown",
allowed_tables=["users", "orders"],
blocked_tables=["secrets"],
)
| Option | Default | Description |
|---|---|---|
read_only |
True |
Blocks all write queries |
max_rows |
500 |
Maximum rows returned by query() |
timeout |
30 |
Query timeout in seconds (1–300) |
output_format |
"markdown" |
"markdown" or "json" |
allowed_tables |
None |
Allowlist — only these tables are accessible |
blocked_tables |
None |
Blocklist — these tables are never accessible |
Methods
list_tables()
List all accessible tables and views.
describe_table(table)
Show columns, types, primary keys, and foreign keys for a table.
schema_summary()
Compact schema of all tables — ideal for giving an LLM context about your database.
query(sql, *, output_format=None, max_rows=None)
Run a SELECT statement. Both output_format and max_rows can be overridden per-call.
Write queries are blocked when read_only=True.
db.query("SELECT * FROM users") # markdown (default)
db.query("SELECT * FROM users", output_format="json") # JSON array
db.query("SELECT * FROM logs", max_rows=100) # override row cap
execute(sql)
Run a write statement (INSERT / UPDATE / DELETE / DDL).
Raises PermissionError if read_only=True. If allowed_tables or blocked_tables
are configured, access rules are still enforced and return an error string rather than
raising.
db_write = StrandsSQL("sqlite:///./local.db", read_only=False)
db_write.execute("INSERT INTO users (name, age) VALUES ('Eve', 22)")
db_write.execute("UPDATE users SET age = 30 WHERE name = 'Alice'")
db_write.execute("DELETE FROM users WHERE name = 'Bob'")
as_tool()
Return a Strands-compatible tool bound to this instance's settings.
Output Formats
db.query("SELECT * FROM users", output_format="markdown") # default
db.query("SELECT * FROM users", output_format="json")
Low-level API
For advanced use cases, two additional functions are available:
get_tool()— returns a StrandsToolthat readsDATABASE_URLfrom the environment at call time. Useful when you don't want to construct aStrandsSQLinstance.run_sql_database(**kwargs)— calls the tool handler directly without theToolUsewrapper format. PreferStrandsSQLfor new code.
Development
git clone https://github.com/NithiN-1808/strands-sql
cd strands-sql
pip install -e ".[dev]"
pytest
pytest --cov=strands_sql --cov-report=term-missing
ruff check src/ tests/
mypy src/strands_sql/
License
Apache 2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strands_sql-0.1.9.tar.gz.
File metadata
- Download URL: strands_sql-0.1.9.tar.gz
- Upload date:
- Size: 24.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07ada041d0e1aa94a1035e58d46beeb33a1de24cb836c8a08f9237a5f280f510
|
|
| MD5 |
ad228bb46c50310208affe0d9513d7f6
|
|
| BLAKE2b-256 |
2d88d064c712f627687b88e66d1d9aa39767ebf5e1f460780f16c4be622a3749
|
Provenance
The following attestation bundles were made for strands_sql-0.1.9.tar.gz:
Publisher:
publish.yml on NithiN-1808/strands-sql
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strands_sql-0.1.9.tar.gz -
Subject digest:
07ada041d0e1aa94a1035e58d46beeb33a1de24cb836c8a08f9237a5f280f510 - Sigstore transparency entry: 1269625315
- Sigstore integration time:
-
Permalink:
NithiN-1808/strands-sql@70021e1e4adb5ee67bb1ebb1b2bf14abb35206a3 -
Branch / Tag:
refs/tags/v1.0.9 - Owner: https://github.com/NithiN-1808
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@70021e1e4adb5ee67bb1ebb1b2bf14abb35206a3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file strands_sql-0.1.9-py3-none-any.whl.
File metadata
- Download URL: strands_sql-0.1.9-py3-none-any.whl
- Upload date:
- Size: 18.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4333c4c6f887472e77bb80706f2c9b1978171dc454e805e0b99c74307af5fe42
|
|
| MD5 |
187d5c8fdc5867b41ef22c460a32fd81
|
|
| BLAKE2b-256 |
4f1ca5ffddfef42e978d002e01f8f16d2bc2d64bfbd9de45df517c15db84c495
|
Provenance
The following attestation bundles were made for strands_sql-0.1.9-py3-none-any.whl:
Publisher:
publish.yml on NithiN-1808/strands-sql
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strands_sql-0.1.9-py3-none-any.whl -
Subject digest:
4333c4c6f887472e77bb80706f2c9b1978171dc454e805e0b99c74307af5fe42 - Sigstore transparency entry: 1269625376
- Sigstore integration time:
-
Permalink:
NithiN-1808/strands-sql@70021e1e4adb5ee67bb1ebb1b2bf14abb35206a3 -
Branch / Tag:
refs/tags/v1.0.9 - Owner: https://github.com/NithiN-1808
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@70021e1e4adb5ee67bb1ebb1b2bf14abb35206a3 -
Trigger Event:
push
-
Statement type: