The read-only MCP tool refused the whole `seal-use tool <name>` area, because the 98-entry internal tool registry had no read/write classification. Now it does: SEAL_READ_ONLY_TOOLS (66) and SEAL_MUTATING_TOOLS (32), exported from src/domains/seal/tools.ts, with the policy delegating `tool` to them. The two sets are exhaustive by construction — a test asserts they partition sealTools exactly — so a newly added tool fails the suite until it is classified, rather than silently defaulting into one bucket. Classifying by inspection was not enough. Cross-checking mechanically (every exported function in src/domains/seal that performs a mutating HTTP call, mapped to the tools that invoke it) caught a real mistake shipped in #115: `analytics analyze` was allow-listed as read-only, but it calls seal_insight_record_analyze, which issues a POST and starts an analysis server-side. Removed from the CLI allow-list; a comment records why it is absent, since the name reads like a query. `approval-runs pick` went the other way into read-only: it dispatches to seal_approval_run_pick, which is a query over approval runs rather than a local-state mutation. Read-only means: reads tenant state, or writes only a local file. Anything that creates, updates, deletes, publishes, pushes, replays, uploads, activates or mints a credential counts as mutating. `seal_webhook_material_pack` is mutating because push mode sends the pack even though preview mode does not; `seal_approval_runs_export` and `seal_insight_export` are read-only because they only write a local workbook.
266 lines
10 KiB
TypeScript
266 lines
10 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { spawn } from "node:child_process";
|
|
import { existsSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import {
|
|
createServer,
|
|
resolveMcpEnabled,
|
|
SEAL_CLI_TOOL_NAME,
|
|
SEAL_CLI_WRITE_TOOL_NAME,
|
|
SEAL_MCP_ENABLED_ENV,
|
|
tokenizeCommand
|
|
} from "./server.js";
|
|
|
|
describe("seal-use MCP server", () => {
|
|
test("tokenizes quoted JSON without invoking a shell", () => {
|
|
expect(tokenizeCommand(
|
|
`seal-use tool seal_approval_search --json '{"keywords":["差旅 发票"]}' --corp corp-a`
|
|
)).toEqual([
|
|
"seal-use",
|
|
"tool",
|
|
"seal_approval_search",
|
|
"--json",
|
|
'{"keywords":["差旅 发票"]}',
|
|
"--corp",
|
|
"corp-a"
|
|
]);
|
|
});
|
|
|
|
test("defaults MCP tools to disabled and parses the plugin boolean setting", () => {
|
|
const original = process.env[SEAL_MCP_ENABLED_ENV];
|
|
delete process.env[SEAL_MCP_ENABLED_ENV];
|
|
|
|
try {
|
|
expect(resolveMcpEnabled()).toBe(false);
|
|
expect(resolveMcpEnabled("false")).toBe(false);
|
|
expect(resolveMcpEnabled("0")).toBe(false);
|
|
expect(resolveMcpEnabled("true")).toBe(true);
|
|
expect(resolveMcpEnabled("1")).toBe(true);
|
|
expect(resolveMcpEnabled("yes")).toBe(true);
|
|
} finally {
|
|
if (original === undefined) delete process.env[SEAL_MCP_ENABLED_ENV];
|
|
else process.env[SEAL_MCP_ENABLED_ENV] = original;
|
|
}
|
|
});
|
|
|
|
test("advertises the read-only and write tools and executes version through the CLI runner", async () => {
|
|
const server = createServer({ enableCli: true });
|
|
const client = new Client({ name: "seal-use-test", version: "1.0.0" });
|
|
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
|
|
try {
|
|
await Promise.all([
|
|
server.connect(serverTransport),
|
|
client.connect(clientTransport)
|
|
]);
|
|
|
|
const listed = await client.listTools();
|
|
expect(listed.tools.map((tool) => tool.name)).toEqual([SEAL_CLI_TOOL_NAME, SEAL_CLI_WRITE_TOOL_NAME]);
|
|
expect(listed.tools[0]?.inputSchema).toMatchObject({
|
|
type: "object",
|
|
required: ["command"],
|
|
additionalProperties: false
|
|
});
|
|
// The read-only tool promises it cannot mutate, so a client may auto-approve it.
|
|
expect(listed.tools[0]?.annotations).toMatchObject({
|
|
readOnlyHint: true,
|
|
destructiveHint: false,
|
|
openWorldHint: true
|
|
});
|
|
// The write tool can reach mutating verbs, so it must not be advertised as safe to auto-run
|
|
// or safe to retry.
|
|
expect(listed.tools[1]?.annotations).toMatchObject({
|
|
readOnlyHint: false,
|
|
destructiveHint: true,
|
|
idempotentHint: false,
|
|
openWorldHint: true
|
|
});
|
|
|
|
const result = await client.callTool({
|
|
name: SEAL_CLI_TOOL_NAME,
|
|
arguments: { command: "seal-use version" }
|
|
});
|
|
const content = result.content as Array<{ type: "text"; text: string }>;
|
|
const text = content[0];
|
|
expect(result.isError).not.toBe(true);
|
|
expect(text?.type).toBe("text");
|
|
if (text?.type === "text") {
|
|
expect(JSON.parse(text.text)).toMatchObject({ name: "seal-use" });
|
|
}
|
|
|
|
const invalid = await client.callTool({
|
|
name: SEAL_CLI_TOOL_NAME,
|
|
arguments: { command: "not-a-command" }
|
|
});
|
|
expect(invalid.isError).toBe(true);
|
|
const invalidContent = invalid.content as Array<{ type: "text"; text: string }>;
|
|
const errorText = invalidContent[0];
|
|
expect(errorText?.type).toBe("text");
|
|
if (errorText?.type === "text") {
|
|
expect(errorText.text).toContain(SEAL_CLI_WRITE_TOOL_NAME);
|
|
}
|
|
} finally {
|
|
await Promise.allSettled([client.close(), server.close()]);
|
|
}
|
|
});
|
|
|
|
test("the read-only tool refuses mutating and unclassified commands", async () => {
|
|
const server = createServer({ enableCli: true });
|
|
const client = new Client({ name: "seal-use-policy-test", version: "1.0.0" });
|
|
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
|
|
const callRead = async (command: string) => {
|
|
const result = await client.callTool({ name: SEAL_CLI_TOOL_NAME, arguments: { command } });
|
|
const content = result.content as Array<{ type: "text"; text: string }>;
|
|
return { isError: result.isError, text: content[0]?.text ?? "" };
|
|
};
|
|
|
|
try {
|
|
await Promise.all([
|
|
server.connect(serverTransport),
|
|
client.connect(clientTransport)
|
|
]);
|
|
|
|
for (const command of [
|
|
"seal-use corps mark corp-a --env production --confirm",
|
|
"seal-use rules push",
|
|
"seal-use simulation create --mode hosecloud",
|
|
"seal-use deploy hosecloud sync sign-key",
|
|
"seal-use users reset-password corp-a:u1",
|
|
"seal-use webhook replay",
|
|
"seal-use update",
|
|
"seal-use mcp",
|
|
"seal-use tool seal_approval_rule_create --json '{}'",
|
|
"seal-use tool seal_webhook_replay --json '{}'",
|
|
"seal-use totally-unknown-area list",
|
|
""
|
|
]) {
|
|
const refused = await callRead(command);
|
|
expect({ command, isError: refused.isError }).toEqual({ command, isError: true });
|
|
}
|
|
|
|
// Read-only commands must get past the policy; `version` is a top-level command with no
|
|
// registry row, and `corp` is an area alias the CLI dispatches as `corps`.
|
|
for (const command of ["seal-use version", "seal-use corps list", "seal-use corp list", "seal-use tool seal_approval_search --json '{}'"]) {
|
|
const allowed = await callRead(command);
|
|
expect({ command, refusedByPolicy: allowed.text.includes("only runs read-only commands") }).toEqual({
|
|
command,
|
|
refusedByPolicy: false
|
|
});
|
|
}
|
|
|
|
// A read-only command must survive the policy check and fail (if at all) for its own
|
|
// reasons — here the corp is unknown — never because the policy refused it.
|
|
const allowed = await callRead("seal-use approval-runs get does-not-exist --corp missing");
|
|
expect(allowed.text).not.toContain(`only runs read-only commands`);
|
|
} finally {
|
|
await Promise.allSettled([client.close(), server.close()]);
|
|
}
|
|
});
|
|
|
|
test("the write tool accepts mutating commands that the read-only tool refuses", async () => {
|
|
const server = createServer({ enableCli: true });
|
|
const client = new Client({ name: "seal-use-write-test", version: "1.0.0" });
|
|
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
|
|
try {
|
|
await Promise.all([
|
|
server.connect(serverTransport),
|
|
client.connect(clientTransport)
|
|
]);
|
|
|
|
const result = await client.callTool({
|
|
name: SEAL_CLI_WRITE_TOOL_NAME,
|
|
arguments: { command: "seal-use corps mark missing-corp --env production --confirm" }
|
|
});
|
|
const content = result.content as Array<{ type: "text"; text: string }>;
|
|
const text = content[0]?.text ?? "";
|
|
// It must get past the policy and reach the CLI, which then fails on its own terms.
|
|
expect(text).not.toContain("only runs read-only commands");
|
|
} finally {
|
|
await Promise.allSettled([client.close(), server.close()]);
|
|
}
|
|
});
|
|
|
|
test("does not advertise tools when MCP is disabled", async () => {
|
|
const server = createServer({ enableCli: false });
|
|
const client = new Client({ name: "seal-home-disabled-test", version: "1.0.0" });
|
|
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
|
|
try {
|
|
await Promise.all([
|
|
server.connect(serverTransport),
|
|
client.connect(clientTransport)
|
|
]);
|
|
|
|
const listed = await client.listTools();
|
|
expect(listed.tools).toEqual([]);
|
|
} finally {
|
|
await Promise.allSettled([client.close(), server.close()]);
|
|
}
|
|
});
|
|
});
|
|
|
|
// The plugin ships the bundled artifact and runs it with plain node, so a bundle that cannot even
|
|
// boot is invisible to the in-memory tests above (which exercise source, not the artifact). This
|
|
// guards the entry/module graph that bundling is sensitive to.
|
|
describe("packaged MCP bundle", () => {
|
|
test("dist/mcp/server.js boots and completes a handshake", async () => {
|
|
const bundle = join(import.meta.dir, "..", "dist", "mcp", "server.js");
|
|
if (!existsSync(bundle)) {
|
|
throw new Error("dist/mcp/server.js is missing. Run 'bun run build:plugin' before testing.");
|
|
}
|
|
|
|
const child = spawn(process.execPath, [bundle], {
|
|
env: { ...process.env, [SEAL_MCP_ENABLED_ENV]: "true" },
|
|
stdio: ["pipe", "pipe", "pipe"]
|
|
});
|
|
|
|
const messages: any[] = [];
|
|
let stdout = "";
|
|
let stderr = "";
|
|
child.stdout.on("data", (chunk) => {
|
|
stdout += chunk.toString();
|
|
let newline;
|
|
while ((newline = stdout.indexOf("\n")) >= 0) {
|
|
const line = stdout.slice(0, newline).trim();
|
|
stdout = stdout.slice(newline + 1);
|
|
if (line) messages.push(JSON.parse(line));
|
|
}
|
|
});
|
|
child.stderr.on("data", (chunk) => { stderr += chunk.toString(); });
|
|
|
|
const waitForId = async (id: number) => {
|
|
for (let i = 0; i < 200; i++) {
|
|
const found = messages.find((m) => m.id === id);
|
|
if (found) return found;
|
|
await new Promise((r) => setTimeout(r, 50));
|
|
}
|
|
return undefined;
|
|
};
|
|
const send = (msg: unknown) => child.stdin.write(`${JSON.stringify(msg)}\n`);
|
|
|
|
try {
|
|
await new Promise((r) => setTimeout(r, 500));
|
|
send({
|
|
jsonrpc: "2.0",
|
|
id: 1,
|
|
method: "initialize",
|
|
params: { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "bundle-test", version: "1.0.0" } }
|
|
});
|
|
const initialized = await waitForId(1);
|
|
expect(stderr + JSON.stringify(initialized)).not.toContain("is not a constructor");
|
|
expect(initialized?.result?.serverInfo?.name).toBe("seal-use");
|
|
|
|
send({ jsonrpc: "2.0", method: "notifications/initialized" });
|
|
send({ jsonrpc: "2.0", id: 2, method: "tools/list" });
|
|
const listed = await waitForId(2);
|
|
expect(listed?.result?.tools?.map((t: any) => t.name)).toEqual([SEAL_CLI_TOOL_NAME, SEAL_CLI_WRITE_TOOL_NAME]);
|
|
} finally {
|
|
child.kill("SIGTERM");
|
|
await new Promise((r) => setTimeout(r, 100));
|
|
}
|
|
}, 30000);
|
|
});
|