A read-only database query tool for AI agents and CLI users. Works as both a CLI tool and an MCP (Model Context Protocol) server. Designed to be safe for production databases — all connections are enforced read-only at the database session level.
curl -fsSL https://raw.githubusercontent.com/AdamTovatt/database-mcp/master/install-db.sh | bashThis detects your platform, downloads the latest release, and installs the db binary to /usr/local/bin. Requires the GitHub CLI (gh) to be installed and authenticated. Run the same command again to update.
If you use updaemon (v0.8.0+) for managing tools:
updaemon new db --from github --remote AdamTovatt/database-mcp/db-linux-arm64.zip --type cli
updaemon set-exec-name db DatabaseMcp.Cli
updaemon init dbReplace db-linux-arm64.zip with db-linux-x64.zip on x86_64 systems. Future updates are handled by updaemon update.
dotnet tool install --global DatabaseMcpAfter installation, the db command will be available globally.
To update:
dotnet tool update --global DatabaseMcpTo uninstall:
dotnet tool uninstall --global DatabaseMcpTo register it as an MCP tool in Claude Code:
claude mcp add database -- db --mcpFor Cursor or other MCP clients, add this to your MCP configuration:
{
"mcpServers": {
"database": {
"command": "db",
"args": ["--mcp"]
}
}
}db add <name> <connection-string> # Save a named database connection
db remove <name> # Remove a saved connection
db list # List all saved connections
db test [name] # Test a database connection
db schema [name] # Show database schema
db query [name] <sql> # Execute a read-only SQL query
db help # Show help informationAll common PostgreSQL formats are supported:
# URI format
db add production "postgres://user:pass@host:5432/database"
db add staging "postgresql://user:pass@host/database?sslmode=require"
# Standard ADO.NET format
db add local "Host=localhost;Port=5432;Database=mydb;Username=user;Password=pass"The provider type is auto-detected from the connection string format.
# Add connections
db add production "postgres://admin:secret@db.prod.com:5432/myapp"
db add local "Host=localhost;Database=myapp_dev;Username=postgres;Password=postgres"
# List saved connections (never shows passwords)
db list
# Test that a connection works
db test production
# View the database schema
db schema production
# Run queries (read-only enforced)
db query production "SELECT count(*) FROM users"
db query local "SELECT * FROM orders WHERE created_at > '2025-01-01' LIMIT 10"
# If you omit the connection name, you'll be prompted to pick one
db schema
db testAll database connections execute SET default_transaction_read_only = on at the session level immediately after connecting. This means PostgreSQL itself rejects any INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, or other write operations. This cannot be bypassed by the SQL query — it is enforced by the database engine.
A 30-second statement timeout is applied to all queries to prevent runaway queries from blocking the connection.
Query results are capped at 1000 rows. If a query returns more, the output indicates the results were truncated.
Connection strings are stored encrypted (AES-256) in ~/.config/database-mcp/connections.json. Display metadata (host, database, username) is stored in plaintext alongside the encrypted connection string so that db list and MCP tools can show connection info without decrypting.
Note on encryption: The encryption key is derived from a value compiled into the binary. This is not a cryptographic security measure — it is accident prevention. It protects against LLMs reading plaintext credentials from the config file, automated credential scanners, and casual file exposure. For production secrets management, use a proper secrets vault. Anyone with access to the source code could derive the key.
On Unix systems, the config file is created with permissions 600 (owner read/write only).
The tool is designed to support multiple database providers. Currently supported:
- PostgreSQL via Npgsql
The provider type is stored per connection, so adding support for additional databases (SQLite, SQL Server, etc.) requires only a new provider implementation.
db --mcpWhen running as an MCP server, the following tools are available:
db_list_connections()— List saved connections with safe metadata (never exposes passwords or connection strings)db_schema(connectionName)— Get the full database schema (tables, columns, types, primary keys, foreign keys)db_query(connectionName, sql)— Execute a read-only SQL query (max 1000 rows)
git clone <repository-url>
cd database-mcp
dotnet build DatabaseMcp.slnx
dotnet test DatabaseMcp.slnxTo run as MCP server during development:
dotnet run --project DatabaseMcp.Cli/DatabaseMcp.Cli.csproj -- --mcpTo package:
dotnet pack DatabaseMcp.Cli/DatabaseMcp.Cli.csproj --configuration ReleaseMIT License