solidworks-mcp-pro
# solidworks-mcp-pro
一个从零编写的 SolidWorks MCP 自动建模仓库,用于把 Codex / 其他 MCP 客户端连接到本机 Windows 上的 SolidWorks,并通过 Python COM / SolidWorks API 自动创建零件、行业模板、导出文件和做基础审查。
> 说明:本仓库代码为独立生成的实现,没有复制第三方仓库代码。你可以把它作为自己的 GitHub 仓库第一版,然后继续扩展。
## 目标架构
```text
Codex / MCP Client
↓ stdio MCP
solidworks-mcp-pro
↓ Python COM / pywin32
SldWorks.Application
↓
SolidWorks Part / Assembly / Drawing / STEP / PDF / PNG
```
## 当前第一版能力
- MCP Server:通过 `stdio` 暴露 SolidWorks 工具。
- SolidWorks 会话管理:连接、启动、活动文档、保存、导出。
- 安全输出目录:默认只能写入 `SW_MCP_OUTPUT_ROOT`。
- 基础零件:长方体、圆柱。
- 行业模板:法兰、简化管板、简化立式换热器概念件。
- JSON Spec 驱动:用结构化参数生成模型,减少自然语言歧义。
- 基础审查:活动文档标题、路径、类型、质量属性、导出预览。
- Codex 配置示例和 PowerShell 注册脚本。
- 单元测试骨架和 Windows 集成测试标记。
## 系统要求
- Windows 10 / 11
- 已安装 SolidWorks
- Python 3.10+
- Codex CLI 或其他支持 MCP 的客户端
## 安装
```powershell
cd C:\Projects
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -e C:\Projects\solidworks-mcp-pro
```
也可以直接在仓库根目录执行:
```powershell
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -e .
```
## 配置环境变量
最少建议设置输出目录:
```powershell
setx SW_MCP_OUTPUT_ROOT "C:\SW-MCP-Output"
```
如果 SolidWorks 默认模板读取失败,手动设置模板路径:
```powershell
setx SW_MCP_PART_TEMPLATE "C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2025\templates\Part.prtdot"
setx SW_MCP_ASSEMBLY_TEMPLATE "C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2025\templates\Assembly.asmdot"
setx SW_MCP_DRAWING_TEMPLATE "C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2025\templates\Drawing.drwdot"
```
## 直接启动 MCP Server
```powershell
.\.venv\Scripts\Activate.ps1
solidworks-mcp-pro
```
正常情况下它不会输出普通日志,因为 MCP 使用 stdio 通信。
## 注册到 Codex
方式一:命令注册
```powershell
codex mcp add solidworks-pro -- C:\Projects\solidworks-mcp-pro\.venv\Scripts\python.exe -m sw_mcp_pro.mcp_server.server
```
方式二:参考 `codex/config.example.toml` 手动加入 Codex 配置。
## 在 Codex 中测试
```text
请调用 solidworks-pro 的 sw_health,检查 SolidWorks 是否可连接。
```
```text
请调用 solidworks-pro 创建一个法兰:外径 200mm,厚度 20mm,中心孔 80mm,6 个直径 12mm 螺栓孔,PCD 150mm,保存为 flange_demo.SLDPRT。
```
## 推荐工作方式
先用结构化 spec:
```json
{
"type": "flange",
"name": "DN80_flange",
"params": {
"outer_diameter_mm": 200,
"thickness_mm": 20,
"center_hole_diameter_mm": 80,
"bolt_count": 6,
"bolt_hole_diameter_mm": 12,
"bolt_circle_diameter_mm": 150,
"material": "Plain Carbon Steel"
},
"save_as": "DN80_flange.SLDPRT"
}
```
然后让 Codex 调用:
```text
请调用 sw_create_from_spec_json,使用这个 JSON 创建模型。
```
## 目录结构
```text
solidworks-mcp-pro/
├── sw_mcp_pro/
│ ├── mcp_server/ # MCP 工具层
│ ├── sw_api/ # SolidWorks COM 封装层
│ ├── templates/ # 参数化行业模板
│ ├── specs/ # JSON spec 解析与校验
│ └── utils/ # 日志、安全路径、JSON 工具
├── examples/ # 示例 spec
├── installers/ # Codex 注册脚本
├── codex/ # Codex 配置示例
├── docs/ # 文档
└── tests/ # 测试
```
## 安全边界
本项目默认不提供任意 Python / VBA 执行工具。所有文件写入默认限制在 `SW_MCP_OUTPUT_ROOT` 内,避免 AI 把文件保存到不可控目录。若确实需要写入任意路径,可设置:
```powershell
setx SW_MCP_ALLOW_OUTSIDE_ROOT "1"
```
不建议长期开启。
## 发展路线
- 更完整草图约束与尺寸系统
- 孔向导 / 螺纹孔 / 配置表
- 复杂装配体 Mate 模板
- 工程图自动标注 / BOM / 标题栏
- 换热器、管板、U 型管、支架、接管、封头模板
- 模型对照 spec 自动验收
- 多工作站任务队列
TDQS
Scored across 10 tools
Most tools target clearly distinct actions: health check, document info, creation of specific shapes, export, and review. The main overlap is between the specific creation tools and sw_create_from_spec_json, which can create the same supported types and may cause an agent to pick the wrong pathway.
The sw_ prefix and verb_noun structure are used consistently, creating a readable pattern like sw_create_box, sw_export_active, and sw_review_active. Minor deviations exist with sw_health and sw_active_document_info, which omit a verb, but the overall convention is still predictable.
Ten tools is well-scoped for a SolidWorks automation server covering health, modeling, export, and review. Each tool serves a distinct role in the workflow without unnecessary redundancy or bloat.
The tool surface covers the core workflow: health check, inspect active document, create common part types, export to standard formats, and review the result. Missing capabilities like opening/closing documents, editing existing models, or assembly support are notable but likely outside the intended focused scope.