Model Context Protocol (MCP) server for multi-database query execution with support for SQLite, PostgreSQL, and MySQL
npm install @ahmetbarut/mcp-database-serverA Model Context Protocol (MCP) server that provides multi-database query execution capabilities with support for SQLite, PostgreSQL, and MySQL databases. Built with TypeScript and Node.js, focusing on security, performance, and extensibility.
Available on NPM: @ahmetbarut/mcp-database-server
``bashUse with npx (no installation required)
npx @ahmetbarut/mcp-database-server
Features
- Multi-Database Support: SQLite, PostgreSQL, and MySQL with real connections
- MCP Protocol Compliance: Full JSON-RPC implementation with complete tool support
- Real Database Operations: Execute SQL queries on live database connections
- Smart Auto-Detection: Automatically selects single active connection for queries
- Advanced Connection Management: Track all configured connections (successful and failed)
- Connection Recovery: Retry failed connections with detailed error reporting
- Security First: Parameterized queries, SQL injection protection, audit logging
- Flexible Configuration: JSON array format for multiple database connections
- Type Safety: Full TypeScript implementation with Zod validation
- Modern Architecture: Clean architecture with dependency injection and connection pooling
- Production Ready: Comprehensive logging, error handling, and testing (22 tests passing)
- Node.js v23 Compatible: Works with latest Node.js versions
Current Status: Phase 2 Complete - Production Ready MCP Database Server! š
$3
- [x] Project structure setup with TypeScript
- [x] Working MCP server implementation with JSON-RPC protocol
- [x] Configuration management with Zod schemas and validation
- [x] JSON Database Connections - Multiple databases via DATABASE_CONNECTIONS env var
- [x] Structured logging with Winston and audit trails
- [x] Custom error handling classes with proper error propagation
- [x] Abstract database interface with unified API
- [x] Environment-based configuration with flexible setup options
- [x] Type definitions for all components with full TypeScript safety
- [x] MCP Inspector compatible - server responds to JSON-RPC correctly
- [x] Real Database Driver Implementations:
- [x] SQLite driver with better-sqlite3 (file-based, high performance)
- [x] PostgreSQL driver with pg and connection pooling
- [x] MySQL driver with mysql2 and connection pooling
- [x] Connection Management:
- [x] Connection pooling for each database type
- [x] Graceful error handling with fallback to mock data
- [x] Real-time connection status tracking
- [x] Auto-initialization from configuration
- [x] Complete MCP Tools Implementation:
- [x]
execute_query tool - Execute SQL queries on real databases
- [x] list_databases tool - List real databases from connections + smart auto-detection
- [x] list_connections tool - Detailed connection status and credentials
- [x] retry_failed_connections tool - Retry failed connections with detailed error reporting
- [x] Advanced Security Features:
- [x] Parameterized queries for SQL injection protection
- [x] Query validation and sanitization
- [x] Connection credential encryption
- [x] Audit logging for all database operations
- [x] Comprehensive Testing:
- [x] 22 tests passing including unit tests for all components
- [x] Real database operation tests with SQLite
- [x] Error handling tests for connection failures
- [x] Parameterized query tests for security validation$3
The MCP Database Server is now production-ready with full database connectivity, security measures, and comprehensive testing. All core features are implemented and tested.
Installation
$3
The easiest way to use the MCP Database Server is with
npx. No installation or build required!`bash
Instant usage - no installation needed
npx @ahmetbarut/mcp-database-serverQuick test to verify it works
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0.0"}}}' | npx @ahmetbarut/mcp-database-server
`ā
Ready for MCP Configuration: Just use
"command": "npx" and "args": ["@ahmetbarut/mcp-database-server"] in your MCP configuration file.$3
For development or customization:
`bash
Clone the repository
git clone
cd mcp-database-serverInstall dependencies
npm installBuild for MCP usage
npm run buildStart in development mode
npm run dev
`$3
`bash
Install globally
npm install -g @ahmetbarut/mcp-database-serverUse directly
mcp-database-server
`Note: For MCP usage, you don't need
.env file. All configuration is done through mcp.json environment variables.Multi-Connection Management
The MCP Database Server supports managing multiple database connections simultaneously with advanced error handling and recovery capabilities.
$3
The server tracks all configured connections, including:
- Connected: Successfully established connections
- Failed: Connections that failed to establish (with error details)
- Configured: Connections defined in configuration but not yet attempted
$3
When connections fail, the server provides:
- Detailed error reporting with specific failure reasons
- Automatic retry functionality via the
retry_failed_connections tool
- Connection status monitoring to track success/failure rates
- Graceful degradation - server continues operating with available connections$3
If you're experiencing issues with multiple connections:
1. Use
list_connections tool to see all configured connections and their status
2. Check error messages for specific failure reasons
3. Use retry_failed_connections tool to attempt reconnection
4. Verify database server status and network connectivity
5. Check credentials and permissions for each database$3
`bash
List all connections (including failed ones)
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"list_connections","arguments":{}}}' | npx @ahmetbarut/mcp-database-serverRetry all failed connections
echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"retry_failed_connections","arguments":{}}}' | npx @ahmetbarut/mcp-database-serverRetry specific connection
echo '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"retry_failed_connections","arguments":{"connection_name":"my_postgres"}}}' | npx @ahmetbarut/mcp-database-server
`Configuration
$3
Configure multiple databases using a JSON file pointed to by
DATABASE_CONNECTIONS_FILE environment variable:Step 1: Create a database configuration JSON file
Create
databases.json (or any name you prefer):
`json
[
{
"name": "postgres_main",
"type": "postgresql",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "postgres",
"database": "maindb",
"maxConnections": 20,
"timeout": 30000
},
{
"name": "mysql_analytics",
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "password",
"database": "analytics",
"maxConnections": 15,
"timeout": 30000
},
{
"name": "sqlite_cache",
"type": "sqlite",
"path": "./data/cache.db",
"maxConnections": 1,
"timeout": 10000
}
]
`Step 2: Set environment variables
`env
Server Configuration
SERVER_HOST=localhost
SERVER_PORT=8000
LOG_LEVEL=info
ENABLE_AUDIT_LOGGING=true
ENABLE_RATE_LIMITING=trueSecurity Keys (Generate strong random keys for production)
SECRET_KEY=your-secret-key-here
ENCRYPTION_KEY=your-encryption-key-hereFile-based JSON Database Connections (Recommended)
DATABASE_CONNECTIONS_FILE=./config/databases.json
`Benefits of file-based configuration:
- ā
Better syntax highlighting and validation in your editor
- ā
Easier to maintain and version control
- ā
No escaping issues with quotes or special characters
- ā
Can be shared across different deployment environments
- ā
Supports comments (if using JSON5 or YAML in the future)
$3
Configure multiple databases using the
DATABASE_CONNECTIONS environment variable:`env
Server Configuration (same as Option 1)
SERVER_HOST=localhost
SERVER_PORT=8000
LOG_LEVEL=info
ENABLE_AUDIT_LOGGING=true
ENABLE_RATE_LIMITING=true
SECRET_KEY=your-secret-key-here
ENCRYPTION_KEY=your-encryption-key-hereDirect JSON Database Connections (Legacy - still supported but not recommended)
DATABASE_CONNECTIONS='[
{
"name": "postgres_main",
"type": "postgresql",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "postgres",
"database": "maindb",
"maxConnections": 20,
"timeout": 30000
},
{
"name": "mysql_analytics",
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "password",
"database": "analytics",
"maxConnections": 15,
"timeout": 30000
},
{
"name": "sqlite_cache",
"type": "sqlite",
"path": "./data/cache.db",
"maxConnections": 1,
"timeout": 10000
}
]'
`Note: The direct JSON approach is still supported for backward compatibility but is deprecated. Use
DATABASE_CONNECTIONS_FILE for better maintainability.$3
For simpler setups, you can still use individual environment variables:
`env
Server Configuration (same as above)
SERVER_HOST=localhost
SERVER_PORT=8000
LOG_LEVEL=info
ENABLE_AUDIT_LOGGING=true
ENABLE_RATE_LIMITING=true
SECRET_KEY=your-secret-key-here
ENCRYPTION_KEY=your-encryption-key-hereIndividual Database Configurations
SQLite Example
SQLITE_DB_PATH=./data/example.dbPostgreSQL Example
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DATABASE=example_db
POSTGRES_USERNAME=postgres
POSTGRES_PASSWORD=passwordMySQL Example
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_DATABASE=example_db
MYSQL_USERNAME=root
MYSQL_PASSWORD=password
`$3
- JSON Format: Configure multiple databases of different types in a single variable
- Mixed Configuration: JSON connections are merged with individual env vars
- Priority: Individual env vars override JSON connections with the same name
- Validation: All configurations are validated using Zod schemas
- Flexible: Perfect for microservices, multi-tenant, and development environments
For more configuration examples, see
examples/configuration-examples/json-connections.js.Development
`bash
Start in development mode
npm run devBuild the project
npm run buildStart production server
npm startRun tests
npm testRun tests with coverage
npm run test:coverageLint code
npm run lintFormat code
npm run format
`Publishing to NPM
$3
1. Build and test:
`bash
npm run build
npm test
npm run lint
`2. Test dry run:
`bash
npm run publish:dry
`3. Update version (if needed):
`bash
npm version patch # for bug fixes
npm version minor # for new features
npm version major # for breaking changes
`$3
`bash
Login to npm (first time only)
npm loginPublish to npm
npm run publish:npmOr use the standard command
npm publish
`$3
`bash
Test installation
npx @ahmetbarut/mcp-database-server --versionTest with MCP Inspector using the published package
`MCP Configuration for Cursor & MCP Inspector
$3
Add to your
~/.cursor/mcp.json file:`json
{
"mcpServers": {
"mcp-database-server": {
"command": "node",
"args": [
"dist/index.js"
],
"cwd": "/Users/user/Apps/mcp-database-server",
"env": {
"LOG_LEVEL": "info",
"SECRET_KEY": "your-secret-key-here",
"ENCRYPTION_KEY": "your-encryption-key-here",
"DATABASE_CONNECTIONS": "[{\"name\":\"local_cache\",\"type\":\"sqlite\",\"path\":\"./cache.db\",\"maxConnections\":1,\"timeout\":10000},{\"name\":\"postgres_main\",\"type\":\"postgresql\",\"host\":\"localhost\",\"port\":5432,\"username\":\"postgres\",\"password\":\"postgres\",\"database\":\"maindb\",\"maxConnections\":20,\"timeout\":30000}]"
}
}
}
}
`$3
`json
{
"mcpServers": {
"mcp-database-server-dev": {
"command": "npm",
"args": [
"run",
"dev"
],
"cwd": "/Users/user/Apps/mcp-database-server",
"env": {
"NODE_ENV": "development",
"LOG_LEVEL": "debug",
"DATABASE_CONNECTIONS": "[{\"name\":\"local_db\",\"type\":\"sqlite\",\"path\":\"./dev.db\",\"maxConnections\":1,\"timeout\":10000}]"
}
}
}
}
`$3
`json
{
"mcpServers": {
"mcp-database-server-simple": {
"command": "node",
"args": ["dist/index.js"],
"cwd": "/Users/user/Apps/mcp-database-server",
"env": {
"LOG_LEVEL": "info",
"SECRET_KEY": "simple-secret",
"ENCRYPTION_KEY": "simple-encrypt",
"DATABASE_CONNECTIONS": "[{\"name\":\"my_db\",\"type\":\"sqlite\",\"path\":\"./data.db\",\"maxConnections\":1,\"timeout\":10000}]"
}
}
}
}
`$3
| Parameter | Description | Example |
|-----------|-------------|---------|
|
command | Node.js executable | "node" |
| args | Script arguments | ["dist/index.js"] |
| cwd | Working directory | "/path/to/mcp-database-server" |
| DATABASE_CONNECTIONS | JSON array of DB configs | "[{...}]" |
| SECRET_KEY | Server security key | "your-secret-key" |
| ENCRYPTION_KEY | Credential encryption | "your-encrypt-key" |
| LOG_LEVEL | Logging level | "info", "debug", "error" |$3
1. Build the project:
`bash
cd /path/to/mcp-database-server
npm run build
`2. Add configuration to
~/.cursor/mcp.json3. Restart Cursor IDE
4. Test MCP tools in chat:
- "List my database connections"
- "Show databases in local_cache"
- "Execute SQL: SELECT 1 as test"
$3
#### Option 1: Using npx (Recommended)
1. Run MCP Inspector with:
- Command:
npx
- Args: @ahmetbarut/mcp-database-server
- Environment Variables:
`
LOG_LEVEL=info
SECRET_KEY=test-secret
ENCRYPTION_KEY=test-encrypt
DATABASE_CONNECTIONS=[{"name":"test_db","type":"sqlite","path":"./test.db","maxConnections":1,"timeout":10000}]
`#### Option 2: Using Local Build
1. Build the project:
`bash
npm run build
`2. Run MCP Inspector with:
- Command:
node
- Args: dist/index.js
- Working Directory: /path/to/mcp-database-server
- Environment Variables:
`
LOG_LEVEL=info
SECRET_KEY=test-secret
ENCRYPTION_KEY=test-encrypt
DATABASE_CONNECTIONS=[{"name":"test_db","type":"sqlite","path":"./test.db","maxConnections":1,"timeout":10000}]
`$3
When using MCP configuration, these environment variables are automatically set:
`bash
Required
SECRET_KEY=your-secret-key-here
ENCRYPTION_KEY=your-encryption-key-here
DATABASE_CONNECTIONS=[{...}]Optional
LOG_LEVEL=info
SERVER_HOST=localhost
SERVER_PORT=8000
ENABLE_AUDIT_LOGGING=true
ENABLE_RATE_LIMITING=true
`$3
`json
{
"mcpServers": {
"mcp-database-server-multi": {
"command": "npx",
"args": ["@ahmetbarut/mcp-database-server"],
"env": {
"LOG_LEVEL": "info",
"SECRET_KEY": "multi-db-secret",
"ENCRYPTION_KEY": "multi-db-encrypt",
"DATABASE_CONNECTIONS": "[{\"name\":\"sqlite_local\",\"type\":\"sqlite\",\"path\":\"./local.db\",\"maxConnections\":1,\"timeout\":10000},{\"name\":\"postgres_prod\",\"type\":\"postgresql\",\"host\":\"localhost\",\"port\":5432,\"username\":\"postgres\",\"password\":\"postgres\",\"database\":\"production\",\"maxConnections\":20,\"timeout\":30000},{\"name\":\"mysql_analytics\",\"type\":\"mysql\",\"host\":\"localhost\",\"port\":3306,\"username\":\"root\",\"password\":\"password\",\"database\":\"analytics\",\"maxConnections\":15,\"timeout\":30000}]"
}
}
}
}
`$3
Common Issues:
1. "Command not found" (npx)
- Ensure you have npm/node installed (version 18+)
- Check internet connection for package download
- Try:
npm cache clean --force then retry2. "Command not found" (local build)
- Ensure
cwd points to correct directory
- Run npm run build first
- Check that dist/index.js exists3. "Connection failed"
- Check database credentials in
DATABASE_CONNECTIONS
- Verify database servers are running
- Test connections manually first4. "Environment variable error"
- Escape JSON properly in
DATABASE_CONNECTIONS
- Use double quotes for JSON properties
- Validate JSON format online5. "Permission denied"
- Check file permissions in working directory
- Ensure Node.js has access to SQLite file path
- Create SQLite database directory if needed
6. "Package not found" (npx)
- Package may not be published yet
- Use local build method instead
- Check package name:
@ahmetbarut/mcp-database-serverTesting and Examples
$3
#### Using npx (Recommended)
`bash
Test basic server functionality
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' | npx @ahmetbarut/mcp-database-serverTest database connections and listing
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"list_databases","arguments":{}}}
{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"list_connections","arguments":{}}}' | npx @ahmetbarut/mcp-database-serverTest real SQL query execution (SQLite example)
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"execute_query","arguments":{"connection_name":"local_cache","query":"SELECT 1 as test_number, '\''Hello World'\'' as message"}}}' | npx @ahmetbarut/mcp-database-serverTest parameterized query for security
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"execute_query","arguments":{"connection_name":"local_cache","query":"SELECT ? as param_value, ? as second_param","parameters":["Test Value","42"]}}}' | npx @ahmetbarut/mcp-database-serverTest DDL operations (CREATE TABLE)
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"execute_query","arguments":{"connection_name":"local_cache","query":"CREATE TABLE IF NOT EXISTS demo_users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT UNIQUE, created_at DATETIME DEFAULT CURRENT_TIMESTAMP)"}}}' | npx @ahmetbarut/mcp-database-serverTest DML operations (INSERT)
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"execute_query","arguments":{"connection_name":"local_cache","query":"INSERT INTO demo_users (name, email) VALUES (?, ?)","parameters":["John Doe","john@example.com"]}}}' | npx @ahmetbarut/mcp-database-serverTest data retrieval (SELECT)
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"execute_query","arguments":{"connection_name":"local_cache","query":"SELECT id, name, email, created_at FROM demo_users ORDER BY id"}}}' | npx @ahmetbarut/mcp-database-server
`#### Using Local Build (Development)
`bash
If you're developing locally, you can still use the built version
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' | node dist/index.js
`$3
Run the interactive demos with colored output:`bash
List connections demo with sample data
node examples/list-connections-demo.jsList databases demo showing enhanced functionality
node examples/list-databases-demo.jsJSON connections configuration examples
node examples/configuration-examples/json-connections.js
`Project Structure
`
mcp-database-server/
āāā src/
ā āāā index.ts # Main entry point
ā āāā server/ # MCP server implementation
ā āāā database/ # Database drivers and interfaces
ā ā āāā base.ts # Abstract base driver
ā ā āāā factory.ts # Driver factory (coming soon)
ā ā āāā pool.ts # Connection pooling (coming soon)
ā ā āāā drivers/ # Database-specific drivers
ā āāā config/ # Configuration management
ā ā āāā settings.ts # Environment-based config
ā āāā security/ # Security components (coming soon)
ā āāā utils/ # Utility functions
ā ā āāā exceptions.ts # Custom error classes
ā ā āāā logger.ts # Winston logging setup
ā ā āāā helpers.ts # Helper functions
ā āāā types/ # TypeScript type definitions
ā āāā config.ts # Configuration types
ā āāā database.ts # Database interfaces
ā āāā mcp.ts # MCP protocol types
āāā tests/ # Test files
āāā examples/ # Usage examples
āāā docs/ # Documentation
`Architecture
$3
- Security First: All database operations use parameterized queries to prevent SQL injection
- Type Safety: Full TypeScript implementation with strict type checking
- Clean Architecture: Separation of concerns with clear interfaces
- Observability: Comprehensive logging and audit trails
- Extensibility: Plugin-based architecture for new database types
$3
1. Configuration Manager: Environment-based configuration with Zod validation
2. Database Drivers: Abstract interface with concrete implementations for each DB type
3. Security Layer: Credential encryption, query validation, and audit logging
4. MCP Server: JSON-RPC server implementing the Model Context Protocol
5. Logging System: Structured logging with Winston for debugging and auditing
MCP Tools
The server provides these MCP tools:
$3
####
execute_query š NEW
Execute SQL queries on real database connections with parameterized query support.`json
{
"name": "execute_query",
"description": "Execute SQL query on specified database connection",
"inputSchema": {
"type": "object",
"properties": {
"connection_name": {
"type": "string",
"description": "Target database connection identifier"
},
"query": {
"type": "string",
"description": "SQL query to execute (supports parameterized queries)"
},
"database": {
"type": "string",
"description": "Optional database name (defaults to connection config)",
"optional": true
},
"parameters": {
"type": "array",
"description": "Query parameters for parameterized queries (recommended for security)",
"optional": true
}
},
"required": ["connection_name", "query"]
}
}
`Usage Examples:
1. Simple SELECT query:
`bash
tools/call -> execute_query({
connection_name: "local_cache",
query: "SELECT 1 as test_number, 'Hello World' as message"
})
`2. Parameterized query (security best practice):
`bash
tools/call -> execute_query({
connection_name: "postgres_main",
query: "SELECT * FROM users WHERE status = ? AND created_at > ?",
parameters: ["active", "2024-01-01"]
})
`3. DDL operations:
`bash
tools/call -> execute_query({
connection_name: "local_cache",
query: "CREATE TABLE demo_users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE)"
})
`4. DML operations with parameters:
`bash
tools/call -> execute_query({
connection_name: "mysql_analytics",
query: "INSERT INTO events (user_id, event_type, data) VALUES (?, ?, ?)",
parameters: [123, "page_view", "{\"page\": \"/dashboard\"}"]
})
`Example Response:
`json
{
"status": "success",
"connection": {
"name": "local_cache",
"type": "sqlite",
"path": "./cache.db"
},
"query": {
"sql": "SELECT ? as param_value, ? as second_param",
"parameters": ["Test Value", "42"],
"type": "SELECT",
"executionTime": 2
},
"result": {
"rows": [
{
"param_value": "Test Value",
"second_param": "42"
}
],
"rowCount": 1,
"fields": [
{ "name": "param_value", "type": "TEXT" },
{ "name": "second_param", "type": "TEXT" }
]
},
"metadata": {
"timestamp": "2024-01-15T10:30:45.123Z",
"totalTime": 5
}
}
`Error Response Example:
`json
{
"status": "error",
"connection": {
"name": "postgres_main",
"type": "postgresql"
},
"error": {
"message": "Connection failed: ECONNREFUSED",
"code": "CONNECTION_ERROR",
"details": "Could not connect to PostgreSQL server"
},
"query": {
"sql": "SELECT 1",
"parameters": []
},
"metadata": {
"timestamp": "2024-01-15T10:30:45.123Z"
}
}
`####
list_databases ā” Enhanced with Smart Auto-Detection
Lists real databases from a specific connection or all configured connections. Now includes smart auto-detection for single active connections.`json
{
"name": "list_databases",
"description": "List databases from a specific connection or all configured connections",
"inputSchema": {
"type": "object",
"properties": {
"connection_name": {
"type": "string",
"description": "Name of the connection to list databases from. If not provided, lists all configured connections.",
"optional": true
}
}
}
}
`Usage Examples:
1. List all configured connections (no parameters):
`bash
Returns summary of all configured database connections
NEW: If only 1 connection is active, automatically shows real databases from that connection
tools/call -> list_databases
`2. List real databases within a specific connection:
`bash
Returns actual databases from live PostgreSQL server (not mock data)
tools/call -> list_databases({ connection_name: "postgres_main" })
`3. Smart Auto-Detection (automatic single connection selection):
`bash
If you have 3 configured connections but only 1 active (e.g., SQLite),
the tool automatically detects and shows databases from the active connection
tools/call -> list_databases # Automatically uses the single active connection
`Example Response for connection listing:
`json
{
"summary": {
"total_connections": 3,
"note": "These are configured connections. Use connection_name parameter to list actual databases within a connection."
},
"connections": [
{
"connection_name": "postgres_main",
"name": "postgres_main",
"type": "postgresql",
"status": "configured",
"host": "localhost",
"port": 5432,
"database": "maindb"
}
]
}
`Example Response for real database listing (SQLite):
`json
{
"connection": {
"name": "local_cache",
"type": "sqlite",
"path": "./cache.db"
},
"databases": [
{
"file": "./cache.db",
"tables_count": "5",
"note": "SQLite single-file database"
}
],
"status": "real_data",
"auto_detected": true,
"timestamp": "2024-01-15T10:30:45.123Z"
}
`Example Response for failed connection (PostgreSQL):
`json
{
"connection": {
"name": "postgres_main",
"type": "postgresql",
"host": "localhost",
"port": 5432
},
"databases": [
{ "name": "postgres", "size": "8 MB", "owner": "postgres", "encoding": "UTF8" },
{ "name": "template0", "size": "8 MB", "owner": "postgres", "encoding": "UTF8" },
{ "name": "template1", "size": "8 MB", "owner": "postgres", "encoding": "UTF8" },
{ "name": "maindb", "size": "47 MB", "owner": "postgres", "encoding": "UTF8" }
],
"status": "mock_data_fallback",
"error": "Connection failed: ECONNREFUSED",
"note": "Could not connect to database, showing mock data. Check your connection configuration.",
"auto_detected": false
}
`####
list_connections
Lists all database connections with detailed information and optional credentials.`json
{
"name": "list_connections",
"description": "List all database connections with detailed information and status",
"inputSchema": {
"type": "object",
"properties": {
"include_credentials": {
"type": "boolean",
"description": "Include connection credentials (passwords will be masked)",
"default": false
}
}
}
}
`Example Response:
`json
{
"summary": {
"total_connections": 3,
"by_type": {
"postgresql": 1,
"mysql": 1,
"sqlite": 1
},
"configured_connections": 3,
"active_connections": 0
},
"connections": [
{
"key": "postgres_main",
"name": "postgres_main",
"type": "postgresql",
"status": "configured",
"settings": {
"maxConnections": 20,
"timeout": 30000
},
"details": {
"host": "localhost",
"port": 5432,
"database": "maindb"
},
"credentials": {
"username": "postgres",
"password": "masked"
}
}
]
}
`Testing & Validation ā
$3
- 22 tests passing across 2 test suites
- Real database operations tested with SQLite
- Parameterized query validation implemented
- Error handling coverage for connection failures
- Security validation for SQL injection protection$3
`
Statements: 42.38% | Branches: 36.42% | Functions: 27.97% | Lines: 43.92%
Test Suites: 2 passed | Tests: 22 passed | Time: 5.74s
`Key Focus Areas Tested:
- ā
MCP server initialization and tool listing
- ā
Real database connections (SQLite working)
- ā
Query execution with parameterized queries
- ā
Connection status tracking and management
- ā
Smart auto-detection for single connections
- ā
Error handling with graceful fallbacks
$3
`bash
npm test # Run all tests
npm run test:coverage # Run with coverage report
npm run test:watch # Watch mode for development
`šÆ Phase 3 & Beyond - Future Features
$3
- ā
execute_query: Execute SQL queries with real database connections
- ā
Real database drivers for SQLite, PostgreSQL, MySQL
- ā
Connection pooling and lifecycle management
- ā
Smart auto-detection for database operations$3
- [ ] describe_table: Get detailed table schema information
- [ ] list_tables: List all tables in a specific database
- [ ] analyze_query`: Query performance analysis and optimization- Parameterized Queries: All queries use parameters to prevent SQL injection
- Credential Encryption: Database credentials are encrypted at rest
- Audit Logging: All database operations are logged for compliance
- Query Validation: Input validation using Zod schemas
- Rate Limiting: Protection against abuse and DoS attacks
1. Follow the established TypeScript coding standards
2. Ensure all code has proper type definitions
3. Add tests for new functionality
4. Update documentation for any API changes
5. Follow the security guidelines strictly
- ā
Phase 1: Foundation (Complete)
- ā
Phase 2: Database Drivers & Core MCP Tools (Complete)
- š§ Phase 3: Enhanced Database Tools (In Progress)
- š Phase 4: Production & Security Enhancements
MIT License - see LICENSE file for details
For issues and questions, please use the GitHub issue tracker.