insert_part_from_library
Insert predefined 3D parts into FreeCAD designs using relative paths from the parts library addon. Simplifies CAD workflows by enabling quick part integration and generating design screenshots for verification.
Instructions
Insert a part from the parts library addon.
Args:
relative_path: The relative path of the part to insert.
Returns:
A message indicating the success or failure of the part insertion and a screenshot of the object.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| relative_path | Yes |
Implementation Reference
- src/freecad_mcp/server.py:467-497 (handler)MCP tool handler for 'insert_part_from_library'. Proxies the call to FreeCADConnection which forwards to the XML-RPC server in the FreeCAD addon. Includes schema via args/docstring and registration via @mcp.tool() decorator.@mcp.tool() def insert_part_from_library(ctx: Context, relative_path: str) -> list[TextContent | ImageContent]: """Insert a part from the parts library addon. Args: relative_path: The relative path of the part to insert. Returns: A message indicating the success or failure of the part insertion and a screenshot of the object. """ freecad = get_freecad_connection() try: res = freecad.insert_part_from_library(relative_path) screenshot = freecad.get_active_screenshot() if res["success"]: response = [ TextContent(type="text", text=f"Part inserted from library: {res['message']}"), ] return add_screenshot_if_available(response, screenshot) else: response = [ TextContent(type="text", text=f"Failed to insert part from library: {res['error']}"), ] return add_screenshot_if_available(response, screenshot) except Exception as e: logger.error(f"Failed to insert part from library: {str(e)}") return [ TextContent(type="text", text=f"Failed to insert part from library: {str(e)}") ]
- src/freecad_mcp/server.py:39-40 (helper)Proxy method in FreeCADConnection class that calls the XML-RPC server's insert_part_from_library method.def insert_part_from_library(self, relative_path: str) -> dict[str, Any]: return self.server.insert_part_from_library(relative_path)
- Core implementation of inserting a part by merging the FCStd file from the user's parts_library mod directory into the active FreeCAD document.def insert_part_from_library(relative_path): parts_lib_path = os.path.join(FreeCAD.getUserAppDataDir(), "Mod", "parts_library") part_path = os.path.join(parts_lib_path, relative_path) if not os.path.exists(part_path): raise FileNotFoundError(f"Not found: {part_path}") FreeCADGui.ActiveDocument.mergeProject(part_path)
- RPC server's queued GUI-safe handler that calls the parts_library.insert_part_from_library.def _insert_part_from_library(self, relative_path): try: insert_part_from_library(relative_path) return True except Exception as e: return str(e)
- src/freecad_mcp/server.py:550-564 (helper)Related MCP tool 'get_parts_list' to discover available parts before inserting.@mcp.tool() def get_parts_list(ctx: Context) -> list[str]: """Get the list of parts in the parts library addon. """ freecad = get_freecad_connection() parts = freecad.get_parts_list() if parts: return [ TextContent(type="text", text=json.dumps(parts)) ] else: return [ TextContent(type="text", text=f"No parts found in the parts library. You must add parts_library addon.") ]