Skip to main content
Glama
COMSOL Multiphysics MCP Server

COMSOL Multiphysics MCP Server

by zixuanguo89
README.md
# COMSOL Multiphysics MCP Server

[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/)
[![COMSOL](https://img.shields.io/badge/COMSOL-6.x-orange)](https://www.comsol.com/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
[![CI](https://github.com/zixuanguo89/comsol-mcp/actions/workflows/ci.yml/badge.svg)](https://github.com/zixuanguo89/comsol-mcp/actions/workflows/ci.yml)
[![GitHub stars](https://img.shields.io/github/stars/zixuanguo89/comsol-mcp?style=social)](https://github.com/zixuanguo89/comsol-mcp)

> GitHub: <https://github.com/zixuanguo89/comsol-mcp> · 274 tools · tested against real COMSOL 6.4

**目前功能最全面的 COMSOL Multiphysics MCP 服务器** —— 让 AI agent 直接建模、求解、
评估、绘图、动画、导出、写报告,并复用 COMSOL 官方应用库。

**The most comprehensive MCP server for COMSOL Multiphysics** — build, mesh, solve,
evaluate, plot, animate, export, report and reuse the official application library,
all through the Model Context Protocol.

```text
274 个工具 / tools   ·   949 个物理场接口 / physics interfaces   ·   7,343 个物理场特征类型
97 个 3D 几何特征   ·   51 个 3D 网格特征   ·   81 个研究类型   ·   121 种绘图特征   ·   86 种求解器特征
2,081 个官方应用库模型   ·   3,141 种材料 / 19 个材料库   ·   13 篇内置建模指南   ·   已验证 COMSOL 6.4 (build 293)
```

> 工具总数以 `python -m comsol_mcp --check` 的实时输出为准(本仓库仍在持续扩充)。
> Tool count is what `--check` reports live; the repository keeps growing.

## 目录 / Table of Contents

- [1. 为什么是"最全面"的 COMSOL MCP](#1-为什么是最全面的-comsol-mcp)
- [2. 功能矩阵 / Feature matrix](#2-功能矩阵--feature-matrix)
- [3. 架构 / Architecture](#3-架构--architecture)
- [4. 环境要求 / Requirements](#4-环境要求--requirements)
- [5. 安装 / Installation](#5-安装--installation)
- [6. 快速开始 / Quick start](#6-快速开始--quick-start)
- [7. 给 AI Agent 的示例提示词 / Example prompts](#7-给-ai-agent-的示例提示词--example-prompts)
- [8. 典型工作流 / Typical workflow](#8-典型工作流--typical-workflow)
- [9. DeepSeek Harness (DSH) 集成](#9-deepseek-harness-dsh-集成)
- [10. 环境变量 / Environment variables](#10-环境变量--environment-variables)
- [11. 工作区与产物 / Workspace](#11-工作区与产物--workspace)
- [12. 故障排查 / Troubleshooting](#12-故障排查--troubleshooting)
- [13. 安全说明 / Security](#13-安全说明--security)
- [14. 测试与验证 / Tested against real COMSOL](#14-测试与验证--tested-against-real-comsol)
- [15. 文档 / Documentation](#15-文档--documentation)
- [16. 许可证 / License](#16-许可证--license)
- [English summary](#english-summary)

## 1. 为什么是"最全面"的 COMSOL MCP

大多数 COMSOL 自动化工具只覆盖"建模"或"求解"的某个切片。本项目把**整个 COMSOL
工作流**做成 274 个 MCP 工具,并解决了让 AI agent 真正可用的三个核心问题:

1. **完整覆盖** —— 几何、材料、物理场、多物理场耦合、网格、研究/求解器、
   结果评估、绘图、动画/视频、数据导出、报告、模型库、知识库、后台任务全部覆盖;
   还提供 `tree_*` 通用模型树层和 `java_eval` 原生 API 逃生舱口,理论上没有
   "工具没覆盖"的死角。
2. **可发现性** —— 从本机 COMSOL 6.4 安装中提取的完整类型目录(949 个物理场接口、
   7,343 个物理场特征类型、97 个 3D 几何特征、51 个 3D 网格特征、81 个研究类型、
   121 种绘图特征、86 种求解器特征、64 种数据集、33 种数值评估、5 种绘图组……)
   随包离线分发。Agent 先查目录再建模型,
   `catalog_*` / `phys_feature_types` / `tree_properties` 让类型名和属性名可自省。
3. **AI 友好** —— 所有工具返回紧凑的 `{"ok": ..., ...}` JSON;错误带
   `code`/`hint` 和"可用选项"建议;长任务走异步 job;导出产物返回绝对路径 + 自检
   字段(文件大小、图片尺寸、GIF 帧数、mp4 有效性);列表有上限,避免撑爆上下文。

配套交付:安装器、4 种 MCP 客户端配置示例、DeepSeek Harness 直接接入、自动生成的
令牌级工具文档([`docs/TOOL_REFERENCE.md`](docs/TOOL_REFERENCE.md))、用户指南与
打包冒烟测试。

## 2. 功能矩阵 / Feature matrix

| 领域 / Domain | 工具数 | 能做什么 / Capabilities | 代表工具 / Key tools |
|---|---:|---|---|
| **几何 / Geometry** | 30 | 3D/2D/1D/轴对称几何;所有图元(块/柱/球/锥/环/金字塔/棱柱…);布尔、拉伸、旋转、扫掠、放样、倒角/圆角、抽壳、中面、变换/阵列;工作平面;CAD/STEP/IGES/DXF/STL 导入导出;实体几何统计、包围盒、测量;框选/球选/逻辑选择 | `geom_primitive` `geom_boolean` `geom_extrude` `geom_revolve` `geom_sweep` `geom_import` `geom_entity_info` `geom_measure` `geom_selection_actions` |
| **材料 / Materials** | 14 | 19 个官方材料库、3,141 种材料离线检索;按名/库/类别/所需物性搜索;从库添加(支持保持 LibraryLink 或解链成可编辑副本);手动物性(各向异性张量、多物性组);物性读写;XML 材料导入;域分配 | `mat_library_search` `mat_add` `mat_create` `mat_properties` `mat_set_property` `mat_import_xml` |
| **物理场 / Physics** | 19 | 任意物理场接口(949 种);友好 BC 别名(温度/热流/对流/辐射/固定/入口/出口/接地/端口…);特征创建/删除/启停;选择集;属性读写;接口级属性;特征树内省 | `phys_add` `phys_add_bc` `phys_add_feature` `phys_properties` `phys_set_property` `phys_feature_types` |
| **多物理场 / Multiphysics** | 7 | 耦合添加/删除:热膨胀、非等温流动、电磁热、声-固耦合、流-固耦合等;子特征;属性读写 | `multiphysics_add` `multiphysics_add_feature` `multiphysics_properties` |
| **网格 / Mesh** | 22 | 自由四面体/三角形、映射、扫掠、边界层、尺寸预设与自定义、细化、导入;统计(单元/顶点/质量/体积)、按实体统计、质量直方图;STL/Nastran/3MF/PLY/原生导出 | `mesh_create_sequence` `mesh_free_tet` `mesh_sweep` `mesh_boundary_layers` `mesh_set_size` `mesh_stats` `mesh_quality` `mesh_export` |
| **研究 / 求解 / Study** | 34 | 稳态、瞬态、特征频率、频域、参数/材料/批量扫描、灵敏度;研究步骤任意类型;求解器配置树编辑;同步/异步求解;解对象管理;数据集绑定;求解消息与计时 | `study_stationary` `study_transient` `study_eigenfrequency` `study_parametric` `study_solve_async` `solver_configure` `solution_info` |
| **结果 / Results** | 27 | 全局/点/线/面/体求值;积分、平均、最值、标准差、测量、质量属性;所有解/时间/参数点批量评估;表达式合法性预检;CSV/TXT 数据导出;结果表;数据集管理 | `result_evaluate` `result_eval_point` `result_integrate` `result_minmax` `result_eval_all_solutions` `result_export_data` `dataset_create` |
| **绘图 / Plots** | 13 | 一/二/三维绘图组;Surface/Volume/Slice/Isosurface/Contour/Arrow/Streamline/LineGraph/Point/Mesh/Global 等;色表、标题、图例、固定色标;PNG/JPEG/BMP 导出并用 Pillow 校验尺寸;绘图数据 CSV | `plot_quick` `plot_group_create` `plot_add_feature` `plot_export_image` `plot_export_plot_data` |
| **动画 / Animation** | 16 | 一步出 GIF/MP4(固定色标、防闪烁);COMSOL 原生 movie/PNG 序列;任意格式转换、拼接、缩放、加标题;预览/接触表;帧合成(无需 COMSOL);视频信息;聊天内直接展示图像与动画帧 | `anim_quick` `anim_create` `anim_export` `anim_convert` `anim_add_title` `media_frames_to_mp4` `visual_view_animation` |
| **导出 / Export** | 9 | 网格、几何(STL/STEP/IGES/Parasolid/ACIS/原生)、模型 `.mph`、Java 源码、Plot/Touchstone 导出特征;格式能力查询 | `export_mesh` `export_geometry` `export_model_file` `export_model_java` `export_touchstone` |
| **报告 / Report** | 5 | HTML / DOCX 报告(含内嵌图片);TitlePage/Section/TOC;插入绘图、表格、网格、物理场、研究等节点;生成后校验文件;Presentation/pptx | `report_create` `report_add_section` `report_add_content` `report_generate` |
| **知识库 / Knowledge** | 4 | 13 篇内置建模指南(入门/几何/物理/材料/网格/求解/结果/绘图/动画/单位/API/排障/工作流);检索离线 COMSOL HTML 文档;按目标推荐工具序列 | `docs_list` `docs_get` `docs_search` `docs_workflow` |
| **模型库 / Model library** | 7 | 官方应用库 2,081 个模型离线索引;按关键词/物理场/研究类型/模块/维度检索;元数据(预计耗时、大小、文档);加载、批量求解并保存到工作区 | `library_search` `library_describe` `library_load` `library_run` `library_stats` |
| **通用模型树 / Tree** | 16 | 任意模型树节点的浏览/搜索/属性读写/子节点创建/删除/重命名/改 tag/运行/选择集/启停;通用逃生舱口 | `tree_children` `tree_properties` `tree_set_property` `tree_create` `tree_dump` `tree_find` |
| **原生 Java 通道 / java_eval** | 1 | 在 COMSOL 工作线程内执行任意 Python/Java API 代码,访问 `model/jm/client/jcast/jset/...`(有安全风险,见 §13) | `java_eval` |
| **可视化 / Visual** | 2 | 在对话中直接显示导出的 PNG/JPEG/GIF/WebP 或动画抽样帧(filmstrip) | `visual_view_image` `visual_view_animation` |
| **会话 / 模型生命周期** | 22 | 启动/连接/重启/停止 COMSOL;模型新建/加载/保存/另存/克隆/重命名/删除/快照;当前模型;模型描述、问题列表;能力与健康检查 | `session_start` `model_create` `model_load` `model_save` `model_describe` `health_check` `comsol_capabilities` |
| **参数/变量/函数/选择** | 15 | 全局参数批量读写/删除/描述;变量;解析/插值/阶跃/脉冲等全局函数;命名选择(显式/框/球/柱/逻辑组合) | `parameter_set` `parameter_list` `variable_set` `function_create` `selection_create` |
| **后台任务 / Jobs** | 4 | 异步求解/批量作业:状态、等待、取消、列表;作业带模型与 JSON 日志产物 | `study_solve_async` `job_status` `job_wait` `job_cancel` |
| **类型目录 / Catalogs** | 7 | 物理场接口/特征、几何、网格、研究、绘图特征、数值/数据集/导出/求解器类型查询(离线,无需 COMSOL) | `catalog_physics_interfaces` `catalog_physics_features` `catalog_plot_features` `catalog_types` |
| **MCP 集成 / Integration** | — | stdio / HTTP(streamable-http) 传输;`comsol://` 只读资源(状态/模型/参数/目录/工作区);服务器 instructions;DeepSeek Harness、Claude Desktop、Codex、opencode、Cursor/Cline 配置示例 | `comsol://status` `comsol://model/{name}/tree` `docs/DSH_INTEGRATION.md` |

合计 274 个 MCP 工具 + 4 个 `comsol://` 资源 + 2 个资源模板(以 `--check` 为准)。

> **上下文预算 / Context budget**:客户端上下文有限时可设
> `COMSOL_MCP_TOOL_PROFILE=minimal`,只暴露 **166 个核心工具**(覆盖完整的日常
> 建模→求解→结果→绘图→动画流程);默认 `full` 暴露全部 274 个。也可以用
> `COMSOL_MCP_TOOL_ALLOW` / `COMSOL_MCP_TOOL_DENY` 自定义白/黑名单。

## 3. 架构 / Architecture

```text
┌──────────────────────────┐   MCP (stdio / HTTP)   ┌──────────────────────────────┐
│ MCP client                │ <────────────────────> │ comsol-mcp 服务器进程          │
│ DSH / Claude / Codex /…   │                        │  MCPServer (mcp>=2)           │
└──────────────────────────┘                        │  ├── 274 tools (自动发现)      │
                                                    │  ├── comsol:// resources     │
                                                    │  ├── 类型目录 / 知识库 / 模型库 │
                                                    │  └── App                     │
                                                    │      ├── SessionManager      │
                                                    │      ├── ModelManager        │
                                                    │      ├── 单线程 ComsolExecutor│
                                                    │      └── JobManager          │
                                                    └──────────────┬───────────────┘
                                                                   │ JPype + MPh client
                                                                   ▼
                                                    ┌──────────────────────────────┐
                                                    │ comsolmphserver.exe (独立进程) │
                                                    │  建模 / 求解 / 渲染 / 导出     │
                                                    └──────────────────────────────┘
```

* **所有 COMSOL 调用串行化**:MCP SDK 在线程池里跑同步工具,每个 COMSOL 调用都提交到
  唯一的 `ComsolExecutor` 工作线程,保证 Java API 不会并发交错。
* **COMSOL 在独立进程**(client-server 模式,默认):求解器崩溃不会带走 MCP 服务器,
  可以 `session_restart` 恢复;`stand-alone` 模式则进程内运行、API 往返更快。
* **长任务异步化**:求解/批量作业经 `JobManager` 提交,返回 `job_id`,可轮询、等待、
  协作式取消;产物与日志附着在 job 上。
* **离线优先**:类型目录、材料库索引、应用库索引、COMSOL 文档检索全部来自本机安装,
  不需要联网。

详见 [`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md)。

## 4. 环境要求 / Requirements

| 项目 | 要求 |
|---|---|
| COMSOL Multiphysics | 6.x(在 **6.4 build 293**、全模块授权下完成完整验证);默认安装路径 `C:\Program Files\COMSOL\COMSOL64` |
| Python | 3.10+(本机验证 3.13.15;需 64 位) |
| Python 包 | `mcp>=2.0.0`、`mph>=1.3.1`、`jpype1>=1.5.0`、`numpy>=1.24`、`Pillow>=9.0` |
| 可选(MP4 动画) | `imageio` + `imageio-ffmpeg`(**本机已安装**,自带 ffmpeg 7.1 二进制,MP4 无需系统 ffmpeg);GIF 只需要 Pillow;`pymupdf`(PDF 文档) |
| 操作系统 | Windows(COMSOL 桌面版;脚本与配置以 Windows 为主);Linux 理论上可用但未验证 |
| MCP 客户端 | 任意支持 stdio MCP 的客户端:DeepSeek Harness、Claude Desktop、Codex CLI、opencode、Cursor/Cline… |

## 5. 安装 / Installation

### 5.1 一键安装器(推荐)/ Installer

```powershell
git clone https://github.com/zixuanguo89/comsol-mcp.git
cd comsol-mcp
powershell -ExecutionPolicy Bypass -File .\install.ps1
```

安装器:找到 Python 3.10+ → 若系统 Python 已具备全部依赖则就地 `pip install -e .`,
否则创建 `.venv` 并安装依赖(默认走阿里云 PyPI 镜像回退)→ 运行
`python -m comsol_mcp --check` → 验证 `comsol-mcp` 控制台脚本 → 打印 stdio 启动命令
与 MCP 客户端 JSON 片段。

常用参数:`-Venv`(强制 venv)、`-SystemSite`(venv 复用系统包)、`-Offline`、
`-CheckOnly`(只校验不安装)、`-IndexUrl <url>`。

### 5.2 手动安装 / Manual

```powershell
# 方式 A:源码直跑(不安装,最省事);<repo> 替换为克隆后的仓库路径
$env:PYTHONPATH = "<repo>\src"
python -m comsol_mcp --check

# 方式 B:安装为包(获得 comsol-mcp 命令)
python -m pip install -e .
python -m pip install imageio imageio-ffmpeg   # 可选:MP4 动画
comsol-mcp --check
```

> PyPI 不可达时加镜像:
> `--index-url https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com`。

### 5.3 自检 / Self-check

```powershell
$env:PYTHONPATH = "\\path\to\comsol-mcp\src"
& "python" -m comsol_mcp --check
```

输出 `{"ok": true, "tool_count": 274, "tools": [...], "settings": {...}, "catalogs": {...}}`
即成功(不需要 COMSOL 许可证)。打包回归测试:

```powershell
& "python" tests\smoke_packaging.py
& "python" tests\smoke_packaging.py --full   # 额外验证安装后的控制台脚本
```

## 6. 快速开始 / Quick start

### 6.1 DeepSeek Harness (DSH)

本机 DSH Desktop profile 已配置好(stdio 启动 + `serverName: comsol`),改动记录、
精确 diff、验证证据与回滚方法见 [`docs/DSH_INTEGRATION.md`](docs/DSH_INTEGRATION.md)。
重启 DeepSeek Harness 后新开会话,工具以 `mcp__comsol__<tool>` 出现,例如
`mcp__comsol__comsol_capabilities`、`mcp__comsol__geom_primitive`。

### 6.2 Claude Desktop / Codex / opencode / 通用客户端

直接使用 [`config/`](config/) 中的示例:

| 客户端 | 配置文件 | 放置位置 |
|---|---|---|
| Claude Desktop | [`config/claude_desktop_config.json`](config/claude_desktop_config.json) | `%APPDATA%\Claude\claude_desktop_config.json` |
| Codex CLI | [`config/codex_config.toml`](config/codex_config.toml) | `%USERPROFILE%\.codex\config.toml` |
| opencode | [`config/opencode.json`](config/opencode.json) | 项目根目录或 `~/.config/opencode/opencode.json` |
| Cursor / Cline / Windsurf | [`config/mcp.json`](config/mcp.json) | 客户端对应位置 |

配置要点:`command` 用绝对路径的 `python.exe`,`args` 为
`["-m","comsol_mcp","--transport","stdio"]`,`env` 里显式给 `PYTHONPATH` 和
`COMSOL_MCP_WORKDIR`(MCP 客户端用干净环境启动子进程,不会继承你的 shell 变量)。
路径替换说明见 [`config/README.md`](config/README.md)。

### 6.3 手工验证 stdio 链路

```powershell
$env:PYTHONPATH = "\\path\to\comsol-mcp\src"
& "python" -m comsol_mcp --transport stdio
# 或 HTTP 传输(默认 127.0.0.1:8765)
& "python" -m comsol_mcp --transport http --http-port 8765
```

## 7. 给 AI Agent 的示例提示词 / Example prompts

配置好客户端后,直接把这些话发给 AI(agent 会自动选择工具):

**中文**

> 帮我用 COMSOL 建一个 3D 稳态传热模型:0.1×0.05×0.02 m 的钢块,底面 400 K,
> 其余表面对流散热(h=10 W/(m²·K),环境 293.15 K),求解并告诉我最高温度,
> 再画一张温度云图保存为 PNG。

> 做一个 2D 微通道层流:通道 2 mm × 20 mm,水,入口速度 0.1 m/s,出口压力 0,
> 求解后给我速度场云图和沿中心线的压力曲线(PNG + CSV)。

> 加载 COMSOL 官方应用库里的热传导示例模型,求解,导出温度云图和一份 DOCX 报告。

> 把我模型的 q0 改成 1e3、5e3、1e4 W/m² 三个工况做参数扫描,把每个工况的最高温度
> 导出成 CSV,并做一个温度动画 GIF。

> 用 COMSOL 算一个悬臂梁:一端固定,自由端 100 N 向下,求最大位移和最大应力,
> 输出位移云图和应力云图。

**English**

> Build a 3D steady-state heat transfer model in COMSOL: 0.1×0.05×0.02 m steel
> block, bottom face at 400 K, other faces convective (h=10 W/m²K, ambient
> 293.15 K). Solve it, report the maximum temperature, and export a PNG
> temperature plot.

> Load the heat conduction example from the installed COMSOL application
> library, solve it, and generate a DOCX report with the temperature plot.

> Run a parametric sweep over inlet velocity 0.01/0.05/0.1 m/s, export the
> pressure drop for each case to CSV, and render a GIF of the velocity field.

## 8. 典型工作流 / Typical workflow

```text
session_start
  → model_create(dimension=3)
  → parameter_set({...})                      # 先集中定义参数
  → geom_primitive / geom_boolean / geom_import / geom_build
  → mat_add / mat_create + mat_assign         # 材料
  → phys_add + phys_add_bc / phys_add_feature # 物理场与边界条件
  → multiphysics_add                          # 多物理场耦合(可选)
  → mesh_create_sequence + mesh_auto_size + mesh_run
  → study_stationary / study_transient / study_parametric / …
  → result_evaluate / result_minmax / result_export_data
  → plot_quick / plot_export_image
  → anim_quick / anim_export                  # 动画(可选)
  → report_create + report_generate           # 报告(可选)
  → model_save
```

不确定流程时先调 `docs_workflow(goal="...")`;不确定类型名时调 `catalog_*`;
不确定属性名时调 `tree_properties` / `phys_properties` / `mat_properties`。

## 9. DeepSeek Harness (DSH) 集成

DSH 通过随安装分发的 `@deepseek-ai/dsh-mcp-client` 插件接入 MCP 服务器。本机
`desktop` profile 的 `cordis.patch.yml` 已追加一条 `insert` 补丁:

```yaml
- insert:
    - id: mcp-comsol
      name: '@deepseek-ai/dsh-mcp-client'
      config:
        serverName: comsol
        transport: stdio
        command: 'python'
        args: ['-m', 'comsol_mcp', '--transport', 'stdio']
        # env: PYTHONPATH / COMSOL_MCP_WORKDIR / COMSOL_MCP_COMSOL_ROOT ...
```

* 修改前已备份 profile 文件;YAML 已校验;原有条目未改动。
* 模型侧看到的工具名为 `mcp__comsol__<tool>`;`comsol://` 资源可用
  `list_mcp_resources` / `read_mcp_resource` 读取。
* 通常需重启 DSH 生效;本次 Desktop 运行中保存补丁后已热加载成功(详见
  [`docs/DSH_INTEGRATION.md`](docs/DSH_INTEGRATION.md) 的验证证据与回滚方法)。
* ⚠️ **重启提示**:DSH 里正在运行的 MCP 子进程是**启动那一刻的模块快照**。
  仓库后续新增/修改的工具(例如新增的 `catalog_plot_features`,当前共 274 个)
  以及服务器 instructions 的更新,需要**重启 DSH**(或让 MCP 客户端重连)后
  才会出现在新的工具列表中;当前运行中的旧快照仍按 273 个工具工作。

## 10. 环境变量 / Environment variables

| 变量 | 默认 | 说明 |
|---|---|---|
| `COMSOL_MCP_COMSOL_ROOT` | 自动探测 | COMSOL 安装根目录,如 `C:\Program Files\COMSOL\COMSOL64` |
| `COMSOL_MCP_VERSION` | 最新安装 | 指定 COMSOL 版本(如 `6.4`) |
| `COMSOL_MCP_CORES` | 0(全部) | 求解核数 |
| `COMSOL_MCP_SESSION_MODE` | `client-server` | 或 `stand-alone` |
| `COMSOL_MCP_SERVER_PORT` | 0(随机) | COMSOL server 端口 |
| `COMSOL_MCP_CONNECT_HOST` | `localhost` | 连接主机 |
| `COMSOL_MCP_AUTOSTART` | `1` | 首次调用 COMSOL 工具时自动启动 |
| `COMSOL_MCP_KEEP_ALIVE` | `1` | MCP 退出后保留 COMSOL |
| `COMSOL_MCP_START_TIMEOUT` | 180 | 等待启动秒数 |
| `COMSOL_MCP_SOLVE_TIMEOUT` | 0(无限) | 同步求解超时 |
| `COMSOL_MCP_WORKDIR` | `<cwd>\comsol_workspace` | 工作区根目录 |
| `COMSOL_MCP_ARTIFACT_ROOT` / `COMSOL_MCP_MODEL_ROOT` | 工作区子目录 | 产物 / 模型目录 |
| `COMSOL_MCP_LOG_LEVEL` / `COMSOL_MCP_LOG_FILE` | `INFO` / `<workdir>\logs\...` | 日志 |
| `COMSOL_MCP_MAX_OUTPUT_ITEMS` | 400 | 返回列表上限 |
| `COMSOL_MCP_TOOL_PROFILE` | `full` | `full`(全部 274 个)、`minimal`(166 个核心工具,节省客户端上下文)或 `custom` |
| `COMSOL_MCP_TOOL_ALLOW` / `COMSOL_MCP_TOOL_DENY` | 空 | 逗号分隔的工具名/前缀白/黑名单(如 `COMSOL_MCP_TOOL_DENY=java_eval` 禁用逃生舱口) |
| `COMSOL_MCP_ALLOW_EVAL` | `1` | 设为 `0`/`false` 禁用 `java_eval`(推荐在共享环境关闭) |
| `COMSOL_MCP_DOC_ROOT` | 自动探测 | 离线 COMSOL HTML 文档根目录(`docs_search(scope="help")` 用) |
| `COMSOL_MCP_FFMPEG` | 自动探测 | ffmpeg 路径(MP4) |
| `COMSOL_MCP_CONFIG` | 空 | JSON 设置文件路径 |

完整列表与 JSON 配置示例见 [`docs/USER_GUIDE.md`](docs/USER_GUIDE.md#9-环境变量--environment-variables)。

## 11. 工作区与产物 / Workspace

```text
\\path\to\comsol-mcp\comsol_workspace\
├── artifacts\<model>\<timestamp>_<operation>\   # PNG / GIF / MP4 / CSV / HTML / DOCX
├── models\                                      # 保存的 .mph
├── .scratch\                                    # 快照与临时文件
└── logs\comsol_mcp.log                          # 服务器日志
```

导出工具都返回绝对路径和可供自检的元数据(`size`、`width/height`、`frames`、
`valid` 等),AI agent 可以据此确认产物真实生成。

## 12. 故障排查 / Troubleshooting

| 现象 | 处理 |
|---|---|
| **找不到 COMSOL**(`COMSOL installation not found`) | 设置 `COMSOL_MCP_COMSOL_ROOT` 指向实际安装(如 `C:\Program Files\COMSOL\COMSOL64`);运行 `health_check` 诊断。 |
| **许可证错误 / 模块缺失** | 先用 COMSOL Desktop 确认许可证服务可用;`session_status` 查看 `modules`;某些模型需要特定模块授权。 |
| **JPype/JVM 错误** | 确认 `jpype1` 与 Python 位数/版本匹配;同一进程不要加载多个 JVM;重启 MCP 服务器进程。 |
| **MP4 动画失败** | 本机已安装 `imageio` + `imageio-ffmpeg`(自带 ffmpeg 7.1 二进制,MP4 直接可用,无需系统 ffmpeg);如缺失则 `pip install imageio imageio-ffmpeg`。GIF 只需要 Pillow。`media_tools_status` 可查看媒体工具链状态;无 ffmpeg 时动画会自动回退 GIF。 |
| **中文标签 / 中文字段名** | 正常现象:COMSOL 按系统语言本地化**显示名**,tag/类型仍是英文,工具按 tag 工作。 |
| **中文图表标题乱码** | COMSOL 渲染字体限制;图表标题建议英文,或用 `anim_add_title`/Pillow 叠字。 |
| **工具调用超时** | 长求解用 `study_solve_async` + `job_status`/`job_wait`;MCP 客户端工具超时建议 ≥600 s。 |
| **"已有客户端连接" / 端口占用** | COMSOL server 只允许一个客户端;不要并行运行两个驱动 COMSOL 的进程;用 `session_connect(port=...)` 复用已有 server。 |
| **客户端看不到工具** | 先 `python -m comsol_mcp --check` 验证;检查客户端配置的绝对路径/PYTHONPATH;重启客户端。 |

更多见 [`docs/USER_GUIDE.md`](docs/USER_GUIDE.md#10-故障排查--troubleshooting)。

## 13. 安全说明 / Security

* **`java_eval` 是任意代码执行通道**:它在 COMSOL 工作线程内执行 Python 代码,
  可访问 `model/jm/client` 及底层 Java API。只应在**可信 agent / 可信提示词**下使用;
  可用 `COMSOL_MCP_ALLOW_EVAL=0` 直接禁用,或用
  `COMSOL_MCP_TOOL_DENY=java_eval` 在工具层屏蔽;多用户环境还可
  `COMSOL_MCP_TOOL_PROFILE=minimal` 只暴露常用 166 个工具。
* **文件系统访问**:导出/报告工具会写工作区,`geom_import`/`model_load`/`report_add_content`
  可读取本地文件路径;请不要把 agent 指向含敏感数据的目录。
* **不要暴露 HTTP 传输到公网**:默认建议 stdio;若用 `--transport http`,只绑定
  `127.0.0.1` 并加网络层访问控制。
* **无需任何凭证**:服务器不读取账号/许可证密钥,MCP 配置里不要写密钥。
* **单客户端约束**:COMSOL server 只接受一个客户端连接,避免多个自动化进程同时驱动
  同一 COMSOL server。

## 14. 测试与验证 / Tested against real COMSOL

* **COMSOL 6.4(build 293,全模块授权)** 上完成端到端验证:真实几何/材料/物理场/
  网格/求解/结果/绘图/动画/导出/报告流程,包含官方应用库模型。
* **240+ live assertions**:11 个 live 测试脚本针对真实 COMSOL 运行(当前源码中
  约 500 个 `check(...)` 断言点,覆盖 session/model、几何、材料与网格、物理场、
  研究/求解、结果、绘图与可视化、动画、模型库、MCP 协议、会话管理)。
* **官方 2,081 个模型的应用库**:完整离线索引(含物理场、研究类型、预计耗时),
  可 `library_search` → `library_load`/`library_run`。
* **材料库**:19 个库、3,141 种材料离线索引,支持按物性搜索。
* **打包验证**(无需 COMSOL):`tests/smoke_packaging.py` 检查包导入、
  `build_server()` 工具注册、`python -m comsol_mcp --check`、工具参考生成、
  4 种客户端配置合法性、DSH profile 补丁;`--full` 额外在临时 venv 中安装并运行
  `comsol-mcp` 控制台脚本。
* `python -m pytest tests/unit -q`:核心逻辑单元测试(约 200 个断言)。
* **验收矩阵与独立 QA 报告**:L0–L4 验收矩阵见 [`docs/TEST_PLAN.md`](docs/TEST_PLAN.md);
  独立端到端 QA 的最终报告(MCP 全链路场景、真实产物、性能数据)见
  [`docs/TEST_REPORT.md`](docs/TEST_REPORT.md)(由 `qa-e2e` 收尾生成,
  过程记录在 `docs/progress/qa.md`)。
* **独立性 / 竞品对比**:与现有开源 COMSOL MCP 项目的能力对比、以及本项目为何是
  独立实现且覆盖面更广,见 [`docs/COMPETITIVE_ANALYSIS.md`](docs/COMPETITIVE_ANALYSIS.md)。

复现:

```powershell
$env:PYTHONPATH = "\\path\to\comsol-mcp\src"
& "python" tests\smoke_packaging.py
& "python" -m pytest tests\unit -q
# live 测试需要可用的 COMSOL(见 docs/DEVELOPMENT.md)
```

## 15. 文档 / Documentation

| 文档 | 内容 |
|---|---|
| [`docs/TOOL_REFERENCE.md`](docs/TOOL_REFERENCE.md) | **自动生成**的 274 个工具完整参考(描述 + 参数 + 必填项 + JSON Schema 文件) |
| [`docs/USER_GUIDE.md`](docs/USER_GUIDE.md) | 逐步使用指南:首个模型、求解、结果、动画、报告、模型库、最佳实践 |
| [`docs/DSH_INTEGRATION.md`](docs/DSH_INTEGRATION.md) | DeepSeek Harness 精确配置、验证证据、重启/回滚、排障 |
| [`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md) | 架构、线程模型、持久化、扩展方式 |
| [`CONTRIBUTING.md`](CONTRIBUTING.md) | 开发者指南:新增工具模块、live 测试、clientapi 陷阱 |
| [`docs/TEST_PLAN.md`](docs/TEST_PLAN.md) | 验收矩阵(L0–L4) |
| [`docs/TEST_REPORT.md`](docs/TEST_REPORT.md) | 独立 QA 端到端测试报告(最终验收证据) |
| [`docs/COMPETITIVE_ANALYSIS.md`](docs/COMPETITIVE_ANALYSIS.md) | 现有 COMSOL MCP 项目对比与独立性说明 |
| [`config/README.md`](config/README.md) | Claude / Codex / opencode / 通用 MCP 客户端配置说明 |

重新生成工具参考(工具数量变化后运行):

```powershell
python scripts\gen_tool_reference.py
# -> docs/TOOL_REFERENCE.md + docs/tool_reference.json
```

## 16. 许可证 / License

MIT License(Copyright © 2026 The comsol-mcp contributors),详见根目录
[`LICENSE`](LICENSE)。SPDX-License-Identifier: `MIT`。

本项目是独立的开源集成层,与 COMSOL AB 无隶属关系。COMSOL Multiphysics® 是
COMSOL AB 的商标;使用本项目仍需有效的 COMSOL 许可证。
This is an independent open-source integration and is not affiliated with
COMSOL AB. COMSOL Multiphysics® is a trademark of COMSOL AB; a valid COMSOL
license is still required.

---

## English summary

**comsol-mcp** is the most comprehensive MCP server for COMSOL Multiphysics: it
exposes the whole modelling workflow as **274 MCP tools** over stdio or
streamable HTTP, so an AI agent can build geometry, assign materials, add
physics and multiphysics couplings, mesh, solve stationary/transient/
eigenfrequency/frequency/parametric studies, evaluate results, render plots
and animations (GIF/MP4), export data/models/geometry/meshes, generate
HTML/DOCX reports, and browse/search/load/solve the official application
library — all driving a locally installed COMSOL 6.4 through its Java API
(MPh + JPype).

**Highlights**

- Complete coverage: 30 geometry, 14 materials, 19 physics, 7 multiphysics,
  22 mesh, 34 study/solver, 27 result, 13 plot, 16 animation/media, 9 export,
  5 report, 7 model-library, 16 model-tree, 1 `java_eval` escape hatch, plus
  session/model/parameter/selection/job/catalog/knowledge tools.
- Discoverability from the real installation: 949 physics interfaces, 7,343
  physics feature types, 97 3D geometry features, 51 3D mesh features, 81
  study types, 121 plot feature types, 86 solver feature types, 64 dataset
  types, 33 numerical evaluation types — shipped offline.
- Context budget: `COMSOL_MCP_TOOL_PROFILE=minimal` exposes a 166-tool
  everyday subset; the default `full` profile exposes all 274 tools.
- Offline indexes: 2,081 official application-library models and 19 material
  libraries with 3,141 materials; 13 curated modelling guides; searchable
  installed COMSOL HTML documentation.
- Reliability: all COMSOL calls serialized on one worker thread; COMSOL runs
  in a separate `comsolmphserver` process; cancellable async solve jobs;
  structured JSON errors with actionable hints; every artifact export returns
  an absolute path and validated metadata.
- Integrations: DeepSeek Harness (profile patch, described in
  `docs/DSH_INTEGRATION.md`), Claude Desktop, Codex CLI, opencode and generic
  `mcpServers` clients (`config/`); `comsol://` read-only MCP resources.
- Verified against **COMSOL 6.4 build 293** with all modules, **240+ live
  assertions** (≈500 assertion sites) across 11 live test scripts, plus
  packaging smoke tests and unit tests.

**Requirements**: COMSOL 6.x (tested 6.4), Python 3.10+ (tested 3.13), `mcp>=2`,
`mph>=1.3.1`, `jpype1>=1.5`, `numpy`, `Pillow`; `imageio` + `imageio-ffmpeg`
for MP4 animation (installed here with a bundled ffmpeg; GIF needs Pillow only).

**Install**

```powershell
powershell -ExecutionPolicy Bypass -File .\install.ps1
# or manually:
$env:PYTHONPATH = "\\path\to\comsol-mcp\src"
& "python" -m comsol_mcp --check
& "python" -m pip install -e "\\path\to\comsol-mcp"
```

**Client configuration**: see `config/` (Claude Desktop, Codex, opencode,
generic) and `docs/DSH_INTEGRATION.md` for DeepSeek Harness. Use absolute
paths and pass `PYTHONPATH` / `COMSOL_MCP_WORKDIR` explicitly in the client's
`env` block, because MCP clients start the server in a cleaned environment.

**Documentation**: `docs/TOOL_REFERENCE.md` (all 274 tools with schemas),
`docs/USER_GUIDE.md` (worked examples), `docs/ARCHITECTURE.md`,
`docs/DEVELOPMENT.md`, `docs/DSH_INTEGRATION.md`,
`docs/TEST_PLAN.md` + `docs/TEST_REPORT.md` (acceptance and QA evidence) and
`docs/COMPETITIVE_ANALYSIS.md` (independent-implementation comparison).

机器无关的约定:`COMSOL_MCP_TOOL_PROFILE=minimal` 可把工具集裁剪到 166 个常用工具
(客户端上下文受限时推荐),`COMSOL_MCP_ALLOW_EVAL=0` 可禁用 `java_eval`。

**Security**: `java_eval` executes arbitrary Python against the live COMSOL
Java API — use it only with trusted agents/prompts, and set
`COMSOL_MCP_ALLOW_EVAL=0` (or `COMSOL_MCP_TOOL_DENY=java_eval`) where it is not
needed. `COMSOL_MCP_TOOL_PROFILE=minimal` exposes a 166-tool everyday subset.
Do not expose the HTTP transport publicly. A COMSOL server accepts only one
client at a time.

**License**: MIT. COMSOL Multiphysics® is a trademark of COMSOL AB; a valid
COMSOL license is required to run simulations.

TDQS

B3.2/5.0

Scored across 274 tools

Disambiguation3/5

The tools are grouped by domain, but there are many overlapping or alias-like entries such as mesh_run/mesh_build, export_mesh/mesh_export, geom_export/export_geometry, and generic evaluators alongside specialized result_eval_* tools. Descriptions often clarify the intended use, but with 274 tools an agent faces a high risk of selecting a near-duplicate or lower-level escape hatch instead of the intended tool.

Naming Consistency5/5

Nearly all tools use a consistent snake_case prefix_action or prefix_noun convention grouped by domain: geom_*, mesh_*, phys_*, mat_*, study_*, solver_*, plot_*, result_*, anim_*, and so on. A few generic names like java_eval, health_check, and server_info deviate slightly, but the overall naming pattern is highly predictable.

Tool Count1/5

A 274-tool surface is far beyond what is appropriate for reliable tool selection, even for a broad domain like COMSOL Multiphysics. The set contains many aliases, specialized shortcuts, low-level escape hatches, and duplicated export paths, making the surface feel sprawling rather than well-scoped.

Completeness5/5

The toolset covers the full simulation lifecycle: model/session management, parameters, geometry, selections, materials, physics, multiphysics couplings, meshing, studies, solver configuration, results evaluation, plots, animations, exports, reports, libraries, and documentation. The few limitations noted, such as unsupported study duplication or solver-feature deletion, are COMSOL API constraints rather than missing surface coverage.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues