sql-query-mcp is a general-purpose MCP server designed to let AI work with multiple databases within clear boundaries. Through a controlled MCP interface, it helps AI clients discover database schemas, sample data, and analyze read-only queries.
Purpose This tool is intended for scenarios where an AI assistant needs to understand database structures, generate SQL, or execute controlled queries. It keeps connection handling, namespace rules, SQL validation, and audit logging on the server side, so it can expose useful database context to the AI without exposing raw connection strings and without flattening database-engine-specific concepts.
Main capabilities The current toolset focuses on database discovery, controlled query workflows, asynchronous read-only queries, batch export of query results, and a narrow local file import path. Specifically, it includes: listing namespaces, inspecting table definitions, viewing indexes, sampling records, running short read-only queries with run_select, running long read-only queries with start_query, get_query, and cancel_query, analyzing read-only queries with EXPLAIN, and exporting PostgreSQL or MySQL query results to local CSV/XLSX files. It can also import prepared local files. MySQL and Hive support explain_query; Hive uses EXPLAIN and EXPLAIN ANALYZE.
How boundaries are constrained Product boundaries are intentionally kept narrow for now. PostgreSQL, MySQL, and Hive are currently supported. Query tools remain read-only, PostgreSQL and MySQL query results can be exported to local files, and the only database write path is controlled local CSV/XLSX import into existing tables. The service makes these boundaries explicit in the following ways: connections explicitly declare the engine, and the server does not guess from connection_id; PostgreSQL uses schema, while MySQL and Hive use database, and the two are not merged into one vague namespace field; real DSNs are kept in environment variables, and configuration files store only environment variable names; query execution is validated by sqlglot before reaching the database; the server accepts only SELECT and WITH ... SELECT, rejects comments and multi-statement input, and records an audit log for every call.
export_query_file in MCP Write files on the server machine. It is synchronous, but reads database rows in batches and writes them to CSV/XLSX files. Large exports may still trigger MCP client tool timeouts. For XLSX output, UUID values are written as text, and datetime values with time zones are written without time zones. Hive export is not currently supported.
import_table_file does not accept raw SQL. It only inserts columns whose file column headers exactly match the existing table columns. Hive's import_table_file is only for small files and rejects files with more than 1000 data rows. Hive import writes row by row and may be slow and trigger MCP client tool timeouts. For bulk Hive loading, use Hive native LOAD DATA, external tables, or an existing data ingestion pipeline.
Usage sql-query-mcp supports two official PyPI-based installation modes, both aimed at real use rather than local testing only.
Choose how the MCP client starts the server:
- Installed command mode: install once and then use a simple local command.
- Managed launch mode: declare the package source directly in the MCP client configuration.
Use pipx install 'sql-query-mcp==X.Y.Z' or pipx run --spec 'sql-query-mcp==X.Y.Z' sql-query-mcp to pin the version. Use pipx upgrade sql-query-mcp to upgrade installed command mode.
Create the configuration file. Server configuration should be kept outside the repository so that the same file works in both launch modes. Then save the example JSON as ~/.config/sql-query-mcp/connections.json.
Register the server in the MCP client. For Codex, see docs/codex-setup.md; for OpenCode, see docs/opencode-setup.md. Installed command mode means the client runs sql-query-mcp directly; managed launch mode means the client starts the server via pipx run. In both modes, SQL_QUERY_MCP_CONFIG and the real database DSN should be placed in the MCP client's environment block, not exported in the shell.
The console entry point is sql-query-mcp, mapped to sql_query_mcp.app:main. The PyPI installation name is sql-query-mcp, and the Python package import path is sql_query_mcp. For pipx install and pipx run, explicitly set SQL_QUERY_MCP_CONFIG to the configuration file path. The default config/connections.json path is mainly used for source checkouts and local development.
In the example configuration, the DSN is set in the MCP client environment. For Hive, use a Hive DSN.
Use cases Suitable for tasks that require an AI assistant to understand the database structure before generating SQL, run bounded queries, start long-running read-only queries, export PostgreSQL or MySQL results to local files, or import prepared CSV/XLSX files into existing tables.
Limitations Currently only PostgreSQL, MySQL, and Hive are supported. The query tools are read-only, and the only write path is controlled local CSV/XLSX import. Hive export is not currently supported. Hive import is limited to small files (no more than 1000 data rows), and row-by-row writes may be slow. Large exports may trigger MCP client tool timeouts. In XLSX output, UUIDs are written as text, and datetimes with time zones are written without time zones.
Development and license When modifying or verifying the project locally, an editable install is possible and requires Python 3.10+. The main entry point is sql_query_mcp/app.py. Core modules include config.py, validator.py, introspection.py, executor.py, and adapters/. Before submitting changes, run PYTHONPATH=. python3 -m unittest discover -s tests. The project uses the MIT license.