2 releases
| 0.1.1 | Jul 3, 2025 |
|---|---|
| 0.1.0 | Jul 1, 2025 |
#957 in Development tools
39 downloads per month
23KB
209 lines
MCP Database Tools
A Model Context Protocol (MCP) server that provides secure database access through MCP Server and other MCP-compatible clients.
π Features
- YAML Configuration: Load database configurations from
~/.mcp-dbtools/config.yml - Environment Variable Support: Use
${VAR_NAME}syntax for secure credential management - Multiple Server Support: Configure and switch between different database connections
- SQL Execution: Execute SQL queries safely through the MCP protocol
- Fallback Support: Automatic fallback to environment variables if YAML config fails
- Comment-Safe Parsing: Environment variables in YAML comments are ignored
π Prerequisites
- Rust 1.70 or later
- A supported SQL database
- MCP Server (for MCP integration)
π οΈ Installation
-
Clone the repository:
git clone <repository-url> cd mcp-dbtools -
Build the project:
cargo build --release -
Create configuration directory:
mkdir -p ~/.mcp-dbtools
βοΈ Configuration
YAML Configuration File
Create ~/.mcp-dbtools/config.yml:
# Configuration for MCP DB Tools
# Use ${VAR_NAME} to load sensitive values from environment variables.
servers:
# Local development database
test_db:
adapter: "postgres"
enabled: true
host: "localhost"
port: 5432
username: "postgres"
password: "postgres" # WARNING: Use env vars for production
dbname: "pocket_db"
read_only: false
# Production database
prod_db:
adapter: "mysql"
enabled: true
host: "prod.example.com"
port: 3306
username: "produser"
password: "${PROD_DB_PASSWORD}"
dbname: "production_db"
read_only: true
Environment Variables
Set required environment variables:
export PROD_DB_PASSWORD="your-secure-password"
πββοΈ Usage
Command Line
# Run with specific server configuration
cargo run --bin mcp-dbtools test_db
# Run with default server (test_db)
cargo run --bin mcp-dbtools
# Use release build
./target/release/mcp-dbtools prod_db
MCP Server Integration
Add to your MCP Server settings file:
macOS: ~/Library/Application Support/MCP Server/mcp_server_config.json
Windows: %APPDATA%\MCP Server\mcp_server_config.json
{
"mcpServers": {
"db-tools-pocket": {
"command": "cargo",
"args": ["run", "--bin", "mcp-dbtools", "--", "test_db"],
"cwd": "/path/to/mcp-dbtools",
"env": {}
}
}
}
Or with pre-built binary:
{
"mcpServers": {
"db-tools-pocket": {
"command": "/path/to/mcp-dbtools/target/release/mcp-dbtools",
"args": ["test_db"],
"env": {}
}
}
}
π§ͺ Testing
Test Database Connection
# Test database schema and data
cargo run --bin test_db
# Explore database content
cargo run --bin explore_data
Test MCP Server
# Test server with YAML config
cargo run --bin mcp-dbtools test_db
π§ Configuration Options
| Field | Type | Required | Description |
|---|---|---|---|
adapter |
string | Yes | Database type (e.g., "postgres", "mysql") |
enabled |
boolean | No | Enable/disable this server config |
host |
string | Yes | Database hostname |
port |
number | Yes | Database port |
username |
string | Yes | Database username |
password |
string | No | Database password (use env vars!) |
dbname |
string | Yes | Database name |
read_only |
boolean | No | Restrict to read-only operations |
π Security Best Practices
- Never hardcode sensitive data in YAML files
- Use environment variables for passwords:
password: "${DB_PASSWORD}" - Set appropriate file permissions on config files:
chmod 600 ~/.mcp-dbtools/config.yml - Use read-only mode for production databases when possible
π Available SQL Operations
Through MCP Server, you can:
- Query tables: "Show me all records from the users table"
- Describe schema: "What columns does the products table have?"
- Run aggregations: "Calculate total sales by month"
- Join tables: "Join users and orders tables"
- Filter data: "Find all orders from last week"
π Troubleshooting
Common Issues
-
"Failed to load YAML config"
- Check if
~/.mcp-dbtools/config.ymlexists - Verify YAML syntax is correct
- Ensure environment variables are set
- Check if
-
"Environment variable not found"
- Set required environment variables before running
- Check variable names match exactly (case-sensitive)
-
"Failed to connect to database"
- Verify database is running
- Check host, port, username, and password
- Ensure database exists
-
"Server 'xyz' not found in config"
- Check server name spelling in YAML file
- Verify the server section exists under
servers:
Debug Mode
Run with verbose output:
RUST_LOG=debug cargo run --bin mcp-dbtools test_db
ποΈ Development
Project Structure
mcp-dbtools/
βββ src/
β βββ main.rs # MCP server entry point
β βββ config.rs # Configuration management
β βββ test_db.rs # Database testing utility
β βββ explore_data.rs # Data exploration utility
βββ mcp-rust-sdk/ # MCP SDK dependency
βββ ~/.mcp-dbtools/ # Configuration directory
β βββ config.yml # Database configurations
βββ README.md
Building
# Development build
cargo build
# Release build
cargo build --release
# Run tests
cargo test
# Check code quality
cargo clippy
cargo fmt
π License
[Add your license information here]
π€ Contributing
[Add contribution guidelines here]
π Support
[Add support contact information here]
Dependencies
~45β62MB
~1M SLoC