/mcp-database

SQLite database operations for Claude - queries, schema management, and transactions

Primary LanguageJavaScript

MCP Database Server

A Model Context Protocol (MCP) server that provides database operations for Claude, with SQLite support.

Features

  • 🗄️ 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

Installation

  1. Prerequisites:

    • Node.js 18+ installed
  2. Install the MCP server:

    cd /Users/bard/Code/mcp-database
    npm install
  3. 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"]
        }
      }
    }
  4. Restart Claude Desktop

Usage

Connect to Database

// 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 })

Execute Queries

// 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"]
})

Schema Operations

// 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" }
  ]
})

CRUD Operations

// 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 }
})

Transactions

// 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"]
    }
  ]
})

Connection Management

// Close a database connection
db_close({ path: "/path/to/db.db" })

Tool Reference

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

SQLite Data Types

Supported SQLite data types:

  • INTEGER - Signed integer
  • REAL - Floating point
  • TEXT - Text string
  • BLOB - Binary data
  • DATETIME - Date/time (stored as TEXT)

Security Considerations

  • 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

Examples

Create a Todo App Database

// 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"
})

Development

Testing the server:

# Run directly
node src/index.js

# Test with sample commands
echo '{"jsonrpc": "2.0", "method": "tools/list", "id": 1}' | node src/index.js

Common Issues

  1. Database locked: Close other connections or use WAL mode
  2. Permission denied: Check file permissions
  3. SQL syntax error: Verify your SQL syntax
  4. Module not found: Run npm install to install sqlite3

License

MIT