sentinelayer-cli 0.30.13 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,231 @@
1
+ import { buildCliProgram } from "../cli.js";
2
+ import { MCP_TOOL_REGISTRY_SCHEMA_VERSION } from "./registry.js";
3
+
4
+ const CLI_TOOL_PREFIX = "sl";
5
+ const CLI_TOOL_TIMEOUT_MS = 60_000;
6
+ const CLI_TOOL_MAX_CALLS_PER_RUN = 5;
7
+
8
+ function normalizeDescription(value, fallback) {
9
+ const normalized = String(value || "").trim();
10
+ return normalized || fallback;
11
+ }
12
+
13
+ function toTitle(value) {
14
+ return String(value || "")
15
+ .split(/[\s_.:-]+/g)
16
+ .filter(Boolean)
17
+ .map((part) => part.slice(0, 1).toUpperCase() + part.slice(1))
18
+ .join(" ");
19
+ }
20
+
21
+ function optionPropertyName(option) {
22
+ if (typeof option.attributeName === "function") {
23
+ return option.attributeName();
24
+ }
25
+ const long = String(option.long || option.flags || "")
26
+ .split(/[ ,|]+/g)
27
+ .find((part) => part.startsWith("--"));
28
+ return String(long || "")
29
+ .replace(/^--no-/, "")
30
+ .replace(/^--/, "")
31
+ .replace(/-([a-zA-Z0-9])/g, (_, char) => char.toUpperCase());
32
+ }
33
+
34
+ function schemaForArgument(argument) {
35
+ const description = normalizeDescription(argument.description, "");
36
+ if (argument.variadic) {
37
+ return {
38
+ type: "array",
39
+ items: { type: "string" },
40
+ ...(description ? { description } : {}),
41
+ };
42
+ }
43
+ return {
44
+ type: "string",
45
+ ...(description ? { description } : {}),
46
+ };
47
+ }
48
+
49
+ function optionTakesValue(option) {
50
+ return Boolean(option.required || option.optional || option.variadic);
51
+ }
52
+
53
+ function schemaForOption(option) {
54
+ const description = normalizeDescription(option.description, "");
55
+ let schema;
56
+ if (option.variadic) {
57
+ schema = {
58
+ type: "array",
59
+ items: { type: "string" },
60
+ };
61
+ } else if (optionTakesValue(option)) {
62
+ schema = { type: "string" };
63
+ } else {
64
+ schema = { type: "boolean" };
65
+ }
66
+
67
+ if (description) {
68
+ schema.description = description;
69
+ }
70
+ if (
71
+ option.defaultValue !== undefined &&
72
+ ["string", "number", "boolean"].includes(typeof option.defaultValue)
73
+ ) {
74
+ schema.default = option.defaultValue;
75
+ }
76
+ return schema;
77
+ }
78
+
79
+ function summarizeArgument(argument) {
80
+ return {
81
+ name: argument.name(),
82
+ required: Boolean(argument.required),
83
+ variadic: Boolean(argument.variadic),
84
+ };
85
+ }
86
+
87
+ function summarizeOption(option) {
88
+ return {
89
+ name: optionPropertyName(option),
90
+ flags: option.flags,
91
+ required: Boolean(option.mandatory),
92
+ takes_value: optionTakesValue(option),
93
+ variadic: Boolean(option.variadic),
94
+ negated: Boolean(option.negate),
95
+ };
96
+ }
97
+
98
+ function isHiddenCommand(command) {
99
+ return Boolean(command.hidden);
100
+ }
101
+
102
+ function commandPath(command) {
103
+ const names = [];
104
+ let current = command;
105
+ while (current && current.parent) {
106
+ names.unshift(current.name());
107
+ current = current.parent;
108
+ }
109
+ return names;
110
+ }
111
+
112
+ function collectLeafCommands(command, { includeHidden = false } = {}) {
113
+ const leaves = [];
114
+ for (const child of command.commands || []) {
115
+ if (!includeHidden && isHiddenCommand(child)) {
116
+ continue;
117
+ }
118
+ if (child.commands && child.commands.length > 0) {
119
+ leaves.push(...collectLeafCommands(child, { includeHidden }));
120
+ continue;
121
+ }
122
+ leaves.push(child);
123
+ }
124
+ return leaves;
125
+ }
126
+
127
+ function buildToolInputSchema(command) {
128
+ const properties = {};
129
+ const required = [];
130
+
131
+ for (const argument of command.registeredArguments || []) {
132
+ const name = argument.name();
133
+ properties[name] = schemaForArgument(argument);
134
+ if (argument.required) {
135
+ required.push(name);
136
+ }
137
+ }
138
+
139
+ for (const option of command.options || []) {
140
+ const name = optionPropertyName(option);
141
+ if (!name || properties[name]) {
142
+ continue;
143
+ }
144
+ properties[name] = schemaForOption(option);
145
+ if (option.mandatory) {
146
+ required.push(name);
147
+ }
148
+ }
149
+
150
+ return {
151
+ type: "object",
152
+ additionalProperties: false,
153
+ ...(required.length > 0 ? { required } : {}),
154
+ properties,
155
+ };
156
+ }
157
+
158
+ function buildCliTool(command) {
159
+ const pathParts = commandPath(command);
160
+ const commandName = pathParts.join(" ");
161
+ const dottedName = pathParts.join(".");
162
+ const toolName = `${CLI_TOOL_PREFIX}.${dottedName}`;
163
+ return {
164
+ name: toolName,
165
+ title: toTitle(toolName),
166
+ description: normalizeDescription(
167
+ command.description(),
168
+ `Run sentinelayer-cli ${commandName}.`
169
+ ),
170
+ input_schema: buildToolInputSchema(command),
171
+ transport: {
172
+ type: "bridge",
173
+ method: "POST",
174
+ url: `sentinelayer://cli/${dottedName}`,
175
+ timeout_ms: CLI_TOOL_TIMEOUT_MS,
176
+ auth: { mode: "none" },
177
+ },
178
+ budgets: {
179
+ max_calls_per_run: CLI_TOOL_MAX_CALLS_PER_RUN,
180
+ max_runtime_ms: CLI_TOOL_TIMEOUT_MS,
181
+ },
182
+ security: {
183
+ requires_human_approval: true,
184
+ kill_switch: "enabled",
185
+ scopes: ["cli:execute"],
186
+ },
187
+ metadata: {
188
+ provider: "sentinelayer",
189
+ adapter: "sentinelayer-cli",
190
+ command: commandName,
191
+ argv: pathParts,
192
+ generated_from: "commander",
193
+ execution: "bridge",
194
+ arguments: (command.registeredArguments || []).map(summarizeArgument),
195
+ options: (command.options || []).map(summarizeOption),
196
+ },
197
+ };
198
+ }
199
+
200
+ /**
201
+ * Build an MCP tool registry for the installed SentinelLayer CLI command tree.
202
+ *
203
+ * The generated registry is intentionally execution-policy-first: every tool is
204
+ * marked as requiring human approval because this is the full CLI surface.
205
+ *
206
+ * @param {{ generatedAt?: string, program?: import("commander").Command, includeHidden?: boolean }} [options]
207
+ * @returns {Promise<Record<string, any>>}
208
+ */
209
+ export async function buildSentinelayerCliRegistryTemplate({
210
+ generatedAt = new Date().toISOString(),
211
+ program = null,
212
+ includeHidden = false,
213
+ } = {}) {
214
+ const cliProgram =
215
+ program ||
216
+ (await buildCliProgram({
217
+ invokeLegacy: async () => {},
218
+ }));
219
+ const tools = collectLeafCommands(cliProgram, { includeHidden }).map(buildCliTool);
220
+ return {
221
+ version: MCP_TOOL_REGISTRY_SCHEMA_VERSION,
222
+ generated_at: generatedAt,
223
+ tools,
224
+ };
225
+ }
226
+
227
+ export const __cliRegistryInternals = Object.freeze({
228
+ collectLeafCommands,
229
+ buildToolInputSchema,
230
+ optionPropertyName,
231
+ });