@introspection-ai/recipes 0.13.0 → 0.14.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.
- package/README.md +45 -58
- package/dist/{agent-tool.d.ts → agents.d.ts} +4 -1
- package/dist/agents.d.ts.map +1 -0
- package/dist/{agent-tool.js → agents.js} +1 -1
- package/dist/agents.js.map +1 -0
- package/dist/api/extensions.d.ts +3 -0
- package/dist/api/extensions.d.ts.map +1 -0
- package/dist/api/extensions.js +2 -0
- package/dist/api/extensions.js.map +1 -0
- package/dist/api/mcp.d.ts +4 -0
- package/dist/api/mcp.d.ts.map +1 -0
- package/dist/api/mcp.js +2 -0
- package/dist/api/mcp.js.map +1 -0
- package/dist/api/session.d.ts +3 -0
- package/dist/api/session.d.ts.map +1 -0
- package/dist/api/session.js +2 -0
- package/dist/api/session.js.map +1 -0
- package/dist/child-agent.d.ts +5 -4
- package/dist/child-agent.d.ts.map +1 -1
- package/dist/child-agent.js +26 -117
- package/dist/child-agent.js.map +1 -1
- package/dist/child-session.d.ts +23 -0
- package/dist/child-session.d.ts.map +1 -0
- package/dist/child-session.js +48 -0
- package/dist/child-session.js.map +1 -0
- package/dist/extensions.d.ts +29 -0
- package/dist/extensions.d.ts.map +1 -0
- package/dist/extensions.js +119 -0
- package/dist/extensions.js.map +1 -0
- package/dist/index.d.ts +4 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -13
- package/dist/index.js.map +1 -1
- package/dist/inspect.d.ts +38 -5
- package/dist/inspect.d.ts.map +1 -1
- package/dist/inspect.js +92 -41
- package/dist/inspect.js.map +1 -1
- package/dist/interactions.d.ts +18 -2
- package/dist/interactions.d.ts.map +1 -1
- package/dist/interactions.js +41 -10
- package/dist/interactions.js.map +1 -1
- package/dist/mcp-catalog.d.ts +1 -0
- package/dist/mcp-catalog.d.ts.map +1 -1
- package/dist/mcp-catalog.js +14 -6
- package/dist/mcp-catalog.js.map +1 -1
- package/dist/mcp-chunks/auth-command-ZLGYIEUY.js +273 -0
- package/dist/mcp-chunks/call-arguments-LU3W6D3K.js +13 -0
- package/dist/mcp-chunks/call-command-LKTKSYTH.js +612 -0
- package/dist/mcp-chunks/chunk-2U4BSDC4.js +3951 -0
- package/dist/mcp-chunks/chunk-2XL7MG7R.js +272 -0
- package/dist/mcp-chunks/chunk-3BJA27PY.js +2321 -0
- package/dist/mcp-chunks/chunk-4SQKWYFF.js +15 -0
- package/dist/mcp-chunks/chunk-5QOSCSQB.js +290 -0
- package/dist/mcp-chunks/chunk-6W5QFASN.js +77 -0
- package/dist/mcp-chunks/chunk-6XCZGU3G.js +1004 -0
- package/dist/mcp-chunks/chunk-7FHEBT5G.js +149 -0
- package/dist/mcp-chunks/chunk-7KA2VJVZ.js +114 -0
- package/dist/mcp-chunks/chunk-7UDVSOOF.js +138 -0
- package/dist/mcp-chunks/chunk-BNV3TPE4.js +66 -0
- package/dist/mcp-chunks/chunk-BTLAWHTA.js +129 -0
- package/dist/mcp-chunks/chunk-BV56DXPW.js +13 -0
- package/dist/mcp-chunks/chunk-BWHIA4PC.js +90 -0
- package/dist/mcp-chunks/chunk-BZIQOWE6.js +267 -0
- package/dist/mcp-chunks/chunk-D3MRAZ6H.js +16140 -0
- package/dist/mcp-chunks/chunk-DHBE7UBH.js +750 -0
- package/dist/mcp-chunks/chunk-E22DYOW5.js +251 -0
- package/dist/mcp-chunks/chunk-ECIVB7P4.js +139 -0
- package/dist/mcp-chunks/chunk-HKRJ6O36.js +1076 -0
- package/dist/mcp-chunks/chunk-IABRYROW.js +44 -0
- package/dist/mcp-chunks/chunk-J6LYIZXN.js +45 -0
- package/dist/mcp-chunks/chunk-L7NTQVYN.js +161 -0
- package/dist/mcp-chunks/chunk-LEZSACDE.js +141 -0
- package/dist/mcp-chunks/chunk-LKIBAYKU.js +208 -0
- package/dist/mcp-chunks/chunk-LNH5LAJD.js +152 -0
- package/dist/mcp-chunks/chunk-LQQEYD76.js +124 -0
- package/dist/mcp-chunks/chunk-LRKZP7DJ.js +47 -0
- package/dist/mcp-chunks/chunk-LS7PMWSR.js +109 -0
- package/dist/mcp-chunks/chunk-M3TPODXI.js +95 -0
- package/dist/mcp-chunks/chunk-MZ2NARG7.js +400 -0
- package/dist/mcp-chunks/chunk-N46NEFAF.js +45 -0
- package/dist/mcp-chunks/chunk-NNOEGEUZ.js +17273 -0
- package/dist/mcp-chunks/chunk-NRAJX5E4.js +69 -0
- package/dist/mcp-chunks/chunk-P3PSC54P.js +869 -0
- package/dist/mcp-chunks/chunk-PIIYJR44.js +6341 -0
- package/dist/mcp-chunks/chunk-PWYFKGKG.js +385 -0
- package/dist/mcp-chunks/chunk-SJCCBNZ4.js +45 -0
- package/dist/mcp-chunks/chunk-TATVT5IT.js +194 -0
- package/dist/mcp-chunks/chunk-TEUKFFQL.js +29 -0
- package/dist/mcp-chunks/chunk-UKYNQGNE.js +150 -0
- package/dist/mcp-chunks/chunk-UNKB4SBV.js +37 -0
- package/dist/mcp-chunks/chunk-WD6BOD24.js +138 -0
- package/dist/mcp-chunks/chunk-WQJZMCJN.js +73 -0
- package/dist/mcp-chunks/chunk-XANPGTLI.js +364 -0
- package/dist/mcp-chunks/chunk-XQK2JSW4.js +35 -0
- package/dist/mcp-chunks/chunk-YHQUTU6L.js +571 -0
- package/dist/mcp-chunks/chunk-YVZKMV5H.js +57 -0
- package/dist/mcp-chunks/cli-UV4QJZZM.js +929 -0
- package/dist/mcp-chunks/client-S7RQUG5U.js +13 -0
- package/dist/mcp-chunks/config-command-NLAGKID5.js +1101 -0
- package/dist/mcp-chunks/daemon-command-U242Z6VY.js +26 -0
- package/dist/mcp-chunks/dist-LK3SHAMX.js +6478 -0
- package/dist/mcp-chunks/emit-ts-command-VBXVXO6V.js +386 -0
- package/dist/mcp-chunks/generate-cli-runner-6CAZZA3P.js +1426 -0
- package/dist/mcp-chunks/inspect-cli-command-3ZORTOR7.js +111 -0
- package/dist/mcp-chunks/launch-YKRXHXJI.js +10 -0
- package/dist/mcp-chunks/lifecycle-XVKRHPH3.js +16 -0
- package/dist/mcp-chunks/list-command-2MEGGNFQ.js +4190 -0
- package/dist/mcp-chunks/output-utils-NKESLAJ6.js +12 -0
- package/dist/mcp-chunks/prompt-B1Yc1NPt-VGOTWJMN.js +918 -0
- package/dist/mcp-chunks/record-command-YXHTTTT5.js +17 -0
- package/dist/mcp-chunks/replay-command-25HJ6AQU.js +85 -0
- package/dist/mcp-chunks/resource-command-OO4NYF5C.js +97 -0
- package/dist/mcp-chunks/result-utils-MOHHLNM7.js +15 -0
- package/dist/mcp-chunks/runtime-5WJ3G25Y.js +33 -0
- package/dist/mcp-chunks/runtime-wrapper-RPWDIDBF.js +10 -0
- package/dist/mcp-chunks/serve-command-DHXJZMMN.js +3302 -0
- package/dist/mcp-chunks/timeouts-W2XWCCXR.js +18 -0
- package/dist/mcp-chunks/vault-command-PIYIUEZL.js +154 -0
- package/dist/mcp-daemon-client.d.ts +6 -1
- package/dist/mcp-daemon-client.d.ts.map +1 -1
- package/dist/mcp-daemon-client.js +119 -2
- package/dist/mcp-daemon-client.js.map +1 -1
- package/dist/mcp-daemon-protocol.d.ts +18 -1
- package/dist/mcp-daemon-protocol.d.ts.map +1 -1
- package/dist/mcp-daemon-protocol.js +31 -0
- package/dist/mcp-daemon-protocol.js.map +1 -1
- package/dist/mcp-daemon.js +169 -76602
- package/dist/mcp-daemon.js.map +1 -1
- package/dist/mcp-policy.d.ts +11 -0
- package/dist/mcp-policy.d.ts.map +1 -0
- package/dist/mcp-policy.js +27 -0
- package/dist/mcp-policy.js.map +1 -0
- package/dist/mcp-run-worker.js +38 -76540
- package/dist/mcp-tools.d.ts +39 -0
- package/dist/mcp-tools.d.ts.map +1 -0
- package/dist/mcp-tools.js +493 -0
- package/dist/mcp-tools.js.map +1 -0
- package/dist/mcp.d.ts +21 -9
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +101 -31
- package/dist/mcp.js.map +1 -1
- package/dist/model-binding.d.ts +39 -0
- package/dist/model-binding.d.ts.map +1 -0
- package/dist/model-binding.js +103 -0
- package/dist/model-binding.js.map +1 -0
- package/dist/pi-extension.d.ts +2 -22
- package/dist/pi-extension.d.ts.map +1 -1
- package/dist/pi-extension.js +296 -71
- package/dist/pi-extension.js.map +1 -1
- package/dist/recipe/resolve.d.ts +43 -20
- package/dist/recipe/resolve.d.ts.map +1 -1
- package/dist/recipe/resolve.js +215 -70
- package/dist/recipe/resolve.js.map +1 -1
- package/dist/recipe-agent.d.ts +24 -27
- package/dist/recipe-agent.d.ts.map +1 -1
- package/dist/recipe-agent.js +278 -230
- package/dist/recipe-agent.js.map +1 -1
- package/dist/recipe-check.d.ts +17 -0
- package/dist/recipe-check.d.ts.map +1 -0
- package/dist/recipe-check.js +258 -0
- package/dist/recipe-check.js.map +1 -0
- package/dist/recipe-extensions.d.ts.map +1 -1
- package/dist/recipe-extensions.js +0 -3
- package/dist/recipe-extensions.js.map +1 -1
- package/dist/recipe-model.d.ts +3 -1
- package/dist/recipe-model.d.ts.map +1 -1
- package/dist/recipe-model.js +15 -12
- package/dist/recipe-model.js.map +1 -1
- package/dist/recipe-package.d.ts +6 -28
- package/dist/recipe-package.d.ts.map +1 -1
- package/dist/recipe-package.js +248 -191
- package/dist/recipe-package.js.map +1 -1
- package/dist/recipe-skills.d.ts.map +1 -1
- package/dist/recipe-skills.js +42 -29
- package/dist/recipe-skills.js.map +1 -1
- package/dist/run-controller.d.ts +10 -9
- package/dist/run-controller.d.ts.map +1 -1
- package/dist/run-controller.js +28 -21
- package/dist/run-controller.js.map +1 -1
- package/dist/session.d.ts +40 -53
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +312 -173
- package/dist/session.js.map +1 -1
- package/dist/test-utils.d.ts +7 -7
- package/dist/test-utils.d.ts.map +1 -1
- package/dist/test-utils.js +32 -15
- package/dist/test-utils.js.map +1 -1
- package/docs/agent-composition.md +41 -35
- package/docs/host-api.md +201 -0
- package/docs/index.md +8 -14
- package/docs/interactions.md +12 -11
- package/docs/mcp-auth.md +6 -6
- package/docs/mcp-configuration.md +72 -24
- package/docs/pi-extension.md +39 -11
- package/docs/recipe-flow.md +9 -9
- package/docs/recipe-format.md +90 -41
- package/package.json +23 -17
- package/vendor/mcp-client/darwin-arm64/mcp-client +0 -0
- package/vendor/mcp-client/darwin-x64/mcp-client +0 -0
- package/vendor/mcp-client/linux-arm64/mcp-client +0 -0
- package/vendor/recipe-check/darwin-arm64/recipe-check +0 -0
- package/vendor/recipe-check/darwin-x64/recipe-check +0 -0
- package/vendor/recipe-check/linux-arm64/recipe-check +0 -0
- package/vendor/recipe-check/linux-x64/recipe-check +0 -0
- package/vendor/recipe-check/linux-x64-musl/recipe-check +0 -0
- package/vendor/recipe-check/win32-x64/recipe-check.exe +0 -0
- package/dist/agent-tool.d.ts.map +0 -1
- package/dist/agent-tool.js.map +0 -1
- package/dist/run.d.ts +0 -28
- package/dist/run.d.ts.map +0 -1
- package/dist/run.js +0 -84
- package/dist/run.js.map +0 -1
- package/dist/testing.d.ts +0 -25
- package/dist/testing.d.ts.map +0 -1
- package/dist/testing.js +0 -96
- package/dist/testing.js.map +0 -1
- package/docs/deployment-configuration.md +0 -65
- package/docs/migration.md +0 -70
- package/docs/python-bindings-release.md +0 -47
- package/docs/recipe-evals.md +0 -70
- package/docs/recipe-judges.md +0 -152
- package/docs/runtime-library.md +0 -171
|
@@ -0,0 +1,750 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
buildToolDoc
|
|
4
|
+
} from "./chunk-MZ2NARG7.js";
|
|
5
|
+
import {
|
|
6
|
+
buildEmbeddedSchemaMap
|
|
7
|
+
} from "./chunk-XANPGTLI.js";
|
|
8
|
+
import {
|
|
9
|
+
parsePositiveInteger
|
|
10
|
+
} from "./chunk-BNV3TPE4.js";
|
|
11
|
+
import {
|
|
12
|
+
MCPORTER_VERSION
|
|
13
|
+
} from "./chunk-4SQKWYFF.js";
|
|
14
|
+
|
|
15
|
+
// node_modules/.pnpm/mcporter@0.12.3/node_modules/mcporter/dist/cli/generate/flag-parser.js
|
|
16
|
+
function extractGeneratorFlags(args, options = {}) {
|
|
17
|
+
const result = {};
|
|
18
|
+
let index = 0;
|
|
19
|
+
while (index < args.length) {
|
|
20
|
+
const token = args[index];
|
|
21
|
+
if (!token) {
|
|
22
|
+
index += 1;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (token === "--runtime") {
|
|
26
|
+
const value = args[index + 1];
|
|
27
|
+
if (value !== "node" && value !== "bun") {
|
|
28
|
+
throw new Error("--runtime must be 'node' or 'bun'.");
|
|
29
|
+
}
|
|
30
|
+
result.runtime = value;
|
|
31
|
+
args.splice(index, 2);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (token === "--timeout") {
|
|
35
|
+
const raw = args[index + 1];
|
|
36
|
+
if (!raw) {
|
|
37
|
+
throw new Error("Flag '--timeout' requires a value.");
|
|
38
|
+
}
|
|
39
|
+
const parsed = parsePositiveInteger(raw);
|
|
40
|
+
if (parsed === void 0) {
|
|
41
|
+
throw new Error("--timeout must be a positive integer.");
|
|
42
|
+
}
|
|
43
|
+
result.timeout = parsed;
|
|
44
|
+
args.splice(index, 2);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (options.allowIncludeOptional && (token === "--include-optional" || token === "--all-parameters")) {
|
|
48
|
+
result.includeOptional = true;
|
|
49
|
+
args.splice(index, 1);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
index += 1;
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// node_modules/.pnpm/mcporter@0.12.3/node_modules/mcporter/dist/cli/generate/template.js
|
|
58
|
+
import { realpathSync } from "node:fs";
|
|
59
|
+
import fs2 from "node:fs/promises";
|
|
60
|
+
import path from "node:path";
|
|
61
|
+
|
|
62
|
+
// node_modules/.pnpm/mcporter@0.12.3/node_modules/mcporter/dist/cli/generate/fs-helpers.js
|
|
63
|
+
import fs from "node:fs/promises";
|
|
64
|
+
async function markExecutable(filePath) {
|
|
65
|
+
try {
|
|
66
|
+
await fs.chmod(filePath, 493);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (!shouldIgnorePosixPermissionError(error)) {
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function safeCopyFile(sourcePath, targetPath) {
|
|
74
|
+
try {
|
|
75
|
+
await fs.copyFile(sourcePath, targetPath);
|
|
76
|
+
return;
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (!shouldIgnorePosixPermissionError(error)) {
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const data = await fs.readFile(sourcePath);
|
|
83
|
+
await fs.writeFile(targetPath, data);
|
|
84
|
+
}
|
|
85
|
+
function shouldIgnorePosixPermissionError(error) {
|
|
86
|
+
if (!error || typeof error !== "object") {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
const code = error.code;
|
|
90
|
+
return code === "EPERM" || code === "EINVAL" || code === "ENOSYS" || code === "EACCES";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// node_modules/.pnpm/mcporter@0.12.3/node_modules/mcporter/dist/cli/generate/template-help.js
|
|
94
|
+
function renderEmbeddedHelpSource() {
|
|
95
|
+
return `function configureGeneratedCommandHelp(command: Command): void {
|
|
96
|
+
command.configureHelp({
|
|
97
|
+
commandUsage(target) {
|
|
98
|
+
const usage = (target.name() + ' ' + target.usage()).trim() || target.name();
|
|
99
|
+
return supportsAnsiColor ? tint.bold(usage) : usage;
|
|
100
|
+
},
|
|
101
|
+
optionTerm(option) {
|
|
102
|
+
const term = option.flags ?? '';
|
|
103
|
+
return supportsAnsiColor ? tint.bold(term) : term;
|
|
104
|
+
},
|
|
105
|
+
optionDescription(option) {
|
|
106
|
+
const description = option.description ?? '';
|
|
107
|
+
return supportsAnsiColor ? tint.extraDim(description) : description;
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function configureToolCommandHelps(): void {
|
|
113
|
+
program.commands.forEach((cmd) => {
|
|
114
|
+
if (cmd.name() === '__mcporter_inspect') {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
configureGeneratedCommandHelp(cmd);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function renderStandaloneHelp(): string {
|
|
122
|
+
const colorfulTitle = tint.bold(embeddedName) + ' ' + tint.dim('\u2014 ' + embeddedDescription);
|
|
123
|
+
const plainTitle = embeddedName + ' \u2014 ' + embeddedDescription;
|
|
124
|
+
const title = supportsAnsiColor ? colorfulTitle : plainTitle;
|
|
125
|
+
const lines = [title, '', 'Usage: ' + embeddedName + ' <command> [options]', ''];
|
|
126
|
+
if (generatorTools) {
|
|
127
|
+
lines.push(formatEmbeddedTools());
|
|
128
|
+
}
|
|
129
|
+
lines.push('', formatGlobalFlags(), '', formatQuickStart());
|
|
130
|
+
if (generatorInfo) {
|
|
131
|
+
lines.push('', tint.extraDim(generatorInfo));
|
|
132
|
+
}
|
|
133
|
+
return lines.join('\\n') + '\\n';
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
program.helpInformation = () => renderStandaloneHelp();
|
|
137
|
+
|
|
138
|
+
function formatEmbeddedTools(): string {
|
|
139
|
+
const header = supportsAnsiColor ? tint.bold('Embedded tools') : 'Embedded tools';
|
|
140
|
+
if (!generatorTools.length) {
|
|
141
|
+
return header;
|
|
142
|
+
}
|
|
143
|
+
const lines = [header];
|
|
144
|
+
generatorTools.forEach((entry) => {
|
|
145
|
+
const renderedDesc = entry.description
|
|
146
|
+
? supportsAnsiColor
|
|
147
|
+
? tint.extraDim(entry.description)
|
|
148
|
+
: entry.description
|
|
149
|
+
: undefined;
|
|
150
|
+
const base = renderedDesc ? entry.name + ' - ' + renderedDesc : entry.name;
|
|
151
|
+
lines.push(' ' + base);
|
|
152
|
+
if (entry.flags) {
|
|
153
|
+
const renderedFlags = supportsAnsiColor ? tint.extraDim(entry.flags) : entry.flags;
|
|
154
|
+
lines.push(' ' + renderedFlags);
|
|
155
|
+
}
|
|
156
|
+
lines.push('');
|
|
157
|
+
});
|
|
158
|
+
if (lines[lines.length - 1] === '') {
|
|
159
|
+
lines.pop();
|
|
160
|
+
}
|
|
161
|
+
return lines.join('\\n');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function formatGlobalFlags(): string {
|
|
165
|
+
const header = supportsAnsiColor ? tint.bold('Global flags') : 'Global flags';
|
|
166
|
+
const entries = [
|
|
167
|
+
['-t, --timeout <ms>', 'Call timeout in milliseconds'],
|
|
168
|
+
['-o, --output <format>', 'text | markdown | json | raw (default text)'],
|
|
169
|
+
];
|
|
170
|
+
const formatted = entries.map(([flag, summary]) => ' ' + flag.padEnd(28) + summary);
|
|
171
|
+
return [header, ...formatted].join('\\n');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function formatQuickStart(): string {
|
|
175
|
+
const header = supportsAnsiColor ? tint.bold('Quick start') : 'Quick start';
|
|
176
|
+
const examples = quickStartExamples();
|
|
177
|
+
if (!examples.length) {
|
|
178
|
+
return header;
|
|
179
|
+
}
|
|
180
|
+
const formatted = examples.map(([cmd, note]) => ' ' + cmd + '\\n ' + tint.dim('# ' + note));
|
|
181
|
+
return [header, ...formatted].join('\\n');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function quickStartExamples(): Array<[string, string]> {
|
|
185
|
+
const examples: Array<[string, string]> = [];
|
|
186
|
+
const commandMap = new Map<string, string>();
|
|
187
|
+
program.commands.forEach((cmd) => {
|
|
188
|
+
const name = cmd.name();
|
|
189
|
+
if (name !== '__mcporter_inspect') {
|
|
190
|
+
commandMap.set(name, name);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
const embedded = Array.isArray(generatorTools) ? generatorTools : [];
|
|
194
|
+
for (const entry of embedded.slice(0, 3)) {
|
|
195
|
+
const commandName = commandMap.get(entry.name) ?? entry.name;
|
|
196
|
+
const flags = entry.flags ? ' ' + entry.flags.replace(/<[^>]+>/g, '<value>') : '';
|
|
197
|
+
examples.push([embeddedName + ' ' + commandName + flags, 'invoke ' + commandName]);
|
|
198
|
+
}
|
|
199
|
+
if (!examples.length) {
|
|
200
|
+
examples.push([embeddedName + ' <tool> --key value', 'invoke a tool with flags']);
|
|
201
|
+
}
|
|
202
|
+
return examples;
|
|
203
|
+
}
|
|
204
|
+
`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// node_modules/.pnpm/mcporter@0.12.3/node_modules/mcporter/dist/cli/generate/stable-json.js
|
|
208
|
+
function stableJsonStringify(value, space) {
|
|
209
|
+
const json = JSON.stringify(sortJsonValue(value), void 0, space);
|
|
210
|
+
if (json === void 0) {
|
|
211
|
+
throw new TypeError("Cannot serialize unsupported JSON root value.");
|
|
212
|
+
}
|
|
213
|
+
return json;
|
|
214
|
+
}
|
|
215
|
+
function sortJsonValue(value) {
|
|
216
|
+
if (Array.isArray(value)) {
|
|
217
|
+
return value.map((entry) => sortJsonValue(entry));
|
|
218
|
+
}
|
|
219
|
+
if (!isPlainObject(value)) {
|
|
220
|
+
return value;
|
|
221
|
+
}
|
|
222
|
+
const result = {};
|
|
223
|
+
for (const key of Object.keys(value).toSorted()) {
|
|
224
|
+
const entry = value[key];
|
|
225
|
+
if (entry !== void 0) {
|
|
226
|
+
result[key] = sortJsonValue(entry);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
function isPlainObject(value) {
|
|
232
|
+
if (!value || typeof value !== "object") {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
const prototype = Object.getPrototypeOf(value);
|
|
236
|
+
return prototype === Object.prototype || prototype === null;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// node_modules/.pnpm/mcporter@0.12.3/node_modules/mcporter/dist/cli/generate/template.js
|
|
240
|
+
async function writeTemplate(input) {
|
|
241
|
+
const resolvedOutput = input.outputPath ? path.resolve(input.outputPath) : path.resolve(process.cwd(), `${input.serverName}.ts`);
|
|
242
|
+
const runtimeScriptPath = input.runtimeScriptPath ? path.resolve(input.runtimeScriptPath) : resolvedOutput;
|
|
243
|
+
await fs2.mkdir(path.dirname(resolvedOutput), { recursive: true });
|
|
244
|
+
await fs2.writeFile(resolvedOutput, renderTemplate({ ...input, outputPath: resolvedOutput, runtimeScriptPath }), "utf8");
|
|
245
|
+
await markExecutable(resolvedOutput);
|
|
246
|
+
return resolvedOutput;
|
|
247
|
+
}
|
|
248
|
+
async function readPackageMetadata() {
|
|
249
|
+
const packageJsonPath = path.resolve(process.cwd(), "package.json");
|
|
250
|
+
try {
|
|
251
|
+
const buffer = await fs2.readFile(packageJsonPath, "utf8");
|
|
252
|
+
const pkg = JSON.parse(buffer);
|
|
253
|
+
return {
|
|
254
|
+
name: pkg.name ?? "mcporter",
|
|
255
|
+
version: pkg.version ?? "0.0.0"
|
|
256
|
+
};
|
|
257
|
+
} catch (error) {
|
|
258
|
+
if (error.code !== "ENOENT") {
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
return { name: "mcporter", version: MCPORTER_VERSION };
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function renderTemplate({ runtimeKind, timeoutMs, definition, serverName, tools, generator, metadata, outputPath, runtimeScriptPath }) {
|
|
265
|
+
const imports = [
|
|
266
|
+
"import path from 'node:path';",
|
|
267
|
+
"import { fileURLToPath } from 'node:url';",
|
|
268
|
+
"import { Command } from 'commander';",
|
|
269
|
+
"import { createGeneratedKeepAliveRuntime, createRuntime, createServerProxy, handleDaemonCli } from 'mcporter';",
|
|
270
|
+
"import { createCallResult } from 'mcporter';"
|
|
271
|
+
].join("\n");
|
|
272
|
+
const embedded = stableJsonStringify(JSON.parse(JSON.stringify(definition, (_key, value) => value instanceof URL ? value.toString() : value)), 2);
|
|
273
|
+
const relativeStdioCwd = computeRelativeStdioCwd(definition, runtimeScriptPath ?? outputPath);
|
|
274
|
+
const generatorHeader = `Generated by ${generator.name}@${generator.version} \u2014 https://github.com/openclaw/mcporter`;
|
|
275
|
+
const toolDocs = tools.map((tool) => ({
|
|
276
|
+
tool,
|
|
277
|
+
doc: buildToolDoc({
|
|
278
|
+
serverName,
|
|
279
|
+
toolName: tool.tool.name,
|
|
280
|
+
description: tool.tool.description,
|
|
281
|
+
outputSchema: tool.tool.outputSchema,
|
|
282
|
+
options: tool.options,
|
|
283
|
+
requiredOnly: true,
|
|
284
|
+
colorize: false,
|
|
285
|
+
flagExtras: [{ text: "--raw <json>" }]
|
|
286
|
+
})
|
|
287
|
+
}));
|
|
288
|
+
const renderedTools = toolDocs.map((entry) => Object.assign(renderToolCommand(entry.tool, timeoutMs, serverName, entry.doc), {
|
|
289
|
+
doc: entry.doc,
|
|
290
|
+
tool: entry.tool
|
|
291
|
+
}));
|
|
292
|
+
assertUniqueGeneratedCommandNames(renderedTools);
|
|
293
|
+
const toolHelp = renderedTools.map((entry) => ({
|
|
294
|
+
name: entry.commandName,
|
|
295
|
+
description: entry.tool.tool.description ?? "",
|
|
296
|
+
usage: entry.doc.flagUsage ? `${entry.commandName} ${entry.doc.flagUsage}` : void 0,
|
|
297
|
+
flags: entry.doc.flagUsage ?? ""
|
|
298
|
+
}));
|
|
299
|
+
const generatorHeaderLiteral = JSON.stringify(generatorHeader);
|
|
300
|
+
const toolHelpLiteral = stableJsonStringify(toolHelp, 2);
|
|
301
|
+
const embeddedSchemas = stableJsonStringify(buildEmbeddedSchemaMap(tools), 2);
|
|
302
|
+
const embeddedMetadata = stableJsonStringify(metadata, 2);
|
|
303
|
+
const toolBlocks = renderedTools.map((entry) => entry.block).join("\n\n");
|
|
304
|
+
const signatureMap = Object.fromEntries(renderedTools.map((entry) => [entry.commandName, entry.tsSignature]));
|
|
305
|
+
const signatureMapLiteral = stableJsonStringify(signatureMap, 2);
|
|
306
|
+
const generatedHeaderComment = `// @generated by ${generator.name}@${generator.version}. DO NOT EDIT.`;
|
|
307
|
+
return `#!/usr/bin/env ${runtimeKind === "bun" ? "bun" : "node"}
|
|
308
|
+
${generatedHeaderComment}
|
|
309
|
+
${imports}
|
|
310
|
+
|
|
311
|
+
const __mcpScriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
312
|
+
const __mcpRelativeStdioCwd: string | null = ${JSON.stringify(relativeStdioCwd)};
|
|
313
|
+
const embeddedServer = ${embedded} as const;
|
|
314
|
+
const embeddedSchemas = ${embeddedSchemas} as const;
|
|
315
|
+
const embeddedName = ${JSON.stringify(serverName)};
|
|
316
|
+
const embeddedDescription = ${JSON.stringify(definition.description ?? `Standalone CLI for the ${serverName} MCP server.`)};
|
|
317
|
+
const generatorInfo = ${generatorHeaderLiteral};
|
|
318
|
+
const generatorTools = ${toolHelpLiteral} as const;
|
|
319
|
+
const embeddedMetadata = ${embeddedMetadata} as const;
|
|
320
|
+
const artifactKind = determineArtifactKind();
|
|
321
|
+
const program = new Command();
|
|
322
|
+
program.name(embeddedName);
|
|
323
|
+
program.description(embeddedDescription);
|
|
324
|
+
program.option('-t, --timeout <ms>', 'Call timeout in milliseconds', (value) => parseInt(value, 10), ${timeoutMs});
|
|
325
|
+
program.option('-o, --output <format>', 'Output format: text|markdown|json|raw', 'text');
|
|
326
|
+
const commandSignatures: Record<string, string> = ${signatureMapLiteral};
|
|
327
|
+
program.configureHelp({
|
|
328
|
+
commandTerm(cmd) {
|
|
329
|
+
const term = cmd.name();
|
|
330
|
+
return commandSignatures[term] ?? cmd.name();
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
program.showSuggestionAfterError(true);
|
|
334
|
+
|
|
335
|
+
${toolBlocks}
|
|
336
|
+
|
|
337
|
+
program
|
|
338
|
+
.command('__mcporter_inspect', { hidden: true })
|
|
339
|
+
.description('Internal metadata printer for mcporter inspect-cli.')
|
|
340
|
+
.action(() => {
|
|
341
|
+
const payload = buildMetadataPayload();
|
|
342
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
configureToolCommandHelps();
|
|
346
|
+
|
|
347
|
+
const FORCE_COLOR = process.env.FORCE_COLOR?.toLowerCase();
|
|
348
|
+
const forceDisableColor = FORCE_COLOR === '0' || FORCE_COLOR === 'false';
|
|
349
|
+
const forceEnableColor = FORCE_COLOR === '1' || FORCE_COLOR === 'true' || FORCE_COLOR === '2' || FORCE_COLOR === '3';
|
|
350
|
+
const hasNoColor = process.env.NO_COLOR !== undefined;
|
|
351
|
+
const stdoutStream = process.stdout as NodeJS.WriteStream | undefined;
|
|
352
|
+
const supportsAnsiColor = !hasNoColor && (forceEnableColor || (!forceDisableColor && Boolean(stdoutStream?.isTTY)));
|
|
353
|
+
|
|
354
|
+
const tint = {
|
|
355
|
+
bold(text: string): string {
|
|
356
|
+
return supportsAnsiColor ? '\x1B[1m' + text + '\x1B[0m' : text;
|
|
357
|
+
},
|
|
358
|
+
dim(text: string): string {
|
|
359
|
+
return supportsAnsiColor ? '\x1B[90m' + text + '\x1B[0m' : text;
|
|
360
|
+
},
|
|
361
|
+
extraDim(text: string): string {
|
|
362
|
+
return supportsAnsiColor ? '\x1B[38;5;244m' + text + '\x1B[0m' : text;
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
${renderEmbeddedHelpSource()}
|
|
367
|
+
|
|
368
|
+
function printResult(result: unknown, format: string) {
|
|
369
|
+
const wrapped = createCallResult(result);
|
|
370
|
+
const rawPayload = unwrapRawPayload(wrapped.raw);
|
|
371
|
+
switch (format) {
|
|
372
|
+
case 'json': {
|
|
373
|
+
const json = wrapped.json();
|
|
374
|
+
if (json !== null) {
|
|
375
|
+
console.log(JSON.stringify(json, null, 2));
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
console.log(JSON.stringify(rawPayload, null, 2));
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
case 'markdown': {
|
|
382
|
+
const markdown = wrapped.markdown();
|
|
383
|
+
if (markdown) {
|
|
384
|
+
console.log(markdown);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
case 'raw': {
|
|
390
|
+
console.log(JSON.stringify(rawPayload, null, 2));
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
const text = wrapped.text();
|
|
395
|
+
if (text) {
|
|
396
|
+
console.log(text);
|
|
397
|
+
} else {
|
|
398
|
+
console.log(JSON.stringify(rawPayload, null, 2));
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function unwrapRawPayload(value: unknown): unknown {
|
|
403
|
+
if (value && typeof value === 'object' && 'raw' in value) {
|
|
404
|
+
return (value as { raw: unknown }).raw;
|
|
405
|
+
}
|
|
406
|
+
return value;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function parseArrayOption(value: string, itemType: 'string' | 'number' | 'boolean' | 'json') {
|
|
410
|
+
const trimmed = value.trim();
|
|
411
|
+
if (trimmed.startsWith('[')) {
|
|
412
|
+
const parsed = JSON.parse(trimmed);
|
|
413
|
+
if (!Array.isArray(parsed)) {
|
|
414
|
+
throw new Error('Expected a JSON array.');
|
|
415
|
+
}
|
|
416
|
+
return parsed;
|
|
417
|
+
}
|
|
418
|
+
if (itemType === 'json') {
|
|
419
|
+
const parsed = JSON.parse('[' + value + ']');
|
|
420
|
+
if (!Array.isArray(parsed)) {
|
|
421
|
+
throw new Error('Expected JSON array items.');
|
|
422
|
+
}
|
|
423
|
+
return parsed;
|
|
424
|
+
}
|
|
425
|
+
const values = value.split(',').map((entry) => entry.trim());
|
|
426
|
+
if (itemType === 'number') {
|
|
427
|
+
return values.map((entry) => parseFiniteNumber(entry));
|
|
428
|
+
}
|
|
429
|
+
if (itemType === 'boolean') {
|
|
430
|
+
return values.map((entry) => entry !== 'false');
|
|
431
|
+
}
|
|
432
|
+
return values;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function parseFiniteNumber(value: string): number {
|
|
436
|
+
const trimmed = value.trim();
|
|
437
|
+
const parsed = Number(trimmed);
|
|
438
|
+
if (trimmed === '' || !Number.isFinite(parsed)) {
|
|
439
|
+
throw new Error('Expected a finite number.');
|
|
440
|
+
}
|
|
441
|
+
return parsed;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function normalizeEmbeddedServer(server: typeof embeddedServer) {
|
|
445
|
+
const base = { ...server } as Record<string, unknown>;
|
|
446
|
+
if ((server.command as any).kind === 'http') {
|
|
447
|
+
const urlRaw = (server.command as any).url;
|
|
448
|
+
const urlValue = typeof urlRaw === 'string' ? urlRaw : String(urlRaw);
|
|
449
|
+
return {
|
|
450
|
+
...base,
|
|
451
|
+
command: {
|
|
452
|
+
...(server.command as Record<string, unknown>),
|
|
453
|
+
url: new URL(urlValue),
|
|
454
|
+
},
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
if ((server.command as any).kind === 'stdio') {
|
|
458
|
+
const stdio = server.command as Record<string, unknown>;
|
|
459
|
+
const resolvedCwd =
|
|
460
|
+
__mcpRelativeStdioCwd !== null
|
|
461
|
+
? path.resolve(__mcpScriptDir, __mcpRelativeStdioCwd)
|
|
462
|
+
: typeof stdio.cwd === 'string' && stdio.cwd.length > 0
|
|
463
|
+
? stdio.cwd
|
|
464
|
+
: undefined;
|
|
465
|
+
return {
|
|
466
|
+
...base,
|
|
467
|
+
command: {
|
|
468
|
+
...stdio,
|
|
469
|
+
args: [ ...((server.command as any).args ?? []) ],
|
|
470
|
+
...(resolvedCwd !== undefined ? { cwd: resolvedCwd } : {}),
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
return base;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function determineArtifactKind(): 'template' | 'bundle' | 'binary' {
|
|
478
|
+
const scriptPath = typeof process !== 'undefined' && Array.isArray(process.argv) ? process.argv[1] ?? '' : '';
|
|
479
|
+
if (scriptPath.endsWith('.ts')) {
|
|
480
|
+
return 'template';
|
|
481
|
+
}
|
|
482
|
+
if (scriptPath.endsWith('.js') || scriptPath.endsWith('.mjs') || scriptPath.endsWith('.cjs')) {
|
|
483
|
+
return 'bundle';
|
|
484
|
+
}
|
|
485
|
+
return 'binary';
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function resolveArtifactPath(): string {
|
|
489
|
+
if (typeof process !== 'undefined' && Array.isArray(process.argv) && process.argv.length > 1) {
|
|
490
|
+
const script = process.argv[1];
|
|
491
|
+
if (script) {
|
|
492
|
+
return script;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return embeddedMetadata.artifact.path;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function buildMetadataPayload() {
|
|
499
|
+
const invocation = { ...embeddedMetadata.invocation };
|
|
500
|
+
const path = resolveArtifactPath();
|
|
501
|
+
if (artifactKind === 'template' && path) {
|
|
502
|
+
invocation.outputPath = invocation.outputPath ?? path;
|
|
503
|
+
} else if (artifactKind === 'bundle' && path) {
|
|
504
|
+
invocation.bundle = invocation.bundle ?? path;
|
|
505
|
+
} else if (artifactKind === 'binary' && path) {
|
|
506
|
+
invocation.compile = invocation.compile ?? path;
|
|
507
|
+
}
|
|
508
|
+
return {
|
|
509
|
+
...embeddedMetadata,
|
|
510
|
+
artifact: {
|
|
511
|
+
path,
|
|
512
|
+
kind: artifactKind,
|
|
513
|
+
},
|
|
514
|
+
invocation,
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
async function ensureRuntime() {
|
|
519
|
+
const server = normalizeEmbeddedServer(embeddedServer);
|
|
520
|
+
const baseRuntime = await createRuntime({
|
|
521
|
+
servers: [server as any],
|
|
522
|
+
});
|
|
523
|
+
return await createGeneratedKeepAliveRuntime(baseRuntime, server as any);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
async function invokeWithTimeout<T>(call: Promise<T>, timeout: number): Promise<T> {
|
|
527
|
+
if (!Number.isFinite(timeout) || timeout <= 0) {
|
|
528
|
+
return await call;
|
|
529
|
+
}
|
|
530
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
531
|
+
try {
|
|
532
|
+
return await Promise.race([
|
|
533
|
+
call,
|
|
534
|
+
new Promise<never>((_, reject) => {
|
|
535
|
+
timer = setTimeout(() => {
|
|
536
|
+
reject(new Error('Call timed out after ' + timeout + 'ms.'));
|
|
537
|
+
}, timeout);
|
|
538
|
+
}),
|
|
539
|
+
]);
|
|
540
|
+
} finally {
|
|
541
|
+
if (timer) {
|
|
542
|
+
clearTimeout(timer);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function parseGeneratedDaemonInvocation(rawArgs: string[]): { args: string[]; configPath: string; rootDir?: string } | null {
|
|
548
|
+
const args = [...rawArgs];
|
|
549
|
+
let configPath: string | undefined;
|
|
550
|
+
let rootDir: string | undefined;
|
|
551
|
+
while (args.length > 0) {
|
|
552
|
+
const token = args[0];
|
|
553
|
+
if (token === '--config') {
|
|
554
|
+
args.shift();
|
|
555
|
+
configPath = args.shift();
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
if (token?.startsWith('--config=')) {
|
|
559
|
+
args.shift();
|
|
560
|
+
configPath = token.slice('--config='.length);
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
if (token === '--root') {
|
|
564
|
+
args.shift();
|
|
565
|
+
rootDir = args.shift();
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
if (token?.startsWith('--root=')) {
|
|
569
|
+
args.shift();
|
|
570
|
+
rootDir = token.slice('--root='.length);
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
break;
|
|
574
|
+
}
|
|
575
|
+
if (args[0] !== 'daemon') {
|
|
576
|
+
return null;
|
|
577
|
+
}
|
|
578
|
+
if (!configPath) {
|
|
579
|
+
throw new Error('Generated daemon invocation is missing --config.');
|
|
580
|
+
}
|
|
581
|
+
return { args: args.slice(1), configPath, rootDir };
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
async function runCli(): Promise<void> {
|
|
585
|
+
const args = process.argv.slice(2);
|
|
586
|
+
const daemonInvocation = parseGeneratedDaemonInvocation(args);
|
|
587
|
+
if (daemonInvocation) {
|
|
588
|
+
await handleDaemonCli([...daemonInvocation.args], {
|
|
589
|
+
configPath: daemonInvocation.configPath,
|
|
590
|
+
configExplicit: true,
|
|
591
|
+
rootDir: daemonInvocation.rootDir,
|
|
592
|
+
});
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
if (args.length === 0) {
|
|
596
|
+
program.outputHelp();
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
await program.parseAsync(process.argv);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
if (process.env.MCPORTER_DISABLE_AUTORUN !== '1') {
|
|
603
|
+
runCli().catch((error) => {
|
|
604
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
605
|
+
console.error(message);
|
|
606
|
+
process.exit(1);
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
`;
|
|
610
|
+
}
|
|
611
|
+
function renderToolCommand(tool, defaultTimeout, serverName, existingDoc) {
|
|
612
|
+
const commandName = tool.tool.name.replace(/[^a-zA-Z0-9-]/g, "-");
|
|
613
|
+
const description = tool.tool.description ?? `Invoke the ${tool.tool.name} tool.`;
|
|
614
|
+
const doc = existingDoc ?? buildToolDoc({
|
|
615
|
+
serverName,
|
|
616
|
+
toolName: tool.tool.name,
|
|
617
|
+
description: tool.tool.description,
|
|
618
|
+
outputSchema: tool.tool.outputSchema,
|
|
619
|
+
options: tool.options,
|
|
620
|
+
requiredOnly: true,
|
|
621
|
+
colorize: false,
|
|
622
|
+
flagExtras: [{ text: "--raw <json>" }]
|
|
623
|
+
});
|
|
624
|
+
const buildArgs = tool.options.map((option) => {
|
|
625
|
+
const camelCaseProp = option.cliName.split("-").filter(Boolean).map((segment, index) => index === 0 ? segment : `${segment.charAt(0).toUpperCase()}${segment.slice(1)}`).join("");
|
|
626
|
+
const source = `cmdOpts.${camelCaseProp}`;
|
|
627
|
+
return `if (${source} !== undefined) args.${option.property} = ${source};`;
|
|
628
|
+
}).join("\n ");
|
|
629
|
+
const requiredChecks = tool.options.filter((option) => option.required).map((option) => {
|
|
630
|
+
const camelCaseProp = option.cliName.split("-").filter(Boolean).map((segment, index) => index === 0 ? segment : `${segment.charAt(0).toUpperCase()}${segment.slice(1)}`).join("");
|
|
631
|
+
return { option, camelCaseProp };
|
|
632
|
+
});
|
|
633
|
+
const requiredValidation = requiredChecks.length > 0 ? `const missingRequired = [${requiredChecks.map(({ option, camelCaseProp }) => `{ value: cmdOpts.${camelCaseProp}, flag: ${JSON.stringify(`--${option.cliName}`)} }`).join(", ")}].filter((entry) => entry.value === undefined || (typeof entry.value === 'string' && entry.value.trim() === '')).map((entry) => entry.flag);
|
|
634
|
+
if (missingRequired.length > 0) {
|
|
635
|
+
throw new Error('Missing required option' + (missingRequired.length === 1 ? '' : 's') + ': ' + missingRequired.join(', '));
|
|
636
|
+
}` : "";
|
|
637
|
+
const flagUsage = doc.flagUsage;
|
|
638
|
+
const optionLines = doc.optionDocs.map((entry) => renderOption(entry)).join("\n");
|
|
639
|
+
const summary = flagUsage ? `${commandName} ${flagUsage}` : commandName;
|
|
640
|
+
const signature = summary;
|
|
641
|
+
const usageSnippet = flagUsage ? `.usage(${JSON.stringify(flagUsage)})
|
|
642
|
+
` : "";
|
|
643
|
+
const tsSignature = doc.tsSignature;
|
|
644
|
+
const exampleText = doc.examples[0];
|
|
645
|
+
const exampleSnippet = exampleText ? `
|
|
646
|
+
.addHelpText('after', () => '\\nExample:\\n ' + ${JSON.stringify(exampleText)})` : "";
|
|
647
|
+
const optionalSnippet = doc.optionalSummary ? `
|
|
648
|
+
.addHelpText('afterAll', () => '\\n' + ${JSON.stringify(doc.optionalSummary)} + '\\n')` : "";
|
|
649
|
+
const aliasSnippet = tool.tool.name !== commandName ? `
|
|
650
|
+
.alias(${JSON.stringify(tool.tool.name)})` : "";
|
|
651
|
+
const block = `program
|
|
652
|
+
.command(${JSON.stringify(commandName)})
|
|
653
|
+
.summary(${JSON.stringify(summary)})
|
|
654
|
+
.description(${JSON.stringify(description)})
|
|
655
|
+
${usageSnippet ? ` ${usageSnippet}` : ""} .option('--raw <json>', 'Provide raw JSON arguments to the tool, bypassing flag parsing.')
|
|
656
|
+
${optionLines ? `
|
|
657
|
+
${optionLines}` : ""}
|
|
658
|
+
${aliasSnippet ? ` ${aliasSnippet}` : ""} .action(async (cmdOpts) => {
|
|
659
|
+
const globalOptions = program.opts();
|
|
660
|
+
const runtimeContext = await ensureRuntime();
|
|
661
|
+
const runtime = runtimeContext.runtime;
|
|
662
|
+
const serverName = embeddedName;
|
|
663
|
+
const proxy = createServerProxy(runtime, serverName, {
|
|
664
|
+
initialSchemas: embeddedSchemas,
|
|
665
|
+
});
|
|
666
|
+
try {
|
|
667
|
+
const args = cmdOpts.raw ? JSON.parse(cmdOpts.raw) : ({} as Record<string, unknown>);
|
|
668
|
+
if (!cmdOpts.raw) {
|
|
669
|
+
${requiredValidation}
|
|
670
|
+
${buildArgs}
|
|
671
|
+
}
|
|
672
|
+
const call = (proxy.${tool.methodName} as any)(args);
|
|
673
|
+
const result = await invokeWithTimeout(call, globalOptions.timeout || ${defaultTimeout});
|
|
674
|
+
printResult(result, globalOptions.output ?? 'text');
|
|
675
|
+
} finally {
|
|
676
|
+
await runtimeContext.close(serverName).catch(() => {});
|
|
677
|
+
}
|
|
678
|
+
})${exampleSnippet}${optionalSnippet};`;
|
|
679
|
+
return { block, commandName, signature, tsSignature };
|
|
680
|
+
}
|
|
681
|
+
function renderOption(optionDoc) {
|
|
682
|
+
const parser = optionParser(optionDoc.option);
|
|
683
|
+
return ` .option(${JSON.stringify(optionDoc.flagLabel)}, ${JSON.stringify(optionDoc.description)}${parser ? `, ${parser}` : ""})`;
|
|
684
|
+
}
|
|
685
|
+
function computeRelativeStdioCwd(definition, outputPath) {
|
|
686
|
+
if (!outputPath || definition.command.kind !== "stdio") {
|
|
687
|
+
return null;
|
|
688
|
+
}
|
|
689
|
+
const rawCwd = definition.command.cwd;
|
|
690
|
+
if (typeof rawCwd === "string" && path.isAbsolute(rawCwd)) {
|
|
691
|
+
return null;
|
|
692
|
+
}
|
|
693
|
+
const baseCwd = typeof rawCwd === "string" && rawCwd.length > 0 ? rawCwd : process.cwd();
|
|
694
|
+
const absoluteCwd = realpathIfExists(path.resolve(process.cwd(), baseCwd));
|
|
695
|
+
const outputDir = realpathIfExists(path.dirname(path.resolve(outputPath)));
|
|
696
|
+
const relative = path.relative(outputDir, absoluteCwd);
|
|
697
|
+
if (path.isAbsolute(relative)) {
|
|
698
|
+
return null;
|
|
699
|
+
}
|
|
700
|
+
return relative === "" ? "." : relative;
|
|
701
|
+
}
|
|
702
|
+
function realpathIfExists(value) {
|
|
703
|
+
try {
|
|
704
|
+
return realpathSync.native(value);
|
|
705
|
+
} catch {
|
|
706
|
+
return value;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
function optionParser(option) {
|
|
710
|
+
switch (option.type) {
|
|
711
|
+
case "number":
|
|
712
|
+
return "(value) => parseFiniteNumber(value)";
|
|
713
|
+
case "boolean":
|
|
714
|
+
return "(value) => value !== 'false'";
|
|
715
|
+
case "object":
|
|
716
|
+
return "(value) => JSON.parse(value)";
|
|
717
|
+
case "array":
|
|
718
|
+
switch (option.arrayItemType) {
|
|
719
|
+
case "number":
|
|
720
|
+
return "(value) => parseArrayOption(value, 'number')";
|
|
721
|
+
case "boolean":
|
|
722
|
+
return "(value) => parseArrayOption(value, 'boolean')";
|
|
723
|
+
case "object":
|
|
724
|
+
return "(value) => parseArrayOption(value, 'json')";
|
|
725
|
+
default:
|
|
726
|
+
return "(value) => parseArrayOption(value, 'string')";
|
|
727
|
+
}
|
|
728
|
+
default:
|
|
729
|
+
return void 0;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
function assertUniqueGeneratedCommandNames(tools) {
|
|
733
|
+
const commands = /* @__PURE__ */ new Map();
|
|
734
|
+
for (const entry of tools) {
|
|
735
|
+
const previous = commands.get(entry.commandName);
|
|
736
|
+
if (previous) {
|
|
737
|
+
throw new Error(`Generated command name collision '${entry.commandName}' for tools '${previous}' and '${entry.tool.tool.name}'.`);
|
|
738
|
+
}
|
|
739
|
+
commands.set(entry.commandName, entry.tool.tool.name);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
export {
|
|
744
|
+
extractGeneratorFlags,
|
|
745
|
+
markExecutable,
|
|
746
|
+
safeCopyFile,
|
|
747
|
+
stableJsonStringify,
|
|
748
|
+
writeTemplate,
|
|
749
|
+
readPackageMetadata
|
|
750
|
+
};
|