mcp-database-server

ahmetbarut/mcp-database-server

3.2

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

The MCP Database Server is a robust and secure server designed to execute multi-database queries across SQLite, PostgreSQL, and MySQL databases, utilizing the Model Context Protocol (MCP) with JSON-RPC compliance.

Tools
  1. execute_query

    Execute SQL queries on specified database connections with parameterized query support.

  2. list_databases

    List databases from a specific connection or all configured connections with smart auto-detection.

  3. list_connections

    List all database connections with detailed information and status.

MCP Database Server

A 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.

📦 NPM Package

Available on NPM: @ahmetbarut/mcp-database-server

# Use with npx (no installation required)
npx @ahmetbarut/mcp-database-server

# Or install globally
npm install -g @ahmetbarut/mcp-database-server

# Or install locally
npm install @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
  • 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! 🎉

✅ Completed Features (Phases 1 & 2)

  • Project structure setup with TypeScript
  • Working MCP server implementation with JSON-RPC protocol
  • Configuration management with Zod schemas and validation
  • JSON Database Connections - Multiple databases via DATABASE_CONNECTIONS env var
  • Structured logging with Winston and audit trails
  • Custom error handling classes with proper error propagation
  • Abstract database interface with unified API
  • Environment-based configuration with flexible setup options
  • Type definitions for all components with full TypeScript safety
  • MCP Inspector compatible - server responds to JSON-RPC correctly
  • Real Database Driver Implementations:
    • SQLite driver with better-sqlite3 (file-based, high performance)
    • PostgreSQL driver with pg and connection pooling
    • MySQL driver with mysql2 and connection pooling
  • Connection Management:
    • Connection pooling for each database type
    • Graceful error handling with fallback to mock data
    • Real-time connection status tracking
    • Auto-initialization from configuration
  • Complete MCP Tools Implementation:
    • execute_query tool - Execute SQL queries on real databases
    • list_databases tool - List real databases from connections + smart auto-detection
    • list_connections tool - Detailed connection status and credentials
  • Advanced Security Features:
    • Parameterized queries for SQL injection protection
    • Query validation and sanitization
    • Connection credential encryption
    • Audit logging for all database operations
  • Comprehensive Testing:
    • 22 tests passing including unit tests for all components
    • Real database operation tests with SQLite
    • Error handling tests for connection failures
    • Parameterized query tests for security validation

🚀 Ready for Production Use

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

Option 1: Quick Start with npx (⭐ Recommended)

The easiest way to use the MCP Database Server is with npx. No installation or build required!

# Instant usage - no installation needed
npx @ahmetbarut/mcp-database-server

# Quick 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.

Option 2: Local Development Setup

For development or customization:

# Clone the repository
git clone <repository-url>
cd mcp-database-server

# Install dependencies
npm install

# Build for MCP usage
npm run build

# Start in development mode
npm run dev

Option 3: Global Installation

# Install globally
npm install -g @ahmetbarut/mcp-database-server

# Use directly
mcp-database-server

Note: For MCP usage, you don't need .env file. All configuration is done through mcp.json environment variables.

Configuration

Option 1: File-based JSON Configuration (⭐ Recommended)

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):

[
  {
    "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

# Server Configuration
SERVER_HOST=localhost
SERVER_PORT=8000
LOG_LEVEL=info
ENABLE_AUDIT_LOGGING=true
ENABLE_RATE_LIMITING=true

# Security Keys (Generate strong random keys for production)
SECRET_KEY=your-secret-key-here
ENCRYPTION_KEY=your-encryption-key-here

# File-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)

Option 2: Direct JSON Environment Variable (Legacy)

Configure multiple databases using the DATABASE_CONNECTIONS environment variable:

# 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-here

# Direct 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.

Option 3: Individual Environment Variables (Legacy)

For simpler setups, you can still use individual environment variables:

# 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-here

# Individual Database Configurations
# SQLite Example
SQLITE_DB_PATH=./data/example.db

# PostgreSQL Example
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DATABASE=example_db
POSTGRES_USERNAME=postgres
POSTGRES_PASSWORD=password

# MySQL Example
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_DATABASE=example_db
MYSQL_USERNAME=root
MYSQL_PASSWORD=password

Configuration Features

  • 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

# Start in development mode
npm run dev

# Build the project
npm run build

# Start production server
npm start

# Run tests
npm test

# Run tests with coverage
npm run test:coverage

# Lint code
npm run lint

# Format code
npm run format

Publishing to NPM

Pre-publish Checklist

  1. Build and test:

    npm run build
    npm test
    npm run lint
    
  2. Test dry run:

    npm run publish:dry
    
  3. Update version (if needed):

    npm version patch  # for bug fixes
    npm version minor  # for new features
    npm version major  # for breaking changes
    

Publishing Steps

# Login to npm (first time only)
npm login

# Publish to npm
npm run publish:npm

# Or use the standard command
npm publish

Verify Publication

# Test installation
npx @ahmetbarut/mcp-database-server --version

# Test with MCP Inspector using the published package

MCP Configuration for Cursor & MCP Inspector

For Cursor IDE

Add to your ~/.cursor/mcp.json file:

{
  "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}]"
      }
    }
  }
}

