-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
798 lines (640 loc) · 33.5 KB
/
server.py
File metadata and controls
798 lines (640 loc) · 33.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
import kcl
from kittycad.models.modeling_cmd import OptionDefaultCameraLookAt, Point3d
from mcp.server.fastmcp import FastMCP
from mcp.types import ImageContent
from zoo_mcp import ZooMCPException, logger
from zoo_mcp.ai_tools import edit_kcl_project as _edit_kcl_project
from zoo_mcp.ai_tools import text_to_cad as _text_to_cad
from zoo_mcp.utils.image_utils import encode_image, save_image_to_disk
from zoo_mcp.zoo_tools import (
CameraView,
zoo_calculate_bounding_box_cad,
zoo_calculate_bounding_box_kcl,
zoo_calculate_cad_physical_properties,
zoo_calculate_center_of_mass,
zoo_calculate_kcl_physical_properties,
zoo_calculate_mass,
zoo_calculate_surface_area,
zoo_calculate_volume,
zoo_convert_cad_file,
zoo_execute_kcl,
zoo_export_kcl,
zoo_format_kcl,
zoo_lint_and_fix_kcl,
zoo_mock_execute_kcl,
zoo_multi_isometric_snapshot_of_cad,
zoo_multi_isometric_snapshot_of_kcl,
zoo_multiview_snapshot_of_cad,
zoo_multiview_snapshot_of_kcl,
zoo_snapshot_of_cad,
zoo_snapshot_of_kcl,
)
mcp = FastMCP(
name="Zoo MCP Server",
log_level="INFO",
)
@mcp.tool()
async def calculate_center_of_mass(input_file: str, unit_length: str) -> dict | str:
"""Calculate the center of mass of a 3d object represented by the input file.
Args:
input_file (str): The path of the file to get the mass from. The file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
unit_length (str): The unit of length to return the result in. One of 'cm', 'ft', 'in', 'm', 'mm', 'yd'
Returns:
str: The center of mass of the file in the specified unit of length, or an error message if the operation fails.
"""
logger.info("calculate_center_of_mass tool called for file: %s", input_file)
try:
com = await zoo_calculate_center_of_mass(
file_path=input_file, unit_length=unit_length
)
return com
except Exception as e:
return f"There was an error calculating the center of mass of the file: {e}"
@mcp.tool()
async def calculate_mass(
input_file: str, unit_mass: str, unit_density: str, density: float
) -> float | str:
"""Calculate the mass of a 3d object represented by the input file.
Args:
input_file (str): The path of the file to get the mass from. The file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
unit_mass (str): The unit of mass to return the result in. One of 'g', 'kg', 'lb'.
unit_density (str): The unit of density to calculate the mass. One of 'lb:ft3', 'kg:m3'.
density (float): The density of the material.
Returns:
str: The mass of the file in the specified unit of mass, or an error message if the operation fails.
"""
logger.info("calculate_mass tool called for file: %s", input_file)
try:
mass = await zoo_calculate_mass(
file_path=input_file,
unit_mass=unit_mass,
unit_density=unit_density,
density=density,
)
return mass
except Exception as e:
return f"There was an error calculating the mass of the file: {e}"
@mcp.tool()
async def calculate_surface_area(input_file: str, unit_area: str) -> float | str:
"""Calculate the surface area of a 3d object represented by the input file.
Args:
input_file (str): The path of the file to get the surface area from. The file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
unit_area (str): The unit of area to return the result in. One of 'cm2', 'dm2', 'ft2', 'in2', 'km2', 'm2', 'mm2', 'yd2'.
Returns:
str: The surface area of the file in the specified unit of area, or an error message if the operation fails.
"""
logger.info("calculate_surface_area tool called for file: %s", input_file)
try:
surface_area = await zoo_calculate_surface_area(
file_path=input_file, unit_area=unit_area
)
return surface_area
except Exception as e:
return f"There was an error calculating the surface area of the file: {e}"
@mcp.tool()
async def calculate_volume(input_file: str, unit_volume: str) -> float | str:
"""Calculate the volume of a 3d object represented by the input file.
Args:
input_file (str): The path of the file to get the volume from. The file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
unit_volume (str): The unit of volume to return the result in. One of 'cm3', 'ft3', 'in3', 'm3', 'yd3', 'usfloz', 'usgal', 'l', 'ml'.
Returns:
str: The volume of the file in the specified unit of volume, or an error message if the operation fails.
"""
logger.info("calculate_volume tool called for file: %s", input_file)
try:
volume = await zoo_calculate_volume(file_path=input_file, unit_vol=unit_volume)
return volume
except Exception as e:
return f"There was an error calculating the volume of the file: {e}"
@mcp.tool()
async def calculate_cad_physical_properties(
input_file: str,
unit_length: str,
unit_mass: str,
unit_density: str,
density: float,
unit_area: str,
unit_volume: str,
) -> dict | str:
"""Calculate physical properties (volume, mass, surface area, center of mass, bounding box) of a CAD file.
Args:
input_file (str): The path of the file. The file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
unit_length (str): The unit of length for center of mass. One of 'cm', 'ft', 'in', 'm', 'mm', 'yd'.
unit_mass (str): The unit of mass for the mass result. One of 'g', 'kg', 'lb'.
unit_density (str): The unit of density for the material. One of 'lb:ft3', 'kg:m3'.
density (float): The density of the material.
unit_area (str): The unit of area for surface area. One of 'cm2', 'dm2', 'ft2', 'in2', 'km2', 'm2', 'mm2', 'yd2'.
unit_volume (str): The unit of volume. One of 'cm3', 'ft3', 'in3', 'm3', 'yd3', 'usfloz', 'usgal', 'l', 'ml'.
Returns:
dict | str: A dictionary with keys 'volume', 'mass', 'surface_area', 'center_of_mass', and 'bounding_box', or an error message if the operation fails.
"""
logger.info(
"calculate_cad_physical_properties tool called for file: %s", input_file
)
try:
return await zoo_calculate_cad_physical_properties(
file_path=input_file,
unit_length=unit_length,
unit_mass=unit_mass,
unit_density=unit_density,
density=density,
unit_area=unit_area,
unit_vol=unit_volume,
)
except Exception as e:
return f"There was an error calculating physical properties of the file: {e}"
@mcp.tool()
async def calculate_kcl_physical_properties(
kcl_code: str | None = None,
kcl_path: str | None = None,
unit_length: str = "mm",
unit_mass: str = "g",
unit_density: str = "kg:m3",
density: float = 1000.0,
unit_area: str = "mm2",
unit_volume: str = "cm3",
) -> dict | str:
"""Calculate physical properties (volume, mass, surface area, center of mass, bounding box) of a KCL model.
Either kcl_code or kcl_path must be provided. If kcl_path is provided, it should point
to a .kcl file or a directory containing a main.kcl file.
Args:
kcl_code (str | None): The KCL code to evaluate.
kcl_path (str | None): Path to a .kcl file or a directory containing a main.kcl file.
unit_length (str): The unit of length for center of mass. One of 'cm', 'ft', 'in', 'm', 'mm', 'yd'.
unit_mass (str): The unit of mass for the mass result. One of 'g', 'kg', 'lb'.
unit_density (str): The unit of density for the material. One of 'lb:ft3', 'kg:m3'.
density (float): The density of the material.
unit_area (str): The unit of area for surface area. One of 'cm2', 'dm2', 'ft2', 'in2', 'km2', 'm2', 'mm2', 'yd2'.
unit_volume (str): The unit of volume. One of 'cm3', 'ft3', 'in3', 'm3', 'yd3', 'usfloz', 'usgal', 'l', 'ml'.
Returns:
dict | str: A dictionary with keys 'volume', 'mass', 'surface_area', 'center_of_mass', and 'bounding_box', or an error message if the operation fails.
"""
logger.info("calculate_kcl_physical_properties tool called")
try:
return await zoo_calculate_kcl_physical_properties(
kcl_code=kcl_code,
kcl_path=kcl_path,
unit_length=unit_length,
unit_mass=unit_mass,
unit_density=unit_density,
density=density,
unit_area=unit_area,
unit_vol=unit_volume,
)
except Exception as e:
return (
f"There was an error calculating physical properties of the KCL model: {e}"
)
@mcp.tool()
async def calculate_bounding_box_kcl(
unit_length: str,
kcl_code: str | None = None,
kcl_path: str | None = None,
) -> dict | str:
"""Calculate the bounding box of a KCL model.
Either kcl_code or kcl_path must be provided. If kcl_path is provided, it should point
to a .kcl file or a directory containing a main.kcl file.
Args:
unit_length (str): The unit of length to return the result in. One of 'cm', 'ft', 'in', 'm', 'mm', 'yd'
kcl_code (str | None): The KCL code to evaluate.
kcl_path (str | None): Path to a .kcl file or a directory containing a main.kcl file.
Returns:
dict | str: A dictionary with 'center' (dict with x,y,z) and 'dimensions' (dict with x,y,z),
or an error message if the operation fails.
"""
logger.info("calculate_bounding_box_kcl tool called")
try:
return await zoo_calculate_bounding_box_kcl(
unit_length=unit_length,
kcl_code=kcl_code,
kcl_path=kcl_path,
)
except Exception as e:
return f"There was an error calculating bounding box of the KCL model: {e}"
@mcp.tool()
async def calculate_bounding_box_cad(
input_file: str,
) -> dict | str:
"""Calculate the bounding box of a CAD file.
Args:
input_file (str): The path of the CAD file. The file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
Returns:
dict | str: A dictionary with 'center' (dict with x,y,z) and 'dimensions' (dict with x,y,z),
or an error message if the operation fails.
"""
logger.info("calculate_bounding_box_cad tool called for file: %s", input_file)
try:
return await zoo_calculate_bounding_box_cad(file_path=input_file)
except Exception as e:
return f"There was an error calculating the bounding box of the file: {e}"
@mcp.tool()
async def convert_cad_file(
input_path: str,
export_path: str | None,
export_format: str | None,
) -> str:
"""Convert a CAD file from one format to another CAD file format.
Args:
input_path (str): The input cad file to convert. The file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
export_path (str | None): The path to save the converted CAD file to. If the path is a directory, a temporary file will be created in the directory. If the path is a file, it will be overwritten if the extension is valid.
export_format (str | None): The format of the exported CAD file. This should be one of 'fbx', 'glb', 'gltf', 'obj', 'ply', 'step', 'stl'. If no format is provided, the default is 'step'.
Returns:
str: The path to the converted CAD file, or an error message if the operation fails.
"""
logger.info("convert_cad_file tool called")
try:
step_path = await zoo_convert_cad_file(
input_path=input_path, export_path=export_path, export_format=export_format
)
return str(step_path)
except Exception as e:
return f"There was an error converting the CAD file: {e}"
@mcp.tool()
async def execute_kcl(
kcl_code: str | None = None,
kcl_path: str | None = None,
) -> tuple[bool, str]:
"""Execute KCL code given a string of KCL code or a path to a KCL project. Either kcl_code or kcl_path must be provided. If kcl_path is provided, it should point to a .kcl file or a directory containing a main.kcl file.
Args:
kcl_code (str | None): The KCL code to execute.
kcl_path (str | None): The path to a KCL file to execute. The path should point to a .kcl file or a directory containing a main.kcl file.
Returns:
tuple(bool, str): Returns True if the KCL code executed successfully and a success message, False otherwise and the error message.
"""
logger.info("execute_kcl tool called")
try:
return await zoo_execute_kcl(kcl_code=kcl_code, kcl_path=kcl_path)
except Exception as e:
return False, f"Failed to execute KCL code: {e}"
@mcp.tool()
async def export_kcl(
kcl_code: str | None = None,
kcl_path: str | None = None,
export_path: str | None = None,
export_format: str | None = None,
) -> str:
"""Export KCL code to a CAD file. Either kcl_code or kcl_path must be provided. If kcl_path is provided, it should point to a .kcl file or a directory containing a main.kcl file.
Args:
kcl_code (str | None): The KCL code to export to a CAD file.
kcl_path (str | None): The path to a KCL file to export to a CAD file. The path should point to a .kcl file or a directory containing a main.kcl file.
export_path (str | None): The path to export the CAD file. If no path is provided, a temporary file will be created.
export_format (str | None): The format to export the file as. This should be one of 'fbx', 'glb', 'gltf', 'obj', 'ply', 'step', 'stl'. If no format is provided, the default is 'step'.
Returns:
str: The path to the converted CAD file, or an error message if the operation fails.
"""
logger.info("convert_kcl_to_step tool called")
try:
cad_path = await zoo_export_kcl(
kcl_code=kcl_code,
kcl_path=kcl_path,
export_path=export_path,
export_format=export_format,
)
return str(cad_path)
except Exception as e:
return f"There was an error exporting the CAD file: {e}"
@mcp.tool()
def format_kcl(
kcl_code: str | None = None,
kcl_path: str | None = None,
) -> str:
"""Format KCL code given a string of KCL code or a path to a KCL project. Either kcl_code or kcl_path must be provided. If kcl_path is provided, it should point to a .kcl file or a directory containing .kcl files.
Args:
kcl_code (str | None): The KCL code to format.
kcl_path (str | None): The path to a KCL file to format. The path should point to a .kcl file or a directory containing a main.kcl file.
Returns:
str | None: Returns the formatted kcl code if the kcl_code is used otherwise returns None, the KCL in the kcl_path will be formatted in place
"""
logger.info("format_kcl tool called")
try:
res = zoo_format_kcl(kcl_code=kcl_code, kcl_path=kcl_path)
if isinstance(res, str):
return res
else:
return f"Successfully formatted KCL code at: {kcl_path}"
except Exception as e:
return f"There was an error formatting the KCL: {e}"
@mcp.tool()
async def lint_and_fix_kcl(
kcl_code: str | None = None,
kcl_path: str | None = None,
) -> tuple[str, list[str]]:
"""Lint and fix KCL code given a string of KCL code or a path to a KCL project. Either kcl_code or kcl_path must be provided. If kcl_path is provided, it should point to a .kcl file or a directory containing .kcl files.
Args:
kcl_code (str | None): The KCL code to lint and fix.
kcl_path (str | None): The path to a KCL file to lint and fix. The path should point to a .kcl file or a directory containing a main.kcl file.
Returns:
tuple[str, list[str]]: If kcl_code is provided, it returns a tuple containing the fixed KCL code and a list of unfixed lints.
If kcl_path is provided, it returns a tuple containing a success message and a list of unfixed lints for each file in the project.
"""
logger.info("lint_and_fix_kcl tool called")
try:
res, lints = zoo_lint_and_fix_kcl(kcl_code=kcl_code, kcl_path=kcl_path)
if isinstance(res, str):
return res, lints
else:
return f"Successfully linted and fixed KCL code at: {kcl_path}", lints
except Exception as e:
return f"There was an error linting and fixing the KCL: {e}", []
@mcp.tool()
async def mock_execute_kcl(
kcl_code: str | None = None,
kcl_path: str | None = None,
) -> tuple[bool, str]:
"""Mock execute KCL code given a string of KCL code or a path to a KCL project. Either kcl_code or kcl_path must be provided. If kcl_path is provided, it should point to a .kcl file or a directory containing a main.kcl file.
Args:
kcl_code (str | None): The KCL code to mock execute.
kcl_path (str | None): The path to a KCL file to mock execute. The path should point to a .kcl file or a directory containing a main.kcl file.
Returns:
tuple(bool, str): Returns True if the KCL code executed successfully and a success message, False otherwise and the error message.
"""
logger.info("mock_execute_kcl tool called")
try:
return await zoo_mock_execute_kcl(kcl_code=kcl_code, kcl_path=kcl_path)
except Exception as e:
return False, f"Failed to mock execute KCL code: {e}"
@mcp.tool()
async def multiview_snapshot_of_cad(
input_file: str,
) -> ImageContent | str:
"""Save a multiview snapshot of a CAD file. The input file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
This multiview image shows the render of the model from 4 different views:
The top left images is a front view.
The top right image is a right side view.
The bottom left image is a top view.
The bottom right image is an isometric view
Args:
input_file (str): The path of the file to get the mass from. The file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
Returns:
ImageContent | str: The multiview snapshot of the CAD file as an image, or an error message if the operation fails.
"""
logger.info("multiview_snapshot_of_cad tool called for file: %s", input_file)
try:
image = zoo_multiview_snapshot_of_cad(
input_path=input_file,
)
return encode_image(image)
except Exception as e:
return f"There was an error creating the multiview snapshot: {e}"
@mcp.tool()
async def multiview_snapshot_of_kcl(
kcl_code: str | None = None,
kcl_path: str | None = None,
) -> ImageContent | str:
"""Save a multiview snapshot of KCL code. Either kcl_code or kcl_path must be provided. If kcl_path is provided, it should point to a .kcl file or a directory containing a main.kcl file.
This multiview image shows the render of the model from 4 different views:
The top left images is a front view.
The top right image is a right side view.
The bottom left image is a top view.
The bottom right image is an isometric view
Args:
kcl_code (str | None): The KCL code to export to a CAD file.
kcl_path (str | None): The path to a KCL file to export to a CAD file. The path should point to a .kcl file or a directory containing a main.kcl file.
Returns:
ImageContent | str: The multiview snapshot of the KCL code as an image, or an error message if the operation fails.
"""
logger.info("multiview_snapshot_of_kcl tool called")
try:
image = await zoo_multiview_snapshot_of_kcl(
kcl_code=kcl_code,
kcl_path=kcl_path,
)
return encode_image(image)
except Exception as e:
return f"There was an error creating the multiview snapshot: {e}"
@mcp.tool()
async def multi_isometric_snapshot_of_cad(
input_file: str,
) -> ImageContent | str:
"""Save a multi-isometric snapshot of a CAD file showing 4 isometric views. The input file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
This multi-isometric image shows the render of the model from 4 different isometric views:
The top left image is an isometric view from the front-right corner.
The top right image is an isometric view from the front-left corner.
The bottom left image is an isometric view from the back-right corner.
The bottom right image is an isometric view from the back-left corner.
Args:
input_file (str): The path of the file to snapshot. The file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
Returns:
ImageContent | str: The multi-isometric snapshot of the CAD file as an image, or an error message if the operation fails.
"""
logger.info("multi_isometric_snapshot_of_cad tool called for file: %s", input_file)
try:
image = zoo_multi_isometric_snapshot_of_cad(
input_path=input_file,
)
return encode_image(image)
except Exception as e:
return f"There was an error creating the multi-isometric snapshot: {e}"
@mcp.tool()
async def multi_isometric_snapshot_of_kcl(
kcl_code: str | None = None,
kcl_path: str | None = None,
) -> ImageContent | str:
"""Save a multi-isometric snapshot of KCL code showing 4 isometric views. Either kcl_code or kcl_path must be provided. If kcl_path is provided, it should point to a .kcl file or a directory containing a main.kcl file.
This multi-isometric image shows the render of the model from 4 different isometric views:
The top left image is an isometric view from the front-right corner.
The top right image is an isometric view from the front-left corner.
The bottom left image is an isometric view from the back-right corner.
The bottom right image is an isometric view from the back-left corner.
Args:
kcl_code (str | None): The KCL code to export to a CAD file.
kcl_path (str | None): The path to a KCL file to export to a CAD file. The path should point to a .kcl file or a directory containing a main.kcl file.
Returns:
ImageContent | str: The multi-isometric snapshot of the KCL code as an image, or an error message if the operation fails.
"""
logger.info("multi_isometric_snapshot_of_kcl tool called")
try:
image = await zoo_multi_isometric_snapshot_of_kcl(
kcl_code=kcl_code,
kcl_path=kcl_path,
)
return encode_image(image)
except Exception as e:
return f"There was an error creating the multi-isometric snapshot: {e}"
@mcp.tool()
async def snapshot_of_cad(
input_file: str,
camera_view: dict[str, list[float]] | str = "isometric",
) -> ImageContent | str:
"""Save a snapshot of a CAD file.
Args:
input_file (str): The path of the file to get the mass from. The file should be one of the supported formats: .fbx, .gltf, .obj, .ply, .sldprt, .step, .stp, .stl (case-insensitive)
camera_view (dict | str): The camera to use for the snapshot.
1. If a string is provided, it should be one of 'front', 'back', 'left', 'right', 'top', 'bottom', 'isometric', 'isometric_front_right', 'isometric_front_left', 'isometric_back_right', 'isometric_back_left' to set the camera to a predefined view.
2. If a dict is provided, supply a dict with the following keys and values:
"up" (list of 3 floats) defining the up vector of the camera, "vantage" (list of 3 floats), and "center" (list of 3 floats).
For example camera = {"up": [0, 0, 1], "vantage": [0, -1, 0], "center": [0, 0, 0]} would set the camera to be looking at the origin from the front side (-y direction).
Returns:
ImageContent | str: The snapshot of the CAD file as an image, or an error message if the operation fails.
"""
logger.info("snapshot_of_cad tool called for file: %s", input_file)
try:
if isinstance(camera_view, dict):
camera = OptionDefaultCameraLookAt(
up=Point3d(
x=camera_view["up"][0],
y=camera_view["up"][1],
z=camera_view["up"][2],
),
vantage=Point3d(
x=camera_view["vantage"][0],
y=-camera_view["vantage"][1],
z=camera_view["vantage"][2],
),
center=Point3d(
x=camera_view["center"][0],
y=camera_view["center"][1],
z=camera_view["center"][2],
),
)
else:
if camera_view not in CameraView.views.value:
raise ZooMCPException(
f"Invalid camera view: {camera_view}. Must be one of {list(CameraView.views.value.keys())}"
)
camera = CameraView.to_kittycad_camera(CameraView.views.value[camera_view])
image = zoo_snapshot_of_cad(
input_path=input_file,
camera=camera,
)
return encode_image(image)
except Exception as e:
return f"There was an error creating the snapshot: {e}"
@mcp.tool()
async def snapshot_of_kcl(
kcl_code: str | None = None,
kcl_path: str | None = None,
camera_view: dict[str, list[float]] | str = "isometric",
) -> ImageContent | str:
"""Save a snapshot of a model represented by KCL. Either kcl_code or kcl_path must be provided. If kcl_path is provided, it should point to a .kcl file or a directory containing a main.kcl file.
Args:
kcl_code (str | None): The KCL code to export to a CAD file.
kcl_path (str | None): The path to a KCL file to export to a CAD file. The path should point to a .kcl file or a directory containing a main.kcl file.
camera_view (dict | str): The camera to use for the snapshot.
1. If a string is provided, it should be one of 'front', 'back', 'left', 'right', 'top', 'bottom', 'isometric', 'isometric_front_right', 'isometric_front_left', 'isometric_back_right', 'isometric_back_left' to set the camera to a predefined view.
2. If a dict is provided, supply a dict with the following keys and values:
"up" (list of 3 floats) defining the up vector of the camera, "vantage" (list of 3 floats), and "center" (list of 3 floats).
For example camera = {"up": [0, 0, 1], "vantage": [0, -1, 0], "center": [0, 0, 0]} would set the camera to be looking at the origin from the front side (-y direction).
Returns:
ImageContent | str: The snapshot of the CAD file as an image, or an error message if the operation fails.
"""
logger.info("snapshot_of_kcl tool called")
try:
if isinstance(camera_view, dict):
camera = kcl.CameraLookAt(
up=kcl.Point3d(
x=camera_view["up"][0],
y=camera_view["up"][1],
z=camera_view["up"][2],
),
vantage=kcl.Point3d(
x=camera_view["vantage"][0],
y=-camera_view["vantage"][1],
z=camera_view["vantage"][2],
),
center=kcl.Point3d(
x=camera_view["center"][0],
y=camera_view["center"][1],
z=camera_view["center"][2],
),
)
else:
if camera_view not in CameraView.views.value:
raise ZooMCPException(
f"Invalid camera view: {camera_view}. Must be one of {list(CameraView.views.value.keys())}"
)
camera = CameraView.to_kcl_camera(CameraView.views.value[camera_view])
image = await zoo_snapshot_of_kcl(
kcl_code=kcl_code,
kcl_path=kcl_path,
camera=camera,
)
return encode_image(image)
except Exception as e:
return f"There was an error creating the snapshot: {e}"
@mcp.tool()
async def text_to_cad(prompt: str) -> str:
"""Generate a CAD model as KCL code from a text prompt.
# General Tips
- You can use verbs like "design a..." or "create a...", but those aren't needed. Prompting "A gear" works as well as "Create a gear".
- If your prompt omits important dimensions, Text-to-CAD will make its best guess to fill in missing details.
- Traditional, simple mechanical parts such as fasteners, bearings and connectors work best right now.
- Text-to-CAD returns a 422 error code if it fails to generate a valid geometry internally, even if it understands your prompt. We're working on reducing the amount of errors.
- Shorter prompts, 1-2 sentences in length, succeed more often than longer prompts.
- The maximum prompt length is approximately 6000 words. Generally, shorter prompts of one or two sentences work best. Longer prompts take longer to resolve.
- The same prompt can generate different results when submitted multiple times. Sometimes a failing prompt will succeed on the next attempt, and vice versa.
# Examples
- "A 21-tooth involute helical gear."
- "A plate with a hole in each corner for a #10 bolt. The plate is 4" wide, 6" tall."
- "A dodecahedron."
- "A camshaft."
- "A 1/2 inch gear with 21 teeth."
- "A 3x6 lego."
Args:
prompt (str): The text prompt to be realized as KCL code.
Returns:
str: The generated KCL code if Text-to-CAD is successful, otherwise the error message.
"""
logger.info("text_to_cad tool called with prompt: %s", prompt)
try:
return await _text_to_cad(prompt=prompt)
except Exception as e:
return f"There was an error generating the CAD file from text: {e}"
@mcp.tool()
async def edit_kcl_project(
prompt: str,
proj_path: str,
) -> dict | str:
"""Modify an existing KCL project by sending a prompt and a KCL project path to Zoo's Text-To-CAD "edit KCL project" endpoint. The proj_path will upload all contained files to the endpoint. There must be a main.kcl file in the root of the project.
# General Tips
- You can use verbs like "add", "remove", "change", "make", "fillet", etc. to describe the modification you want to make.
- Be specific about what you want to change in the model. For example, "add a hole to the center" is more specific than "add a hole".
- If your prompt omits important dimensions, Text-to-CAD will make its best guess to fill in missing details.
- Text-to-CAD returns a 422 error code if it fails to generate a valid geometry internally, even if it understands your prompt.
- Shorter prompts, 1-2 sentences in length, succeed more often than longer prompts.
- The maximum prompt length is approximately 6000 words. Generally, shorter prompts of one or two sentences work best. Longer prompts take longer to resolve.
- The same prompt can generate different results when submitted multiple times. Sometimes a failing prompt will succeed on the next attempt, and vice versa.
# Examples
- "Add a hole to the center of the plate."
- "Make the gear twice as large."
- "Remove the top face of the box."
- "Fillet each corner"
Args:
prompt (str): The text prompt describing the modification to be made.
proj_path (str): A path to a KCL project directory containing a main.kcl file in the root. All contained files (found recursively) will be sent to the endpoint.
Returns:
dict | str: A dictionary containing the complete KCL code of the CAD model if Text-To-CAD edit KCL project was successful.
Each key in the dict refers to a KCL file path relative to the project path, and each value is the complete KCL code for that file.
If unsuccessful, returns an error message from Text-To-CAD.
"""
logger.info("edit_kcl_project tool called with prompt: %s", prompt)
try:
return await _edit_kcl_project(
proj_path=proj_path,
prompt=prompt,
)
except Exception as e:
return f"There was an error modifying the KCL project from text: {e}"
@mcp.tool()
async def save_image(
image: ImageContent,
output_path: str | None = None,
) -> str:
"""Save an ImageContent object to disk. This allows a human to review images locally that an LLM has requested.
Args:
image (ImageContent): The ImageContent object to save. This is typically returned by snapshot tools like snapshot_of_kcl, snapshot_of_cad, multiview_snapshot_of_kcl, or multiview_snapshot_of_cad.
output_path (str | None): The path where the image should be saved. Can be a file path (e.g., '/path/to/image.png') or a directory (e.g., '/path/to/dir'). If a directory is provided, the file will be named 'image.png'. If not provided, a temporary file will be created.
Returns:
str: The absolute path to the saved image file, or an error message if the operation fails.
"""
logger.info("save_image tool called with output_path: %s", output_path)
try:
saved_path = save_image_to_disk(image=image, output_path=output_path)
return saved_path
except Exception as e:
return f"There was an error saving the image: {e}"
def main():
logger.info("Starting MCP server...")
mcp.run(transport="stdio")
if __name__ == "__main__":
main()