<- back to docs

Database tools

Let the agent search your schema, run queries, make changes you approve, and open or add connections.

By default, a coding agent in DearSQL can do more than read the context you pin — it can query your database directly. That’s what turns “write me a query” into “look at the data and tell me why this is slow”.

DearSQL does this by running a small MCP server on your machine and handing the agent its address when the session starts.

The tools

Tool What it does
search_schema Finds tables, views and columns by name. Forgiving: user id, userId and a typo like usres all find users.user_id
describe_table Shows one table’s columns, types, keys, indexes and which tables point at it
run_query Runs a read-only query and returns the rows
explain_query Shows a statement’s query plan without running it, with warnings for full scans, repeated scans and big sorts (PostgreSQL, MySQL 8, SQLite)
review_indexes Checks the schema for foreign keys with no index and indexes another index already covers, with a statement to consider
explain_finding Explains a warning code from explain_query or review_indexes: what it means, how to confirm it, the usual fix, and when to leave it
profile_table Per column of a table: how many values are null, how many distinct, the smallest and largest, and the most common values, over the first 100,000 rows
diff_schema Compares two databases’ tables, columns, indexes and foreign keys and lists the statements that would make the first match the second, without running them
list_tables Lists every table and view with its size
list_connections Lists your saved connections, which are open, and the databases on them
connect Opens one of your saved connections by name
export_query Saves the rows of a read-only query as an Excel file (.xlsx) after you approve the file
execute_sql Runs a change (INSERT, UPDATE, DELETE, schema changes) after you approve it
add_connection Saves a new connection from a URL or file after you approve it, then opens it

When a query is slow, the agent can call explain_query instead of guessing. It gets the plan as a short indented tree, with the filter and join conditions, and a list of warnings from fixed rules: a full scan that reads 100,000 rows to keep 2,000, a table read in full once per row of a join, a sort that spilled to disk, or (with analyze, read-only statements only) a row estimate ten times off. The warnings are hints to check, not verdicts; an index the agent wants to add still goes through execute_sql and your approval.

review_indexes looks at the schema instead of a query: a foreign key with no index makes joins on it and deletes of the referenced rows scan the whole table, and an index whose columns lead another’s (or duplicate them) costs every write for nothing. Each finding and plan warning carries a code such as [unindexed-fk], and explain_finding returns the same fixed explanation every time, so the agent quotes it rather than improvising. These checks read only the schema and the plan, never your data.

profile_table is the exception that reads data on purpose: before writing a filter, the agent can see that status has four values and which is most common, or that half of deleted_at is empty. It reads the first 100,000 rows (not a random sample) in one query, so it stays cheap on big tables. diff_schema compares two databases, say your dev and production copies, and returns a script: new tables first, then column and index changes, drops last. Nothing runs; the agent shows you the script and applies only what you pick, through execute_sql and your approval.

Two ready-made workflows come with the tools as MCP prompts, which clients like Claude Code offer as slash commands: diagnose-slow-query (explain, confirm each warning, review the tables’ indexes, then report the fix without running it) and review-schema (a prioritized index review).

An agent can also browse your S3 connections: list_objects lists buckets and folders, and read_object returns the start of a file as text. Copying, moving, deleting and uploading files are changes, so they ask for your approval like execute_sql.

In a terminal, dearsql --mcp also serves list_objects and read_object for your S3 connections.

The agent works on the connection the chat was opened from (or a table you pinned with @) unless it names another connection or database. It’s told to look names up with search_schema and describe_table before writing SQL, so it doesn’t guess column names, and when a query still trips over a misspelled table it gets back “did you mean …” suggestions instead of a bare error.

You’ll see each call appear in the transcript as it happens, and can expand it to read what came back.

What’s allowed

run_query only accepts read-only statements — SELECT, SHOW, EXPLAIN, DESCRIBE, WITH, PRAGMA and friends. Anything that writes is rejected before it reaches your database (changes go through execute_sql, below), including a write hidden after a semicolon:

SELECT 1; DROP TABLE users   -- rejected

On Redis it accepts read commands (GET, HGETALL, SCAN, INFO …), and on MongoDB read commands such as find, aggregate and countDocuments — but not a pipeline that writes with $out or $merge.

Changes need your approval

When the agent wants to change something, it calls execute_sql and the chat shows the statement and the connection with three buttons:

  • Run runs it once and the agent gets the affected row count.
  • Allow for this chat runs it and lets later changes on that connection run without asking, until you close the chat. It isn’t offered for connections whose tag starts with prod; those always ask.
  • Reject tells the agent no, and nothing reaches the database.

A connection marked Read-only connection in its settings never accepts a change from the agent. An unanswered request is rejected after 5 minutes.

Saving a spreadsheet with export_query asks the same way, showing the file and the query. Approving a file never lets the agent change a database, and the agent has to give a full path (~/Desktop/report.xlsx); it can’t replace an existing file unless it asks to.

In a terminal, dearsql --mcp only offers execute_sql with --allow-writes, and your agent asks before each call instead (see Command line).

Results come back 50 rows at a time. If there are more, the agent can ask for the next page without re-running the query; a page also stops at about 30 KB of text (the result says so), and a single query stops at 1,000 rows, so a careless SELECT * on a huge table can’t flood the conversation. Long values (a big JSON or text column) are cut at 200 characters, and the result says so; when you need the whole value, the agent can ask for it with maxCellChars set to 0 (dearsql query --maxCellChars 0 in a terminal).

connect opens a saved connection, and naming a closed connection in any other tool opens it the same way. To add one you haven’t saved — say, a database the agent just started in Docker — it calls add_connection with a URL or a file path, and the chat asks you before saving it (the password isn’t shown). Saved connections count toward the free tier’s limit like any other. With dearsql --mcp in a terminal there’s no app to ask, so connect takes a connection URL or a SQLite, DuckDB or CSV file path instead; that connection lasts until the server stops and is never saved.

Turning it off

The gear icon opens AI settings, which has Let the agent query my database. It’s on by default.

The AI Settings dialog, with the API key, provider, and the agent access toggle

Turning it off stops the running agent and closes the port, so nothing is listening. The assistant still works — it just only knows what you tell it and what you pin with @.

Worth knowing when deciding:

  • Query results are sent to whichever model provider your agent uses. Read-only still means the contents of those rows leave your machine.
  • The server listens only on 127.0.0.1 and every request must carry a bearer token that’s generated fresh for each session and shared only with the agent DearSQL launched.
  • Letting the agent open connections means it can trigger the side effects of connecting, such as an SSH tunnel, or a keychain prompt if you keep the encryption key there.

If you’re working against production, it’s reasonable to leave this off and pin schema by hand with @.