@constructorfleet/sketchforge3d 1.31.0 → 1.33.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 +65 -3
- package/dist/cli.d.ts +3 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +97 -6
- package/dist/cli.js.map +1 -1
- package/dist/client.d.ts +41 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +63 -0
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +39 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +105 -2
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/resources/configurations.d.ts +21 -0
- package/dist/resources/configurations.d.ts.map +1 -0
- package/dist/resources/configurations.js +44 -0
- package/dist/resources/configurations.js.map +1 -0
- package/dist/resources/parameters.d.ts +16 -0
- package/dist/resources/parameters.d.ts.map +1 -0
- package/dist/resources/parameters.js +53 -0
- package/dist/resources/parameters.js.map +1 -0
- package/dist/resources/sketch-constraints.d.ts +11 -0
- package/dist/resources/sketch-constraints.d.ts.map +1 -0
- package/dist/resources/sketch-constraints.js +24 -0
- package/dist/resources/sketch-constraints.js.map +1 -0
- package/dist/types.d.ts +160 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -1
- package/examples/parametric-box.macro.mjs +44 -0
- package/mcp/filesystemPaths.mjs +9 -2
- package/mcp/hosted.d.mts +10 -0
- package/mcp/hosted.mjs +79 -0
- package/mcp/localEngine.mjs +15 -6
- package/mcp/operation-parity.mjs +12 -1
- package/mcp/parameterTools.mjs +253 -0
- package/mcp/protocol.mjs +87 -0
- package/mcp/server.mjs +10 -960
- package/mcp/streamableHttp.mjs +116 -0
- package/mcp/toolCatalog.mjs +758 -0
- package/mcp/tools.mjs +296 -0
- package/package.json +2 -2
package/mcp/tools.mjs
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { resolveMcpReadPath, resolveMcpWritePath } from "./filesystemPaths.mjs";
|
|
4
|
+
import { listMacros, macroTools, runMacroByName } from "./macroTools.mjs";
|
|
5
|
+
import { callLocalEngineTool } from "./localEngine.mjs";
|
|
6
|
+
import { callParameterTool, McpToolError, PARAMETER_TOOL_NAMES } from "./parameterTools.mjs";
|
|
7
|
+
import { engineTools } from "./toolCatalog.mjs";
|
|
8
|
+
|
|
9
|
+
const MCP_ROUTE = "/api/sketchforge-mcp";
|
|
10
|
+
|
|
11
|
+
export const tools = [
|
|
12
|
+
...engineTools,
|
|
13
|
+
// Macros run only from the directory the operator configured.
|
|
14
|
+
...macroTools,
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const API_TOKEN_ENV = "SKETCHFORGE_API_TOKEN";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* One request to the headless REST API, as the operator's token.
|
|
21
|
+
*
|
|
22
|
+
* The editor bridge cannot serve every tool here: hollowing is a server-side
|
|
23
|
+
* OCCT operation on a stored project, with no editor tab involved and nothing
|
|
24
|
+
* to ask one for. The token is the same one the server was started with, and
|
|
25
|
+
* without it every route answers 404 rather than saying it is switched off.
|
|
26
|
+
*/
|
|
27
|
+
async function restRequest(method, path, { query = {}, body, baseUrl, withDetail = false } = {}) {
|
|
28
|
+
const token = process.env[API_TOKEN_ENV];
|
|
29
|
+
if (!token) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`${API_TOKEN_ENV} is not set. It is the same token the SketchForge server was started with; without it the REST API answers 404 to everything.`,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
const url = new URL(`/api/v1${path}`, baseUrl);
|
|
35
|
+
for (const [key, value] of Object.entries(query)) {
|
|
36
|
+
if (value !== undefined && value !== null) url.searchParams.set(key, String(value));
|
|
37
|
+
}
|
|
38
|
+
const response = await fetch(url, {
|
|
39
|
+
method,
|
|
40
|
+
headers: { authorization: `Bearer ${token}`, ...(body ? { "Content-Type": "application/json" } : {}) },
|
|
41
|
+
...(body ? { body: JSON.stringify(body) } : {}),
|
|
42
|
+
});
|
|
43
|
+
const payload = await response.json().catch(() => null);
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
// The sentence is for whoever reads the log; the code is the half a client
|
|
46
|
+
// can act on, so carry both rather than flattening to a status.
|
|
47
|
+
if (!payload?.error) throw new McpToolError(`SketchForge API returned HTTP ${response.status}`);
|
|
48
|
+
const { error, code, ...detail } = payload;
|
|
49
|
+
throw new McpToolError(error, code, withDetail ? detail : {});
|
|
50
|
+
}
|
|
51
|
+
return payload;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function bridgeUrl(baseUrl) {
|
|
55
|
+
return new URL(MCP_ROUTE, baseUrl);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function bridgeGet(baseUrl) {
|
|
59
|
+
const response = await fetch(bridgeUrl(baseUrl));
|
|
60
|
+
const payload = await response.json().catch(() => null);
|
|
61
|
+
if (!response.ok) {
|
|
62
|
+
throw new Error(payload?.error || `SketchForge bridge returned HTTP ${response.status}`);
|
|
63
|
+
}
|
|
64
|
+
return payload;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function bridgeCommand(action, args = {}, defaultTimeoutMs = 15000, baseUrl) {
|
|
68
|
+
const { editorNumber, editorId, timeoutMs, url, ...params } = args || {};
|
|
69
|
+
let targetNumber = typeof editorNumber === "number" ? editorNumber : undefined;
|
|
70
|
+
let targetId = typeof editorId === "string" ? editorId : undefined;
|
|
71
|
+
|
|
72
|
+
if (!targetNumber && !targetId) {
|
|
73
|
+
const { editors = [] } = await bridgeGet(baseUrl);
|
|
74
|
+
if (editors.length === 1) {
|
|
75
|
+
targetNumber = editors[0].editorNumber;
|
|
76
|
+
} else {
|
|
77
|
+
throw new Error(editors.length === 0 ? "No open SketchForge editors found" : "Provide editorNumber because multiple SketchForge editors are open");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const response = await fetch(bridgeUrl(baseUrl), {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: { "Content-Type": "application/json" },
|
|
84
|
+
body: JSON.stringify({
|
|
85
|
+
type: "command",
|
|
86
|
+
editorNumber: targetNumber,
|
|
87
|
+
editorId: targetId,
|
|
88
|
+
action,
|
|
89
|
+
params,
|
|
90
|
+
timeoutMs: typeof timeoutMs === "number" ? timeoutMs : defaultTimeoutMs,
|
|
91
|
+
}),
|
|
92
|
+
});
|
|
93
|
+
const payload = await response.json().catch(() => null);
|
|
94
|
+
if (!payload?.ok) {
|
|
95
|
+
throw new Error(payload?.error || `SketchForge command failed with HTTP ${response.status}`);
|
|
96
|
+
}
|
|
97
|
+
return payload.data;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function callRemoteTool(name, args, baseUrl) {
|
|
101
|
+
if (PARAMETER_TOOL_NAMES.has(name)) {
|
|
102
|
+
return callParameterTool(name, args, (method, route, options) => restRequest(method, route, { ...options, baseUrl, withDetail: true }));
|
|
103
|
+
}
|
|
104
|
+
switch (name) {
|
|
105
|
+
case "sketchforge_list_editors":
|
|
106
|
+
return bridgeGet(baseUrl);
|
|
107
|
+
case "sketchforge_read_scene":
|
|
108
|
+
return bridgeCommand("get_scene", args, 15000, baseUrl);
|
|
109
|
+
case "sketchforge_list_objects":
|
|
110
|
+
return bridgeCommand("list_objects", args, 15000, baseUrl);
|
|
111
|
+
case "sketchforge_select_objects":
|
|
112
|
+
return bridgeCommand("select_objects", args, 15000, baseUrl);
|
|
113
|
+
case "sketchforge_mirror_objects":
|
|
114
|
+
return bridgeCommand("mirror_objects", args, 15000, baseUrl);
|
|
115
|
+
case "sketchforge_mirror_baked":
|
|
116
|
+
return restRequest("POST", "/mirrors", {
|
|
117
|
+
query: { projectId: args.projectId },
|
|
118
|
+
body: { ids: args.ids, axis: args.axis },
|
|
119
|
+
baseUrl,
|
|
120
|
+
});
|
|
121
|
+
case "sketchforge_delete_objects":
|
|
122
|
+
return bridgeCommand("delete_objects", args, 15000, baseUrl);
|
|
123
|
+
case "sketchforge_create_shape":
|
|
124
|
+
return bridgeCommand("create_shape", args, 15000, baseUrl);
|
|
125
|
+
case "sketchforge_import_mesh":
|
|
126
|
+
return bridgeCommand("import_mesh", args, 60000, baseUrl);
|
|
127
|
+
case "sketchforge_import_file": {
|
|
128
|
+
const sourcePath = await resolveMcpReadPath(args.filePath);
|
|
129
|
+
const extension = path.extname(sourcePath).toLowerCase();
|
|
130
|
+
const mediaType = extension === ".stl" ? "model/stl" : extension === ".obj" ? "model/obj" : extension === ".svg" ? "image/svg+xml" : undefined;
|
|
131
|
+
if (!mediaType) throw new Error("filePath must point to an .stl, .obj, or .svg file.");
|
|
132
|
+
const bytes = readFileSync(sourcePath);
|
|
133
|
+
return restRequest("POST", "/imports", {
|
|
134
|
+
query: { projectId: args.projectId },
|
|
135
|
+
body: { fileName: path.basename(sourcePath), data: bytes.toString("base64"), mediaType },
|
|
136
|
+
baseUrl,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
case "sketchforge_update_object":
|
|
140
|
+
return bridgeCommand("update_object", args, 15000, baseUrl);
|
|
141
|
+
case "sketchforge_align_objects":
|
|
142
|
+
return bridgeCommand("align_objects", args, 15000, baseUrl);
|
|
143
|
+
case "sketchforge_distribute_objects":
|
|
144
|
+
return bridgeCommand("distribute_objects", args, 15000, baseUrl);
|
|
145
|
+
case "sketchforge_pattern_objects":
|
|
146
|
+
return bridgeCommand("pattern_objects", args, 15000, baseUrl);
|
|
147
|
+
case "sketchforge_group_objects":
|
|
148
|
+
return bridgeCommand("group_objects", args, 30000, baseUrl);
|
|
149
|
+
case "sketchforge_ungroup_objects":
|
|
150
|
+
return bridgeCommand("ungroup_objects", args, 15000, baseUrl);
|
|
151
|
+
case "sketchforge_boolean_cut":
|
|
152
|
+
return bridgeCommand("boolean_cut", args, 45000, baseUrl);
|
|
153
|
+
case "sketchforge_separate_parts":
|
|
154
|
+
return bridgeCommand("separate_parts", args, 15000, baseUrl);
|
|
155
|
+
case "sketchforge_explode_step_assembly":
|
|
156
|
+
return restRequest("POST", "/explode", { query: { projectId: args.projectId, shapeId: args.shapeId }, body: {}, baseUrl });
|
|
157
|
+
case "sketchforge_list_edges":
|
|
158
|
+
return bridgeCommand("list_edges", args, 30000, baseUrl);
|
|
159
|
+
case "sketchforge_apply_edge_treatment":
|
|
160
|
+
return bridgeCommand("apply_edge_treatment", args, 60000, baseUrl);
|
|
161
|
+
case "sketchforge_list_faces":
|
|
162
|
+
return restRequest("GET", "/faces", { query: { projectId: args.projectId, shapeId: args.shapeId }, baseUrl });
|
|
163
|
+
case "sketchforge_measure_shape":
|
|
164
|
+
return restRequest("GET", "/measurements", { query: { projectId: args.projectId, shapeId: args.shapeId }, baseUrl });
|
|
165
|
+
case "sketchforge_shape_properties":
|
|
166
|
+
return restRequest("GET", "/properties", { query: { projectId: args.projectId, shapeId: args.shapeId }, baseUrl });
|
|
167
|
+
case "sketchforge_estimate_wall_thickness":
|
|
168
|
+
return restRequest("GET", "/thickness", { query: { projectId: args.projectId, shapeId: args.shapeId }, baseUrl });
|
|
169
|
+
case "sketchforge_measure_between":
|
|
170
|
+
return restRequest("POST", "/measurements", {
|
|
171
|
+
query: { projectId: args.projectId },
|
|
172
|
+
body: { from: args.from, to: args.to },
|
|
173
|
+
baseUrl,
|
|
174
|
+
});
|
|
175
|
+
case "sketchforge_hollow_shape":
|
|
176
|
+
return restRequest("POST", "/shell", {
|
|
177
|
+
query: { projectId: args.projectId, shapeId: args.shapeId },
|
|
178
|
+
body: {
|
|
179
|
+
thickness: args.thickness,
|
|
180
|
+
...(args.openFaces === undefined ? {} : { openFaces: args.openFaces }),
|
|
181
|
+
},
|
|
182
|
+
baseUrl,
|
|
183
|
+
});
|
|
184
|
+
case "sketchforge_inspect_errors":
|
|
185
|
+
return bridgeCommand("inspect_errors", args, 15000, baseUrl);
|
|
186
|
+
case "sketchforge_capture_image":
|
|
187
|
+
return bridgeCommand("capture_image", args, 30000, baseUrl);
|
|
188
|
+
case "sketchforge_project_view": {
|
|
189
|
+
const artifact = await restRequest("POST", "/exports", {
|
|
190
|
+
query: { projectId: args.projectId },
|
|
191
|
+
body: {
|
|
192
|
+
format: "svg",
|
|
193
|
+
...(args.view === undefined ? {} : { view: args.view }),
|
|
194
|
+
...(args.featureAngle === undefined ? {} : { featureAngle: args.featureAngle }),
|
|
195
|
+
...(args.shapeIds === undefined ? {} : { shapeIds: args.shapeIds }),
|
|
196
|
+
...(args.name === undefined ? {} : { name: args.name }),
|
|
197
|
+
},
|
|
198
|
+
baseUrl,
|
|
199
|
+
});
|
|
200
|
+
const bytes = Buffer.from(artifact.base64, "base64");
|
|
201
|
+
const response = { view: args.view ?? "multiview", filename: artifact.filename, mimeType: artifact.mimeType, bytes: artifact.bytes, shapeCount: artifact.shapeCount };
|
|
202
|
+
if (typeof args.outputPath === "string" && args.outputPath.trim()) {
|
|
203
|
+
const destination = await resolveMcpWritePath(args.outputPath, { overwrite: args.overwrite === true });
|
|
204
|
+
writeFileSync(destination, bytes, { flag: args.overwrite === true ? "w" : "wx" });
|
|
205
|
+
return { ...response, savedTo: path.resolve(args.outputPath) };
|
|
206
|
+
}
|
|
207
|
+
return { ...response, svg: bytes.toString("utf8") };
|
|
208
|
+
}
|
|
209
|
+
case "sketchforge_section_view": {
|
|
210
|
+
const artifact = await restRequest("POST", "/exports", {
|
|
211
|
+
query: { projectId: args.projectId },
|
|
212
|
+
body: {
|
|
213
|
+
format: "svg",
|
|
214
|
+
section: { plane: args.plane, offset: args.offset },
|
|
215
|
+
...(args.shapeIds === undefined ? {} : { shapeIds: args.shapeIds }),
|
|
216
|
+
...(args.name === undefined ? {} : { name: args.name }),
|
|
217
|
+
},
|
|
218
|
+
baseUrl,
|
|
219
|
+
});
|
|
220
|
+
const bytes = Buffer.from(artifact.base64, "base64");
|
|
221
|
+
const response = { plane: args.plane, offset: args.offset, filename: artifact.filename, mimeType: artifact.mimeType, bytes: artifact.bytes, shapeCount: artifact.shapeCount };
|
|
222
|
+
if (typeof args.outputPath === "string" && args.outputPath.trim()) {
|
|
223
|
+
const destination = await resolveMcpWritePath(args.outputPath, { overwrite: args.overwrite === true });
|
|
224
|
+
writeFileSync(destination, bytes, { flag: args.overwrite === true ? "w" : "wx" });
|
|
225
|
+
return { ...response, savedTo: path.resolve(args.outputPath) };
|
|
226
|
+
}
|
|
227
|
+
return { ...response, svg: bytes.toString("utf8") };
|
|
228
|
+
}
|
|
229
|
+
case "sketchforge_export_scene": {
|
|
230
|
+
// An SVG is drawn on the server from the stored project; the editor
|
|
231
|
+
// bridge has no such export, so this one takes the REST path.
|
|
232
|
+
const artifact = args.format === "svg" || args.format === "dxf"
|
|
233
|
+
? await restRequest("POST", "/exports", {
|
|
234
|
+
query: { projectId: args.projectId },
|
|
235
|
+
body: {
|
|
236
|
+
format: args.format,
|
|
237
|
+
...(args.view === undefined ? {} : { view: args.view }),
|
|
238
|
+
...(args.section === undefined ? {} : { section: args.section }),
|
|
239
|
+
...(args.shapeIds === undefined ? {} : { shapeIds: args.shapeIds }),
|
|
240
|
+
...(args.name === undefined ? {} : { name: args.name }),
|
|
241
|
+
},
|
|
242
|
+
baseUrl,
|
|
243
|
+
})
|
|
244
|
+
: await bridgeCommand("export_scene", args, 60000, baseUrl);
|
|
245
|
+
if (typeof args.outputPath === "string" && args.outputPath.trim()) {
|
|
246
|
+
const destination = await resolveMcpWritePath(args.outputPath, { overwrite: args.overwrite === true });
|
|
247
|
+
writeFileSync(destination, Buffer.from(artifact.base64, "base64"), { flag: args.overwrite === true ? "w" : "wx" });
|
|
248
|
+
return { filename: artifact.filename, mimeType: artifact.mimeType, bytes: artifact.bytes, shapeCount: artifact.shapeCount, savedTo: path.resolve(args.outputPath) };
|
|
249
|
+
}
|
|
250
|
+
return artifact;
|
|
251
|
+
}
|
|
252
|
+
case "sketchforge_validate_scene":
|
|
253
|
+
return bridgeCommand("validate_scene", args, 60000, baseUrl);
|
|
254
|
+
case "sketchforge_check_interference":
|
|
255
|
+
return restRequest("POST", "/interference", {
|
|
256
|
+
query: { projectId: args.projectId },
|
|
257
|
+
body: { ...(Array.isArray(args.ids) && args.ids.length ? { ids: args.ids } : {}), ...(args.clearance === undefined ? {} : { clearance: args.clearance }) },
|
|
258
|
+
baseUrl,
|
|
259
|
+
});
|
|
260
|
+
case "sketchforge_list_macros":
|
|
261
|
+
return listMacros();
|
|
262
|
+
case "sketchforge_run_macro":
|
|
263
|
+
return runMacroByName(args, { environment: { ...process.env, SKETCHFORGE_URL: baseUrl } });
|
|
264
|
+
default:
|
|
265
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function normalizedServerUrl(value) {
|
|
270
|
+
let parsed;
|
|
271
|
+
try {
|
|
272
|
+
parsed = new URL(value);
|
|
273
|
+
} catch {
|
|
274
|
+
throw new Error("url must be an absolute http:// or https:// SketchForge server URL.");
|
|
275
|
+
}
|
|
276
|
+
if (!["http:", "https:"].includes(parsed.protocol) || parsed.username || parsed.password) {
|
|
277
|
+
throw new Error("url must be an absolute http:// or https:// SketchForge server URL without embedded credentials.");
|
|
278
|
+
}
|
|
279
|
+
return parsed.toString().replace(/\/$/, "");
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export async function callTool(name, args = {}) {
|
|
283
|
+
const { url, ...toolArgs } = args || {};
|
|
284
|
+
if (name === "sketchforge_import_stl" && typeof url === "string" && url.trim()) {
|
|
285
|
+
throw new Error("sketchforge_import_stl is only available in local mode; omit url.");
|
|
286
|
+
}
|
|
287
|
+
if (["sketchforge_list_projects", "sketchforge_create_project", "sketchforge_import_project"].includes(name) && typeof url === "string" && url.trim()) {
|
|
288
|
+
throw new Error(`${name} is only available in local mode; omit url.`);
|
|
289
|
+
}
|
|
290
|
+
if (typeof url === "string" && url.trim()) {
|
|
291
|
+
return callRemoteTool(name, toolArgs, normalizedServerUrl(url.trim()));
|
|
292
|
+
}
|
|
293
|
+
if (name === "sketchforge_list_macros") return listMacros();
|
|
294
|
+
if (name === "sketchforge_run_macro") return runMacroByName(toolArgs);
|
|
295
|
+
return callLocalEngineTool(name, toolArgs);
|
|
296
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructorfleet/sketchforge3d",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.33.0",
|
|
4
4
|
"description": "Typed client and macro runner for the SketchForge REST API.",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"author": {
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"client"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@constructorfleet/sketchforge-engine": "1.
|
|
22
|
+
"@constructorfleet/sketchforge-engine": "1.33.0",
|
|
23
23
|
"fflate": "^0.8.3",
|
|
24
24
|
"sharp": "^0.35.5"
|
|
25
25
|
},
|