@constructorfleet/sketchforge3d 1.5.0 → 1.7.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 +38 -6
- package/dist/client.d.ts +27 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +38 -0
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/resources/arrange.d.ts +75 -0
- package/dist/resources/arrange.d.ts.map +1 -0
- package/dist/resources/arrange.js +137 -0
- package/dist/resources/arrange.js.map +1 -0
- package/dist/resources/imports.d.ts +34 -0
- package/dist/resources/imports.d.ts.map +1 -0
- package/dist/resources/imports.js +60 -0
- package/dist/resources/imports.js.map +1 -0
- package/dist/types.d.ts +87 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -1
- package/mcp/localEngine.mjs +402 -0
- package/mcp/localMacroRunner.mjs +125 -0
- package/mcp/macroTools.mjs +8 -1
- package/mcp/server.mjs +107 -51
- package/package.json +7 -2
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { writeFile, unlink } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { directoryProjectStore, createEngine } from "@constructorfleet/sketchforge-engine";
|
|
6
|
+
import { macroFromModule, runMacro } from "../dist/index.js";
|
|
7
|
+
|
|
8
|
+
const root = process.env.SKETCHFORGE_PROJECTS_DIR;
|
|
9
|
+
if (!root) throw new Error("SKETCHFORGE_PROJECTS_DIR is required for local macro execution.");
|
|
10
|
+
const store = directoryProjectStore(path.resolve(root));
|
|
11
|
+
const engine = createEngine(store);
|
|
12
|
+
const unwrap = (result) => {
|
|
13
|
+
if (result.ok) return result.value;
|
|
14
|
+
throw new Error(`Engine operation failed: ${JSON.stringify(result.failure)}`);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
async function projectSummary(record) {
|
|
18
|
+
const project = unwrap(await engine.project(record.projectId));
|
|
19
|
+
return {
|
|
20
|
+
id: record.projectId,
|
|
21
|
+
name: String(record.metadata.name ?? "Untitled design"),
|
|
22
|
+
shapes: project.shapes.length,
|
|
23
|
+
revision: project.revision,
|
|
24
|
+
updatedAt: record.updatedAt,
|
|
25
|
+
packageBytes: record.packageBytes,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function createProject(name) {
|
|
30
|
+
const id = randomUUID();
|
|
31
|
+
const now = Date.now();
|
|
32
|
+
await writeFile(path.join(root, `${id}.json`), JSON.stringify({ name: String(name).slice(0, 80), createdAt: now, updatedAt: now, revision: 1 }));
|
|
33
|
+
return { id, name: String(name).slice(0, 80), shapes: 0, revision: 1, updatedAt: now, packageBytes: 0 };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function projectHandle(id) {
|
|
37
|
+
const read = async () => unwrap(await engine.project(id));
|
|
38
|
+
const addMany = async (shapes) => unwrap(await engine.addShapes(id, shapes));
|
|
39
|
+
const getEdges = async (shapeId) => unwrap(await engine.edges(id, shapeId));
|
|
40
|
+
const withTriangleCount = (written) => ({ ...written, triangles: written.shape.importedMesh?.triangleCount ?? 0 });
|
|
41
|
+
const treat = async (shapeId, treatment) => withTriangleCount(unwrap(await engine.treat(id, shapeId, treatment)));
|
|
42
|
+
const shell = async (shapeId, request) => withTriangleCount(unwrap(await engine.shell(id, shapeId, request)));
|
|
43
|
+
return {
|
|
44
|
+
id,
|
|
45
|
+
get: async () => {
|
|
46
|
+
const project = await read();
|
|
47
|
+
return { id, name: project.name, createdAt: project.createdAt, snapGrid: project.snapGrid, workspace: project.workspace, shapes: project.shapes };
|
|
48
|
+
},
|
|
49
|
+
listShapes: async () => (await read()).shapes,
|
|
50
|
+
add: async (shape) => (await addMany([shape])).shapes[0],
|
|
51
|
+
addMany,
|
|
52
|
+
update: async (shapeId, changes) => unwrap(await engine.changeShape(id, shapeId, changes)),
|
|
53
|
+
remove: (shapeId) => engine.removeShape(id, shapeId).then(unwrap),
|
|
54
|
+
clear: async () => {
|
|
55
|
+
const shapes = (await read()).shapes;
|
|
56
|
+
for (const shape of shapes) await unwrap(await engine.removeShape(id, shape.id));
|
|
57
|
+
return shapes.length;
|
|
58
|
+
},
|
|
59
|
+
edges: getEdges,
|
|
60
|
+
filletableEdges: async (shapeId) => (await getEdges(shapeId)).filter((edge) => edge.filletable).map((edge) => edge.index),
|
|
61
|
+
treat,
|
|
62
|
+
fillet: (shapeId, radius, edges) => treat(shapeId, { kind: "fillet", amount: radius, ...(edges ? { edges } : {}) }),
|
|
63
|
+
chamfer: (shapeId, distance, options = {}) => treat(shapeId, { kind: "chamfer", amount: distance, ...options }),
|
|
64
|
+
faces: async (shapeId) => unwrap(await engine.faces(id, shapeId)),
|
|
65
|
+
facesFacing: async (shapeId, direction) => (await unwrap(await engine.faces(id, shapeId))).filter((face) => face.direction === direction).map((face) => face.index),
|
|
66
|
+
measurements: (shapeId) => engine.measurements(id, shapeId).then(unwrap),
|
|
67
|
+
measureBetween: (from, to) => engine.measureBetween(id, from, to).then(unwrap),
|
|
68
|
+
shell,
|
|
69
|
+
hollow: (shapeId, thickness) => shell(shapeId, { thickness }),
|
|
70
|
+
tray: (shapeId, thickness, openFaces) => shell(shapeId, { thickness, ...(openFaces ? { openFaces } : {}) }),
|
|
71
|
+
align: (request) => engine.align(id, request).then(unwrap),
|
|
72
|
+
alignTops: (ids, anchorId) => engine.align(id, { ids: [...ids], axis: "y", target: "max", ...(anchorId ? { anchorId } : {}) }).then(unwrap),
|
|
73
|
+
distribute: (request) => engine.distribute(id, request).then(unwrap),
|
|
74
|
+
pattern: (request) => engine.pattern(id, request).then(unwrap),
|
|
75
|
+
row: (ids, count, stepX, stepZ = 0) => engine.pattern(id, { ids: [...ids], kind: "linear", count, stepX, stepZ }).then(unwrap),
|
|
76
|
+
ring: (ids, count, center, options = {}) => engine.pattern(id, { ids: [...ids], kind: "circular", count, centerX: center.x, centerZ: center.z, ...options }).then(unwrap),
|
|
77
|
+
importMesh: (request) => engine.importMesh(id, request).then(unwrap),
|
|
78
|
+
cut: (request) => engine.cut(id, request).then(unwrap),
|
|
79
|
+
group: (ids) => engine.group(id, [...ids]).then(unwrap),
|
|
80
|
+
ungroup: (shapeId) => engine.ungroup(id, shapeId).then(unwrap),
|
|
81
|
+
separate: (shapeId) => engine.separate(id, shapeId).then(unwrap),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const client = {
|
|
86
|
+
project: projectHandle,
|
|
87
|
+
projects: {
|
|
88
|
+
list: async () => Promise.all((await store.list()).map(projectSummary)),
|
|
89
|
+
findByName: async (name) => (await Promise.all((await store.list()).map(projectSummary))).find((project) => project.name === name) ?? null,
|
|
90
|
+
create: createProject,
|
|
91
|
+
get: async (id) => projectHandle(id).get(),
|
|
92
|
+
delete: async (id) => {
|
|
93
|
+
const metadata = path.join(root, `${id}.json`);
|
|
94
|
+
const file = path.join(root, `${id}.skf`);
|
|
95
|
+
let deleted = false;
|
|
96
|
+
for (const target of [metadata, file]) try { await unlink(target); deleted = true; } catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
97
|
+
return { deleted };
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
client.shapes = {
|
|
102
|
+
list: async (id) => projectHandle(id).listShapes(),
|
|
103
|
+
create: async (id, shape) => projectHandle(id).add(shape),
|
|
104
|
+
createMany: async (id, shapes) => projectHandle(id).addMany(shapes),
|
|
105
|
+
update: async (id, shapeId, changes) => projectHandle(id).update(shapeId, changes),
|
|
106
|
+
delete: async (id, shapeId) => projectHandle(id).remove(shapeId),
|
|
107
|
+
deleteAll: async (id) => projectHandle(id).clear(),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const argv = process.argv.slice(2);
|
|
111
|
+
const file = argv[0];
|
|
112
|
+
if (!file) throw new Error("Expected a macro file path.");
|
|
113
|
+
const options = { params: {}, targetProjectId: undefined };
|
|
114
|
+
for (const argument of argv.slice(1)) {
|
|
115
|
+
if (argument === "--json") continue;
|
|
116
|
+
if (argument.startsWith("--project=")) options.targetProjectId = argument.slice("--project=".length);
|
|
117
|
+
else if (argument.startsWith("--")) {
|
|
118
|
+
const [key, ...value] = argument.slice(2).split("=");
|
|
119
|
+
options.params[key] = value.join("=");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const module = await import(pathToFileURL(path.resolve(file)).href);
|
|
123
|
+
const definition = macroFromModule(module, path.basename(file).replace(/\.[cm]?[jt]s$/, ""));
|
|
124
|
+
const report = await runMacro(client, definition, options);
|
|
125
|
+
process.stdout.write(`${JSON.stringify(report)}\n`);
|
package/mcp/macroTools.mjs
CHANGED
|
@@ -97,6 +97,12 @@ function javascriptRunner(environment) {
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
function runnerFor(macroName, environment) {
|
|
100
|
+
if (environment.SKETCHFORGE_PROJECTS_DIR) {
|
|
101
|
+
if (path.extname(macroName).toLowerCase() === ".py") {
|
|
102
|
+
throw new MacroToolError("Python macros need the HTTP API adapter; local project mode runs JavaScript macros directly against the engine.");
|
|
103
|
+
}
|
|
104
|
+
return { command: process.execPath, leadingArguments: [path.join(moduleDirectory, "localMacroRunner.mjs")], extraEnvironment: {} };
|
|
105
|
+
}
|
|
100
106
|
if (path.extname(macroName).toLowerCase() === ".py") {
|
|
101
107
|
const python = environment.SKETCHFORGE_MACRO_PYTHON || "python3";
|
|
102
108
|
const pythonPath = existsSync(bundledPythonSource)
|
|
@@ -221,11 +227,12 @@ export const macroTools = [
|
|
|
221
227
|
{
|
|
222
228
|
name: "sketchforge_run_macro",
|
|
223
229
|
description:
|
|
224
|
-
"Run one installed macro by name, against
|
|
230
|
+
"Run one installed macro by name, offline by default or against an optional SketchForge server URL. 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
231
|
inputSchema: {
|
|
226
232
|
type: "object",
|
|
227
233
|
required: ["macro"],
|
|
228
234
|
properties: {
|
|
235
|
+
url: { type: "string", description: "Optional SketchForge server URL. Omit to run against local project files." },
|
|
229
236
|
macro: { type: "string", description: "A file name from sketchforge_list_macros." },
|
|
230
237
|
params: {
|
|
231
238
|
type: "object",
|
package/mcp/server.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import { readFileSync } from "node:fs";
|
|
3
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
import { listMacros, macroTools, runMacroByName } from "./macroTools.mjs";
|
|
7
|
+
import { callLocalEngineTool } from "./localEngine.mjs";
|
|
7
8
|
|
|
8
|
-
const DEFAULT_BASE_URL = "http://127.0.0.1:3000";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* The version this bridge reports is the package's, read at startup rather than
|
|
@@ -23,11 +23,11 @@ const version = (() => {
|
|
|
23
23
|
}
|
|
24
24
|
})();
|
|
25
25
|
const MCP_ROUTE = "/api/sketchforge-mcp";
|
|
26
|
-
const baseUrl = process.env.SKETCHFORGE_URL || DEFAULT_BASE_URL;
|
|
27
|
-
|
|
28
26
|
const editorTargetSchema = {
|
|
29
27
|
type: "object",
|
|
30
28
|
properties: {
|
|
29
|
+
url: { type: "string", description: "Optional SketchForge server URL. Omit to work offline with local project files." },
|
|
30
|
+
projectId: { type: "string", description: "Project id in SKETCHFORGE_PROJECTS_DIR when running the MCP in local mode." },
|
|
31
31
|
editorNumber: { type: "number", description: "The 5-digit SketchForge editor number from sketchforge_list_editors." },
|
|
32
32
|
editorId: { type: "string", description: "Optional internal editor id. Prefer editorNumber for human-directed use." },
|
|
33
33
|
timeoutMs: { type: "number", description: "Command timeout in milliseconds. Defaults to 15000." },
|
|
@@ -38,22 +38,25 @@ const tools = [
|
|
|
38
38
|
{
|
|
39
39
|
name: "sketchforge_list_editors",
|
|
40
40
|
description: "List SketchForge editor tabs that are currently open and heartbeating, including editorNumber and projectName.",
|
|
41
|
-
inputSchema: {
|
|
41
|
+
inputSchema: { ...editorTargetSchema, properties: { url: editorTargetSchema.properties.url } },
|
|
42
42
|
},
|
|
43
43
|
{
|
|
44
44
|
name: "sketchforge_read_scene",
|
|
45
|
-
description: "Read the current scene
|
|
45
|
+
description: "Read the current scene and exact object dimensions from local project files by default, or from an editor when a server url is supplied.",
|
|
46
46
|
inputSchema: {
|
|
47
47
|
...editorTargetSchema,
|
|
48
48
|
properties: {
|
|
49
49
|
...editorTargetSchema.properties,
|
|
50
50
|
includeRawShapes: { type: "boolean", description: "Include raw WorkplaneShape JSON. Can be large for imported meshes." },
|
|
51
|
+
shapeIds: { type: "array", items: { type: "string" }, description: "Limit the response to these object ids (including rawShapes)." },
|
|
52
|
+
excludeShapeIds: { type: "array", items: { type: "string" }, description: "Omit these object ids, useful for large reference meshes." },
|
|
53
|
+
includeRawShapeIds: { type: "array", items: { type: "string" }, description: "Include raw geometry only for these ids; takes precedence over includeRawShapes." },
|
|
51
54
|
},
|
|
52
55
|
},
|
|
53
56
|
},
|
|
54
57
|
{
|
|
55
58
|
name: "sketchforge_list_objects",
|
|
56
|
-
description: "List all
|
|
59
|
+
description: "List all objects in the local project by default, or in the editor when a server url is supplied, with exact dimensions, position, rotation, and object ids.",
|
|
57
60
|
inputSchema: editorTargetSchema,
|
|
58
61
|
},
|
|
59
62
|
{
|
|
@@ -68,6 +71,11 @@ const tools = [
|
|
|
68
71
|
},
|
|
69
72
|
},
|
|
70
73
|
},
|
|
74
|
+
{
|
|
75
|
+
name: "sketchforge_mirror_objects",
|
|
76
|
+
description: "Mirror objects across the center plane of their combined bounds, using the editor's mirror behavior.",
|
|
77
|
+
inputSchema: { ...editorTargetSchema, required: ["axis"], properties: { ...editorTargetSchema.properties, ids: { type: "array", items: { type: "string" }, description: "Object ids; defaults to the current selection." }, axis: { type: "string", enum: ["x", "y", "z"] } } },
|
|
78
|
+
},
|
|
71
79
|
{
|
|
72
80
|
name: "sketchforge_delete_objects",
|
|
73
81
|
description: "Delete objects by id, or delete the current selection when ids are omitted.",
|
|
@@ -91,7 +99,7 @@ const tools = [
|
|
|
91
99
|
kind: { type: "string", enum: ["box", "cube", "cylinder", "sketch", "loft", "revolve", "thread"] },
|
|
92
100
|
profile: {
|
|
93
101
|
type: "array",
|
|
94
|
-
description: "For kind
|
|
102
|
+
description: "For kind sketch: a closed 2D outline to extrude, as [x, z] pairs. For kind revolve: the closed outline to revolve, where x is distance from axis and z runs along it.",
|
|
95
103
|
items: { type: "array", items: { type: "number" }, minItems: 2, maxItems: 2 },
|
|
96
104
|
},
|
|
97
105
|
pitch: { type: "number", description: "For kind revolve/thread: axial rise per turn in mm. 0 is a plain revolve; negative is a left-hand thread." },
|
|
@@ -315,7 +323,7 @@ const tools = [
|
|
|
315
323
|
},
|
|
316
324
|
{
|
|
317
325
|
name: "sketchforge_capture_image",
|
|
318
|
-
description: "Capture a PNG image
|
|
326
|
+
description: "Capture a PNG image from current/home/top/bottom/front/back/right/left view. Local project mode renders the model headlessly; editor mode captures the live viewport.",
|
|
319
327
|
inputSchema: {
|
|
320
328
|
...editorTargetSchema,
|
|
321
329
|
properties: {
|
|
@@ -324,30 +332,39 @@ const tools = [
|
|
|
324
332
|
},
|
|
325
333
|
},
|
|
326
334
|
},
|
|
327
|
-
|
|
328
|
-
|
|
335
|
+
{
|
|
336
|
+
name: "sketchforge_export_scene",
|
|
337
|
+
description: "Export selected objects or the whole scene as STL, OBJ, 3MF, STEP, SKF, or a ZIP kit. Set outputPath to save directly on the MCP host and avoid returning large base64 data. Use shapeIds to keep a reference mesh out of mesh exports.",
|
|
338
|
+
inputSchema: { ...editorTargetSchema, required: ["format"], properties: { ...editorTargetSchema.properties, format: { type: "string", enum: ["stl", "obj", "3mf", "step", "skf", "zip"] }, shapeIds: { type: "array", items: { type: "string" }, description: "Optional object ids; defaults to the current selection, or the whole scene when nothing is selected." }, name: { type: "string" }, outputPath: { type: "string", description: "Optional destination path on the computer running the SketchForge MCP server." } } },
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
name: "sketchforge_validate_scene",
|
|
342
|
+
description: "Check triangle topology, conservative AABB clearance bounds, and exact Manifold intersection volumes for overlapping scene objects.",
|
|
343
|
+
inputSchema: { ...editorTargetSchema, properties: { ...editorTargetSchema.properties, shapeIds: { type: "array", items: { type: "string" } }, excludeShapeIds: { type: "array", items: { type: "string" } } } },
|
|
344
|
+
},
|
|
345
|
+
// CAD operations use the local engine by default and the server API when a URL is supplied.
|
|
329
346
|
{
|
|
330
347
|
name: "sketchforge_list_faces",
|
|
331
|
-
description: "List a stored project shape's CAD faces, with the direction name each one can be opened by, so a hollow can leave the right ones off. Works
|
|
348
|
+
description: "List a stored project shape's CAD faces, with the direction name each one can be opened by, so a hollow can leave the right ones off. Works offline or against a server when url is supplied.",
|
|
332
349
|
inputSchema: {
|
|
333
350
|
type: "object",
|
|
334
351
|
required: ["projectId", "shapeId"],
|
|
335
352
|
properties: {
|
|
353
|
+
url: { type: "string", description: "Optional SketchForge server URL. Omit to work offline with local project files." },
|
|
336
354
|
projectId: { type: "string", description: "The server-side project id." },
|
|
337
355
|
shapeId: { type: "string", description: "The id of the shape to read." },
|
|
338
356
|
},
|
|
339
357
|
},
|
|
340
358
|
},
|
|
341
|
-
// Measuring
|
|
342
|
-
// numbers come from server-side OCCT's own curve and surface analysis, which
|
|
343
|
-
// is the only place the exact geometry lives.
|
|
359
|
+
// Measuring uses exact CAD geometry in either the local engine or server API.
|
|
344
360
|
{
|
|
345
361
|
name: "sketchforge_measure_shape",
|
|
346
|
-
description: "Measure every edge and face of a stored project shape from its exact CAD geometry: edge length, the radius and diameter of a circular edge or arc, face area, and a cylindrical face's radius. Exact (OCCT), so a round hole has a real radius rather than the chord length of a facet. A measurement that is not meaningful for the entity -- the radius of a straight edge, of an ellipse, or of a cone whose radius changes along it -- comes back null with a reason in radiusUnavailable. Works
|
|
362
|
+
description: "Measure every edge and face of a stored project shape from its exact CAD geometry: edge length, the radius and diameter of a circular edge or arc, face area, and a cylindrical face's radius. Exact (OCCT), so a round hole has a real radius rather than the chord length of a facet. A measurement that is not meaningful for the entity -- the radius of a straight edge, of an ellipse, or of a cone whose radius changes along it -- comes back null with a reason in radiusUnavailable. Works offline or against a server when url is supplied.",
|
|
347
363
|
inputSchema: {
|
|
348
364
|
type: "object",
|
|
349
365
|
required: ["projectId", "shapeId"],
|
|
350
366
|
properties: {
|
|
367
|
+
url: { type: "string", description: "Optional SketchForge server URL. Omit to work offline with local project files." },
|
|
351
368
|
projectId: { type: "string", description: "The server-side project id." },
|
|
352
369
|
shapeId: { type: "string", description: "The id of the shape to measure." },
|
|
353
370
|
},
|
|
@@ -355,11 +372,12 @@ const tools = [
|
|
|
355
372
|
},
|
|
356
373
|
{
|
|
357
374
|
name: "sketchforge_measure_between",
|
|
358
|
-
description: "The exact minimum distance between two CAD entities in a stored project, and the angle between them when both are flat faces. Each end names a shape and either the whole solid, or one edge or face by the index the measurement listing gave it. Distance is measured between the real surfaces where the shapes sit, so two round posts 5mm apart at their nearest walls read 5 -- not the gap between their bounding boxes. Works
|
|
375
|
+
description: "The exact minimum distance between two CAD entities in a stored project, and the angle between them when both are flat faces. Each end names a shape and either the whole solid, or one edge or face by the index the measurement listing gave it. Distance is measured between the real surfaces where the shapes sit, so two round posts 5mm apart at their nearest walls read 5 -- not the gap between their bounding boxes. Works offline or against a server when url is supplied.",
|
|
359
376
|
inputSchema: {
|
|
360
377
|
type: "object",
|
|
361
378
|
required: ["projectId", "from", "to"],
|
|
362
379
|
properties: {
|
|
380
|
+
url: { type: "string", description: "Optional SketchForge server URL. Omit to work offline with local project files." },
|
|
363
381
|
projectId: { type: "string", description: "The server-side project id." },
|
|
364
382
|
from: {
|
|
365
383
|
type: "object",
|
|
@@ -388,11 +406,12 @@ const tools = [
|
|
|
388
406
|
},
|
|
389
407
|
{
|
|
390
408
|
name: "sketchforge_hollow_shape",
|
|
391
|
-
description: "Hollow a stored project shape out to a wall of a given thickness, optionally leaving faces open so a box becomes a tray. Exact (OCCT), so the result still exports to STEP and can still be filleted. A sphere, torus, cone or round-walled tube cannot be hollowed at any thickness. Works
|
|
409
|
+
description: "Hollow a stored project shape out to a wall of a given thickness, optionally leaving faces open so a box becomes a tray. Exact (OCCT), so the result still exports to STEP and can still be filleted. A sphere, torus, cone or round-walled tube cannot be hollowed at any thickness. Works offline or against a server when url is supplied.",
|
|
392
410
|
inputSchema: {
|
|
393
411
|
type: "object",
|
|
394
412
|
required: ["projectId", "shapeId", "thickness"],
|
|
395
413
|
properties: {
|
|
414
|
+
url: { type: "string", description: "Optional SketchForge server URL. Omit to work offline with local project files." },
|
|
396
415
|
projectId: { type: "string", description: "The server-side project id." },
|
|
397
416
|
shapeId: { type: "string", description: "The id of the shape to hollow." },
|
|
398
417
|
thickness: { type: "number", description: "Wall thickness in mm, measured inwards. Must be well under half the part's smallest span." },
|
|
@@ -404,8 +423,7 @@ const tools = [
|
|
|
404
423
|
},
|
|
405
424
|
},
|
|
406
425
|
},
|
|
407
|
-
// Macros
|
|
408
|
-
// from the directory the operator configured.
|
|
426
|
+
// Macros run only from the directory the operator configured.
|
|
409
427
|
...macroTools,
|
|
410
428
|
];
|
|
411
429
|
|
|
@@ -419,7 +437,7 @@ const API_TOKEN_ENV = "SKETCHFORGE_API_TOKEN";
|
|
|
419
437
|
* to ask one for. The token is the same one the server was started with, and
|
|
420
438
|
* without it every route answers 404 rather than saying it is switched off.
|
|
421
439
|
*/
|
|
422
|
-
async function restRequest(method, path, { query = {}, body } = {}) {
|
|
440
|
+
async function restRequest(method, path, { query = {}, body, baseUrl } = {}) {
|
|
423
441
|
const token = process.env[API_TOKEN_ENV];
|
|
424
442
|
if (!token) {
|
|
425
443
|
throw new Error(
|
|
@@ -448,12 +466,12 @@ async function restRequest(method, path, { query = {}, body } = {}) {
|
|
|
448
466
|
return payload;
|
|
449
467
|
}
|
|
450
468
|
|
|
451
|
-
function bridgeUrl() {
|
|
469
|
+
function bridgeUrl(baseUrl) {
|
|
452
470
|
return new URL(MCP_ROUTE, baseUrl);
|
|
453
471
|
}
|
|
454
472
|
|
|
455
|
-
async function bridgeGet() {
|
|
456
|
-
const response = await fetch(bridgeUrl());
|
|
473
|
+
async function bridgeGet(baseUrl) {
|
|
474
|
+
const response = await fetch(bridgeUrl(baseUrl));
|
|
457
475
|
const payload = await response.json().catch(() => null);
|
|
458
476
|
if (!response.ok) {
|
|
459
477
|
throw new Error(payload?.error || `SketchForge bridge returned HTTP ${response.status}`);
|
|
@@ -461,13 +479,13 @@ async function bridgeGet() {
|
|
|
461
479
|
return payload;
|
|
462
480
|
}
|
|
463
481
|
|
|
464
|
-
async function bridgeCommand(action, args = {}, defaultTimeoutMs = 15000) {
|
|
465
|
-
const { editorNumber, editorId, timeoutMs, ...params } = args || {};
|
|
482
|
+
async function bridgeCommand(action, args = {}, defaultTimeoutMs = 15000, baseUrl) {
|
|
483
|
+
const { editorNumber, editorId, timeoutMs, url, ...params } = args || {};
|
|
466
484
|
let targetNumber = typeof editorNumber === "number" ? editorNumber : undefined;
|
|
467
485
|
let targetId = typeof editorId === "string" ? editorId : undefined;
|
|
468
486
|
|
|
469
487
|
if (!targetNumber && !targetId) {
|
|
470
|
-
const { editors = [] } = await bridgeGet();
|
|
488
|
+
const { editors = [] } = await bridgeGet(baseUrl);
|
|
471
489
|
if (editors.length === 1) {
|
|
472
490
|
targetNumber = editors[0].editorNumber;
|
|
473
491
|
} else {
|
|
@@ -475,7 +493,7 @@ async function bridgeCommand(action, args = {}, defaultTimeoutMs = 15000) {
|
|
|
475
493
|
}
|
|
476
494
|
}
|
|
477
495
|
|
|
478
|
-
const response = await fetch(bridgeUrl(), {
|
|
496
|
+
const response = await fetch(bridgeUrl(baseUrl), {
|
|
479
497
|
method: "POST",
|
|
480
498
|
headers: { "Content-Type": "application/json" },
|
|
481
499
|
body: JSON.stringify({
|
|
@@ -494,50 +512,53 @@ async function bridgeCommand(action, args = {}, defaultTimeoutMs = 15000) {
|
|
|
494
512
|
return payload.data;
|
|
495
513
|
}
|
|
496
514
|
|
|
497
|
-
async function
|
|
515
|
+
async function callRemoteTool(name, args, baseUrl) {
|
|
498
516
|
switch (name) {
|
|
499
517
|
case "sketchforge_list_editors":
|
|
500
|
-
return bridgeGet();
|
|
518
|
+
return bridgeGet(baseUrl);
|
|
501
519
|
case "sketchforge_read_scene":
|
|
502
|
-
return bridgeCommand("get_scene", args);
|
|
520
|
+
return bridgeCommand("get_scene", args, 15000, baseUrl);
|
|
503
521
|
case "sketchforge_list_objects":
|
|
504
|
-
return bridgeCommand("list_objects", args);
|
|
522
|
+
return bridgeCommand("list_objects", args, 15000, baseUrl);
|
|
505
523
|
case "sketchforge_select_objects":
|
|
506
|
-
return bridgeCommand("select_objects", args);
|
|
524
|
+
return bridgeCommand("select_objects", args, 15000, baseUrl);
|
|
525
|
+
case "sketchforge_mirror_objects":
|
|
526
|
+
return bridgeCommand("mirror_objects", args, 15000, baseUrl);
|
|
507
527
|
case "sketchforge_delete_objects":
|
|
508
|
-
return bridgeCommand("delete_objects", args);
|
|
528
|
+
return bridgeCommand("delete_objects", args, 15000, baseUrl);
|
|
509
529
|
case "sketchforge_create_shape":
|
|
510
|
-
return bridgeCommand("create_shape", args);
|
|
530
|
+
return bridgeCommand("create_shape", args, 15000, baseUrl);
|
|
511
531
|
case "sketchforge_import_mesh":
|
|
512
|
-
return bridgeCommand("import_mesh", args, 60000);
|
|
532
|
+
return bridgeCommand("import_mesh", args, 60000, baseUrl);
|
|
513
533
|
case "sketchforge_update_object":
|
|
514
|
-
return bridgeCommand("update_object", args);
|
|
534
|
+
return bridgeCommand("update_object", args, 15000, baseUrl);
|
|
515
535
|
case "sketchforge_align_objects":
|
|
516
|
-
return bridgeCommand("align_objects", args);
|
|
536
|
+
return bridgeCommand("align_objects", args, 15000, baseUrl);
|
|
517
537
|
case "sketchforge_distribute_objects":
|
|
518
|
-
return bridgeCommand("distribute_objects", args);
|
|
538
|
+
return bridgeCommand("distribute_objects", args, 15000, baseUrl);
|
|
519
539
|
case "sketchforge_pattern_objects":
|
|
520
|
-
return bridgeCommand("pattern_objects", args);
|
|
540
|
+
return bridgeCommand("pattern_objects", args, 15000, baseUrl);
|
|
521
541
|
case "sketchforge_group_objects":
|
|
522
|
-
return bridgeCommand("group_objects", args, 30000);
|
|
542
|
+
return bridgeCommand("group_objects", args, 30000, baseUrl);
|
|
523
543
|
case "sketchforge_ungroup_objects":
|
|
524
|
-
return bridgeCommand("ungroup_objects", args);
|
|
544
|
+
return bridgeCommand("ungroup_objects", args, 15000, baseUrl);
|
|
525
545
|
case "sketchforge_boolean_cut":
|
|
526
|
-
return bridgeCommand("boolean_cut", args, 45000);
|
|
546
|
+
return bridgeCommand("boolean_cut", args, 45000, baseUrl);
|
|
527
547
|
case "sketchforge_separate_parts":
|
|
528
|
-
return bridgeCommand("separate_parts", args);
|
|
548
|
+
return bridgeCommand("separate_parts", args, 15000, baseUrl);
|
|
529
549
|
case "sketchforge_list_edges":
|
|
530
|
-
return bridgeCommand("list_edges", args, 30000);
|
|
550
|
+
return bridgeCommand("list_edges", args, 30000, baseUrl);
|
|
531
551
|
case "sketchforge_apply_edge_treatment":
|
|
532
|
-
return bridgeCommand("apply_edge_treatment", args, 60000);
|
|
552
|
+
return bridgeCommand("apply_edge_treatment", args, 60000, baseUrl);
|
|
533
553
|
case "sketchforge_list_faces":
|
|
534
|
-
return restRequest("GET", "/faces", { query: { projectId: args.projectId, shapeId: args.shapeId } });
|
|
554
|
+
return restRequest("GET", "/faces", { query: { projectId: args.projectId, shapeId: args.shapeId }, baseUrl });
|
|
535
555
|
case "sketchforge_measure_shape":
|
|
536
|
-
return restRequest("GET", "/measurements", { query: { projectId: args.projectId, shapeId: args.shapeId } });
|
|
556
|
+
return restRequest("GET", "/measurements", { query: { projectId: args.projectId, shapeId: args.shapeId }, baseUrl });
|
|
537
557
|
case "sketchforge_measure_between":
|
|
538
558
|
return restRequest("POST", "/measurements", {
|
|
539
559
|
query: { projectId: args.projectId },
|
|
540
560
|
body: { from: args.from, to: args.to },
|
|
561
|
+
baseUrl,
|
|
541
562
|
});
|
|
542
563
|
case "sketchforge_hollow_shape":
|
|
543
564
|
return restRequest("POST", "/shell", {
|
|
@@ -546,20 +567,55 @@ async function callTool(name, args) {
|
|
|
546
567
|
thickness: args.thickness,
|
|
547
568
|
...(args.openFaces === undefined ? {} : { openFaces: args.openFaces }),
|
|
548
569
|
},
|
|
570
|
+
baseUrl,
|
|
549
571
|
});
|
|
550
572
|
case "sketchforge_inspect_errors":
|
|
551
|
-
return bridgeCommand("inspect_errors", args);
|
|
573
|
+
return bridgeCommand("inspect_errors", args, 15000, baseUrl);
|
|
552
574
|
case "sketchforge_capture_image":
|
|
553
|
-
return bridgeCommand("capture_image", args, 30000);
|
|
575
|
+
return bridgeCommand("capture_image", args, 30000, baseUrl);
|
|
576
|
+
case "sketchforge_export_scene": {
|
|
577
|
+
const artifact = await bridgeCommand("export_scene", args, 60000, baseUrl);
|
|
578
|
+
if (typeof args.outputPath === "string" && args.outputPath.trim()) {
|
|
579
|
+
const destination = path.resolve(args.outputPath);
|
|
580
|
+
writeFileSync(destination, Buffer.from(artifact.base64, "base64"));
|
|
581
|
+
return { filename: artifact.filename, mimeType: artifact.mimeType, bytes: artifact.bytes, shapeCount: artifact.shapeCount, savedTo: destination };
|
|
582
|
+
}
|
|
583
|
+
return artifact;
|
|
584
|
+
}
|
|
585
|
+
case "sketchforge_validate_scene":
|
|
586
|
+
return bridgeCommand("validate_scene", args, 60000, baseUrl);
|
|
554
587
|
case "sketchforge_list_macros":
|
|
555
588
|
return listMacros();
|
|
556
589
|
case "sketchforge_run_macro":
|
|
557
|
-
return runMacroByName(args);
|
|
590
|
+
return runMacroByName(args, { environment: { ...process.env, SKETCHFORGE_URL: baseUrl } });
|
|
558
591
|
default:
|
|
559
592
|
throw new Error(`Unknown tool: ${name}`);
|
|
560
593
|
}
|
|
561
594
|
}
|
|
562
595
|
|
|
596
|
+
function normalizedServerUrl(value) {
|
|
597
|
+
let parsed;
|
|
598
|
+
try {
|
|
599
|
+
parsed = new URL(value);
|
|
600
|
+
} catch {
|
|
601
|
+
throw new Error("url must be an absolute http:// or https:// SketchForge server URL.");
|
|
602
|
+
}
|
|
603
|
+
if (!["http:", "https:"].includes(parsed.protocol) || parsed.username || parsed.password) {
|
|
604
|
+
throw new Error("url must be an absolute http:// or https:// SketchForge server URL without embedded credentials.");
|
|
605
|
+
}
|
|
606
|
+
return parsed.toString().replace(/\/$/, "");
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
async function callTool(name, args = {}) {
|
|
610
|
+
const { url, ...toolArgs } = args || {};
|
|
611
|
+
if (typeof url === "string" && url.trim()) {
|
|
612
|
+
return callRemoteTool(name, toolArgs, normalizedServerUrl(url.trim()));
|
|
613
|
+
}
|
|
614
|
+
if (name === "sketchforge_list_macros") return listMacros();
|
|
615
|
+
if (name === "sketchforge_run_macro") return runMacroByName(toolArgs);
|
|
616
|
+
return callLocalEngineTool(name, toolArgs);
|
|
617
|
+
}
|
|
618
|
+
|
|
563
619
|
function textContent(value) {
|
|
564
620
|
return {
|
|
565
621
|
type: "text",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructorfleet/sketchforge3d",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Typed client and macro runner for the SketchForge REST API.",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"author": {
|
|
@@ -18,6 +18,11 @@
|
|
|
18
18
|
"macro",
|
|
19
19
|
"client"
|
|
20
20
|
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@constructorfleet/sketchforge-engine": "1.7.0",
|
|
23
|
+
"fflate": "^0.8.3",
|
|
24
|
+
"sharp": "^0.35.5"
|
|
25
|
+
},
|
|
21
26
|
"type": "module",
|
|
22
27
|
"main": "./dist/index.js",
|
|
23
28
|
"types": "./dist/index.d.ts",
|
|
@@ -38,7 +43,7 @@
|
|
|
38
43
|
"README.md"
|
|
39
44
|
],
|
|
40
45
|
"engines": {
|
|
41
|
-
"node": ">=
|
|
46
|
+
"node": ">=20"
|
|
42
47
|
},
|
|
43
48
|
"publishConfig": {
|
|
44
49
|
"access": "public"
|