fix(packages): polish cli and mcp package contracts
This commit is contained in:
1 parent
2a696be49a
commit
c467372fc8
9 files changed
+154
-45
No files matched your search
@@ -1,7 +1,10 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtemp, rm, readFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
import type { Backend, ConnectionConfig } from "@dbx-app/node-core";
|
||||
import { createDbxMcpServer } from "../src/index.js";
|
||||
import { createDbxMcpServer, DBX_MCP_PACKAGE_VERSION } from "../src/index.js";
|
||||
|
||||
const connection: ConnectionConfig = {
|
||||
id: "1",
|
||||
@@ -34,6 +37,23 @@ test("creates an MCP server without starting stdio transport", () => {
|
||||
assert.equal(typeof server.connect, "function");
|
||||
});
|
||||
|
||||
test("MCP server metadata version matches package metadata", () => {
|
||||
const server = createDbxMcpServer(backend, { isWebMode: true });
|
||||
|
||||
assert.equal((server as any).server._serverInfo.version, DBX_MCP_PACKAGE_VERSION);
|
||||
});
|
||||
|
||||
test("README runtime requirements match package engines", async () => {
|
||||
const packageJson = JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf-8")) as {
|
||||
engines: { node: string };
|
||||
};
|
||||
const readme = await readFile(new URL("../README.md", import.meta.url), "utf-8");
|
||||
const minimumNodeVersion = packageJson.engines.node.replace(">=", "");
|
||||
|
||||
assert.match(readme, new RegExp(`Node\\.js ${minimumNodeVersion.replace(/\./g, "\\.")} or newer`));
|
||||
assert.match(readme, new RegExp(`Node\\.js ${minimumNodeVersion.replace(/\./g, "\\.")} 或更高版本`));
|
||||
});
|
||||
|
||||
test("execute query scopes the connection to the requested database", async () => {
|
||||
let usedDatabase = "";
|
||||
const scopedBackend: Backend = {
|
||||
@@ -104,7 +124,7 @@ test("mongodb execute query formats shell-style find results", async () => {
|
||||
const scopedBackend: Backend = {
|
||||
...backend,
|
||||
findConnection: async () => mongoConnection,
|
||||
executeQuery: async () => ({ columns: ["_id", "name"], rows: [{ _id: "1", name: "demo" }], row_count: 1 }),
|
||||
executeQuery: async () => ({ columns: ["_id", "meta", "missing"], rows: [{ _id: "1", meta: { name: "demo" }, missing: null }], row_count: 1 }),
|
||||
};
|
||||
const server = createDbxMcpServer(scopedBackend, { isWebMode: true });
|
||||
|
||||
@@ -114,10 +134,76 @@ test("mongodb execute query formats shell-style find results", async () => {
|
||||
sql: "db.projects.find({}).limit(1)",
|
||||
});
|
||||
|
||||
assert.match(result.content[0].text, /demo/);
|
||||
assert.match(result.content[0].text, /"name":"demo"/);
|
||||
assert.match(result.content[0].text, /NULL/);
|
||||
assert.match(result.content[0].text, /1 row\(s\)/);
|
||||
});
|
||||
|
||||
test("connection lookup failures include a stable MCP error code", async () => {
|
||||
const server = createDbxMcpServer(backend, { isWebMode: true });
|
||||
|
||||
const result = await (server as any)._registeredTools.dbx_list_tables.handler({
|
||||
connection_name: "missing",
|
||||
});
|
||||
|
||||
assert.equal(result.isError, true);
|
||||
assert.match(result.content[0].text, /CONNECTION_NOT_FOUND:/);
|
||||
assert.match(result.content[0].text, /missing/);
|
||||
});
|
||||
|
||||
test("SQL safety failures include a stable MCP error code", async () => {
|
||||
const server = createDbxMcpServer(backend, { isWebMode: true });
|
||||
|
||||
const result = await (server as any)._registeredTools.dbx_execute_query.handler({
|
||||
connection_name: "local",
|
||||
sql: "drop table users",
|
||||
});
|
||||
|
||||
assert.equal(result.isError, true);
|
||||
assert.match(result.content[0].text, /SQL_BLOCKED:/);
|
||||
assert.match(result.content[0].text, /Dangerous SQL/);
|
||||
});
|
||||
|
||||
test("query exceptions include a stable MCP error code", async () => {
|
||||
const scopedBackend: Backend = {
|
||||
...backend,
|
||||
executeQuery: async () => {
|
||||
throw new Error("database timeout");
|
||||
},
|
||||
};
|
||||
const server = createDbxMcpServer(scopedBackend, { isWebMode: true });
|
||||
|
||||
const result = await (server as any)._registeredTools.dbx_execute_query.handler({
|
||||
connection_name: "local",
|
||||
sql: "select 1",
|
||||
});
|
||||
|
||||
assert.equal(result.isError, true);
|
||||
assert.match(result.content[0].text, /QUERY_ERROR: database timeout/);
|
||||
});
|
||||
|
||||
test("desktop bridge failures include a stable MCP error code", async () => {
|
||||
const oldHome = process.env.HOME;
|
||||
const dir = await mkdtemp(join(tmpdir(), "dbx-mcp-home-"));
|
||||
process.env.HOME = dir;
|
||||
|
||||
try {
|
||||
const server = createDbxMcpServer(backend, { isWebMode: false });
|
||||
const result = await (server as any)._registeredTools.dbx_open_table.handler({
|
||||
connection_name: "local",
|
||||
table: "users",
|
||||
});
|
||||
|
||||
assert.equal(result.isError, true);
|
||||
assert.match(result.content[0].text, /DBX_NOT_RUNNING:/);
|
||||
assert.match(result.content[0].text, /DBX is not running/);
|
||||
} finally {
|
||||
if (oldHome === undefined) delete process.env.HOME;
|
||||
else process.env.HOME = oldHome;
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("mongodb execute-and-show blocks aggregate write stages before desktop bridge", async () => {
|
||||
const oldAllowWrites = process.env.DBX_MCP_ALLOW_WRITES;
|
||||
const oldAllowDangerous = process.env.DBX_MCP_ALLOW_DANGEROUS_SQL;
|
||||
@@ -137,7 +223,7 @@ test("mongodb execute-and-show blocks aggregate write stages before desktop brid
|
||||
sql: 'db.projects.aggregate([{"$out":"projects_dump"}])',
|
||||
});
|
||||
|
||||
assert.match(result.content[0].text, /Query blocked:/);
|
||||
assert.match(result.content[0].text, /SQL_BLOCKED:/);
|
||||
assert.match(result.content[0].text, /DBX_MCP_ALLOW_WRITES=1/);
|
||||
} finally {
|
||||
if (oldAllowWrites === undefined) delete process.env.DBX_MCP_ALLOW_WRITES;
|
||||
|
||||
Reference in new issue
Block a user