@constructorfleet/sketchforge3d 1.4.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/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: { type: "object", properties: {} },
41
+ inputSchema: { ...editorTargetSchema, properties: { url: editorTargetSchema.properties.url } },
42
42
  },
43
43
  {
44
44
  name: "sketchforge_read_scene",
45
- description: "Read the current scene, selection, workspace units, and exact object dimensions from an open SketchForge editor.",
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 current objects available in the editor with exact dimensions, position, rotation, and object ids.",
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 revolve: the closed outline to revolve, as [x, z] pairs. x is the distance from the axis, z runs along it. A triangle makes a thread, a rectangle a tube, a polygon a spring.",
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 of the editor viewport from current/home/top/bottom/front/back/right/left view.",
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,27 +332,86 @@ const tools = [
324
332
  },
325
333
  },
326
334
  },
327
- // Hollowing runs on the headless REST API rather than in an editor tab: the
328
- // operation is server-side OCCT, and the editor has no action for it yet.
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 on a project on the server (REST API), not on an open editor tab.",
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
  },
359
+ // Measuring uses exact CAD geometry in either the local engine or server API.
360
+ {
361
+ name: "sketchforge_measure_shape",
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.",
363
+ inputSchema: {
364
+ type: "object",
365
+ required: ["projectId", "shapeId"],
366
+ properties: {
367
+ url: { type: "string", description: "Optional SketchForge server URL. Omit to work offline with local project files." },
368
+ projectId: { type: "string", description: "The server-side project id." },
369
+ shapeId: { type: "string", description: "The id of the shape to measure." },
370
+ },
371
+ },
372
+ },
373
+ {
374
+ name: "sketchforge_measure_between",
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.",
376
+ inputSchema: {
377
+ type: "object",
378
+ required: ["projectId", "from", "to"],
379
+ properties: {
380
+ url: { type: "string", description: "Optional SketchForge server URL. Omit to work offline with local project files." },
381
+ projectId: { type: "string", description: "The server-side project id." },
382
+ from: {
383
+ type: "object",
384
+ required: ["shapeId"],
385
+ properties: {
386
+ shapeId: { type: "string", description: "The shape this end is part of." },
387
+ entity: {
388
+ type: "string",
389
+ enum: ["shape", "edge", "face"],
390
+ description: "What to measure to. 'shape' is the whole solid and takes no index; 'edge' and 'face' need one.",
391
+ },
392
+ index: { type: "number", description: "Which edge or face, by the index the measurement listing gave it." },
393
+ },
394
+ },
395
+ to: {
396
+ type: "object",
397
+ required: ["shapeId"],
398
+ properties: {
399
+ shapeId: { type: "string", description: "The shape this end is part of." },
400
+ entity: { type: "string", enum: ["shape", "edge", "face"] },
401
+ index: { type: "number" },
402
+ },
403
+ },
404
+ },
405
+ },
406
+ },
341
407
  {
342
408
  name: "sketchforge_hollow_shape",
343
- 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 on a project on the server (REST API), not on an open editor tab.",
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.",
344
410
  inputSchema: {
345
411
  type: "object",
346
412
  required: ["projectId", "shapeId", "thickness"],
347
413
  properties: {
414
+ url: { type: "string", description: "Optional SketchForge server URL. Omit to work offline with local project files." },
348
415
  projectId: { type: "string", description: "The server-side project id." },
349
416
  shapeId: { type: "string", description: "The id of the shape to hollow." },
350
417
  thickness: { type: "number", description: "Wall thickness in mm, measured inwards. Must be well under half the part's smallest span." },
@@ -356,8 +423,7 @@ const tools = [
356
423
  },
357
424
  },
358
425
  },
359
- // Macros drive the headless REST API rather than an editor tab, and run only
360
- // from the directory the operator configured.
426
+ // Macros run only from the directory the operator configured.
361
427
  ...macroTools,
362
428
  ];
363
429
 
@@ -371,7 +437,7 @@ const API_TOKEN_ENV = "SKETCHFORGE_API_TOKEN";
371
437
  * to ask one for. The token is the same one the server was started with, and
