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
| 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 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)}`); } }
- src/index.ts:62-76 (schema)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 });