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。

4.2 KB
Raw Blame
export interface McpLaunchConfig {
  command: string;
  args?: readonly string[];
  env?: Readonly<Record<string, string>>;
}

const DEFAULT_MCP_LAUNCH_CONFIG: McpLaunchConfig = {
  command: "dbx-mcp-server",
};

export function preferMcpNativeLaunch(config?: McpLaunchConfig, nativeBinPath?: string | null): McpLaunchConfig | undefined {
  return nativeBinPath ? { command: nativeBinPath, env: config?.env } : config;
}

function launchConfig(config?: McpLaunchConfig): McpLaunchConfig {
  return config ?? DEFAULT_MCP_LAUNCH_CONFIG;
}

function withLaunchConfig(dbx: Record<string, unknown>, config?: McpLaunchConfig): Record<string, unknown> {
  const launch = launchConfig(config);
  dbx.command = launch.command;
  if (launch.args && launch.args.length > 0) {
    dbx.args = [...launch.args];
  }
  if (launch.env && Object.keys(launch.env).length > 0) {
    dbx.env = { ...launch.env };
  }
  return dbx;
}

function quotedStringArray(values: readonly string[]): string {
  return `[${values.map((value) => JSON.stringify(value)).join(", ")}]`;
}

export function mcpWebBackendUrl(origin: string, apiPath: string): string {
  return new URL(apiPath, origin).toString().replace(/\/api\/?$/, "");
}

export function buildMcpJsonConfig(config?: McpLaunchConfig): string {
  const dbx: Record<string, unknown> = {
    ...withLaunchConfig({}, config),
  };

  return JSON.stringify({ mcpServers: { dbx } }, null, 2);
}

export function buildMcpTraeConfig(config?: McpLaunchConfig, nativeBinPath?: string): string {
  return buildMcpJsonConfig(preferMcpNativeLaunch(config, nativeBinPath));
}

export function buildMcpQoderConfig(config?: McpLaunchConfig, nativeBinPath?: string): string {
  return buildMcpTraeConfig(config, nativeBinPath);
}

export function buildMcpVsCodeConfig(config?: McpLaunchConfig): string {
  const dbx: Record<string, unknown> = {
    type: "stdio",
    ...withLaunchConfig({}, config),
  };

  return JSON.stringify({ servers: { dbx } }, null, 2);
}

export function buildMcpCherryStudioConfig(config?: McpLaunchConfig): string {
  const launch = launchConfig(config);
  const dbx: Record<string, unknown> = {
    name: "dbx",
    description: "",
    baseUrl: "",
    command: launch.command,
    args: [...(launch.args ?? [])],
    env: { ...launch.env },
    isActive: true,
    type: "stdio",
  };

  return JSON.stringify({ mcpServers: { dbx } }, null, 2);
}

export function buildMcpCodexConfig(config?: McpLaunchConfig): string {
  const launch = launchConfig(config);
  const lines = ["[mcp_servers.dbx]", `command = ${JSON.stringify(launch.command)}`];

  if (launch.args && launch.args.length > 0) {
    lines.push(`args = ${quotedStringArray(launch.args)}`);
  }
  if (launch.env && Object.keys(launch.env).length > 0) {
    lines.push("", "[mcp_servers.dbx.env]");
    for (const [key, value] of Object.entries(launch.env)) {
      lines.push(`${key} = ${JSON.stringify(value)}`);
    }
  }

  return lines.join("\n");
}

export function buildMcpDeepSeekHarnessConfig(config?: McpLaunchConfig): string {
  const launch = launchConfig(config);
  const lines = ["- insert:", "    - id: mcp-dbx", "      name: '@deepseek-ai/dsh-mcp-client'", "      config:", "        serverName: dbx", "        transport: stdio", `        command: ${JSON.stringify(launch.command)}`];

  if (launch.args && launch.args.length > 0) {
    lines.push(`        args: ${quotedStringArray(launch.args)}`);
  }
  if (launch.env && Object.keys(launch.env).length > 0) {
    lines.push("        env:");
    for (const [key, value] of Object.entries(launch.env)) {
      lines.push(`          ${JSON.stringify(key)}: ${JSON.stringify(value)}`);
    }
  }

  return lines.join("\n");
}

export function buildMcpOpenCodeConfig(config?: McpLaunchConfig): string {
  const launch = launchConfig(config);
  const dbx: Record<string, unknown> = {
    type: "local",
    command: [launch.command, ...(launch.args ?? [])],
  };
  if (launch.env && Object.keys(launch.env).length > 0) {
    dbx.environment = { ...launch.env };
  }

  return JSON.stringify({ mcp: { dbx } }, null, 2);
}

export function buildMcpPiConfig(config?: McpLaunchConfig): string {
  return buildMcpJsonConfig(config);
}

export function buildMcpWorkBuddyConfig(config?: McpLaunchConfig): string {
  return buildMcpJsonConfig(config);
}