db-mcp

galanblancom/db-mcp

3.2

If you are the rightful owner of db-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.

A Model Context Protocol (MCP) server that provides read-only access to multiple database systems: Oracle, PostgreSQL, and SQL Server.

Tools
7
Resources
0
Prompts
0

Multi-Database MCP Server

Multi-Database MCP Server is a Model Context Protocol (MCP) server that exposes read-only access to Oracle, PostgreSQL, SQL Server, MySQL/MariaDB, and SQLite databases through a uniform toolset. The server focuses on safety and observability while offering advanced capabilities such as streaming, transactions, caching, and schema comparison.

Highlights

  • Read-only query enforcement with SQL injection protections
  • Connection pooling, caching, retry logic, and detailed error mapping by adapter
  • Supports 22 MCP tools spanning querying, metadata discovery, performance insight, and diagnostics
  • Query result streaming, multi-statement execution, and read-only transactions for complex workflows
  • Built-in query logging, uptime tracking, and health checks for operational visibility

Requirements

  • Node.js 20 or later (aligns with the esbuild target and latest oracledb requirements)
  • Database client libraries reachable from the host where the MCP server runs
  • Appropriate database network access and credentials

Installation

npm install

For local development with live reload:

npm run dev

To build the TypeScript sources:

npm run build

To build an optimized bundle (tree-shaken, minified ESM):

npm run build:bundle

Configuration

Set environment variables before starting the server. Only read-only operations are permitted regardless of configuration.

Common Variables

VariableDescriptionDefault
DB_TYPEoracle, postgres, sqlserver, mysql, or sqliteoracle
QUERY_TIMEOUT_MSQuery timeout in milliseconds30000
LOG_QUERIESEnable in-memory query logging (true / false)false
ENABLE_CACHEEnable metadata cachingtrue
CACHE_TTL_MSCache TTL for metadata (ms)300000

Oracle

VariableRequiredNotes
ORACLE_USER
ORACLE_PASSWORD
ORACLE_HOST
ORACLE_PORTe.g. 1521
ORACLE_SERVICEService/SID name
ORACLE_CLIENT_LIB_DIROptionalRequired for Oracle 11g thick mode

PostgreSQL

VariableRequiredNotes
PG_USER
PG_PASSWORD
PG_HOST
PG_PORTe.g. 5432
PG_DATABASE

SQL Server

VariableRequiredNotes
MSSQL_USER
MSSQL_PASSWORD
MSSQL_SERVERHost or IP
MSSQL_DATABASE
MSSQL_PORTOptionalDefaults to 1433
MSSQL_ENCRYPTOptionalDefaults to true
MSSQL_TRUST_CERTOptionalDefaults to true

MySQL / MariaDB

VariableRequiredNotes
MYSQL_USER
MYSQL_PASSWORD
MYSQL_HOST
MYSQL_DATABASE
MYSQL_PORTOptionalDefaults to 3306

SQLite

VariableRequiredNotes
SQLITE_PATHAbsolute path to .db file

Create a .env file (or export variables inline) with the values relevant to your target database before starting the server.

Running

After configuration, start the server:

npm start

For direct execution without a build step:

npx tsx db-mcp.ts

The server communicates via stdio and registers the MCP tools automatically when launched by a compatible MCP client.

Available Tools

CategoryToolPurpose
Query Executionrun-queryExecute SELECT/WITH queries with pagination, formatting, and dry-run support
run-multi-queryExecute a list of read-only statements sequentially
run-transactionExecute read-only statements inside a transaction
stream-queryStream large result sets in batches
Metadatalist-schemasList schemas with table/view counts
list-tablesEnumerate tables with row counts
get-table-infoRetrieve column metadata, PK/FK info, and row counts
get-indexesList indexes and key details
get-foreign-keysList foreign key relationships
list-viewsEnumerate views
get-view-definitionReturn definition SQL for a view
list-stored-proceduresList stored procedures and functions
get-table-statisticsRetrieve table size, index size, and fragmentation metadata
Counting & Samplingget-row-countFast row counts with optional filter
sample-table-dataRetrieve top or random rows
Analysisexplain-queryObtain database-specific execution plans
Templateslist-query-templatesDiscover available parameterized templates
execute-templateRun a predefined template with parameters
Diagnosticstest-connectionValidate connectivity and response time
health-checkAggregate health information for the server
get-performance-metricsView query metrics, cache stats, and uptime
Schema Opscompare-schemasCompare table definitions across schemas

Each tool validates inputs with zod schemas and returns structured JSON (with optional CSV/table formatting for queries).

Security & Reliability

  • Only SELECT and WITH statements are accepted; DDL/DML commands are rejected during validation.
  • WHERE clause validation detects disallowed keywords to mitigate injection attempts.
  • Connection pools and retry logic help recover from transient failures without restarting the server.
  • Metadata caching honors configurable TTLs to reduce database load.

Troubleshooting

  • Test connectivity: Use the test-connection tool; review logs when LOG_QUERIES=true.
  • Check configuration: Confirm environment variables match the chosen DB_TYPE.
  • Oracle thick mode: Set ORACLE_CLIENT_LIB_DIR when using Instant Client 11g packages.
  • SQLite path: Ensure the process has read permissions on the .db file.

Development Notes

  • TypeScript sources live at db-mcp.ts, database-adapters.ts, and utils.ts.
  • Type definitions for the Oracle client extend the upstream declarations in oracledb.d.ts.
  • Run npm run build before publishing to ensure the compiled artifacts in dist/ are current.
  • Use npm run build:bundle to produce a single-file bundle suitable for distribution without TypeScript sources.

License

MIT