MCP server for multiple database types with modular architecture
@nam088/mcp-database-server
MCP (Model Context Protocol) server for multiple database types, designed with a modular architecture for easy extensibility.
š Read in Vietnamese: README.vi.md
Features
The server provides the following tools:
- query: Execute SELECT queries and return results
- execute_sql: Execute any SQL command (INSERT, UPDATE, DELETE, CREATE, etc.)
- list_tables: List all tables in the database
- describe_table: Get detailed information about a table's schema
Architecture
The project is split into separate modules:
src/
āāā index.ts # Entry point, initializes adapter based on DB_TYPE
āāā server.ts # MCP server implementation
āāā adapters/
āāā base.ts # Base adapter interface
āāā postgres.ts # PostgreSQL adapter implementation
āāā mysql.ts # MySQL adapter implementation
āāā sqlite.ts # SQLite adapter implementation
āāā redis.ts # Redis adapter implementation
āāā mongo.ts # MongoDB adapter implementation
āāā ldap.ts # LDAP adapter implementation
Database Adapters
Each database has its own adapter implementing the DatabaseAdapter interface:
PostgresAdapter: PostgreSQL support with 13 SQL toolsMySQLAdapter: MySQL/MariaDB support with 13 SQL toolsSQLiteAdapter: SQLite support with 13 SQL tools (using better-sqlite3)RedisAdapter: Redis support with 16 Redis toolsMongoAdapter: MongoDB support with 15 MongoDB toolsLDAPAdapter: LDAP support with 6 LDAP tools
Installation
- Install dependencies:
npm install
- Build the project:
npm run build
Configuration
Environment Variables
DB_TYPE: Database type (default:postgres)postgresorpostgresql: PostgreSQLmysqlormysql2: MySQL/MariaDBsqlite: SQLiteredis: Redismongodbormongo: MongoDBldap: LDAP
READ_ONLY_MODE: Read-only mode (default:true- safer)trueor not set: Only allows reads, blocks all write operations (default)falseor0: Allows both read and write (must be set explicitly)
POSTGRES_CONNECTION_STRING: Connection string for PostgreSQLMYSQL_CONNECTION_STRINGorMYSQL_URL: Connection string for MySQLSQLITE_CONNECTION_STRINGorSQLITE_URL: Connection string for SQLite (file path)REDIS_CONNECTION_STRINGorREDIS_URL: Connection string for RedisMONGODB_CONNECTION_STRINGorMONGODB_URL: Connection string for MongoDBLDAP_CONNECTION_STRINGorLDAP_URL: Connection string for LDAPLDAP_BIND_DN: Bind DN for LDAP authentication (optional)LDAP_BIND_PASSWORD: Bind password for LDAP authentication (optional)DATABASE_URL: Connection string (fallback for PostgreSQL, MySQL, or SQLite)
Examples:
# PostgreSQL with read-only mode (default, no need to set READ_ONLY_MODE)
export DB_TYPE="postgres"
export POSTGRES_CONNECTION_STRING="postgresql://user:password@localhost:5432/mydb"
# READ_ONLY_MODE defaults to true
# Redis with write access (must be set explicitly)
export DB_TYPE="redis"
export REDIS_CONNECTION_STRING="redis://localhost:6379"
export READ_ONLY_MODE="false"
# MySQL with read-only mode (default)
export DB_TYPE="mysql"
export MYSQL_CONNECTION_STRING="mysql://user:password@localhost:3306/mydb"
# READ_ONLY_MODE defaults to true
# SQLite with read-only mode (default)
export DB_TYPE="sqlite"
export SQLITE_CONNECTION_STRING="sqlite://./database.sqlite"
# READ_ONLY_MODE defaults to true
# MongoDB with read-only mode (default)
export DB_TYPE="mongodb"
export MONGODB_CONNECTION_STRING="mongodb://localhost:27017/mydb"
# READ_ONLY_MODE defaults to true
Usage
Using npx (Recommended)
After publishing, you can run the server directly with npx without installing:
npx @nam088/mcp-database-server
Local Development
Run the server locally:
npm start
Or run in development mode with watch:
npm run dev
MCP Client Configuration
Add the server to your MCP client configuration (e.g., Claude Desktop). You can use either npx (recommended) or node with a local path.
Using npx (Recommended)
The easiest way is to use npx, which will automatically download and run the package:
{
"mcpServers": {
"postgres-readonly": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "postgres",
"POSTGRES_CONNECTION_STRING": "postgresql://user:password@localhost:5432/mydb"
}
}
}
}
Note: The -y flag automatically accepts package installation if not already present.
Using Local Installation
If you prefer to install locally or use a specific path:
{
"mcpServers": {
"postgres-readonly": {
"command": "node",
"args": ["/path/to/database-server/dist/index.js"],
"env": {
"DB_TYPE": "post
Tools (4)
queryExecute SELECT queries and return resultsexecute_sqlExecute any SQL command (INSERT, UPDATE, DELETE, CREATE, etc.)list_tablesList all tables in the databasedescribe_tableGet detailed information about a table's schemaEnvironment Variables
DB_TYPEDatabase type (postgres, mysql, sqlite, redis, mongodb, ldap)READ_ONLY_MODEBlocks write operations if true (default: true)POSTGRES_CONNECTION_STRINGConnection string for PostgreSQLMYSQL_CONNECTION_STRINGConnection string for MySQLSQLITE_CONNECTION_STRINGFile path for SQLiteREDIS_CONNECTION_STRINGConnection string for RedisMONGODB_CONNECTION_STRINGConnection string for MongoDBLDAP_CONNECTION_STRINGConnection string for LDAPConfiguration
{
"mcpServers": {
"postgres-readonly": {
"command": "npx",
"args": ["-y", "@nam088/mcp-database-server"],
"env": {
"DB_TYPE": "postgres",
"POSTGRES_CONNECTION_STRING": "postgresql://user:password@localhost:5432/mydb"
}
}
}
}