# sql-query-mcp

> sql-query-mcp is an open-source MCP server for databases. MCP server giving AI clients controlled read-only access to PostgreSQL, MySQL and Hive. It is available on CLI, Self-hosted and API.

- **Kind:** MCP server
- **Pricing:** Open source
- **Platforms:** CLI, Self-hosted, API
- **Repository:** https://github.com/andywang1688/sql-query-mcp
- **GitHub stars:** 7
- **License:** MIT
- **Categories:** [databases](https://ai-powered.com/categories/databases)
- **Listing updated:** 2026-12-01

## About

Rather than handing an assistant raw connection strings, this server sits between an AI client and your databases and exposes schema discovery, sampling and bounded read-only queries through a fixed set of MCP tools. Connection details live in environment variables, while a JSON config file records only which connections exist and which engine each uses.

Every query is checked by sqlglot before it reaches the database. Only SELECT and WITH ... SELECT statements are accepted, comments and multi-statement input are rejected, and each call is written to an audit log. Short queries run synchronously, while longer ones can be started asynchronously, polled for paginated results and cancelled.

The project suits developers and data teams who want an assistant to understand table structure before writing SQL, without giving it write access. PostgreSQL and MySQL results can also be exported to local CSV or XLSX files, and small prepared files can be imported into existing tables.

## Features

- **Schema discovery tools:** List schemas, databases, tables and views, and inspect columns, keys and indexes.
- **Validated read-only queries:** Queries pass SQL validation that accepts only SELECT-based statements and rejects comments and multiple statements.
- **Async long-running queries:** Start, poll and cancel longer read-only queries with paginated result retrieval.
- **Query plan inspection:** Run EXPLAIN on MySQL and Hive queries, with EXPLAIN ANALYZE for Hive.
- **Audit logging and secret handling:** Each call is audited, and DSNs stay in environment variables instead of config files.
- **Result export and file import:** Export PostgreSQL or MySQL results to CSV/XLSX and import prepared files into matching columns.

## Good for

- Letting an AI assistant inspect table definitions before generating SQL
- Sampling records from production tables for quick data checks
- Running bounded analytical queries without granting write permissions
- Exporting PostgreSQL or MySQL query results to local spreadsheets
- Reviewing query plans with EXPLAIN before running heavy reports on Hive

## Tools

From the tool list in its README, going by the names, 3 of 13 tools change data; 1 name doesn't say. From the server's own tool labels and tool names; not verified by us.

- `list_connections` (name suggests: reads): List configured connections
- `list_schemas` (name suggests: reads): List visible PostgreSQL schemas
- `list_databases` (name suggests: reads): List visible MySQL or Hive databases
- `list_tables` (name suggests: reads): List tables and views
- `describe_table` (name suggests: reads): Inspect columns, keys, and indexes
- `run_select` (name suggests: changes): Run short bounded read-only queries
- `start_query` (name suggests: changes): Start long-running read-only queries
- `get_query` (name suggests: reads): Fetch async query status and paginated results
- `cancel_query` (name suggests: changes): Cancel running async queries
- `explain_query` (name suggests: reads): Inspect query plans
- `get_table_sample` (name suggests: reads): Fetch small table samples
- `export_query_file` (name suggests: reads): Export query results to local CSV/XLSX files
- `import_table_file`: Import local CSV/XLSX files

## FAQ

### Which databases are supported today?

PostgreSQL, MySQL and Hive are available now. SQLite, SQL Server and ClickHouse are listed as candidates and are not yet supported.

### Can the AI write to my database?

Query tools are read-only and only accept SELECT and WITH ... SELECT. The only write path is importing a local CSV or XLSX file into an existing table, matching headers exactly.

### How are database credentials handled?

Config files store only environment variable names. The actual DSNs are set in the MCP client's environment block.

### How do I install sql-query-mcp?

It is published on PyPI as sql-query-mcp and can be installed with pipx, or launched with pipx run in the MCP client config.

### Is sql-query-mcp free?

Yes. sql-query-mcp is open source, so you can use and self-host it for free.

### What are the best alternatives to sql-query-mcp?

The closest alternatives to sql-query-mcp are MCP Postgres, postgres_mcp, mcp-multi-db, mysql_mcp_server and postgres-mcp.

## Where this comes from

- Description: written from the project's README.

## Alternatives

- [MCP Postgres](https://ai-powered.com/mcp/mcp-postgres): MCP Postgres is an open-source MCP server for databases.
- [postgres_mcp](https://ai-powered.com/mcp/postgres-mcp-5): postgres_mcp is an open-source MCP server for databases.
- [mcp-multi-db](https://ai-powered.com/mcp/mcp-multi-db): mcp-multi-db is an open-source MCP server for databases.
- [mysql_mcp_server](https://ai-powered.com/mcp/mysql-mcp-server-4): mysql_mcp_server is an open-source MCP server for databases.
- [postgres-mcp](https://ai-powered.com/mcp/postgres-mcp-4): postgres-mcp is an open-source MCP server for databases.
- [db-mcp-server](https://ai-powered.com/mcp/db-mcp-server-2): db-mcp-server is an open-source MCP server for databases.
- [thinair-data](https://ai-powered.com/mcp/thinair-data): thinair-data is an MCP server for databases.
- [PostgreSQL MCP Server](https://ai-powered.com/mcp/postgresql-mcp-server-2): PostgreSQL MCP Server is an open-source MCP server for databases.

---
Source: https://ai-powered.com/mcp/sql-query-mcp
