Abdulmumin1/db-mcp
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.
The Database MCP Server provides secure, read-only access to databases through AI assistants using the Model Context Protocol.
Database MCP Server
A Model Context Protocol (MCP) server that provides secure, read-only access to databases through AI assistants. This server allows MCP-compatible clients to execute SQL queries against databases while enforcing strict read-only operations.
Features
- Secure Read-Only Queries: Only allows SELECT and WITH statements, blocking all write operations
- Multiple Database Support: Currently supports PostgreSQL and MySQL
- MCP Protocol: Fully compatible with the Model Context Protocol for seamless AI integration
- Environment-Based Configuration: Easy setup through environment variables
- TypeScript: Built with TypeScript for type safety and maintainability
Supported Adapters
- PostgreSQL (
DB_TYPE=postgres) - MySQL (
DB_TYPE=mysql)
Installation
Prerequisites
- Node.js 18+
- A PostgreSQL or MySQL database
Install Dependencies
npm install
# or
pnpm install
Build the Server
npm run build
# or
pnpm build
This creates an executable build/index.js file.
Configuration
Configure the server using environment variables:
| Variable | Required | Description |
|---|---|---|
DB_TYPE | Yes | Database type: postgres or mysql |
DB_HOST | Yes | Database host address |
DB_PORT | No | Database port (uses default if not specified) |
DB_USER | Yes | Database username |
DB_PASSWORD | Yes | Database password |
DB_DATABASE | Yes | Database name |
Example Configuration
For PostgreSQL:
export DB_TYPE=postgres
export DB_HOST=localhost
export DB_PORT=5432
export DB_USER=myuser
export DB_PASSWORD=mypassword
export DB_DATABASE=mydatabase
For MySQL:
export DB_TYPE=mysql
export DB_HOST=localhost
export DB_PORT=3306
export DB_USER=myuser
export DB_PASSWORD=mypassword
export DB_DATABASE=mydatabase
Usage
Running the Server
After building and configuring environment variables:
node build/index.js
The server will start and listen on stdin/stdout for MCP protocol messages.
MCP Client Integration
This server is designed to work with MCP-compatible clients. The server provides a single tool:
run_query
Executes a secure, read-only SQL query against the configured database.
Input:
query(string): The read-only SQL query to execute (must start with SELECT or WITH)
Output:
- JSON response with query results or error information
Security
This server implements several security measures:
- Read-Only Enforcement: Queries are validated to ensure they only contain SELECT or WITH statements
- Keyword Filtering: Blocks queries containing write operations (INSERT, UPDATE, DELETE, etc.)
- Connection Isolation: Each query uses a separate database connection that is properly closed
- No DDL Operations: Prevents schema modifications and administrative commands
Development
Project Structure
src/
├── index.ts # Main server implementation
└── adapters/
├── index.ts # Adapter exports and factory
├── base.ts # Abstract adapter and validation
├── postgres.ts # PostgreSQL adapter
└── mysql.ts # MySQL adapter
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── README.md # This file
Adding New Database Adapters
To add support for a new database type:
- Create a new file in
src/adapters/(e.g.,sqlite.ts) - Create a new adapter class extending
DBAdapterfrom./base.js - Implement the required methods:
connect()andexecuteReadOnlyQuery() - Export the class from the new file
- Add the adapter to the
adapterFactoryobject insrc/adapters/index.ts - Update the dependencies in
package.jsonif needed
Example:
// src/adapters/sqlite.ts
import { DBAdapter } from './base.js';
export class SqliteAdapter extends DBAdapter {
async connect() {
// Implementation
}
async executeReadOnlyQuery(query: string) {
// Implementation
}
}
// src/adapters/index.ts
import { SqliteAdapter } from './sqlite.js';
export const adapterFactory: Record<string, new (details: ConnectionDetails) => DBAdapter> = {
'postgres': PostgresAdapter,
'mysql': MySqlAdapter,
'sqlite': SqliteAdapter, // New adapter
};
Testing
Run the linter:
npm run lint
Fix linting issues:
npm run lint:fix
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
License
ISC License
Dependencies
- @modelcontextprotocol/sdk - MCP protocol implementation
- pg - PostgreSQL client
- mysql2 - MySQL client
- zod - Schema validation