Skip to main content
Glama
sam2332

SQLite MCP Server

by sam2332

connect_database

Establish a connection to a SQLite database file for AI assistants to interact with data, enabling operations like query execution and schema inspection.

Instructions

Connect to a SQLite database file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to the SQLite database file
readonlyNoOpen database in read-only mode

Implementation Reference

  • The core handler function for the 'connect_database' tool. It closes any existing DB connection, opens a new SQLite database connection using better-sqlite3 with optional readonly mode, tests the connection by querying the SQLite version, and returns a success message.
    private async connectDatabase(args: { path: string; readonly?: boolean }): Promise<CallToolResult> {
      try {
        if (this.db) {
          this.db.close();
        }
    
        const dbPath = resolve(args.path);
        this.db = new Database(dbPath, { readonly: args.readonly || false });
        
        // Test the connection
        const result = this.db.prepare("SELECT sqlite_version() as version").get() as { version: string };
        
        return {
          content: [
            {
              type: "text",
              text: `Successfully connected to database: ${dbPath}\nSQLite version: ${result.version}`,
            } satisfies TextContent,
          ],
        };
      } catch (error) {
        throw new Error(`Failed to connect to database: ${error instanceof Error ? error.message : String(error)}`);
      }
    }
  • The input schema for the connect_database tool, defining the expected arguments: path (required string) and optional readonly boolean.
    inputSchema: {
      type: "object",
      properties: {
        path: {
          type: "string",
          description: "Path to the SQLite database file",
        },
        readonly: {
          type: "boolean",
          description: "Open database in read-only mode",
          default: false,
        },
      },
      required: ["path"],
    },
  • src/index.ts:59-77 (registration)
    The tool registration in the ListTools response, including name, description, and input schema for connect_database.
    {
      name: "connect_database",
      description: "Connect to a SQLite database file",
      inputSchema: {
        type: "object",
        properties: {
          path: {
            type: "string",
            description: "Path to the SQLite database file",
          },
          readonly: {
            type: "boolean",
            description: "Open database in read-only mode",
            default: false,
          },
        },
        required: ["path"],
      },
    },
  • src/index.ts:162-163 (registration)
    The dispatch/registration case in the CallToolRequest handler switch statement that routes calls to the connectDatabase method.
    case "connect_database":
      return await this.connectDatabase(args as { path: string; readonly?: boolean });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sam2332/mcp-quick-sqlite3'

If you have feedback or need assistance with the MCP directory API, please join our Discord server