ensure_component
Create or verify a component with specified name and dimension in the active COMSOL model, ensuring it is available for further modeling steps.
Instructions
Ensure a component exists in the selected server-side model.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| component | No | comp1 | |
| dimension | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- comsol_mcp/mcp_server.py:414-418 (handler)Core Java-based handler that ensures a component exists in the COMSOL model. If the component tag is not yet present, it creates it via the Java API.
def _ensure_component_java(model: Any, component: str, dimension: int) -> dict[str, Any]: java = model.java if component not in list(java.component().tags()): java.component().create(component, True) return {"component": component, "dimension": dimension} - comsol_mcp/mcp_server.py:734-741 (registration)MCP tool registration for 'ensure_component'. Decorated with @mcp.tool(), defines the public interface with parameters (component, dimension), and dispatches via _run_tool to the inner _impl handler.
@mcp.tool() def ensure_component(component: str = "comp1", dimension: int = 2) -> str: """Ensure a component exists in the selected server-side model.""" def _impl() -> dict[str, Any]: return _ensure_component_java(_require_model(), component.strip() or "comp1", int(dimension)) return _run_tool("ensure_component", _impl) - comsol_mcp/mcp_server.py:735-735 (schema)Tool signature acts as the schema: component (str, default 'comp1') and dimension (int, default 2).
def ensure_component(component: str = "comp1", dimension: int = 2) -> str: - comsol_mcp/mcp_server.py:494-502 (helper)_run_tool is the generic execution wrapper used by ensure_component (and all other tools) to handle logging, locking, and result/error formatting.
def _run_tool(tool: str, callback) -> str: _setup_logging() with _runtime_lock: try: data = callback() return _tool_result(tool, True, data=data) except Exception as exc: logging.exception("Tool %s failed", tool) return _tool_result(tool, False, error=str(exc))