@constructorfleet/sketchforge3d 1.2.1 → 1.3.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 +26 -0
- package/dist/errors.d.ts +34 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +67 -4
- package/dist/errors.js.map +1 -1
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +4 -1
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/resources/shapes.d.ts +8 -0
- package/dist/resources/shapes.d.ts.map +1 -1
- package/dist/resources/shapes.js +25 -0
- package/dist/resources/shapes.js.map +1 -1
- package/dist/types.d.ts +88 -6
- package/dist/types.d.ts.map +1 -1
- package/mcp/macroTools.mjs +239 -0
- package/mcp/server.mjs +524 -0
- package/package.json +4 -2
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Running macros from the MCP bridge, by name only.
|
|
3
|
+
*
|
|
4
|
+
* `freecad-mcp` exposes an `execute_code` tool, which is remote code execution
|
|
5
|
+
* by design: whatever the model writes, the CAD process runs. This does not.
|
|
6
|
+
* The only thing an MCP client can ask for here is one of the files an operator
|
|
7
|
+
* put in `SKETCHFORGE_MACRO_DIR`, chosen from the listing this module produces,
|
|
8
|
+
* with parameters that reach the macro as strings. There is no inline-code tool
|
|
9
|
+
* and no opt-in flag that adds one: a macro is reviewed once, by the person who
|
|
10
|
+
* installed it, rather than on every call.
|
|
11
|
+
*
|
|
12
|
+
* With no macro directory configured, both tools report that and do nothing, so
|
|
13
|
+
* a default install exposes no way to run a local file at all.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { spawn } from "node:child_process";
|
|
17
|
+
import { existsSync } from "node:fs";
|
|
18
|
+
import { readdir, stat } from "node:fs/promises";
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
import { fileURLToPath } from "node:url";
|
|
21
|
+
|
|
22
|
+
export const MACRO_DIR_ENV = "SKETCHFORGE_MACRO_DIR";
|
|
23
|
+
export const MACRO_TIMEOUT_ENV = "SKETCHFORGE_MACRO_TIMEOUT_MS";
|
|
24
|
+
|
|
25
|
+
const DEFAULT_TIMEOUT_MS = 120_000;
|
|
26
|
+
const MAX_OUTPUT_BYTES = 64 * 1024;
|
|
27
|
+
|
|
28
|
+
/** Macros are scripts for the two runners this repo ships; nothing else is executable. */
|
|
29
|
+
const RUNNABLE_EXTENSIONS = new Set([".mjs", ".js", ".py"]);
|
|
30
|
+
|
|
31
|
+
// Installed from npm, this module sits at <package>/mcp, so the JavaScript
|
|
32
|
+
// macro runner it spawns is its own package's `dist/cli.js` -- the same path it
|
|
33
|
+
// has in a checkout. The Python package only exists in a checkout; without it,
|
|
34
|
+
// a Python macro runs against a pip-installed `sketchforge3d`, which is what
|
|
35
|
+
// anyone running this from npx will have.
|
|
36
|
+
const moduleDirectory = path.dirname(fileURLToPath(import.meta.url));
|
|
37
|
+
const clientPackageRoot = path.resolve(moduleDirectory, "..");
|
|
38
|
+
const bundledPythonSource = path.resolve(clientPackageRoot, "..", "sketchforge-py", "src");
|
|
39
|
+
|
|
40
|
+
export class MacroToolError extends Error {}
|
|
41
|
+
|
|
42
|
+
export function macroDirectory(environment = process.env) {
|
|
43
|
+
const configured = environment[MACRO_DIR_ENV];
|
|
44
|
+
if (!configured) {
|
|
45
|
+
throw new MacroToolError(
|
|
46
|
+
`No macro directory is configured. Set ${MACRO_DIR_ENV} to a folder of reviewed macro files to enable this tool; ` +
|
|
47
|
+
"it runs only files that are already in that folder, never code sent to it.",
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
return path.resolve(configured);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The macros an operator has installed.
|
|
55
|
+
*
|
|
56
|
+
* The listing is also the allow-list: a run request is matched against these
|
|
57
|
+
* names, so a name is never joined onto a path and `../` cannot escape.
|
|
58
|
+
*/
|
|
59
|
+
export async function listMacros(environment = process.env) {
|
|
60
|
+
const directory = macroDirectory(environment);
|
|
61
|
+
let entries;
|
|
62
|
+
try {
|
|
63
|
+
entries = await readdir(directory, { withFileTypes: true });
|
|
64
|
+
} catch (error) {
|
|
65
|
+
throw new MacroToolError(`${directory} could not be read: ${error instanceof Error ? error.message : String(error)}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const macros = [];
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
if (!entry.isFile() && !entry.isSymbolicLink()) continue;
|
|
71
|
+
const extension = path.extname(entry.name).toLowerCase();
|
|
72
|
+
if (!RUNNABLE_EXTENSIONS.has(extension)) continue;
|
|
73
|
+
const full = path.join(directory, entry.name);
|
|
74
|
+
const info = await stat(full).catch(() => null);
|
|
75
|
+
// A symlink out of the directory is still a file the operator put there on
|
|
76
|
+
// purpose; what matters is that the name came from this listing.
|
|
77
|
+
if (!info?.isFile()) continue;
|
|
78
|
+
macros.push({
|
|
79
|
+
name: entry.name,
|
|
80
|
+
language: extension === ".py" ? "python" : "javascript",
|
|
81
|
+
bytes: info.size,
|
|
82
|
+
modifiedAt: new Date(info.mtimeMs).toISOString(),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return { directory, macros: macros.sort((left, right) => left.name.localeCompare(right.name)) };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function javascriptRunner(environment) {
|
|
89
|
+
const configured = environment.SKETCHFORGE_MACRO_RUNNER_JS;
|
|
90
|
+
if (configured) return configured;
|
|
91
|
+
const bundled = path.join(clientPackageRoot, "dist", "cli.js");
|
|
92
|
+
if (existsSync(bundled)) return bundled;
|
|
93
|
+
throw new MacroToolError(
|
|
94
|
+
"No JavaScript macro runner was found. Build it with `npm run build --prefix packages/sketchforge-client`, " +
|
|
95
|
+
"or point SKETCHFORGE_MACRO_RUNNER_JS at an installed sketchforge-macro.",
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function runnerFor(macroName, environment) {
|
|
100
|
+
if (path.extname(macroName).toLowerCase() === ".py") {
|
|
101
|
+
const python = environment.SKETCHFORGE_MACRO_PYTHON || "python3";
|
|
102
|
+
const pythonPath = existsSync(bundledPythonSource)
|
|
103
|
+
? [bundledPythonSource, environment.PYTHONPATH].filter(Boolean).join(path.delimiter)
|
|
104
|
+
: environment.PYTHONPATH;
|
|
105
|
+
return {
|
|
106
|
+
command: python,
|
|
107
|
+
leadingArguments: ["-m", "sketchforge3d.macro"],
|
|
108
|
+
extraEnvironment: pythonPath ? { PYTHONPATH: pythonPath } : {},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return { command: process.execPath, leadingArguments: [javascriptRunner(environment)], extraEnvironment: {} };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function parameterArguments(params) {
|
|
115
|
+
if (params === undefined || params === null) return [];
|
|
116
|
+
if (typeof params !== "object" || Array.isArray(params)) {
|
|
117
|
+
throw new MacroToolError("params must be an object of name/value pairs.");
|
|
118
|
+
}
|
|
119
|
+
return Object.entries(params).map(([name, value]) => {
|
|
120
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
|
|
121
|
+
throw new MacroToolError(`"${name}" is not a parameter name a macro can declare.`);
|
|
122
|
+
}
|
|
123
|
+
if (value !== null && typeof value === "object") {
|
|
124
|
+
throw new MacroToolError(`Parameter "${name}" must be a number, string or boolean.`);
|
|
125
|
+
}
|
|
126
|
+
return `--${name}=${String(value)}`;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function truncate(text) {
|
|
131
|
+
return text.length > MAX_OUTPUT_BYTES ? `${text.slice(0, MAX_OUTPUT_BYTES)}\n...output truncated...` : text;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Runs one installed macro and reports what the runner printed.
|
|
136
|
+
*
|
|
137
|
+
* `spawn` with an argument list rather than a shell: a parameter value is one
|
|
138
|
+
* argument whatever it contains, and no part of it is ever parsed as a command.
|
|
139
|
+
*/
|
|
140
|
+
export async function runMacroByName(request, options = {}) {
|
|
141
|
+
const environment = options.environment ?? process.env;
|
|
142
|
+
const spawnImpl = options.spawn ?? spawn;
|
|
143
|
+
const name = typeof request?.macro === "string" ? request.macro.trim() : "";
|
|
144
|
+
if (!name) throw new MacroToolError("Name the macro to run; sketchforge_list_macros shows what is installed.");
|
|
145
|
+
|
|
146
|
+
const { directory, macros } = await listMacros(environment);
|
|
147
|
+
const match = macros.find((macro) => macro.name === name);
|
|
148
|
+
if (!match) {
|
|
149
|
+
throw new MacroToolError(
|
|
150
|
+
`No macro named "${name}" is installed in ${directory}. Installed: ${macros.map((macro) => macro.name).join(", ") || "none"}.`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const { command, leadingArguments, extraEnvironment } = runnerFor(match.name, environment);
|
|
155
|
+
const args = [...leadingArguments, path.join(directory, match.name), "--json", ...parameterArguments(request.params)];
|
|
156
|
+
if (typeof request.projectId === "string" && request.projectId) args.push(`--project=${request.projectId}`);
|
|
157
|
+
|
|
158
|
+
const timeoutMs = Number(request.timeoutMs ?? environment[MACRO_TIMEOUT_ENV] ?? DEFAULT_TIMEOUT_MS);
|
|
159
|
+
const started = Date.now();
|
|
160
|
+
|
|
161
|
+
return new Promise((resolve, reject) => {
|
|
162
|
+
const child = spawnImpl(command, args, {
|
|
163
|
+
cwd: directory,
|
|
164
|
+
env: { ...environment, ...extraEnvironment },
|
|
165
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
let stdout = "";
|
|
169
|
+
let stderr = "";
|
|
170
|
+
let timedOut = false;
|
|
171
|
+
const timer = setTimeout(() => {
|
|
172
|
+
timedOut = true;
|
|
173
|
+
child.kill("SIGKILL");
|
|
174
|
+
}, Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : DEFAULT_TIMEOUT_MS);
|
|
175
|
+
|
|
176
|
+
child.stdout?.on("data", (chunk) => {
|
|
177
|
+
stdout += chunk;
|
|
178
|
+
});
|
|
179
|
+
child.stderr?.on("data", (chunk) => {
|
|
180
|
+
stderr += chunk;
|
|
181
|
+
});
|
|
182
|
+
child.on("error", (error) => {
|
|
183
|
+
clearTimeout(timer);
|
|
184
|
+
reject(new MacroToolError(`${command} could not be run: ${error.message}`));
|
|
185
|
+
});
|
|
186
|
+
child.on("close", (code) => {
|
|
187
|
+
clearTimeout(timer);
|
|
188
|
+
if (timedOut) {
|
|
189
|
+
reject(new MacroToolError(`${match.name} was still running after ${timeoutMs} ms and was stopped.`));
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (code !== 0) {
|
|
193
|
+
reject(new MacroToolError(`${match.name} failed (exit ${code}): ${truncate(stderr || stdout).trim()}`));
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
let report = null;
|
|
197
|
+
try {
|
|
198
|
+
report = JSON.parse(stdout);
|
|
199
|
+
} catch {
|
|
200
|
+
// A macro that printed something other than the runner's report is not
|
|
201
|
+
// an error; the text is what the caller gets.
|
|
202
|
+
}
|
|
203
|
+
resolve({
|
|
204
|
+
macro: match.name,
|
|
205
|
+
durationMs: Date.now() - started,
|
|
206
|
+
report,
|
|
207
|
+
...(report ? {} : { output: truncate(stdout).trim() }),
|
|
208
|
+
log: truncate(stderr).trim(),
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export const macroTools = [
|
|
215
|
+
{
|
|
216
|
+
name: "sketchforge_list_macros",
|
|
217
|
+
description:
|
|
218
|
+
"List the macro scripts an operator has installed in SKETCHFORGE_MACRO_DIR. These are the only macros sketchforge_run_macro will run.",
|
|
219
|
+
inputSchema: { type: "object", properties: {} },
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
name: "sketchforge_run_macro",
|
|
223
|
+
description:
|
|
224
|
+
"Run one installed macro by name, against the headless REST API. Parameters are the macro's own declared parameters. Only files already present in SKETCHFORGE_MACRO_DIR can be run; code cannot be passed in.",
|
|
225
|
+
inputSchema: {
|
|
226
|
+
type: "object",
|
|
227
|
+
required: ["macro"],
|
|
228
|
+
properties: {
|
|
229
|
+
macro: { type: "string", description: "A file name from sketchforge_list_macros." },
|
|
230
|
+
params: {
|
|
231
|
+
type: "object",
|
|
232
|
+
description: "The macro's parameters, as name/value pairs of numbers, strings or booleans.",
|
|
233
|
+
},
|
|
234
|
+
projectId: { type: "string", description: "The project to build in, for a macro declaring project: target." },
|
|
235
|
+
timeoutMs: { type: "number", description: "How long to let the macro run. Defaults to 120000." },
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
];
|