diff --git a/mcp/src/connections.ts b/mcp/src/connections.ts index e7ce7d0a6..fbb07da6d 100644 --- a/mcp/src/connections.ts +++ b/mcp/src/connections.ts @@ -1,5 +1,6 @@ import { join } from "node:path"; import { homedir, platform } from "node:os"; +import { randomUUID } from "node:crypto"; import Database from "better-sqlite3"; export interface ConnectionConfig { @@ -29,9 +30,9 @@ function appDataDir(): string { } } -function openDb(): Database.Database { +function openDb(readonly = false): Database.Database { const dbPath = join(appDataDir(), "dbx.db"); - return new Database(dbPath, { readonly: true }); + return new Database(dbPath, { readonly }); } function getSecret(db: Database.Database, connectionId: string, key: string): string { @@ -41,7 +42,7 @@ function getSecret(db: Database.Database, connectionId: string, key: string): st export async function loadConnections(): Promise { try { - const db = openDb(); + const db = openDb(true); const rows = db.prepare("SELECT id, config_json FROM connections").all() as { id: string; config_json: string }[]; const configs: ConnectionConfig[] = []; @@ -63,3 +64,37 @@ export async function findConnection(name: string): Promise c.name.toLowerCase() === name.toLowerCase()); } + +export async function addConnection(config: Omit): Promise { + const id = randomUUID(); + const db = openDb(); + + const sanitized = { ...config, id, password: "" }; + const configJson = JSON.stringify(sanitized); + + const insert = db.transaction(() => { + db.prepare("INSERT INTO connections (id, config_json) VALUES (?, ?)").run(id, configJson); + if (config.password) { + db.prepare("INSERT INTO connection_secrets (connection_id, key, secret) VALUES (?, ?, ?)").run(id, "password", config.password); + } + }); + insert(); + db.close(); + + return { ...config, id }; +} + +export async function removeConnection(name: string): Promise { + const connection = await findConnection(name); + if (!connection) return false; + + const db = openDb(); + const remove = db.transaction(() => { + db.prepare("DELETE FROM connections WHERE id = ?").run(connection.id); + db.prepare("DELETE FROM connection_secrets WHERE connection_id = ?").run(connection.id); + }); + remove(); + db.close(); + + return true; +} diff --git a/mcp/src/index.ts b/mcp/src/index.ts index 09f268872..70e993ae5 100644 --- a/mcp/src/index.ts +++ b/mcp/src/index.ts @@ -5,7 +5,12 @@ import { z } from "zod"; import { readFile } from "node:fs/promises"; import { join } from "node:path"; import { homedir, platform } from "node:os"; -import { loadConnections as desktopLoadConnections, findConnection as desktopFindConnection } from "./connections.js"; +import { + loadConnections as desktopLoadConnections, + findConnection as desktopFindConnection, + addConnection as desktopAddConnection, + removeConnection as desktopRemoveConnection, +} from "./connections.js"; import { listTables as desktopListTables, describeTable as desktopDescribeTable, @@ -19,6 +24,8 @@ const isWebMode = !!process.env.DBX_WEB_URL; interface Backend { loadConnections(): Promise; findConnection(name: string): Promise; + addConnection(config: Omit): Promise; + removeConnection(name: string): Promise; listTables(config: ConnectionConfig, schema?: string): Promise; describeTable(config: ConnectionConfig, table: string, schema?: string): Promise; executeQuery(config: ConnectionConfig, sql: string): Promise; @@ -32,6 +39,8 @@ if (isWebMode) { backend = { loadConnections: desktopLoadConnections, findConnection: desktopFindConnection, + addConnection: desktopAddConnection, + removeConnection: desktopRemoveConnection, listTables: desktopListTables, describeTable: desktopDescribeTable, executeQuery: desktopExecuteQuery, @@ -130,6 +139,43 @@ server.tool( }, ); +server.tool( + "dbx_add_connection", + "Add a new database connection to DBX", + { + name: z.string().describe("Connection name"), + db_type: z.string().describe("Database type: postgres, mysql, sqlite, redis, duckdb, clickhouse, sqlserver, mongodb, oracle, elasticsearch"), + host: z.string().describe("Database host"), + port: z.number().describe("Database port"), + username: z.string().default("").describe("Username"), + password: z.string().default("").describe("Password"), + database: z.string().optional().describe("Default database name"), + ssl: z.boolean().default(false).describe("Enable SSL"), + }, + async ({ name, db_type, host, port, username, password, database, ssl }) => { + const existing = await backend.findConnection(name); + if (existing) return text(`Connection "${name}" already exists.`); + const config = await backend.addConnection({ + name, db_type, host, port, username, password, + database, ssl, ssh_enabled: false, + } as Omit); + return text(`Connection "${config.name}" added (id: ${config.id}).`); + }, +); + +server.tool( + "dbx_remove_connection", + "Remove a database connection from DBX", + { + connection_name: z.string().describe("Name of the connection to remove"), + }, + async ({ connection_name }) => { + const removed = await backend.removeConnection(connection_name); + if (!removed) return text(`Connection "${connection_name}" not found.`); + return text(`Connection "${connection_name}" removed.`); + }, +); + function formatCell(value: unknown): string { if (value === null || value === undefined) return "NULL"; if (typeof value === "object") return JSON.stringify(value); diff --git a/mcp/src/web-backend.ts b/mcp/src/web-backend.ts index f295386ce..3cd3d1d0c 100644 --- a/mcp/src/web-backend.ts +++ b/mcp/src/web-backend.ts @@ -61,6 +61,22 @@ export async function findConnection(name: string): Promise c.name.toLowerCase() === name.toLowerCase()); } +export async function addConnection(config: Omit): Promise { + const res = await apiFetch("/api/connection/save", { + method: "POST", + body: JSON.stringify({ configs: [config] }), + }); + const saved = (await res.json()) as ConnectionConfig; + return saved; +} + +export async function removeConnection(name: string): Promise { + const connection = await findConnection(name); + if (!connection) return false; + await apiFetch(`/api/connection/delete?id=${encodeURIComponent(connection.id)}`, { method: "DELETE" }); + return true; +} + async function ensureConnected(config: ConnectionConfig): Promise { await apiFetch("/api/connection/connect", { method: "POST",