这是别人已经做好的项目:freecad-mcp · PyPI

FreeCAD API查询网址:FreeCAD Python API — freecad 0.19.master documentation

齿轮模块freecad gear模块 生成齿轮导出fcstd step_freecad中python控制台制作齿轮-CSDN博客

-------------------------------------------------------------------------------

 这是我不知道他做好的情况下自己做的

from mcp.server.fastmcp import FastMCP
import freecad.gears.commands
import os
from freecad import app
from freecad import part
mcp = FastMCP("Demo")

@mcp.tool()
def create_gear(num_teeth=20,height=10,double_helix= True):
     """
     创建一个渐开线齿轮并导出为 STEP 文件。

     参数:
          num_teeth (int): 齿轮的齿数,默认值为 20。
          height (float): 齿轮的高度(厚度),默认值为 10。
          double_helix (bool): 是否启用双螺旋,默认值为 True。
     
     返回:
           savepath:生成的齿轮保存地址。
     """
     app.newDocument()
     gear = freecad.gears.commands.CreateInvoluteGear.create()
     gear.num_teeth = num_teeth
     gear.height = height
     gear.double_helix = double_helix
     app.ActiveDocument.recompute()
     savepath = os.path.join(os.path.dirname(__file__), "gear.step")
     part.export([gear], savepath)
     return savepath



if __name__ == "__main__":
     mcp.run()
 

 mcp 接freecad画齿轮_哔哩哔哩_bilibili

cline配置

{
  "mcpServers": {
    "freecad-demo": {
      "disabled": false,
      "timeout": 60,
      "type": "stdio",
      "command": "E:\\code\\mcp_freecad\\.venv\\python.exe",
      "args": [
        "e:/code/mcp_freecad/main.py"
      ],
      "env": {}
    }
  }
}

.vscode launch.josn配置

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Python 调试程序: 当前文件",
            "type": "debugpy",
            "request": "launch",
            "program": "main.py",
            "console": "integratedTerminal"
        }
    ]
}

-32000解决办法

加入模块调试

print("freecad module attributes:")
print(dir(freecad))

发现是没有app的

PATH_TO_FREECAD_LIBDIR not specified, using default FreeCAD version in E:/code/mcp_freecad/.ve
nv/Library/bin
freecad module attributes:
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__',
 '__path__', '__spec__', '_extend_path', '_os', '_path_to_freecad_libdir', '_sys', 'gea
rs']

激活环境后再安装

micromamba install freecad

Logo

更多推荐