fix(mcp): route ssh transport layers through desktop bridge
This commit is contained in:
1 parent
f89bf310b4
commit
417a48ccfe
2 files changed
+57
-2
No files matched your search
@@ -149,6 +149,10 @@ async function getMysqlPool(config: ConnectionConfig): Promise<import("mysql2/pr
|
|||||||
|
|
||||||
type ProxyLayer = { type: "proxy" } & ProxyTunnelConfig;
|
type ProxyLayer = { type: "proxy" } & ProxyTunnelConfig;
|
||||||
|
|
||||||
|
function hasActiveSshLayer(config: ConnectionConfig): boolean {
|
||||||
|
return config.transport_layers?.some((layer) => layer.type === "ssh" && layer.enabled !== false && !!layer.host) ?? false;
|
||||||
|
}
|
||||||
|
|
||||||
function firstProxyLayer(config: ConnectionConfig): ProxyLayer | undefined {
|
function firstProxyLayer(config: ConnectionConfig): ProxyLayer | undefined {
|
||||||
return config.transport_layers?.find((layer): layer is ProxyLayer => layer.type === "proxy" && layer.enabled !== false && !!layer.host);
|
return config.transport_layers?.find((layer): layer is ProxyLayer => layer.type === "proxy" && layer.enabled !== false && !!layer.host);
|
||||||
}
|
}
|
||||||
@@ -502,6 +506,17 @@ async function rqliteRequest(config: ConnectionConfig, endpoint: "/db/query" | "
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function executeQuery(config: ConnectionConfig, sql: string, options?: QueryOptions): Promise<QueryResult> {
|
export async function executeQuery(config: ConnectionConfig, sql: string, options?: QueryOptions): Promise<QueryResult> {
|
||||||
|
if (hasActiveSshLayer(config)) {
|
||||||
|
const result = await withTimeout(
|
||||||
|
bridgeDataRequest<BridgeQueryResult>("/data/execute-query", {
|
||||||
|
connection_name: config.name,
|
||||||
|
database: config.database || "",
|
||||||
|
sql,
|
||||||
|
}),
|
||||||
|
resolveTimeoutMs(options),
|
||||||
|
);
|
||||||
|
return convertBridgeQueryResult(result, options);
|
||||||
|
}
|
||||||
if (config.db_type === "mongodb") {
|
if (config.db_type === "mongodb") {
|
||||||
const find = parseMongoFindCommand(sql);
|
const find = parseMongoFindCommand(sql);
|
||||||
if (find) {
|
if (find) {
|
||||||
@@ -561,7 +576,7 @@ export async function listTables(config: ConnectionConfig, schema?: string): Pro
|
|||||||
const result = await query(config, `SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' ORDER BY name`);
|
const result = await query(config, `SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' ORDER BY name`);
|
||||||
return result.rows.map((r) => ({ name: String(r.name || ""), type: String(r.type || "table") }));
|
return result.rows.map((r) => ({ name: String(r.name || ""), type: String(r.type || "table") }));
|
||||||
}
|
}
|
||||||
if (!isDirectQueryType(config.db_type)) {
|
if (hasActiveSshLayer(config) || !isDirectQueryType(config.db_type)) {
|
||||||
const tables = await bridgeDataRequest<BridgeTableInfo[]>("/data/list-tables", {
|
const tables = await bridgeDataRequest<BridgeTableInfo[]>("/data/list-tables", {
|
||||||
connection_name: config.name,
|
connection_name: config.name,
|
||||||
database: config.database || "",
|
database: config.database || "",
|
||||||
@@ -594,7 +609,7 @@ export async function describeTable(config: ConnectionConfig, table: string, sch
|
|||||||
comment: null,
|
comment: null,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
if (!isDirectQueryType(config.db_type)) {
|
if (hasActiveSshLayer(config) || !isDirectQueryType(config.db_type)) {
|
||||||
const columns = await bridgeDataRequest<BridgeColumnInfo[]>("/data/describe-table", {
|
const columns = await bridgeDataRequest<BridgeColumnInfo[]>("/data/describe-table", {
|
||||||
connection_name: config.name,
|
connection_name: config.name,
|
||||||
database: config.database || "",
|
database: config.database || "",
|
||||||
|
|||||||
@@ -21,6 +21,30 @@ function sqliteConfig(path: string): ConnectionConfig {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mysqlSshConfig(): ConnectionConfig {
|
||||||
|
return {
|
||||||
|
id: "mysql-ssh-test",
|
||||||
|
name: "mysql-over-ssh",
|
||||||
|
db_type: "mysql",
|
||||||
|
host: "10.0.0.10",
|
||||||
|
port: 3306,
|
||||||
|
username: "root",
|
||||||
|
password: "secret",
|
||||||
|
ssl: false,
|
||||||
|
transport_layers: [
|
||||||
|
{
|
||||||
|
type: "ssh",
|
||||||
|
id: "bastion",
|
||||||
|
enabled: true,
|
||||||
|
host: "bastion.example.com",
|
||||||
|
port: 22,
|
||||||
|
user: "root",
|
||||||
|
password: "ssh-secret",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
test("queries SQLite connections without the DBX bridge", async () => {
|
test("queries SQLite connections without the DBX bridge", async () => {
|
||||||
const dir = mkdtempSync(join(tmpdir(), "dbx-mcp-sqlite-"));
|
const dir = mkdtempSync(join(tmpdir(), "dbx-mcp-sqlite-"));
|
||||||
const path = join(dir, "app.db");
|
const path = join(dir, "app.db");
|
||||||
@@ -85,3 +109,19 @@ test("lists and describes SQLite tables without the DBX bridge", async () => {
|
|||||||
rmSync(dir, { recursive: true, force: true });
|
rmSync(dir, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("routes SSH transport layer connections through the DBX bridge", async () => {
|
||||||
|
const dir = mkdtempSync(join(tmpdir(), "dbx-mcp-bridge-home-"));
|
||||||
|
const originalHome = process.env.HOME;
|
||||||
|
process.env.HOME = dir;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await assert.rejects(() => executeQuery(mysqlSshConfig(), "select 1"), /DBX desktop app is not running/);
|
||||||
|
await assert.rejects(() => listTables(mysqlSshConfig()), /DBX desktop app is not running/);
|
||||||
|
await assert.rejects(() => describeTable(mysqlSshConfig(), "users"), /DBX desktop app is not running/);
|
||||||
|
} finally {
|
||||||
|
if (originalHome === undefined) delete process.env.HOME;
|
||||||
|
else process.env.HOME = originalHome;
|
||||||
|
rmSync(dir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in new issue
Block a user