MCP SQLite Tools Server
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
-
Error: Cannot find module 'sqlite3' (or 'better-sqlite3')
cause The `mcp-sqlite-tools` server relies on an underlying SQLite driver, but one is not installed or discoverable in the project's dependencies.fixInstall a compatible SQLite driver: `npm install sqlite3` or `npm install better-sqlite3`. Ensure native dependencies are correctly built, especially on different operating systems or Node.js versions. -
Error: Path validation failed. The provided path is not within the allowed directories.
cause The `databasePath` or a path referenced in a query falls outside the `allowedPaths` configured in the `MCPSQLiteServerOptions`, a security measure to prevent directory traversal.fixVerify that `databasePath` and any paths used within queries are correctly configured and explicitly listed in the `allowedPaths` array when instantiating `MCPSQLiteServer`. -
TypeError: Cannot read properties of undefined (reading 'start')
cause The `MCPSQLiteServer` class or `startMCPSQLiteServer` function was not correctly imported, or the instance was not properly created before attempting to call methods on it.fixEnsure `import { MCPSQLiteServer } from 'mcp-sqlite-tools';` is present and that you are instantiating the server correctly with `new MCPSQLiteServer(...)` before calling `.start()`.
Warnings
- breaking As a pre-1.0 package (version 0.0.17), the API is subject to frequent and potentially breaking changes. Direct usage in production without pinning exact versions is discouraged, and migration efforts should be anticipated with minor version bumps.
- gotcha The server's security features, including `allowedPaths` and query classification, are critical. Misconfiguration of `allowedPaths` can prevent the server from accessing intended database files or, conversely, expose it to unintended file system access outside of sandboxed directories. Incorrect client-side approval logic for 'DESTRUCTIVE' or 'SCHEMA CHANGE' tools can lead to unauthorized modifications.
- gotcha This package is an MCP *server*. It exposes an API for MCP clients (like LLMs) to interact with SQLite. It does not provide client-side utilities for directly calling database operations from your Node.js application. Your application interacts with the server, which then handles the SQLite logic.
Install
-
npm install mcp-sqlite-tools -
yarn add mcp-sqlite-tools -
pnpm add mcp-sqlite-tools
Imports
- MCPSQLiteServer
const MCPSQLiteServer = require('mcp-sqlite-tools').MCPSQLiteServer;import { MCPSQLiteServer } from 'mcp-sqlite-tools'; - MCPSQLiteServerOptions
import type { MCPSQLiteServerOptions } from 'mcp-sqlite-tools'; - startMCPSQLiteServer
import { startMCPSQLiteServer } from 'mcp-sqlite-tools';
Quickstart
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();