Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(mcp): add dbx_add_connection and dbx_remove_connection tools

t8y24c97aa0516f1
diff --git a/mcp/src/connections.ts b/mcp/src/connections.ts
index e7ce7d0a6f..fbb07da6d6 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<ConnectionConfig[]> {
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<ConnectionConfig | u
const connections = await loadConnections();
return connections.find((c) => c.name.toLowerCase() === name.toLowerCase());
}
+
+export async function addConnection(config: Omit<ConnectionConfig, "id">): Promise<ConnectionConfig> {
+ 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<boolean> {
+ 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 09f268872a..70e993ae5d 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<ConnectionConfig[]>;
findConnection(name: string): Promise<ConnectionConfig | undefined>;
+ addConnection(config: Omit<ConnectionConfig, "id">): Promise<ConnectionConfig>;
+ removeConnection(name: string): Promise<boolean>;
listTables(config: ConnectionConfig, schema?: string): Promise<TableInfo[]>;
describeTable(config: ConnectionConfig, table: string, schema?: string): Promise<ColumnInfo[]>;
executeQuery(config: ConnectionConfig, sql: string): Promise<QueryResult>;
@@ -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<ConnectionConfig, "id">);
+ 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 f295386ceb..3cd3d1d0c8 100644
--- a/mcp/src/web-backend.ts
+++ b/mcp/src/web-backend.ts
@@ -61,6 +61,22 @@ export async function findConnection(name: string): Promise<ConnectionConfig | u
return connections.find((c) => c.name.toLowerCase() === name.toLowerCase());
}
+export async function addConnection(config: Omit<ConnectionConfig, "id">): Promise<ConnectionConfig> {
+ 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<boolean> {
+ 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<void> {
await apiFetch("/api/connection/connect", {
method: "POST",