372
438
  * without it every route answers 404 rather than saying it is switched off.
373
439
  */
374
- async function restRequest(method, path, { query = {}, body } = {}) {
440
+ async function restRequest(method, path, { query = {}, body, baseUrl } = {}) {
375
441
  const token = process.env[API_TOKEN_ENV];
376
442
  if (!token) {
377
443
  throw new Error(
@@ -400,12 +466,12 @@ async function restRequest(method, path, { query = {}, body } = {}) {
400
466
  return payload;
401
467
  }
402
468
 
403
- function bridgeUrl() {
469
+ function bridgeUrl(baseUrl) {
404
470
  return new URL(MCP_ROUTE, baseUrl);
405
471
  }
406
472
 
407
- async function bridgeGet() {
408
- const response = await fetch(bridgeUrl());
473
+ async function bridgeGet(baseUrl) {
474
+ const response = await fetch(bridgeUrl(baseUrl));
409
475
  const payload = await response.json().catch(() => null);
410
476
  if (!response.ok) {
411
477
  throw new Error(payload?.error || `SketchForge bridge returned HTTP ${response.status}`);
@@ -413,13 +479,13 @@ async function bridgeGet() {
413
479
  return payload;
414
480
  }
415
481
 
416
- async function bridgeCommand(action, args = {}, defaultTimeoutMs = 15000) {
417
- const { editorNumber, editorId, timeoutMs, ...params } = args || {};
482
+ async function bridgeCommand(action, args = {}, defaultTimeoutMs = 15000, baseUrl) {
483
+ const { editorNumber, editorId, timeoutMs, url, ...params } = args || {};
418
484
  let targetNumber = typeof editorNumber === "number" ? editorNumber : undefined;
419
485
  let targetId = typeof editorId === "string" ? editorId : undefined;
420
486
 
421
487
  if (!targetNumber && !targetId) {
422
- const { editors = [] } = await bridgeGet();
488
+ const { editors = [] } = await bridgeGet(baseUrl);
423
489
  if (editors.length === 1) {
424
490
  targetNumber = editors[0].editorNumber;
425
491
  } else {
@@ -427,7 +493,7 @@ async function bridgeCommand(action, args = {}, defaultTimeoutMs = 15000) {
427
493
  }
428
494
  }
429
495
 
430
- const response = await fetch(bridgeUrl(), {
496
+ const response = await fetch(bridgeUrl(baseUrl), {
431
497
  method: "POST",
432
498
  headers: { "Content-Type": "application/json" },
433
499
  body: JSON.stringify({
@@ -446,44 +512,54 @@ async function bridgeCommand(action, args = {}, defaultTimeoutMs = 15000) {
446
512
  return payload.data;
447
513
  }
448
514
 
449
- async function callTool(name, args) {
515
+ async function callRemoteTool(name, args, baseUrl) {
450
516
  switch (name) {
451
517
  case "sketchforge_list_editors":
452
- return bridgeGet();
518
+ return bridgeGet(baseUrl);
453
519
  case "sketchforge_read_scene":
454
- return bridgeCommand("get_scene", args);
520
+ return bridgeCommand("get_scene", args, 15000, baseUrl);
455
521
  case "sketchforge_list_objects":
456
- return bridgeCommand("list_objects", args);
522
+ return bridgeCommand("list_objects", args, 15000, baseUrl);
457
523
  case "sketchforge_select_objects":
458
- 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);
459
527
  case "sketchforge_delete_objects":
460
- return bridgeCommand("delete_objects", args);
528
+ return bridgeCommand("delete_objects", args, 15000, baseUrl);
461
529
  case "sketchforge_create_shape":
462
- return bridgeCommand("create_shape", args);
530
+ return bridgeCommand("create_shape", args, 15000, baseUrl);
463
531
  case "sketchforge_import_mesh":
464
- return bridgeCommand("import_mesh", args, 60000);
532
+ return bridgeCommand("import_mesh", args, 60000, baseUrl);
465
533
  case "sketchforge_update_object":
466
- return bridgeCommand("update_object", args);
534
+ return bridgeCommand("update_object", args, 15000, baseUrl);
467
535
  case "sketchforge_align_objects":
468
- return bridgeCommand("align_objects", args);
536
+ return bridgeCommand("align_objects", args, 15000, baseUrl);
469
537
  case "sketchforge_distribute_objects":
470
- return bridgeCommand("distribute_objects", args);
538
+ return bridgeCommand("distribute_objects", args, 15000, baseUrl);
471
539
  case "sketchforge_pattern_objects":
472
- return bridgeCommand("pattern_objects", args);
540
+ return bridgeCommand("pattern_objects", args, 15000, baseUrl);
473
541
  case "sketchforge_group_objects":
474
- return bridgeCommand("group_objects", args, 30000);
542
+ return bridgeCommand("group_objects", args, 30000, baseUrl);
475
543
  case "sketchforge_ungroup_objects":
476
- return bridgeCommand("ungroup_objects", args);
544
+ return bridgeCommand("ungroup_objects", args, 15000, baseUrl);
477
545
  case "sketchforge_boolean_cut":
478
- return bridgeCommand("boolean_cut", args, 45000);
546
+ return bridgeCommand("boolean_cut", args, 45000, baseUrl);
479
547
  case "sketchforge_separate_parts":
480
- return bridgeCommand("separate_parts", args);
548
+ return bridgeCommand("separate_parts", args, 15000, baseUrl);
481
549
  case "sketchforge_list_edges":
482
- return bridgeCommand("list_edges", args, 30000);
550
+ return bridgeCommand("list_edges", args, 30000, baseUrl);
483
551
  case "sketchforge_apply_edge_treatment":
484
- return bridgeCommand("apply_edge_treatment", args, 60000);
552
+ return bridgeCommand("apply_edge_treatment", args, 60000, baseUrl);
485
553
  case "sketchforge_list_faces":
486
- return restRequest("GET", "/faces", { query: { projectId: args.projectId, shapeId: args.shapeId } });
554
+ return restRequest("GET", "/faces", { query: { projectId: args.projectId, shapeId: args.shapeId }, baseUrl });
555
+ case "sketchforge_measure_shape":
556
+ return restRequest("GET", "/measurements", { query: { projectId: args.projectId, shapeId: args.shapeId }, baseUrl });
557
+ case "sketchforge_measure_between":
558
+ return restRequest("POST", "/measurements", {
559
+ query: { projectId: args.projectId },
560
+ body: { from: args.from, to: args.to },
561
+ baseUrl,
562
+ });
487
563
  case "sketchforge_hollow_shape":
488
564
  return restRequest("POST", "/shell", {
489
565
  query: { projectId: args.projectId, shapeId: args.shapeId },
@@ -491,20 +567,55 @@ async function callTool(name, args) {
491
567
  thickness: args.thickness,
492
568
  ...(args.openFaces === undefined ? {} : { openFaces: args.openFaces }),
493
569
  },
570
+ baseUrl,
494
571
  });
495
572
  case "sketchforge_inspect_errors":
496
- return bridgeCommand("inspect_errors", args);
573
+ return bridgeCommand("inspect_errors", args, 15000, baseUrl);
497
574
  case "sketchforge_capture_image":
498
- 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);
499
587
  case "sketchforge_list_macros":
500
588
  return listMacros();
501
589
  case "sketchforge_run_macro":
502
- return runMacroByName(args);
590
+ return runMacroByName(args, { environment: { ...process.env, SKETCHFORGE_URL: baseUrl } });
503
591
  default:
504
592
  throw new Error(`Unknown tool: ${name}`);
505
593
  }
506
594
  }
507
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
+
508
619
  function textContent(value) {
509
620
  return {
510
621
  type: "text",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructorfleet/sketchforge3d",
3
- "version": "1.4.0",
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": ">=18"
46
+ "node": ">=20"
42
47
  },
43
48
  "publishConfig": {
44
49
  "access": "public"