seal_cli could reach every CLI command, so its tool annotations had to be
all-or-nothing: destructiveHint true on a tool that mostly reads, which meant
clients could not auto-approve anything, and there was no way to gate the
mutating minority. Split it in two:
- seal_cli read-only, readOnlyHint true / destructiveHint false.
- seal_cli_write mutations, publishing, credential minting, self-update and
server commands; destructiveHint true.
The boundary is enforced rather than advertised. seal_cli runs only commands
on an explicit allow-list (84 entries, src/cli/read-only.ts) and fails closed
on anything unclassified, including commands added to the CLI later. Matching
reuses the CLI's own parseArgs and compares whole tokens, so a read prefix
cannot smuggle a mutation (corps listing is refused) and a bare allow-listed
command does not cover its subcommands. Refusals name seal_cli_write.
Deliberately excluded from the read-only set even though they look harmless:
anything writing local config (corps switch, config test-dir), minting or
printing credentials (auth keys, corps extract-keys), self-updating (update),
or standing up a long-running server (http, mcp, service run). seal-use
tool <name> is excluded too, since the 98-entry internal tool registry is not
itself classified.
A test asserts every allow-listed command still exists in the help registry,
so the list cannot silently rot.
Also, per the note that MCP is an optional, default-off path, the skill now
says so explicitly: the CLI is primary, a missing seal_cli* tool is the normal
default rather than a fault, and enabling it is the user's choice in the plugin
settings. Corrected skill docs that presented internal tool-registry names
(seal_webhook_get, seal_webhook_update, ...) as if they were MCP tools; the MCP
server exposes only the two seal_cli* tools.
CI: the MCP build step now boots the freshly built bundle and completes a
handshake, instead of only checking that it compiles. A bundle that cannot
start is invisible to source-level tests, which is how the previous unbootable
entry point shipped. No byte-for-byte dist diff gate here: the committed
bundle is not byte-reproducible with the current toolchain (~186 lines of drift
rebuilding the unmodified release commit), and such a gate would not have
caught that bug anyway, since the broken bundle was in sync with its source.
215 lines
6.9 KiB
TypeScript
215 lines
6.9 KiB
TypeScript
import { dirname, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { McpServer, type CallToolResult } from "@modelcontextprotocol/server";
|
|
import { serveStdio, type StdioServerHandle } from "@modelcontextprotocol/server/stdio";
|
|
import { z } from "zod";
|
|
import { runCli } from "./cli/runner.js";
|
|
import { isReadOnlyCliCommand } from "./cli/read-only.js";
|
|
import { normalizeCliArgs, tokenizeCommand } from "./cli/tokenize.js";
|
|
import { classifyCliError } from "./cli/errors.js";
|
|
import { VERSION } from "./version.js";
|
|
|
|
const serverEntryFile = fileURLToPath(import.meta.url);
|
|
const pluginRoot = serverEntryFile.includes("/dist/mcp/") || serverEntryFile.includes("\\dist\\mcp\\")
|
|
? dirname(dirname(dirname(serverEntryFile)))
|
|
: dirname(dirname(serverEntryFile));
|
|
|
|
export const SEAL_CLI_TOOL_NAME = "seal_cli";
|
|
export const SEAL_CLI_WRITE_TOOL_NAME = "seal_cli_write";
|
|
export const SEAL_MCP_ENABLED_ENV = "SEAL_USE_MCP_ENABLED";
|
|
export const LEGACY_SEAL_MCP_ENABLED_ENV = "SEAL_HOME_MCP_ENABLED";
|
|
|
|
export function resolveMcpEnabled(
|
|
value = process.env[SEAL_MCP_ENABLED_ENV] ?? process.env[LEGACY_SEAL_MCP_ENABLED_ENV]
|
|
): boolean {
|
|
if (value === undefined) return false;
|
|
return ["1", "true", "yes", "on"].includes(value.trim().toLowerCase());
|
|
}
|
|
|
|
export function createServer(options: { enableCli?: boolean } = {}): McpServer {
|
|
const enableCli = options.enableCli ?? true;
|
|
const server = new McpServer(
|
|
{
|
|
name: "seal-use",
|
|
version: VERSION
|
|
},
|
|
{
|
|
// Declared explicitly so that `tools/list` answers with an empty list (rather than a
|
|
// method-not-found error) when seal_cli is switched off by configuration.
|
|
capabilities: { tools: {} }
|
|
}
|
|
);
|
|
|
|
if (enableCli) {
|
|
const inputSchema = z.strictObject({
|
|
command: z
|
|
.string()
|
|
.describe("A seal-use CLI command, with or without the leading 'seal-use' or 'seal-home'.")
|
|
});
|
|
|
|
server.registerTool(
|
|
SEAL_CLI_TOOL_NAME,
|
|
{
|
|
title: "Run read-only seal-use CLI command",
|
|
description:
|
|
"Run a read-only seal-use CLI command and return its JSON stdout. Only commands that " +
|
|
"read tenant state without changing it are accepted here; anything that mutates tenant " +
|
|
"state, publishes configuration, mints credentials, self-updates or starts a server is " +
|
|
`refused — use ${SEAL_CLI_WRITE_TOOL_NAME} for those after confirming with the user.`,
|
|
inputSchema,
|
|
annotations: {
|
|
readOnlyHint: true,
|
|
destructiveHint: false,
|
|
idempotentHint: true,
|
|
openWorldHint: true
|
|
}
|
|
},
|
|
async ({ command }): Promise<CallToolResult> =>
|
|
executeCliCommand(command, { requireReadOnly: true })
|
|
);
|
|
|
|
server.registerTool(
|
|
SEAL_CLI_WRITE_TOOL_NAME,
|
|
{
|
|
title: "Run mutating seal-use CLI command",
|
|
description:
|
|
"Run a seal-use CLI command that changes tenant state, publishes configuration, or sends " +
|
|
"things outward (create/update/push/mark/bind/sync/deploy/delete/reset-password, rule " +
|
|
"publishing, simulation batches, self-update). Confirm the exact command with the user " +
|
|
"before calling this tool.",
|
|
inputSchema,
|
|
annotations: {
|
|
readOnlyHint: false,
|
|
destructiveHint: true,
|
|
idempotentHint: false,
|
|
openWorldHint: true
|
|
}
|
|
},
|
|
async ({ command }): Promise<CallToolResult> => executeCliCommand(command, { requireReadOnly: false })
|
|
);
|
|
}
|
|
|
|
return server;
|
|
}
|
|
|
|
async function executeCliCommand(
|
|
command: string | undefined,
|
|
options: { requireReadOnly: boolean }
|
|
): Promise<CallToolResult> {
|
|
const toolName = options.requireReadOnly ? SEAL_CLI_TOOL_NAME : SEAL_CLI_WRITE_TOOL_NAME;
|
|
if (!command?.trim()) {
|
|
return cliResult({
|
|
stdout: "",
|
|
stderr: `${toolName} requires a non-empty command string`,
|
|
isError: true,
|
|
exitCode: 2
|
|
});
|
|
}
|
|
|
|
let cliArgs: string[];
|
|
try {
|
|
cliArgs = normalizeCliArgs(tokenizeCommand(command));
|
|
if (!cliArgs.length) {
|
|
throw new Error(`${toolName} requires a non-empty command string`);
|
|
}
|
|
} catch (error) {
|
|
const classification = classifyCliError(error);
|
|
return cliResult({
|
|
stdout: "",
|
|
stderr: error instanceof Error ? error.message : String(error),
|
|
isError: true,
|
|
exitCode: classification.exitCode
|
|
});
|
|
}
|
|
|
|
// Enforced, not just annotated: a client that auto-approves the read-only tool must not be able
|
|
// to reach a mutating command through it, and unclassified commands fail closed.
|
|
if (options.requireReadOnly && !isReadOnlyCliCommand(cliArgs)) {
|
|
return cliResult({
|
|
stdout: "",
|
|
stderr:
|
|
`${SEAL_CLI_TOOL_NAME} only runs read-only commands, and "${cliArgs.join(" ")}" is not one of ` +
|
|
`them. Confirm the command with the user, then call ${SEAL_CLI_WRITE_TOOL_NAME}.`,
|
|
isError: true,
|
|
exitCode: 2
|
|
});
|
|
}
|
|
|
|
try {
|
|
return cliResult(await runCli(cliArgs, {
|
|
appRoot: pluginRoot,
|
|
entryFile: serverEntryFile,
|
|
launcher: {
|
|
command: process.execPath,
|
|
args: [serverEntryFile, "--seal-home-cli"]
|
|
}
|
|
}));
|
|
} catch (error) {
|
|
const classification = classifyCliError(error);
|
|
return cliResult({
|
|
stdout: "",
|
|
stderr: error instanceof Error ? error.message : String(error),
|
|
isError: true,
|
|
exitCode: classification.exitCode
|
|
});
|
|
}
|
|
}
|
|
|
|
export function startServer(): StdioServerHandle {
|
|
const enableCli = resolveMcpEnabled();
|
|
const handle = serveStdio(() => createServer({ enableCli }));
|
|
console.error(
|
|
enableCli
|
|
? "seal-use MCP server running on stdio"
|
|
: "seal-use MCP server connected with seal_cli disabled by configuration"
|
|
);
|
|
return handle;
|
|
}
|
|
|
|
export { tokenizeCommand };
|
|
|
|
function cliResult(result: { stdout: string; stderr: string; isError: boolean; exitCode: number }): CallToolResult {
|
|
const text = result.isError ? result.stderr || result.stdout : result.stdout;
|
|
return {
|
|
content: [
|
|
{
|
|
type: "text" as const,
|
|
text
|
|
}
|
|
],
|
|
isError: result.isError
|
|
};
|
|
}
|
|
|
|
async function runEmbeddedCli() {
|
|
const result = await runCli(process.argv.slice(3), {
|
|
appRoot: pluginRoot,
|
|
entryFile: serverEntryFile,
|
|
launcher: {
|
|
command: process.execPath,
|
|
args: [serverEntryFile, "--seal-use-cli"]
|
|
}
|
|
});
|
|
if (result.stdout) process.stdout.write(result.stdout);
|
|
if (result.stderr) process.stderr.write(`${result.stderr}${result.stderr.endsWith("\n") ? "" : "\n"}`);
|
|
process.exitCode = result.exitCode;
|
|
}
|
|
|
|
const isMainModule = Boolean(
|
|
(import.meta as any).main ||
|
|
(!import.meta.url.includes("$bunfs") && process.argv[1] && resolve(process.argv[1]) === resolve(serverEntryFile))
|
|
);
|
|
|
|
if (isMainModule) {
|
|
if (process.argv[2] === "--seal-use-cli" || process.argv[2] === "--seal-home-cli") {
|
|
await runEmbeddedCli();
|
|
} else {
|
|
try {
|
|
startServer();
|
|
} catch (error) {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
}
|
|
}
|
|
}
|