fix(mcp): pass request database to backend
This commit is contained in:
1 parent
bd727c32d0
commit
8d041b438b
2 files changed
+36
-8
No files matched your search
@@ -27,6 +27,10 @@ function mdTable(headers: string[], rows: string[][]): string {
|
|||||||
return `${header}\n${sep}\n${body}`;
|
return `${header}\n${sep}\n${body}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function withDatabase(config: ConnectionConfig, database?: string): ConnectionConfig {
|
||||||
|
return database === undefined ? config : { ...config, database };
|
||||||
|
}
|
||||||
|
|
||||||
export const DBX_CONNECTION_TYPE_DESCRIPTION =
|
export const DBX_CONNECTION_TYPE_DESCRIPTION =
|
||||||
"Database type: postgres, mysql, sqlite, redis, duckdb, clickhouse, sqlserver, mongodb, oracle, elasticsearch, doris, starrocks, redshift, dameng, kingbase, highgo, vastbase, goldendb, gaussdb, yashandb, databricks, saphana, teradata, vertica, firebird, exasol, opengauss, oceanbase-oracle, gbase, h2, snowflake, trino, hive, db2, informix, neo4j, cassandra, bigquery, kylin, sundb, tdengine, jdbc, access";
|
"Database type: postgres, mysql, sqlite, redis, duckdb, clickhouse, sqlserver, mongodb, oracle, elasticsearch, doris, starrocks, redshift, dameng, kingbase, highgo, vastbase, goldendb, gaussdb, yashandb, databricks, saphana, teradata, vertica, firebird, exasol, opengauss, oceanbase-oracle, gbase, h2, snowflake, trino, hive, db2, informix, neo4j, cassandra, bigquery, kylin, sundb, tdengine, jdbc, access";
|
||||||
|
|
||||||
@@ -49,12 +53,13 @@ export function createDbxMcpServer(backend: Backend, options: { isWebMode?: bool
|
|||||||
"List tables and views for a database connection",
|
"List tables and views for a database connection",
|
||||||
{
|
{
|
||||||
connection_name: z.string().describe("Name of the DBX connection"),
|
connection_name: z.string().describe("Name of the DBX connection"),
|
||||||
|
database: z.string().optional().describe("Database name"),
|
||||||
schema: z.string().optional().describe("Schema name (default: public for PostgreSQL)"),
|
schema: z.string().optional().describe("Schema name (default: public for PostgreSQL)"),
|
||||||
},
|
},
|
||||||
async ({ connection_name, schema }) => {
|
async ({ connection_name, database, schema }) => {
|
||||||
const config = await backend.findConnection(connection_name);
|
const config = await backend.findConnection(connection_name);
|
||||||
if (!config) return text(`Connection "${connection_name}" not found`);
|
if (!config) return text(`Connection "${connection_name}" not found`);
|
||||||
const tables = await backend.listTables(config, schema);
|
const tables = await backend.listTables(withDatabase(config, database), schema);
|
||||||
if (tables.length === 0) return text("No tables found.");
|
if (tables.length === 0) return text("No tables found.");
|
||||||
const rows = tables.map((t) => [t.name, t.type]);
|
const rows = tables.map((t) => [t.name, t.type]);
|
||||||
return text(mdTable(["Table", "Type"], rows));
|
return text(mdTable(["Table", "Type"], rows));
|
||||||
@@ -67,12 +72,13 @@ export function createDbxMcpServer(backend: Backend, options: { isWebMode?: bool
|
|||||||
{
|
{
|
||||||
connection_name: z.string().describe("Name of the DBX connection"),
|
connection_name: z.string().describe("Name of the DBX connection"),
|
||||||
table: z.string().describe("Table name"),
|
table: z.string().describe("Table name"),
|
||||||
|
database: z.string().optional().describe("Database name"),
|
||||||
schema: z.string().optional().describe("Schema name (default: public for PostgreSQL)"),
|
schema: z.string().optional().describe("Schema name (default: public for PostgreSQL)"),
|
||||||
},
|
},
|
||||||
async ({ connection_name, table, schema }) => {
|
async ({ connection_name, table, database, schema }) => {
|
||||||
const config = await backend.findConnection(connection_name);
|
const config = await backend.findConnection(connection_name);
|
||||||
if (!config) return text(`Connection "${connection_name}" not found`);
|
if (!config) return text(`Connection "${connection_name}" not found`);
|
||||||
const columns = await backend.describeTable(config, table, schema);
|
const columns = await backend.describeTable(withDatabase(config, database), table, schema);
|
||||||
if (columns.length === 0) return text("No columns found.");
|
if (columns.length === 0) return text("No columns found.");
|
||||||
const rows = columns.map((c) => [
|
const rows = columns.map((c) => [
|
||||||
c.is_primary_key ? `${c.name} (PK)` : c.name,
|
c.is_primary_key ? `${c.name} (PK)` : c.name,
|
||||||
@@ -90,15 +96,16 @@ export function createDbxMcpServer(backend: Backend, options: { isWebMode?: bool
|
|||||||
"Execute a SQL query on a database connection (max 100 rows returned)",
|
"Execute a SQL query on a database connection (max 100 rows returned)",
|
||||||
{
|
{
|
||||||
connection_name: z.string().describe("Name of the DBX connection"),
|
connection_name: z.string().describe("Name of the DBX connection"),
|
||||||
|
database: z.string().optional().describe("Database name"),
|
||||||
sql: z.string().describe("SQL query to execute"),
|
sql: z.string().describe("SQL query to execute"),
|
||||||
},
|
},
|
||||||
async ({ connection_name, sql }) => {
|
async ({ connection_name, database, sql }) => {
|
||||||
const config = await backend.findConnection(connection_name);
|
const config = await backend.findConnection(connection_name);
|
||||||
if (!config) return text(`Connection "${connection_name}" not found`);
|
if (!config) return text(`Connection "${connection_name}" not found`);
|
||||||
const safety = evaluateSqlSafety(sql, sqlSafetyFromEnv());
|
const safety = evaluateSqlSafety(sql, sqlSafetyFromEnv());
|
||||||
if (!safety.allowed) return text(`Query blocked: ${safety.reason}`);
|
if (!safety.allowed) return text(`Query blocked: ${safety.reason}`);
|
||||||
try {
|
try {
|
||||||
const result = await backend.executeQuery(config, sql);
|
const result = await backend.executeQuery(withDatabase(config, database), sql);
|
||||||
if (result.columns.length === 0) return text(`Query executed. ${result.row_count} row(s) affected.`);
|
if (result.columns.length === 0) return text(`Query executed. ${result.row_count} row(s) affected.`);
|
||||||
const rows = result.rows.map((r) => result.columns.map((c) => formatCell(r[c])));
|
const rows = result.rows.map((r) => result.columns.map((c) => formatCell(r[c])));
|
||||||
return text(`${mdTable(result.columns, rows)}\n\n${result.row_count} row(s)`);
|
return text(`${mdTable(result.columns, rows)}\n\n${result.row_count} row(s)`);
|
||||||
@@ -114,14 +121,15 @@ export function createDbxMcpServer(backend: Backend, options: { isWebMode?: bool
|
|||||||
"Get compact table and column context for writing SQL",
|
"Get compact table and column context for writing SQL",
|
||||||
{
|
{
|
||||||
connection_name: z.string().describe("Name of the DBX connection"),
|
connection_name: z.string().describe("Name of the DBX connection"),
|
||||||
|
database: z.string().optional().describe("Database name"),
|
||||||
schema: z.string().optional().describe("Schema name (default: public for PostgreSQL)"),
|
schema: z.string().optional().describe("Schema name (default: public for PostgreSQL)"),
|
||||||
tables: z.array(z.string()).optional().describe("Specific table names to include"),
|
tables: z.array(z.string()).optional().describe("Specific table names to include"),
|
||||||
max_tables: z.number().int().min(1).max(20).default(8).describe("Maximum number of tables to include"),
|
max_tables: z.number().int().min(1).max(20).default(8).describe("Maximum number of tables to include"),
|
||||||
},
|
},
|
||||||
async ({ connection_name, schema, tables, max_tables }) => {
|
async ({ connection_name, database, schema, tables, max_tables }) => {
|
||||||
const config = await backend.findConnection(connection_name);
|
const config = await backend.findConnection(connection_name);
|
||||||
if (!config) return text(`Connection "${connection_name}" not found`);
|
if (!config) return text(`Connection "${connection_name}" not found`);
|
||||||
const context = await buildSchemaContext(backend, config, { schema, tables, maxTables: max_tables });
|
const context = await buildSchemaContext(backend, withDatabase(config, database), { schema, tables, maxTables: max_tables });
|
||||||
if (context.tables.length === 0) return text("No matching tables found.");
|
if (context.tables.length === 0) return text("No matching tables found.");
|
||||||
return text(formatSchemaContext(context));
|
return text(formatSchemaContext(context));
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -33,3 +33,23 @@ test("creates an MCP server without starting stdio transport", () => {
|
|||||||
|
|
||||||
assert.equal(typeof server.connect, "function");
|
assert.equal(typeof server.connect, "function");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("execute query scopes the connection to the requested database", async () => {
|
||||||
|
let usedDatabase = "";
|
||||||
|
const scopedBackend: Backend = {
|
||||||
|
...backend,
|
||||||
|
executeQuery: async (config) => {
|
||||||
|
usedDatabase = config.database || "";
|
||||||
|
return { columns: ["total"], rows: [{ total: 1 }], row_count: 1 };
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const server = createDbxMcpServer(scopedBackend, { isWebMode: true });
|
||||||
|
|
||||||
|
await (server as any)._registeredTools.dbx_execute_query.handler({
|
||||||
|
connection_name: "local",
|
||||||
|
database: "stores_demo",
|
||||||
|
sql: "SELECT FIRST 1 tabname FROM systables",
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.equal(usedDatabase, "stores_demo");
|
||||||
|
});
|
||||||
Reference in new issue
Block a user