Database Admin MCP Server
A Model Context Protocol (MCP) server for database administration. Provides AI assistants with tools for schema inspection, query optimization, data exploration, and migration helpers.
Supports PostgreSQL and MySQL.
Features
- Schema Inspection: List tables, describe columns, view indexes and foreign keys
- Data Exploration: Sample rows from tables with configurable limits
- Query Execution: Run SQL queries (read-only by default for safety)
- Index Analysis: Find unused indexes, missing FK indexes, get optimization suggestions
- Query Plans: EXPLAIN queries with cost analysis and recommendations
- Migration Generation: Generate up/down migration scripts for schema changes
- Relationship Mapping: Visualize foreign key relationships across tables
- Safety First: Read-only mode by default, write operations require explicit opt-in
Installation
npm install -g database-admin-mcp
Or use with npx:
npx database-admin-mcp
Configuration
Claude Desktop
Add to claude_desktop_config.json:
{
"mcpServers": {
"database": {
"command": "npx",
"args": ["database-admin-mcp"],
"env": {
"DATABASE_URL": "postgresql://user:pass@localhost:5432/mydb"
}
}
}
}
Environment Variables
Connection (choose one method):
Using connection string:
DATABASE_URL=postgresql://user:pass@localhost:5432/dbname
DATABASE_URL=mysql://user:pass@localhost:3306/dbname
Using individual variables:
DB_TYPE=postgres
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydb
DB_USER=myuser
DB_PASSWORD=mypassword
DB_SSL=true
Safety settings:
DB_READ_ONLY=true
DB_MAX_ROWS=1000
Available Tools
Schema Tools
list_tables | List all tables with size and index count |
describe_table | Get columns, types, indexes, and foreign keys |
get_relationships | View all foreign key relationships as a graph |
get_connection_info | Show current connection settings |
Data Tools
sample_data | Retrieve sample rows from a table |
execute_query | Run SQL queries (respects read-only mode) |
Optimization Tools
analyze_indexes | Get index usage stats and suggestions |
explain_query | Get query execution plan with recommendations |
Migration Tools
generate_migration | Generate up/down SQL for schema changes |
Examples
List all tables
"Show me all tables in the database"
Describe a table
"What columns does the users table have?"
Sample data
"Show me 5 sample rows from the orders table"
Analyze indexes
"Are there any unused indexes on the products table?"
Generate migration
"Generate a migration to add an email column to the customers table"
Query optimization
"Why is this query slow: SELECT * FROM orders WHERE customer_id = 123"
Safety Features
-
Read-only by default: Write operations (INSERT, UPDATE, DELETE, DROP, etc.) are blocked unless DB_READ_ONLY=false
-
Row limits: Results are limited to DB_MAX_ROWS (default 1000) to prevent memory issues
-
Connection pooling: Efficient connection management with automatic cleanup
-
No credentials in output: Connection info tool hides passwords
Supported Databases
PostgreSQL
- Full feature support
- Schema-aware (supports multiple schemas)
- Uses
pg_stat_user_indexes for index analysis
- JSON query plans with cost estimates
MySQL
- Full feature support
- Uses
information_schema for metadata
SHOW TABLE STATUS for storage analysis
- Standard EXPLAIN output
License
MIT License - see LICENSE