A Model Context Protocol (MCP) server that provides database operations for Claude, with SQLite support.
- 🗄️ SQLite Support: Full SQLite database operations
- 🔍 Query Execution: Run any SQL query with prepared statements
- 📊 Schema Management: Create tables, view schemas, list tables
- ✏️ CRUD Operations: Insert, update, delete with helper methods
- 🔄 Transactions: Execute multiple queries atomically
- 🔌 Connection Management: Connect to multiple databases
- 🛡️ Prepared Statements: Safe parameterized queries
-
Prerequisites:
- Node.js 18+ installed
-
Install the MCP server:
cd /Users/bard/Code/mcp-database npm install -
Add to Claude Desktop config: Edit
~/Library/Application Support/Claude/claude_desktop_config.json:{ "mcpServers": { "database": { "command": "node", "args": ["/Users/bard/Code/mcp-database/src/index.js"] } } } -
Restart Claude Desktop
// Connect to existing database
db_connect({ path: "/path/to/database.db" })
// Create new database if doesn't exist
db_connect({ path: "/path/to/new.db", create: true })// Simple query
db_query({
path: "/path/to/db.db",
query: "SELECT * FROM users WHERE age > 18"
})
// With parameters (prepared statement)
db_query({
path: "/path/to/db.db",
query: "SELECT * FROM users WHERE age > ? AND city = ?",
params: [18, "New York"]
})// List all tables
db_tables({ path: "/path/to/db.db" })
// Get schema for all tables
db_schema({ path: "/path/to/db.db" })
// Get schema for specific table
db_schema({
path: "/path/to/db.db",
table: "users"
})
// Create a table
db_create_table({
path: "/path/to/db.db",
table: "users",
columns: [
{ name: "id", type: "INTEGER", primaryKey: true },
{ name: "name", type: "TEXT", notNull: true },
{ name: "email", type: "TEXT", unique: true },
{ name: "age", type: "INTEGER" },
{ name: "created", type: "DATETIME", default: "CURRENT_TIMESTAMP" }
]
})// Insert data
db_insert({
path: "/path/to/db.db",
table: "users",
data: {
name: "John Doe",
email: "john@example.com",
age: 30
}
})
// Update data
db_update({
path: "/path/to/db.db",
table: "users",
data: { age: 31 },
where: { email: "john@example.com" }
})
// Delete data
db_delete({
path: "/path/to/db.db",
table: "users",
where: { id: 1 }
})// Execute multiple queries in a transaction
db_transaction({
path: "/path/to/db.db",
queries: [
{
query: "INSERT INTO users (name, email) VALUES (?, ?)",
params: ["Alice", "alice@example.com"]
},
{
query: "UPDATE stats SET user_count = user_count + 1"
},
{
query: "INSERT INTO logs (action) VALUES (?)",
params: ["user_created"]
}
]
})// Close a database connection
db_close({ path: "/path/to/db.db" })| Tool | Description | Required Args |
|---|---|---|
db_connect |
Connect to a SQLite database | path |
db_query |
Execute a SQL query | path, query |
db_schema |
Get database schema | path |
db_tables |
List all tables | path |
db_create_table |
Create a new table | path, table, columns |
db_insert |
Insert data | path, table, data |
db_update |
Update data | path, table, data, where |
db_delete |
Delete data | path, table, where |
db_transaction |
Execute transaction | path, queries |
db_close |
Close connection | path |
Supported SQLite data types:
INTEGER- Signed integerREAL- Floating pointTEXT- Text stringBLOB- Binary dataDATETIME- Date/time (stored as TEXT)
- Always use parameterized queries to prevent SQL injection
- Be careful with file paths - ensure databases are in allowed directories
- Consider read-only connections for sensitive data
- Transactions are atomic - all succeed or all fail
// Create database
db_connect({ path: "./todos.db", create: true })
// Create tables
db_create_table({
path: "./todos.db",
table: "todos",
columns: [
{ name: "id", type: "INTEGER", primaryKey: true },
{ name: "title", type: "TEXT", notNull: true },
{ name: "completed", type: "INTEGER", default: 0 },
{ name: "created_at", type: "DATETIME", default: "CURRENT_TIMESTAMP" }
]
})
// Insert a todo
db_insert({
path: "./todos.db",
table: "todos",
data: { title: "Learn MCP Database tools" }
})
// Query todos
db_query({
path: "./todos.db",
query: "SELECT * FROM todos WHERE completed = 0"
})# Run directly
node src/index.js
# Test with sample commands
echo '{"jsonrpc": "2.0", "method": "tools/list", "id": 1}' | node src/index.js- Database locked: Close other connections or use WAL mode
- Permission denied: Check file permissions
- SQL syntax error: Verify your SQL syntax
- Module not found: Run
npm installto install sqlite3
MIT