sqldb-mcp-server
A read-only MCP server that exposes SQL database access to LLMs, supporting multiple database types, compact columnar results, pagination, and file export.
README
sqldb-mcp-server
A read-only Model Context Protocol (MCP) server that exposes SQL database access to LLMs.
Features
- Multi-database – supports MSSQL, PostgreSQL, and MySQL
- Read-only – only
SELECTstatements are allowed (enforced via AST-level SQL parsing with the correct dialect per DB type) - LLM-optimised – results use a compact columnar format (column list + value rows) to reduce token usage
- Pagination –
skip/takeparameters with automatic cap at 100 rows - Total-count aware – every query result includes
meta.totalCountso the LLM knows how many rows exist - Caching – query / schema results are cached with a configurable TTL
- File export – stream query results to CSV or JSON files without a row-count limit
- Five MCP tools:
query,listTables,describeTable,explainQuery,exportQuery
Installation
From npm (recommended)
# Install globally
npm install -g @akrym1582/sqldb-mcp-server
# Or run directly with npx (no install needed)
npx @akrym1582/sqldb-mcp-server
From source
git clone https://github.com/akrym1582/sqldb-mcp-server.git
cd sqldb-mcp-server
npm install
npm run build
Quick Start
# 1. Install globally
npm install -g @akrym1582/sqldb-mcp-server
# 2. Configure environment variables (see below)
export DB_TYPE=postgresql
export DB_HOST=localhost
export DB_USER=myuser
export DB_PASSWORD=mypassword
export DB_NAME=mydb
# 3. Run
sqldb-mcp-server
Or use in your MCP client configuration (e.g. Claude Desktop claude_desktop_config.json):
{
"mcpServers": {
"sqldb": {
"command": "npx",
"args": ["-y", "@akrym1582/sqldb-mcp-server"],
"env": {
"DB_TYPE": "postgresql",
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_USER": "myuser",
"DB_PASSWORD": "mypassword",
"DB_NAME": "mydb"
}
}
}
}
Environment Variables
| Variable | Default | Description |
|---|---|---|
DB_TYPE |
mssql |
Database type: mssql, postgresql, or mysql |
DB_HOST |
– | Database server hostname |
DB_PORT |
1433 / 5432 / 3306 |
Database server port (default depends on DB_TYPE) |
DB_USER |
– | Database username |
DB_PASSWORD |
– | Database password |
DB_NAME |
– | Database name |
DB_ENCRYPT |
true for MSSQL/PostgreSQL, false for MySQL |
Enables encrypted DB connections. MSSQL trusts the server certificate. PostgreSQL tries SSL first and falls back to plain if SSL is unavailable. MySQL uses TLS with certificate verification disabled when enabled. |
DB_QUERY_TIMEOUT |
30000 |
Query timeout in milliseconds (used by query / explainQuery) |
EXPORT_QUERY_TIMEOUT |
300000 |
Export query timeout in milliseconds (used by exportQuery; default 5 min) |
CACHE_TTL |
60 |
Cache TTL in seconds |
Default ports by DB type
DB_TYPE |
Default DB_PORT |
|---|---|
mssql |
1433 |
postgresql |
5432 |
mysql |
3306 |
MCP Tools
query
Execute a SELECT SQL statement.
{
"sql": "SELECT id, name FROM users WHERE active = 1",
"skip": 0,
"take": 10
}
Response format (compact / token-efficient):
{
"meta": { "totalCount": 42, "returnedCount": 10, "skip": 0, "take": 10 },
"columns": ["id", "name"],
"rows": [[1, "Alice"], [2, "Bob"], ...]
}
listTables
List all base tables in the database.
[{ "schema": "dbo", "name": "users" }, ...]
describeTable
Describe a table's columns, indexes, foreign keys, check constraints, and size statistics.
{ "table": "dbo.users" }
explainQuery
Return the estimated execution plan for a SELECT query without executing it.
{ "sql": "SELECT * FROM orders WHERE status = 'open'" }
exportQuery
Stream a SELECT query result to a file. Designed for large datasets – there is no row-count limit and results are written directly to disk using Node.js streams.
{
"sql": "SELECT * FROM large_table",
"filepath": "/tmp/export.csv",
"format": "csv",
"options": { "delimiter": ",", "bom": false }
}
format defaults to "csv" if omitted. "json" is also supported.
CSV options (all optional):
| Option | Default | Description |
|---|---|---|
delimiter |
"," |
Column separator |
nullValue |
"" |
String to write for NULL / undefined cells |
bom |
false |
Prepend UTF-8 BOM (useful for Excel) |
JSON options (all optional):
| Option | Default | Description |
|---|---|---|
pretty |
false |
Indent the output JSON |
Response format:
{
"filepath": "/tmp/export.csv",
"format": "csv",
"rowCount": 50000
}
The tool uses a separate, longer-lived connection pool whose requestTimeout is controlled by EXPORT_QUERY_TIMEOUT (default 300 000 ms = 5 min). Increase this value for very large exports.
Development
# Clone the repository
git clone https://github.com/akrym1582/sqldb-mcp-server.git
cd sqldb-mcp-server
# Install dependencies
npm install
# Configure environment
cp .env.example .env
# Edit .env with your DB credentials
# Run in dev mode (no compile step)
npm run dev
# Or build and run
npm run build
npm start
# Run unit tests
npm test
Project Structure
src/
mcp/
server.ts # MCP server entry point
tools/
query.ts # query tool
listTables.ts # listTables tool
describeTable.ts # describeTable tool
explainQuery.ts # explainQuery tool
exportQuery.ts # exportQuery tool (streaming file export)
db/
index.ts # DB adapter factory (selects adapter from DB_TYPE)
types.ts # DB interfaces (including queryStream)
adapters/
mssql.ts # Microsoft SQL Server implementation
postgresql.ts # PostgreSQL implementation (pg + pg-cursor)
mysql.ts # MySQL implementation (mysql2)
utils/
row-result.ts # Compact columnar result format
sanitize.ts # AST-based SQL read-only validation (dialect-aware)
pagination.ts # skip/take normalisation
cache.ts # TTL in-memory cache
export-writer.ts # Streaming CSV / JSON file writer
__tests__/ # Unit tests
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。