connect_database
Connect to a SQLite database file to enable interaction with its data, including querying, listing tables, and describing schemas for efficient database management.
Instructions
Connect to a SQLite database file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to the SQLite database file | |
| readonly | No | Open database in read-only mode |
Implementation Reference
- src/index.ts:196-219 (handler)The handler function that connects to the specified SQLite database file using better-sqlite3. It closes any existing connection, resolves the path, opens the DB 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)}`); } }
- src/index.ts:62-76 (schema)Input schema definition for the connect_database tool, specifying an object with required 'path' (string) and optional 'readonly' (boolean) properties.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)Tool registration in the ListToolsRequestHandler, defining 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-164 (registration)Dispatcher case in the CallToolRequestHandler switch statement that routes connect_database calls to the connectDatabase method.case "connect_database": return await this.connectDatabase(args as { path: string; readonly?: boolean });