MCP SQLite Tools Server

0.0.17 · active · verified Wed Apr 22

The `mcp-sqlite-tools` package provides a Model Context Protocol (MCP) server designed to enable AI assistants and LLMs to safely and efficiently interact with local SQLite databases. Currently at version 0.0.17, it is under active development, indicating potential API changes before a stable 1.0 release. It offers comprehensive features for database management, table and query operations (read, write, schema), transaction control (with savepoints), schema export/import, and database maintenance like backups and vacuuming. Key differentiators include built-in security features such as query classification, path validation, input validation using Valibot, and advanced connection pooling. The server explicitly categorizes database operations into 'SAFE', 'DESTRUCTIVE', 'SCHEMA CHANGE', and 'TRANSACTION' tools, allowing for granular permission control in MCP clients, preventing unintended data modifications or schema changes by LLMs.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate and run the `mcp-sqlite-tools` server, configuring it to listen on a specific port and manage a local SQLite database within a restricted directory for security. It includes basic error handling and graceful shutdown.

import { MCPSQLiteServer } from 'mcp-sqlite-tools';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function main() {
  const databasePath = path.join(__dirname, 'data', 'my-database.sqlite');

  // Ensure the directory for the database exists
  const databaseDir = path.dirname(databasePath);
  await import('node:fs/promises').then(fs => fs.mkdir(databaseDir, { recursive: true }));

  const server = new MCPSQLiteServer({
    port: 3000,
    databasePath: databasePath,
    // Restrict database operations to a specific directory for security
    allowedPaths: [path.join(__dirname, 'data')],
    // Enable verbose logging for debugging
    logLevel: 'debug',
    // Optional: Configure connection pool settings
    connectionPool: {
      max: 10,
      min: 2,
      idleTimeoutMillis: 30000
    }
  });

  try {
    await server.start();
    console.log(`MCP SQLite Server listening on port 3000. Database: ${databasePath}`);

    // Example: Graceful shutdown on process termination
    process.on('SIGTERM', async () => {
      console.log('SIGTERM signal received. Shutting down server...');
      await server.stop();
      console.log('Server stopped.');
      process.exit(0);
    });

    process.on('SIGINT', async () => {
      console.log('SIGINT signal received. Shutting down server...');
      await server.stop();
      console.log('Server stopped.');
      process.exit(0);
    });

  } catch (error) {
    console.error('Failed to start MCP SQLite Server:', error);
    process.exit(1);
  }
}

main();

view raw JSON →