SolidWorks MCP 服务器
针对SolidWorks 2020的全面模型上下文协议(MCP)服务器,通过Claude Desktop及其他MCP客户端实现对SolidWorks的自然语言控制。
建筑学
这台服务器实现了(或部署了)一个 4种工具类型 将高级工作流工具与灵活、动态的API执行相结合:
1: 高级工作流工具
用于常见操作的简单、确定性工具:
create_part- 创建新的零件文档create_assembly- 创建新的装配文档create_sketch- 在平面上绘制草图create_extrude- 创建挤压特征create_rectangle- 在草图中添加矩形create_circle- 在草图中添加圆圈create_line- 在草图中添加线条create_base_flange- 创建钣金基法兰特征(钣金零件中的第一个特征)create_edge_flange- 在钣金零件上创建边缘翻边 - *由于未能选择/保持边选择,因此无法工作*get_model_info- 获取文档信息save_document- 保存文档 - *取决于进程/目录权限*
2: 动态API执行
灵活的工具,支持任意SolidWorks API调用:
execute_solidworks_api- 执行任何SolidWorks API方法 - *勉强能用,对任何复杂的事情都不好(目前还如此)*access_solidworks_property- 获取/设置接口上的属性 - *在一些事情上有所作为*
3: 国家检查资源
将SolidWorks状态作为可读数据展示:
get_active_document_resource- 当前文档信息get_feature_tree_resource- 功能列表和层级结构
注: 在(此处)提供了额外的资源方法(选择、草图、配置) SolidWorksResources.cs.bak 但需要对SolidWorks 2020进行API签名调整。
4: API文档搜索
自文档化功能:
search_solidworks_apiAPI文档全文搜索 *有待提高*get_interface_documentation- 获取接口详细信息get_method_documentation- 获取方法详情get_code_examples- 查找C#代码示例list_common_interfaces- 列出常见的SolidWorks接口get_workflow_guidance- 获取常见任务的工作流程指导
先决条件
- SolidWorks 2020 已安装(无需运行 - 服务器将连接/启动它)
- .NET 8.0 SDK(软件开发工具包) 或之后
- Windows操作系统 (COM 互操作性所需)
- SolidWorks API 动态链接库(DLLs) 在正确的位置:
- 默认值: C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist\ - 文件: SolidWorks.Interop.sldworks.dll, SolidWorks.Interop.swconst.dll
- SolidWorks API 文档 (可选,但建议启用以使用搜索功能):
- 路径: Path\to\solidworks\Solidworks-SDK\docs\api\ - 如有需要,将CHM格式转换 - 请务必在“ApiDocumentationService.cs”中更新此内容: _apiDocsPath = @"Path\\to\\solidworks\\Solidworks-SDK\\docs\\api\\"; 是占位符(稍后添加到配置中)
安装
1. 构建项目
cd "Path\\To\\SW_MCP"
dotnet restore
dotnet build -c Release2. 验证SolidWorks API DLL路径
如果您的SolidWorks安装位置不同,请更新路径中的信息 SolidWorksMCP.csproj:
YOUR_PATH_HERE\SolidWorks.Interop.sldworks.dll
false
3. 配置Claude桌面版
在您的Claude Desktop配置文件中添加以下内容:
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"solidworks": {
"command": "dotnet",
"args": [
"run",
"--project",
"Path\\To\\SW_MCP\\SolidWorksMCP.csproj"
]
}
}
}或者使用构建好的可执行文件:
{
"mcpServers": {
"solidworks": {
"command": "Path\\To\\SW_MCP\\bin\\Release\\net8.0\\SolidWorksMCP.exe"
}
}
}4. 重启Claude桌面版
重启Claude桌面以加载新的MCP服务器。
使用方法
基本工作流程示例
User: "Create a new part and draw a 100mm x 50mm rectangle, then extrude it 25mm"
Claude will:
1. Use create_part to create a new part
2. Use create_sketch to start a sketch on the Front plane
3. Use create_rectangle with coordinates (0, 0) to (0.1, 0.05) [meters]
4. Use create_extrude with depth 0.025 [meters]钣金示例
User: "Create a sheet metal part with a 100mm x 80mm base flange, 2mm thick"
Claude will:
1. Use create_part to create a new part
2. Use create_sketch to start a sketch on the Front plane
3. Use create_rectangle with coordinates (0, 0) to (0.1, 0.08) [meters]
4. Use create_base_flange with thickness 0.002 [meters]高级API调用 - *勉强能用*
User: "Use the SolidWorks API to create a fillet with radius 5mm on the selected edges"
Claude will:
1. Use search_solidworks_api to find fillet-related methods
2. Use execute_solidworks_api to call IFeatureManager.FeatureFillet with appropriate parameters检查状态
User: "What features are in the current model?"
Claude will use get_feature_tree_resource to list all features.学习API - *有待提高*
User: "How do I create a mate in an assembly?"
Claude will:
1. Use get_workflow_guidance with workflowType: "assembly"
2. Or search_solidworks_api for "mate"
3. Or get_code_examples for "add mate"单位
重要提示: SolidWorks API中的所有尺寸均为 米,无论文档单位如何。
- 1毫米 = 0.001米
- 1 英寸 = 0.0254 米
工作流工具期望的尺寸单位是米。请相应地转换您的尺寸。
建筑细节
连接管理
服务器通过COM互操作连接到SolidWorks Type.GetTypeFromProgID("SldWorks.Application") 和 Activator.CreateInstance()这将连接到正在运行的实例,或者如果SolidWorks未运行,则启动它。连接在服务器启动时建立,并在整个会话期间保持连接。
错误处理
所有工具都返回结构化的响应,其中 success 布尔与(逻辑与) error 消息:
{
"success": true,
"message": "Operation completed successfully"
}或者在失败时:
{
"success": false,
"error": "Error message describing what went wrong"
}API文档搜索
文档搜索服务在首次使用时对HTML文档文件进行索引,并将结果缓存到当前会话中。它使用:
- 在13,000多个HTML文件中进行全文搜索(需从CHM文件转换)
- 基于词频和文件名的相关性评分
- 从结构化文档中提取描述、语法和注释
故障排除
“未连接到SolidWorks”错误
解决方案: 确保SolidWorks已正确安装。服务器将尝试自动启动它。
启动时“无法连接到SolidWorks”
解决方案:
- 验证SolidWorks 2020是否已安装(检查COM注册情况)
- 确认SolidWorks API DLL文件位于正确的位置
- 尝试以管理员身份运行 Claude Desktop
- 确保SolidWorks许可证有效且已激活
- 检查Windows事件查看器中的COM相关错误
“没有活动文档”错误
解决方案: 在执行特定于文档的操作之前,请在SolidWorks中打开或创建一个文档。
API文档未找到
解决方案: 更新文档路径中的 ApiDocumentationService.cs:
_apiDocsPath = @"YOUR_DOCS_PATH_HERE";与SolidWorks互操作DLL相关的构建错误
解决方案:
- 验证DLL路径在
.csproj文件 - 确认已安装SolidWorks 2020(而非仅安装查看器)
- 确保您能访问到
api\redist文件夹
发展
项目结构
SolidWorksMCP/
├── Program.cs # Entry point and DI configuration
├── SolidWorksMCP.csproj # Project file with dependencies
├── Services/
│ ├── ISolidWorksConnection.cs # Connection interface
│ ├── SolidWorksConnection.cs # COM connection implementation
│ ├── IApiDocumentationService.cs # Documentation search interface
│ └── ApiDocumentationService.cs # Documentation search implementation
├── Tools/
│ ├── WorkflowTools.cs # Tier 1: High-level workflow tools
│ ├── DynamicApiTool.cs # Tier 2: Dynamic API execution
│ └── ApiDocumentationTool.cs # Tier 4: Documentation search tools
└── Resources/
└── SolidWorksResources.cs # Tier 3: State inspection resources添加新工具
- 在(某个位置/项目中)创建一个新的静态类
Tools/文件夹 - 用……装饰教室
[McpServerToolType] - 添加用装饰器装饰的静态方法
[McpServerTool] - 使用
[Description(...)]参数文档的属性 - 注入服务如
ISolidWorksConnection作为方法参数
示例:
[McpServerToolType]
public class MyCustomTools
{
[McpServerTool]
[Description("Does something useful")]
public static object MyTool(
ISolidWorksConnection connection,
[Description("A parameter")] string param1)
{
// Implementation
return new { success = true, result = "..." };
}
}扩展接口映射
为了支持更多接口类型 execute_solidworks_api,添加映射到 DynamicApiTool.GetInterfaceInstance():
return interfaceName.ToLowerInvariant() switch
{
// ... existing mappings ...
"imynewinterface" => GetMyNewInterface(connection),
_ => null
};局限性
- 单实例: 连接到正在运行的SolidWorks实例
- 无宏录制功能: 不将操作记录为宏
- 同步执行: 操作在完成前保持阻塞状态
- COM(组件对象模型)的局限性: 受COM线程和封送约束的限制
- 只读 COM 对象: 某些COM对象在响应中无法完全序列化
许可证
此项目按原样提供,供SolidWorks 2020使用。请确保遵守SolidWorks API许可条款。您可以随意使用此处提供的内容,因为它们均来自公开可用的文档。