For Development Mode

{
  "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}]"
      }
    }
  }
}

Simple SQLite-Only Configuration

{
  "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}]"
      }
    }
  }
}

Configuration Parameters

ParameterDescriptionExample
commandNode.js executable"node"
argsScript arguments["dist/index.js"]
cwdWorking directory"/path/to/mcp-database-server"
DATABASE_CONNECTIONSJSON array of DB configs"[{...}]"
SECRET_KEYServer security key"your-secret-key"
ENCRYPTION_KEYCredential encryption"your-encrypt-key"
LOG_LEVELLogging level"info", "debug", "error"

Setup Steps for Cursor

  1. Build the project:

    cd /path/to/mcp-database-server
    npm run build
    
  2. Add configuration to ~/.cursor/mcp.json

  3. Restart Cursor IDE

  4. Test MCP tools in chat:

    • "List my database connections"
    • "Show databases in local_cache"
    • "Execute SQL: SELECT 1 as test"

Setup Steps for MCP Inspector

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:

    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}]
      

Environment Variables for MCP

When using MCP configuration, these environment variables are automatically set:

# 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

Multi-Database Example

{
  "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}]"
      }
    }
  }
}

Troubleshooting MCP Setup

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 retry
  2. "Command not found" (local build)

    • Ensure cwd points to correct directory
    • Run npm run build first
    • Check that dist/index.js exists
  3. "Connection failed"

    • Check database credentials in DATABASE_CONNECTIONS
    • Verify database servers are running
    • Test connections manually first
  4. "Environment variable error"

    • Escape JSON properly in DATABASE_CONNECTIONS
    • Use double quotes for JSON properties
    • Validate JSON format online
  5. "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-server

Testing and Examples

Manual Testing with Real Database Operations

Using npx (Recommended)
# 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-server

# Test 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-server

# Test 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-server

# Test 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-server

# Test 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-server

# Test 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-server

# Test 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)
# 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

Interactive Demos

Run the interactive demos with colored output:

# List connections demo with sample data
node examples/list-connections-demo.js

# List databases demo showing enhanced functionality
node examples/list-databases-demo.js

# JSON 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
└── logs/                         # Application logs

Architecture

Core Principles

  • 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

Key Components

  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:

✅ Available Tools

execute_query 🚀 NEW

Execute SQL queries on real database connections with parameterized query support.

{
  "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:
tools/call -> execute_query({
  connection_name: "local_cache",
  query: "SELECT 1 as test_number, 'Hello World' as message"
})
  1. Parameterized query (security best practice):
tools/call -> execute_query({
  connection_name: "postgres_main",
  query: "SELECT * FROM users WHERE status = ? AND created_at > ?",
  parameters: ["active", "2024-01-01"]
})
  1. DDL operations:
tools/call -> execute_query({
  connection_name: "local_cache",
  query: "CREATE TABLE demo_users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE)"
})
  1. DML operations with parameters:
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:

{
  "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:

{
  "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_databasesEnhanced with Smart Auto-Detection

Lists real databases from a specific connection or all configured connections. Now includes smart auto-detection for single active connections.

{
  "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):
# 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
  1. List real databases within a specific connection:
# Returns actual databases from live PostgreSQL server (not mock data)
tools/call -> list_databases({ connection_name: "postgres_main" })
  1. Smart Auto-Detection (automatic single connection selection):
# 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:

{
  "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):

{
  "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):

{
  "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.

{
  "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:

{
  "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 ✅

Current Test Status

  • 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

Coverage Report

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

Run Tests

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

✅ Completed (Phase 2)

  • 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

🚧 Phase 3 - Enhanced Database Tools

  • describe_table: Get detailed table schema information
  • list_tables: List all tables in a specific database
  • analyze_query: Query performance analysis and optimization
  • Advanced connection management with health checks

📋 Phase 4 - Production & Security Enhancements

  • Enhanced audit logging with detailed query tracking
  • Advanced rate limiting with per-connection quotas
  • Query result caching for performance
  • Connection pooling optimization
  • Comprehensive integration tests for all database types

Security Features

  • 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

Contributing

  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

Development Phases

  • Phase 1: Foundation (Complete)
  • Phase 2: Database Drivers & Core MCP Tools (Complete)
  • 🚧 Phase 3: Enhanced Database Tools (In Progress)
  • 📋 Phase 4: Production & Security Enhancements

License

MIT License - see LICENSE file for details

Support

For issues and questions, please use the GitHub issue tracker.