Skip to content

solidworks_mcp.tools

solidworks_mcp.tools

Tools for SolidWorks MCP Server.

This module provides all the MCP tools for SolidWorks automation, organized by category.

Attributes

__all__ module-attribute

__all__ = ['register_tools']

Functions:

register_analysis_tools async

register_analysis_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]) -> int

Register analysis tools with FastMCP.

Registers comprehensive analysis tools for SolidWorks model evaluation including mass properties, interference checking, geometry analysis, and material properties. These tools provide critical engineering data for design validation and optimization.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config dict[str, Any]

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Example
from solidworks_mcp.tools.analysis import register_analysis_tools

tool_count = await register_analysis_tools(mcp, adapter, config)
print(f"Registered {tool_count} analysis tools")
            Note:
                Analysis tools require an active SolidWorks document with geometry.
                Some analyses may require specific material assignments for accurate results.
Source code in src/solidworks_mcp/tools/analysis.py
async def register_analysis_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]
) -> int:
    """Register analysis tools with FastMCP.

    Registers comprehensive analysis tools for SolidWorks model evaluation including mass
    properties, interference checking, geometry analysis, and material properties. These
    tools provide critical engineering data for design validation and optimization.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (dict[str, Any]): Configuration values for the operation.

    Returns:
        int: The computed numeric result.

    Example:
                        ```python
                        from solidworks_mcp.tools.analysis import register_analysis_tools

                        tool_count = await register_analysis_tools(mcp, adapter, config)
                        print(f"Registered {tool_count} analysis tools")
                        ```

                    Note:
                        Analysis tools require an active SolidWorks document with geometry.
                        Some analyses may require specific material assignments for accurate results.
    """
    mcp = with_tool_boundary(mcp)
    tool_count = 0

    async def _measure_mass() -> dict[str, Any]:
        """The active model's mass properties, as both mass tools return them.

        A plain function, not a tool, so ``get_mass_properties`` doesn't carry
        the deprecated ``calculate_mass_properties``'s warning.

        Returns:
            dict[str, Any]: The success reply.
        """
        result = await adapter.get_mass_properties()

        require_success(result, context="Failed to calculate mass properties")
        props = result.data
        assert props is not None
        return {
            "status": "success",
            "message": "Mass properties calculated successfully",
            "mass_properties": {
                "volume": {"value": props.volume, "units": "mm³"},
                "surface_area": {"value": props.surface_area, "units": "mm²"},
                "mass": {"value": props.mass, "units": "kg"},
                "center_of_mass": {
                    "x": props.center_of_mass[0],
                    "y": props.center_of_mass[1],
                    "z": props.center_of_mass[2],
                    "units": "mm",
                },
                "moments_of_inertia": {
                    **{
                        key: _kg_mm2(props.moments_of_inertia[key])
                        for key in ("Ixx", "Iyy", "Izz", "Ixy", "Ixz", "Iyz")
                    },
                    "units": "kg·mm²",
                },
                "principal_axes": props.principal_axes,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def get_mass_properties(
        input_data: MassPropertiesInput | None = None,
    ) -> dict[str, Any]:
        """Measure the active part or assembly's mass properties: volume, surface area, mass, centre of mass and moments of inertia.

        Use it to check weight or balance. To compare a model with the values you
        expect, use `verify_model`. Mass depends on the assigned material
        (`set_material`). Needs: an open part or assembly. Read-only. It rebuilds first.
        Returns: `mass_properties` with `volume` (mm³), `surface_area` (mm²), `mass`
        (kg), `center_of_mass` (mm), `moments_of_inertia` (kg·mm², about the centre
        of mass) and `principal_axes`.

        Args:
            input_data: Optional, and empty: it always measures the active model.

        Returns:
            dict[str, Any]: The mass properties.
        """
        return await _measure_mass()

    @mcp.tool()
    async def calculate_mass_properties(
        input_data: MassPropertiesInput,
    ) -> dict[str, Any]:
        """Measure the active part or assembly's mass properties (the old name of `get_mass_properties`).

        It returns exactly what `get_mass_properties` returns; use that instead. Needs:
        an open part or assembly. Read-only. It rebuilds first. Returns:
        `mass_properties` with `volume` (mm³), `surface_area` (mm²), `mass` (kg),
        `center_of_mass` (mm), `moments_of_inertia` (kg·mm²) and `principal_axes`.

        Args:
            input_data: Nothing: it always measures the active model.

        Returns:
            dict[str, Any]: The mass properties.
        """
        return await _measure_mass()

    @mcp.tool()
    async def check_interference(input_data: InterferenceCheckInput) -> dict[str, Any]:
        """Find where the components of the active assembly overlap (Tools > Interference Detection).

        Use it after inserting and mating components. A count of 0 is a real
        measurement. To fail a build on interference, use `verify_model` with
        `no_interference`. Needs: an open, active assembly. Read-only. Returns: `data`
        with `interference_found`, `interference_count`, `interferences` (component
        pairs and overlap volume in mm³) and `scope`.

        Args:
            input_data: Nothing is required. `components` limits the report to
                interferences involving those components; `include_hidden` also
                checks hidden components, which are left out by default.

        Returns:
            dict[str, Any]: The interferences found.
        """
        require_capability(
            adapter,
            "check_interference",
            "Active adapter does not support check_interference; no "
            "interference analysis was performed. Use SolidWorks "
            "directly (Tools > Interference Detection) to check this "
            "assembly.",
        )
        options = input_data.model_dump()
        options["ignore_hidden"] = not input_data.include_hidden
        result = await adapter.check_interference(options)
        require_success(result, "Interference check failed")
        return {
            "status": "success",
            "message": "Interference check completed",
            "data": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def verify_model(
        input_data: VerifyModelInput | dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """Check the active part or assembly against what you meant to build, one pass/fail check per expected value.

        Use it as the last step of a build: a modelling tool's success doesn't prove the
        geometry is right. Give only the values you know. Units: mm, mm³, kg;
        `bounding_box_mm` is [x, y, z] in model axes. Widen `relative_tolerance` when
        your sums ignore fillets. Needs: an open part or assembly. Read-only. Returns:
        `verdict` (pass, fail, inconclusive or report_only), `checks` (each `expected`,
        `actual`, `tolerance`, `passed`; null when unmeasured) and `measured`. A
        mismatch is still `status: success`.

        Args:
            input_data: The expectations, all optional: `mass_kg`, `volume_mm3`,
                `bounding_box_mm`, `feature_count`, `solid_body_count`, and for an
                assembly `component_count`, `mate_count` and `no_interference`;
                `relative_tolerance` (0.01) and `length_tolerance_mm` (0.1) have
                defaults. With none it only reports what it measured.

        Returns:
            dict[str, Any]: The verdict and checks.
        """
        expectations = normalize_input(input_data, VerifyModelInput)
        require_capability(
            adapter,
            "get_model_geometry_summary",
            "Active adapter cannot read the model back, so nothing was verified.",
        )
        summary_result = await adapter.get_model_geometry_summary()
        require_success(summary_result, context="Failed to read the model back")
        summary = dict(summary_result.data or {})
        is_assembly = summary.get("document_type") == "assembly"

        if not is_assembly:
            misplaced = [
                name
                for name in _ASSEMBLY_ONLY
                if getattr(expectations, name) is not None
            ]
            if misplaced:
                raise InvalidInputError(
                    f"{', '.join(misplaced)} only apply to assemblies; the active "
                    "document is a part"
                )

        measured: dict[str, Any] = dict(summary)
        checks: list[dict[str, Any]] = []
        rel = expectations.relative_tolerance

        if expectations.mass_kg is not None:
            mass_result = await adapter.get_mass_properties()
            mass = (
                mass_result.data.mass
                if mass_result.is_success and mass_result.data
                else None
            )
            material_result = await adapter.get_material()
            material = (
                (material_result.data or {}).get("material")
                if material_result.is_success
                else None
            )
            measured["mass_kg"] = mass
            measured["material"] = material
            check = _relative_check("mass_kg", expectations.mass_kg, mass, rel, "kg")
            if check["passed"] is False:
                check["note"] = (
                    f"Material: {material}"
                    if material
                    else "No material assigned; SolidWorks used a default density"
                )
            checks.append(check)

        if expectations.volume_mm3 is not None:
            checks.append(
                _relative_check(
                    "volume_mm3",
                    expectations.volume_mm3,
                    summary.get("volume_mm3"),
                    rel,
                    "mm³",
                )
            )

        if expectations.bounding_box_mm is not None:
            checks.append(
                _box_check(
                    expectations.bounding_box_mm,
                    summary.get("bounding_box_mm"),
                    expectations.length_tolerance_mm,
                )
            )

        if expectations.feature_count is not None:
            features_result = await adapter.list_features()
            names = (
                _count_modelling_features(features_result.data or [])
                if features_result.is_success
                else None
            )
            measured["features"] = names
            checks.append(
                _exact_check(
                    "feature_count",
                    expectations.feature_count,
                    None if names is None else len(names),
                    "features",
                )
            )

        for name, unit in (
            ("solid_body_count", "bodies"),
            ("component_count", "components"),
            ("mate_count", "mates"),
        ):
            expected = getattr(expectations, name)
            if expected is not None:
                checks.append(_exact_check(name, expected, summary.get(name), unit))

        if expectations.no_interference is not None:
            interference = await adapter.check_interference({})
            found = (
                (interference.data or {}).get("interference_found")
                if interference.is_success
                else None
            )
            measured["interference"] = (
                interference.data if interference.is_success else None
            )
            clear = None if found is None else not found
            checks.append(
                _check(
                    "no_interference",
                    expectations.no_interference,
                    clear,
                    None,
                    None if clear is None else clear == expectations.no_interference,
                    note=None if interference.is_success else interference.error,
                )
            )

        verdict = _verdict(checks)
        return {
            "status": "success",
            "message": _verdict_message(
                verdict, checks, str(summary.get("document_type") or "document")
            ),
            "verdict": verdict,
            "checks": checks,
            "measured": measured,
            "execution_time": summary_result.execution_time,
        }

    # Future analysis tools:
    # - perform_fea_analysis (if FEA capabilities are available)
    # - analyze_flow (computational fluid dynamics)
    # - thermal_analysis
    # - vibration_analysis
    # - stress_concentration_analysis

    tool_count = 4
    return tool_count

register_assembly_tools async

register_assembly_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: Any) -> int

Register the assembly tools with FastMCP.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config Any

Configuration values for the operation.

required

Returns:

Name Type Description
int int

How many tools were registered.

Source code in src/solidworks_mcp/tools/assembly.py
async def register_assembly_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: Any
) -> int:
    """Register the assembly tools with FastMCP.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (Any): Configuration values for the operation.

    Returns:
        int: How many tools were registered.
    """
    mcp = with_tool_boundary(mcp)

    @mcp.tool()
    async def create_exploded_view(
        input_data: CreateExplodedViewInput,
    ) -> dict[str, Any]:
        """Explode the active assembly, automatically or step by step, and show it.

        Use it to show how an assembly goes together, and before an exploded
        drawing view (`create_drawing_view` with `exploded`, drawing toolset). With no `steps` SolidWorks
        explodes it automatically; each step moves named components a distance
        along x, y or z. Needs: an open assembly. Changes: adds an exploded view
        to the active configuration and shows it. Returns: `exploded_view` with
        its `view` name, `steps`, and the bounding box `size_before_mm` and
        `size_after_mm`. A component it doesn't hold fails as `not_found`.

        Args:
            input_data: `steps` (each `components`, `direction`, `distance`,
                `reverse`); empty explodes it automatically.

        Returns:
            dict[str, Any]: The exploded view.
        """
        input_data = normalize_input(input_data, CreateExplodedViewInput)
        require_capability(
            adapter, "create_exploded_view", "Active adapter can't explode assemblies"
        )
        steps = [step.model_dump() for step in input_data.steps]
        result = await adapter.create_exploded_view(steps or None)
        require_success(result, context="Failed to explode the assembly")
        return {
            "status": "success",
            "message": f"Exploded the assembly in {len(steps) or 'automatic'} step(s)",
            "exploded_view": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def set_exploded_view(input_data: SetExplodedViewInput) -> dict[str, Any]:
        """Show the active assembly exploded, or collapse it.

        Use it to switch between the exploded view `create_exploded_view` made
        and the assembled one. Needs: an open assembly with an exploded view.
        Changes: what the view shows; nothing in the model. Returns:
        `exploded_view` with the `view`, `exploded` as read back and the bounding
        box `size_mm`. No exploded view fails as `not_found`.

        Args:
            input_data: `exploded` (true or false).

        Returns:
            dict[str, Any]: The view's state.
        """
        input_data = normalize_input(input_data, SetExplodedViewInput)
        require_capability(
            adapter, "set_exploded_view", "Active adapter can't explode assemblies"
        )
        result = await adapter.set_exploded_view(input_data.exploded)
        require_success(result, context="Failed to change the exploded view")
        state = "exploded" if input_data.exploded else "collapsed"
        return {
            "status": "success",
            "message": f"The assembly shows {state}",
            "exploded_view": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def pattern_components(input_data: PatternComponentsInput) -> dict[str, Any]:
        """Repeat assembly components in a row along an axis, or around one.

        Use it for a row of bolts or pins, or parts around a hub. `axis` names a
        reference axis (`create_axis` in the assembly); `count` includes the
        seeds. Linear takes `spacing` (mm); circular takes `angle` (degrees, 360
        spaces them equally). The row runs along the axis's own direction, which
        SolidWorks picks (an x axis from `create_axis` runs toward -x), so check
        the result with `verify_model`'s bounding box. Needs: an open assembly
        with the seeds and the axis.
        Changes: adds a component pattern feature. Returns: `pattern` with the
        `feature` name, the component counts before and after and the `added`
        instances.

        Args:
            input_data: `components`, `axis` and `count` are required; `kind`
                (`linear`) with `spacing`, or `circular` with `angle`.

        Returns:
            dict[str, Any]: The pattern.
        """
        input_data = normalize_input(input_data, PatternComponentsInput)
        require_capability(
            adapter, "pattern_components", "Active adapter can't pattern components"
        )
        result = await adapter.pattern_components(
            input_data.components,
            input_data.axis,
            input_data.count,
            input_data.kind,
            input_data.spacing,
            input_data.angle,
        )
        require_success(result, context="Failed to pattern the components")
        added = (result.data or {}).get("added", [])
        return {
            "status": "success",
            "message": f"Added {len(added)} component(s) in a {input_data.kind} pattern",
            "pattern": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def mirror_components(input_data: MirrorComponentsInput) -> dict[str, Any]:
        """Mirror assembly components about a plane, as instances of the same parts.

        Use it for a part that appears on both sides, such as a pair of feet or
        brackets. Needs: an open assembly with the components and the plane.
        Changes: adds mirrored instances (no new part files). Returns: `mirror`
        with the `plane` and the `added` instances' names. A plane it doesn't
        have fails as `not_found`.

        Args:
            input_data: `components` and `plane`, both required.

        Returns:
            dict[str, Any]: The mirrored instances.
        """
        input_data = normalize_input(input_data, MirrorComponentsInput)
        require_capability(
            adapter, "mirror_components", "Active adapter can't mirror components"
        )
        result = await adapter.mirror_components(
            input_data.components, input_data.plane
        )
        require_success(result, context="Failed to mirror the components")
        return {
            "status": "success",
            "message": f"Mirrored {len(input_data.components)} component(s) about "
            f"{input_data.plane}",
            "mirror": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def replace_component(input_data: ReplaceComponentInput) -> dict[str, Any]:
        """Swap an assembly component for another part or assembly file.

        Use it to try another version of a part (a longer pin, a different motor)
        without re-inserting and re-mating it. `all_instances` replaces every
        instance of the same file, patterned copies included. Mates are kept where
        they still fit. Needs: an open assembly and the new file. Changes: the
        component's file. Returns: `replace` with the `replaced` instances and
        their files read back, and the components before and after. A file that
        doesn't exist fails as `invalid_input`.

        Args:
            input_data: `component` and `file_path` are required; `all_instances`
                is off.

        Returns:
            dict[str, Any]: What was replaced.
        """
        input_data = normalize_input(input_data, ReplaceComponentInput)
        require_capability(
            adapter, "replace_component", "Active adapter can't replace components"
        )
        result = await adapter.replace_component(
            input_data.component, input_data.file_path, input_data.all_instances
        )
        require_success(result, context="Failed to replace the component")
        replaced = (result.data or {}).get("replaced", {})
        return {
            "status": "success",
            "message": f"Replaced {len(replaced)} instance(s) with "
            f"{input_data.file_path}",
            "replace": result.data,
            "execution_time": result.execution_time,
        }

    return 5

register_automation_tools async

register_automation_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]) -> int

Register automation tools with FastMCP.

Registers comprehensive automation tools for SolidWorks workflow orchestration including VBA generation, macro recording, batch processing, and template management.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config dict[str, Any]

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Source code in src/solidworks_mcp/tools/automation.py
async def register_automation_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]
) -> int:
    """Register automation tools with FastMCP.

    Registers comprehensive automation tools for SolidWorks workflow orchestration including
    VBA generation, macro recording, batch processing, and template management.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (dict[str, Any]): Configuration values for the operation.

    Returns:
        int: The computed numeric result.
    """
    mcp = with_tool_boundary(mcp)
    tool_count = 0

    @mcp.tool()
    async def generate_vba_code(input_data: GenerateVBAInput) -> dict[str, Any]:
        """Write a VBA macro skeleton for an operation you describe: the SolidWorks connection, a check of the active document's type and optional error handling, with the operation itself left as a TODO.

        Use it as a starting point for a custom macro. For a complete macro for a
        specific feature, use a `generate_vba_*` tool (extrusion, revolve, mates,
        drawing views, batch export). The macro reports through `Debug.Print`, never a
        dialog, so it runs without waiting on anyone: save it as a .swb file and run it
        with `execute_macro` (its `Sub main()` calls the Sub named `sub_name`; SolidWorks
        runs a .swb macro only through `main`). It stops, printing why,
        when the active document isn't `target_document`. Read-only. Nothing runs in
        SolidWorks. Returns: `vba_code` with `code`, `sub_name`, `operation`,
        `target_document`, `lines_of_code` and `includes_error_handling`.

        Args:
            input_data: The macro to write. Nothing is required: give
                `operation_description` (it also names the Sub) and
                `target_document` (Part, Assembly or Drawing);
                `include_error_handling` is on by default.

        Returns:
            dict[str, Any]: The macro text.
        """
        description = input_data.operation_description or ""
        sub_name = _vba_sub_name(description)
        code = _vba_skeleton(
            sub_name,
            description,
            input_data.target_document,
            input_data.include_error_handling,
        )
        return {
            "status": "success",
            "message": f"Generated VBA code for: {description}",
            "vba_code": {
                "code": code,
                "sub_name": sub_name,
                "operation": description,
                "target_document": input_data.target_document,
                "lines_of_code": len(code.split("\n")),
                "includes_error_handling": input_data.include_error_handling,
            },
        }

    tool_count = 1  # generate_vba_code
    return tool_count

register_docs_discovery_tools async

register_docs_discovery_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]) -> int

Register the docs discovery and API lookup tools with FastMCP.

None of them use the adapter or SolidWorks itself: they read the installed type library.

Parameters:

Name Type Description Default
mcp FastMCP

The server to register on.

required
adapter SolidWorksAdapter

Unused; kept for the registrar's uniform signature.

required
config dict[str, Any]

Server configuration (solidworks_year, the index refresh settings).

required

Returns:

Name Type Description
int int

The number of tools registered.

Source code in src/solidworks_mcp/tools/docs_discovery.py
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
async def register_docs_discovery_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]
) -> int:
    """Register the docs discovery and API lookup tools with FastMCP.

    None of them use the adapter or SolidWorks itself: they read the
    installed type library.

    Args:
        mcp (FastMCP): The server to register on.
        adapter (SolidWorksAdapter): Unused; kept for the registrar's
            uniform signature.
        config (dict[str, Any]): Server configuration (``solidworks_year``,
            the index refresh settings).

    Returns:
        int: The number of tools registered.
    """
    mcp = with_tool_boundary(mcp)

    @mcp.tool()
    async def discover_solidworks_docs(
        input_data: DiscoverDocsInput | None = None,
    ) -> dict[str, Any]:
        """Rebuild the SolidWorks API index on purpose. The lookups don't need it.

        The lookup tools (lookup_api_method, lookup_api_interface,
        find_related_api_members, search_solidworks_api_help) build the index
        themselves on first use, so call this only to rebuild it, to read a
        specific SolidWorks year, or to save a copy elsewhere. It reads every
        interface in the installed SolidWorks type library (each member's
        parameters, return type and description), plus the VBA library
        references. SolidWorks must be installed, not running; it is never
        started or called. Changes: writes the index file, replacing the
        saved one.

        Returns ``summary`` (interface, method and property counts),
        ``type_library`` (version, year and .tlb path), ``year`` and
        ``output_file``, the saved index (megabytes, so it is not returned).
        Fails with a message naming the installed years when ``year`` isn't
        installed, or saying SolidWorks isn't installed here.

        Example input: ``{"year": 2026}``

        Args:
            input_data (DiscoverDocsInput | None): The rebuild. Nothing is
                required: `year` defaults to the installed release,
                `output_dir` to the index folder, and `include_vba` to on.

        Returns:
            dict[str, Any]: ``summary``, ``type_library``, ``year``,
            ``output_file`` and ``rag_indexed``, plus
            ``status``/``message``/``execution_time``.
        """
        start_time = time.time()
        normalized = normalize_input(input_data, DiscoverDocsInput)
        output_dir = Path(normalized.output_dir) if normalized.output_dir else None
        year = _resolve_solidworks_year(normalized.year, config)

        logger.info("Starting SolidWorks documentation discovery...")
        discovery, output_file = _build_index(
            year,
            strict=normalized.year is not None,
            output_dir=output_dir,
            include_vba=normalized.include_vba,
        )
        index = discovery.index
        summary = discovery.create_search_summary()

        return {
            "status": "success",
            "message": f"Discovered {summary['total_com_objects']} COM objects, "
            f"{summary['total_methods']} methods, "
            f"{summary['total_properties']} properties",
            "type_library": index["type_library"],
            "summary": summary,
            "year": index["type_library"]["year"],
            "output_file": str(output_file),
            "rag_indexed": _rebuild_rag_index(output_file),
            "execution_time": time.time() - start_time,
        }

    @mcp.tool()
    async def search_solidworks_api_help(
        input_data: SearchApiHelpInput | None = None,
    ) -> dict[str, Any]:
        """Search SolidWorks API interface and member names by keyword.

        Use this when you don't know the exact name: "chamfer" finds
        IFeatureManager.InsertFeatureChamfer. Then call lookup_api_method on
        a match for its parameters and return type. Matches come from the
        SolidWorks type library installed on this machine.

        Setup: none. SolidWorks must be installed, not running; it is never
        contacted. Read-only. The first call builds the index (about a
        second) and later calls reuse it. ``index_status`` is ``ready``
        (saved index), ``rebuilt`` (built for this call) or ``unavailable``
        (no index could be built; ``guidance`` says why).

        Returns ``matches`` (interface, member, member_type, score),
        ``guidance``, and ``fallback_help`` suggesting MCP tools for the
        task, with ``year`` and ``type_library_version`` of the library that
        answered.

        Example input: ``{"query": "chamfer", "max_results": 5}``

        Args:
            input_data (SearchApiHelpInput | None): The search. `query`
                (one or more words) is required; `max_results` and `year`
                have defaults.

        Returns:
            dict[str, Any]: ``matches``, ``guidance``, ``fallback_help``,
            ``year``, ``type_library_version``, ``index_status`` and
            ``source_index_file``, plus ``status``/``message``/``execution_time``.
        """
        start_time = time.time()
        normalized = normalize_input(input_data, SearchApiHelpInput)
        source = _index_for_lookup(normalized.year, normalized.index_file, config)

        matches = _search_index(source.index, normalized.query, normalized.max_results)
        guidance_lines = []
        if matches:
            guidance_lines.append(
                f"Found {len(matches)} API matches for '{normalized.query}'."
            )
            guidance_lines.append(
                "Use lookup_api_method on a match for its parameters and return type."
            )
        else:
            guidance_lines.append(
                "No indexed API hits found; using workflow fallback guidance."
            )
        if source.note:
            guidance_lines.append(source.note)

        return {
            "status": "success",
            "message": "SolidWorks API help search completed",
            "query": normalized.query,
            **source.payload(),
            "matches": [match.model_dump(exclude_unset=True) for match in matches],
            "fallback_help": _fallback_help_for_query(normalized.query),
            "guidance": " ".join(guidance_lines),
            "execution_time": time.time() - start_time,
        }

    @mcp.tool()
    async def lookup_api_method(
        input_data: LookupApiMethodInput | None = None,
    ) -> dict[str, Any]:
        """Look up one SolidWorks API method or property: parameters, return type, description.

        Use this before writing or debugging a SolidWorks API call (VBA, a
        macro or COM) to check that the member exists on that interface and
        how to call it. Answers come from the SolidWorks type library
        installed on this machine, so they match the installed version.

        Setup: none. SolidWorks must be installed, not running; it is never
        contacted. Read-only. The first lookup builds the index (about a
        second). ``index_status`` is ``ready``, ``rebuilt`` or
        ``unavailable``.

        Names match regardless of case, as COM does. ``result.member`` is
        SolidWorks' own spelling, with a ``note`` when yours differs. Each
        parameter has a ``type`` and a ``direction``: ``in``, ``out``
        (SolidWorks fills it) or ``in_out`` (pass a variable by reference).
        ``return_type`` is null when the method returns nothing; a property
        has its value type there and an ``access``. ``result.found`` false
        means there is no such member, and ``result.note`` says why,
        including when no index could be built (SolidWorks isn't installed
        here, or the requested year isn't).

        Example input: ``{"interface": "ISldWorks", "method": "OpenDoc6"}``

        Args:
            input_data (LookupApiMethodInput | None): The member to look up.
                `interface` (e.g. "ISldWorks") and `method` (a method or
                property name) are required; `year` has a default.

        Returns:
            dict[str, Any]: ``result``, ``year``, ``type_library_version``,
            ``index_status`` and ``source_index_file``, plus
            ``status``/``message``/``execution_time``.
        """
        start_time = time.time()
        normalized = normalize_input(input_data, LookupApiMethodInput)
        source = _index_for_lookup(normalized.year, normalized.index_file, config)
        result = source.annotate(
            _lookup_method(source.index, normalized.interface, normalized.method)
        )
        return {
            "status": "success",
            "message": (
                "Method found"
                if result.found
                else "No indexed match for that interface/method"
            ),
            **source.payload(),
            "result": result.model_dump(exclude_unset=True),
            "execution_time": time.time() - start_time,
        }

    @mcp.tool()
    async def lookup_api_interface(
        input_data: LookupApiInterfaceInput | None = None,
    ) -> dict[str, Any]:
        """List every method and property of a SolidWorks COM interface.

        Use this to see what an interface offers, such as IFeatureManager,
        or to check which interface a member is really on before calling it.
        For one member's parameters, use lookup_api_method. Names match
        regardless of case.

        Setup: none. SolidWorks must be installed, not running; it is never
        contacted. Read-only. The first call builds the index (about a
        second) and later calls reuse it. ``index_status`` is ``ready``
        (saved index), ``rebuilt`` (built for this call) or ``unavailable``
        (no index could be built).

        Returns ``result`` with ``methods``, ``properties`` and their counts.
        ``result.found`` false means the interface isn't in the type library,
        or no index could be built, in which case ``result.note`` says why.

        Example input: ``{"interface": "IFeatureManager"}``

        Args:
            input_data (LookupApiInterfaceInput | None): The interface.
                `interface` (e.g. "IFeatureManager") is required; `year` has
                a default.

        Returns:
            dict[str, Any]: ``result``, ``year``, ``type_library_version``,
            ``index_status`` and ``source_index_file``, plus
            ``status``/``message``/``execution_time``.
        """
        start_time = time.time()
        normalized = normalize_input(input_data, LookupApiInterfaceInput)
        source = _index_for_lookup(normalized.year, normalized.index_file, config)
        result = _lookup_interface(source.index, normalized.interface)
        source.annotate(result)
        return {
            "status": "success",
            "message": (
                f"{result.method_count} methods, {result.property_count} properties"
                if result.found
                else "Interface not found in the indexed documentation"
            ),
            **source.payload(),
            "result": result.model_dump(exclude_unset=True),
            "execution_time": time.time() - start_time,
        }

    @mcp.tool()
    async def get_api_enum(
        input_data: GetApiEnumInput | None = None,
    ) -> dict[str, Any]:
        """Look up a SolidWorks API enum's members and their integer values.

        Use this when a call takes an enum value, such as a unit system or a
        user preference, instead of guessing the number: the values aren't in
        the API help pages. Give the enum (``swUnitSystem_e``) or one of its
        members (``swUnitSystem_IPS``); a member's name returns its whole
        enum with ``matched_member`` set. Names match regardless of case. The
        values come from SolidWorks' constants type library installed on this
        machine.

        Setup: none. SolidWorks must be installed, not running; it is never
        contacted. Read-only. The first lookup builds the index (about a
        second) and later ones reuse it. ``index_status`` is ``ready``,
        ``rebuilt`` or ``unavailable``.

        Returns ``result`` with ``enum``, ``description`` and ``members``
        (``name`` and ``value`` in declaration order). ``result.found`` false
        means there's no such enum or member, or no index could be built;
        ``result.note`` says which.

        Example input: ``{"name": "swUnitSystem_e"}``

        Args:
            input_data (GetApiEnumInput | None): The enum. `name` (an
                enum or member name) is required; `year` has a default.

        Returns:
            dict[str, Any]: ``result``, ``year``, ``type_library_version``,
            ``index_status`` and ``source_index_file``, plus
            ``status``/``message``/``execution_time``.
        """
        start_time = time.time()
        normalized = normalize_input(input_data, GetApiEnumInput)
        source = _index_for_lookup(normalized.year, normalized.index_file, config)
        result = source.annotate(_lookup_enum(source.index, normalized.name))
        return {
            "status": "success",
            "message": (
                f"{result.enum}: {len(result.members or [])} members"
                if result.found
                else "No enum or enum member of that name"
            ),
            **source.payload(),
            "result": result.model_dump(exclude_unset=True),
            "execution_time": time.time() - start_time,
        }

    @mcp.tool()
    async def find_related_api_members(
        input_data: FindRelatedApiInput | None = None,
    ) -> dict[str, Any]:
        """Find SolidWorks API members related to an interface or member name.

        For an interface: its own members. For a member: the other members
        of every interface that also has it (often used together), plus
        members whose names contain it. Use it to explore around a call you
        know, such as what sits beside InsertSketch.

        Setup: none. SolidWorks must be installed, not running; it is never
        contacted. Read-only. The first call builds the index (about a
        second) and later calls reuse it. ``index_status`` is ``ready``
        (saved index), ``rebuilt`` (built for this call) or ``unavailable``
        (no index could be built).

        Returns ``result.related``: each member's interface, name, type and
        why it is related. ``result.found`` false means nothing matched, or
        no index could be built, in which case ``result.note`` says why.

        Example input: ``{"name": "InsertSketch", "max_results": 10}``

        Args:
            input_data (FindRelatedApiInput | None): What to explore.
                `name` (an interface or member) is required; `max_results`
                and `year` have defaults.

        Returns:
            dict[str, Any]: ``result``, ``year``, ``type_library_version``,
            ``index_status`` and ``source_index_file``, plus
            ``status``/``message``/``execution_time``.
        """
        start_time = time.time()
        normalized = normalize_input(input_data, FindRelatedApiInput)
        source = _index_for_lookup(normalized.year, normalized.index_file, config)
        result = _find_related_api_members(
            source.index, normalized.name, normalized.max_results
        )
        source.annotate(result)
        return {
            "status": "success",
            "message": (
                f"{len(result.related)} related member(s)"
                if result.found
                else "No related members found"
            ),
            **source.payload(),
            "result": result.model_dump(exclude_unset=True),
            "execution_time": time.time() - start_time,
        }

    # discover_solidworks_docs, search_solidworks_api_help, lookup_api_method,
    # lookup_api_interface, get_api_enum, find_related_api_members
    tool_count = 6
    return tool_count

register_drawing_analysis_tools async

register_drawing_analysis_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: Any) -> int

Register advanced drawing analysis tools with FastMCP.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config Any

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Example

tool_count = await register_drawing_analysis_tools(mcp, adapter, config)

Source code in src/solidworks_mcp/tools/drawing_analysis.py
async def register_drawing_analysis_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: Any
) -> int:
    """Register advanced drawing analysis tools with FastMCP.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (Any): Configuration values for the operation.

    Returns:
        int: The computed numeric result.

    Example:
                        >>> tool_count = await register_drawing_analysis_tools(mcp, adapter, config)
    """
    mcp = with_tool_boundary(mcp)
    tool_count = 0

    @mcp.tool()
    async def compare_drawing_versions(
        input_data: DrawingVersionsInput,
    ) -> dict[str, Any]:
        """Compare two drawing files on disk: whether they are byte-identical, and how their sizes differ.

        Use it to tell whether a drawing changed between two saved versions. It does not
        open SolidWorks, so it can't say which views, dimensions or notes differ.
        Identical files mean nothing changed, but different files may not: SolidWorks
        writes a different file each time it saves, even with no change. Needs:
        both files on disk. Read-only. Returns: `comparison` with `file_1` and `file_2`
        (size, modified time, SHA-256), `identical` and `size_delta_bytes`.

        Args:
            input_data: Both full paths, `drawing_version_1` and
                `drawing_version_2`; both are required.

        Returns:
            dict[str, Any]: The comparison.
        """
        input_data = normalize_input(input_data, DrawingVersionsInput)
        drawing_v1 = input_data.drawing_version_1
        drawing_v2 = input_data.drawing_version_2

        file_1 = file_fact_snapshot(drawing_v1)
        file_2 = file_fact_snapshot(drawing_v2)

        missing = [
            path
            for path, snapshot in ((drawing_v1, file_1), (drawing_v2, file_2))
            if snapshot is None
        ]
        if missing:
            raise ToolOperationError(
                "Cannot compare drawing versions; file(s) not found "
                f"on disk: {', '.join(missing)}. No SolidWorks-level "
                "diff was performed."
            )

        assert file_1 is not None
        assert file_2 is not None
        identical = file_1["sha256"] == file_2["sha256"]

        return {
            "status": "success",
            "message": (
                "Drawing files are byte-identical"
                if identical
                else "Drawing files differ on disk"
            ),
            "comparison": {
                "file_1": file_1,
                "file_2": file_2,
                "identical": identical,
                "size_delta_bytes": file_2["size_bytes"] - file_1["size_bytes"],
            },
            "note": (
                "This is a filesystem-level comparison only; no "
                "SolidWorks session was opened, so geometric, "
                "dimension, and annotation differences were not "
                "analyzed."
            ),
        }

    tool_count = 1  # compare_drawing_versions
    return tool_count

register_drawing_automation_tools async

register_drawing_automation_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]) -> int

Register drawing-automation tools with FastMCP.

Parameters:

Name Type Description Default
mcp FastMCP

The MCP server.

required
adapter SolidWorksAdapter

Adapter used by every tool.

required
config dict[str, Any]

Server configuration (unused).

required

Returns:

Name Type Description
int int

Number of tools registered.

Source code in src/solidworks_mcp/tools/drawing_automation.py
async def register_drawing_automation_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]
) -> int:
    """Register drawing-automation tools with FastMCP.

    Args:
        mcp (FastMCP): The MCP server.
        adapter (SolidWorksAdapter): Adapter used by every tool.
        config (dict[str, Any]): Server configuration (unused).

    Returns:
        int: Number of tools registered.
    """

    mcp = with_tool_boundary(mcp)

    def _supports(method: str) -> None:
        require_capability(
            adapter,
            method,
            f"The active adapter does not support {method}; nothing was done.",
        )

    @mcp.tool()
    async def insert_model_dimensions(
        input_data: InsertModelDimensionsInput | None = None,
    ) -> dict[str, Any]:
        """Bring the model's dimensions into the active drawing's views, each dimension shown once.

        Imports the model's "marked for drawing" dimensions view by view; a repeat in a
        later view is deleted. Pictorial views (isometric, dimetric, trimetric) are
        skipped unless named. Falls back to SolidWorks AutoDimension only when nothing
        could be imported. SolidWorks places them inside the views, so run
        `arrange_dimensions` next. Needs: an open, active drawing with views. Changes:
        adds dimensions to the views. Returns: `data` with per-view `views`
        (before/after counts, `duplicates_removed`), `dimensions_added`,
        `unique_dimensions`, `skipped_pictorial_views` and the `method` used. An unknown
        view is `not_found`.

        Args:
            input_data: Which views. Nothing is required: give `view_name` for one
                view, or omit it for every view; `include_hole_wizard` has a default.

        Returns:
            dict[str, Any]: The dimensions added.
        """
        input_data = normalize_input(input_data, InsertModelDimensionsInput)
        _supports("insert_model_dimensions")
        result = await adapter.insert_model_dimensions(
            input_data.view_name, input_data.include_hole_wizard
        )
        require_success(result, context="Failed to insert model dimensions")
        data = result.data or {}
        return {
            "status": "success",
            "message": (
                f"Added {data.get('dimensions_added', 0)} dimension(s) to "
                f"{len(data.get('views', []))} view(s) ({data.get('method')})"
            ),
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def arrange_dimensions(
        input_data: ArrangeDimensionsInput | None = None,
    ) -> dict[str, Any]:
        """Move drawing dimensions out of their views into tidy rows so nothing overlaps, then check the sheet.

        Rows start 8 mm outside each view, 7 mm apart, shortest span nearest the view. A
        dimension with no room stays put and is listed in `unplaced`. The check reads
        back every text box and line: no text on text, no line through text or across a
        view, nothing off the sheet. Use it after `insert_model_dimensions`. Needs: an
        open, active drawing. Changes: moves dimensions on the sheet. Returns: `data`
        with `dimensions`, `moved`, `overlaps_before`, `overlaps_after` and `unplaced`.
        If any overlap remains the call is an error whose `data` lists
        `remaining_overlaps`; try a smaller sheet scale (`set_sheet_scale`).

        Args:
            input_data: Which views. Nothing is required: give `view_name` for one
                view's dimensions, or omit it for every view.

        Returns:
            dict[str, Any]: The arrangement and its check.
        """
        input_data = normalize_input(input_data, ArrangeDimensionsInput)
        _supports("arrange_dimensions")
        result = await adapter.arrange_dimensions(input_data.view_name)
        require_success(result, context="Failed to arrange dimensions", with_data=True)
        data = result.data or {}
        return {
            "status": "success",
            "message": (
                f"Arranged {data.get('dimensions', 0)} dimension(s): "
                f"{data.get('overlaps_before', 0)} overlap(s) before, "
                f"{data.get('overlaps_after', 0)} after"
            ),
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def insert_bom_table(
        input_data: InsertBomTableInput | None = None,
    ) -> dict[str, Any]:
        """Insert a bill-of-materials table for an assembly view on the active drawing.

        Use it on an assembly drawing after placing a view of the assembly (see
        `list_drawing_views` for view names). Needs: an open, active drawing with a view
        of an assembly. Changes: adds a BOM table to the sheet. Returns: `data` with
        `view`, `bom_type`, `template` and `row_count`. An unknown view is `not_found`
        and the message lists the views.

        Args:
            input_data: The table. `view_name` (a view of an assembly) is
                required; `bom_type` ("top_level_only", "parts_only" or "indented"),
                `template_path` and the position `x_mm`/`y_mm` (the sheet's BOM
                anchor when omitted) have defaults.

        Returns:
            dict[str, Any]: The table inserted.
        """
        _supports("insert_bom_table")
        input_data = normalize_input(input_data, InsertBomTableInput)
        result = await adapter.insert_bom_table(
            input_data.view_name,
            input_data.template_path,
            input_data.bom_type,
            input_data.x_mm,
            input_data.y_mm,
        )
        require_success(result, context="Failed to insert the BOM table")
        return {
            "status": "success",
            "message": f"Inserted a {input_data.bom_type} BOM on {input_data.view_name}",
            "data": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_gtol(input_data: AddGtolInput | None = None) -> dict[str, Any]:
        """Add a GD&T feature control frame to the active drawing, e.g. position Ø0.2 (M) to A|B|C.

        Attach it to a drawing dimension (`dimension_view` + `dimension_name`; the frame
        sits under the dimension's text, the usual place for a hole's position
        tolerance), or place it at `x_mm`/`y_mm` with an optional leader to the edge at
        `attach_x_mm`/`attach_y_mm`. Add the datums it refers to with `add_datum_tag`.
        Needs: an open, active drawing. Changes: adds the frame. Returns: `data`, the
        frame as read back from SolidWorks. No edge at the attach point is `not_found`.

        Args:
            input_data: The frame. `characteristic` (position, flatness,
                perpendicularity, parallelism, profile_surface or concentricity) and
                `tolerance` are required, plus either the dimension or the sheet
                position; `diameter_zone`, `material_condition` (none, mmc, lmc) and
                up to three `datums` (primary first) have defaults.

        Returns:
            dict[str, Any]: The frame added.
        """
        _supports("add_gtol")
        input_data = normalize_input(input_data, AddGtolInput)
        attach = input_data.attach_point()
        result = await adapter.add_gtol(
            input_data.characteristic,
            input_data.tolerance,
            input_data.x_mm,
            input_data.y_mm,
            input_data.diameter_zone,
            input_data.material_condition,
            input_data.datums,
            attach,
            input_data.attach_dimension(),
        )
        require_success(result, context="Failed to add the feature control frame")
        return {
            "status": "success",
            "message": (
                f"Added a {input_data.characteristic} frame "
                f"({input_data.tolerance:g}"
                + (f" to {'|'.join(input_data.datums)}" if input_data.datums else "")
                + ")"
            ),
            "data": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_datum_tag(
        input_data: AddDatumTagInput | None = None,
    ) -> dict[str, Any]:
        """Add a datum feature symbol (A, B, C, ...) on an edge of the active drawing.

        Use it to mark the datums a feature control frame from `add_gtol` refers to.
        Needs: an open, active drawing with an edge at the attach point. Changes: adds
        the datum tag. Returns: `data` with the `label` as read back. No edge at the
        point is `not_found`.

        Args:
            input_data: The tag. `label` (one letter), `attach_x_mm`/`attach_y_mm`
                (a point on the edge it marks) and `x_mm`/`y_mm` (where the tag
                goes) are all required, in mm on the sheet.

        Returns:
            dict[str, Any]: The tag added.
        """
        _supports("add_datum_tag")
        input_data = normalize_input(input_data, AddDatumTagInput)
        result = await adapter.add_datum_tag(
            input_data.label,
            (input_data.attach_x_mm, input_data.attach_y_mm),
            input_data.x_mm,
            input_data.y_mm,
        )
        require_success(result, context="Failed to add the datum tag")
        return {
            "status": "success",
            "message": f"Added datum {input_data.label.strip().upper()}",
            "data": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def set_sheet_scale(
        input_data: SetSheetScaleInput | None = None,
    ) -> dict[str, Any]:
        """Set the active drawing sheet's scale, e.g. 1:2; views that use the sheet scale follow it.

        Use it when views are too large for the sheet, or when `arrange_dimensions`
        reports overlaps it can't clear. Needs: an open, active drawing. Changes: the
        sheet scale. Returns: `data` with the `sheet` name and the new `scale`.

        Args:
            input_data: The scale. `numerator` and `denominator` are required and
                must be above zero, e.g. 1 and 2 for 1:2.

        Returns:
            dict[str, Any]: The new scale.
        """
        _supports("set_sheet_scale")
        input_data = normalize_input(input_data, SetSheetScaleInput)
        result = await adapter.set_sheet_scale(
            input_data.numerator, input_data.denominator
        )
        require_success(result, context="Failed to set the sheet scale")
        return {
            "status": "success",
            "message": f"Sheet scale set to {(result.data or {}).get('scale')}",
            "data": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def generate_drawing_package(
        input_data: GenerateDrawingPackageInput | None = None,
    ) -> dict[str, Any]:
        """Create a complete A3 drawing of a part or assembly in one call, and save it with a PDF.

        It opens a new drawing with an A3 ISO frame and fills the title block (title,
        number, material, weight, drawn by, date, revision). It places front, plan, side
        and isometric views at one standard scale, shows the model dimensions once in
        tidy rows, and adds view captions and a NOTES block. Then it checks nothing
        overlaps. Use it instead of the step-by-step drawing tools when you want the
        whole drawing. Needs: the model file on disk. Changes: saves
        `<drawing_number>.SLDDRW` and a PDF, replacing files of those names, and by
        default writes Description, Material and Weight into the model and saves it
        (turn off with `write_model_properties`). Returns: `data` with `scale`, `views`,
        `dimensions`, `overlap_check`, `title_block`, `drawing_path`, `pdf_path` and
        `steps`. A remaining overlap makes the call an error carrying that data; for
        long title-block text pass `title_block_text_height_mm` (e.g. 3).

        Args:
            input_data: The drawing. `model_path` (.sldprt or .sldasm) is
                required; `output_dir`, the title-block fields (`title`,
                `drawing_number`, `author`, `revision`, `company`), `notes`,
                `include_isometric`, `include_bom` and `write_model_properties` have
                defaults.

        Returns:
            dict[str, Any]: The drawing and its check.
        """
        require_capability(
            adapter,
            "create_standard_drawing",
            "The active adapter does not support generate_drawing_package; "
            "nothing was done.",
        )
        input_data = normalize_input(input_data, GenerateDrawingPackageInput)
        result = await adapter.create_standard_drawing(
            input_data.model_path,
            input_data.output_dir,
            input_data.title,
            input_data.subtitle,
            input_data.drawing_number,
            input_data.author,
            input_data.revision,
            input_data.company,
            None,
            input_data.notes,
            input_data.include_isometric,
            input_data.write_model_properties,
            input_data.title_block_text_height_mm,
            input_data.include_bom,
        )
        require_success(
            result, context="Failed to create the standard drawing", with_data=True
        )
        data = result.data or {}
        return {
            "status": "success",
            "message": (
                f"Created {data.get('drawing_path')} at {data.get('scale')} "
                f"with PDF {data.get('pdf_path')}"
            ),
            "data": data,
            "execution_time": result.execution_time,
        }

    return 7

register_drawing_tools async

register_drawing_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]) -> int

Register drawing tools with FastMCP.

Registers comprehensive technical drawing tools for SolidWorks automation including view creation, dimensioning, annotation, and drawing standards validation. Essential for automated documentation workflows.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config dict[str, Any]

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Example
from solidworks_mcp.tools.drawing import register_drawing_tools

tool_count = await register_drawing_tools(mcp, adapter, config)
print(f"Registered {tool_count} drawing tools")
Source code in src/solidworks_mcp/tools/drawing.py
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
async def register_drawing_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]
) -> int:
    """Register drawing tools with FastMCP.

    Registers comprehensive technical drawing tools for SolidWorks automation including view
    creation, dimensioning, annotation, and drawing standards validation. Essential for
    automated documentation workflows.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (dict[str, Any]): Configuration values for the operation.

    Returns:
        int: The computed numeric result.

    Example:
                        ```python
                        from solidworks_mcp.tools.drawing import register_drawing_tools

                        tool_count = await register_drawing_tools(mcp, adapter, config)
                        print(f"Registered {tool_count} drawing tools")
                        ```
    """
    mcp = with_tool_boundary(mcp)
    tool_count = 0

    @mcp.tool()
    async def create_drawing_view(input_data: CreateDrawingViewInput) -> dict[str, Any]:
        """Place one named view of a part or assembly on the active drawing sheet.

        Use it to add views one at a time; for the three standard views in one call, use
        `create_technical_drawing`, and for a whole drawing from a model, use
        `generate_drawing_package`.
        Needs: an open, active drawing and the model file on disk. Changes: adds a view
        to the sheet. Returns: `data` with the view's `name`, `orientation`, `position`,
        `scale` and the view counts before and after.

        Args:
            input_data: The view. `model_path` and `view_type` (front, back, left, right, top, bottom, isometric (iso), trimetric, dimetric or current) are
                required; `position_x`/`position_y` (mm on the sheet) and `scale`
                have defaults.

        Returns:
            dict[str, Any]: The placed view.
        """
        require_capability(
            adapter,
            "create_drawing_view",
            "Active adapter does not support create_drawing_view; no "
            f"{input_data.view_type} view of {input_data.model_path} was created.",
        )
        result = await adapter.create_drawing_view(input_data.model_dump())
        require_success(result, "Failed to create drawing view")
        return {
            "status": "success",
            "message": f"Created {input_data.view_type} view of {input_data.model_path}",
            "data": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_dimension(input_data: AddDimensionInput) -> dict[str, Any]:
        """Add one dimension to a drawing view, between edges picked by points on the sheet.

        Give points in sheet mm on a view's edges or vertices (read view outlines with
        `check_drawing`). Two parallel edges give their distance; one edge its length;
        a circle's edge its radius or diameter; two lines their angle. For the model's
        own dimensions use `insert_model_dimensions` instead. Needs: an open, active
        drawing. Changes: adds a reference dimension. Returns: `dimension` with `name`,
        `value` and `units` (mm, deg for angular).

        Args:
            input_data: The dimension. `dimension_type`, `entity1_point` and
                `position` are required; `entity2_point` for two entities.

        Returns:
            dict[str, Any]: The dimension and its measured value.
        """
        input_data = normalize_input(input_data, AddDimensionInput)
        require_capability(
            adapter,
            "add_dimension",
            "Active adapter does not support add_dimension; no dimension was added.",
        )
        result = await adapter.add_dimension(
            input_data.dimension_type,
            input_data.entity1_point,
            input_data.position,
            input_data.entity2_point,
        )
        require_success(result, "Failed to add dimension")
        data = result.data or {}
        return {
            "status": "success",
            "message": f"Added a {input_data.dimension_type} dimension: "
            f"{float(data.get('value') or 0):g} {data.get('units', '')}".rstrip(),
            "dimension": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_note(input_data: AddNoteInput) -> dict[str, Any]:
        """Place a text note on the active drawing sheet at a position in millimetres.

        Use it for general notes such as material or finish. For a title block, use
        `generate_drawing_package`, which fills one. Needs: an open, active drawing.
        Changes: adds a note to the sheet. Returns: `data` with `text`, `position`,
        `positioned` (whether the position was applied), `extent_mm` (the note's
        [left, bottom, right, top] on the sheet, read back) and `font_size_points`.

        Args:
            input_data: The note. `text`, `position_x` and `position_y` (the
                note's top-left corner, mm from the sheet's lower-left corner)
                are required; `font_size` (points) has a default.

        Returns:
            dict[str, Any]: The note placed.
        """
        require_capability(
            adapter,
            "add_note",
            "Active adapter does not support add_note; no note was added.",
        )
        result = await adapter.add_note(input_data.model_dump())
        require_success(result, "Failed to add note")
        return {
            "status": "success",
            "message": f"Added note: {input_data.text[:30]}...",
            "data": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_section_view(input_data: CreateSectionViewInput) -> dict[str, Any]:
        """Cut a section view along a line across a drawing view, and place it on the sheet.

        Give the line's ends in sheet mm so it crosses the view (e.g. through a hole's
        centre), and where the new view goes. A line that misses the view, or a position
        off the sheet, is refused before anything changes. Needs: an open, active drawing
        with that view. Changes: adds a section view and its section line. Returns:
        `view` with `name`, `label`, `parent` and `outline_mm`.

        Args:
            input_data: The section. `view`, `line_start`, `line_end` and `position`
                are required; `label` defaults to the next free letter.

        Returns:
            dict[str, Any]: The new view.
        """
        input_data = normalize_input(input_data, CreateSectionViewInput)
        require_capability(
            adapter,
            "create_section_view",
            "Active adapter does not support create_section_view; no view was added.",
        )
        result = await adapter.create_section_view(
            input_data.view,
            input_data.line_start,
            input_data.line_end,
            input_data.position,
            input_data.label,
        )
        require_success(result, "Failed to create section view")
        data = result.data or {}
        return {
            "status": "success",
            "message": f"Added section view {data.get('label')} of {input_data.view}",
            "view": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_detail_view(input_data: CreateDetailViewInput) -> dict[str, Any]:
        """Magnify a circular area of a drawing view as a detail view, at a scale.

        Give the circle's centre (sheet mm, inside the view) and radius, where the new
        view goes, and its scale. A centre outside the view or a position off the sheet
        is refused before anything changes. Needs: an open, active drawing with that
        view. Changes: adds a detail view and its circle. Returns: `view` with `name`,
        `label`, `scale`, `parent` and `outline_mm`.

        Args:
            input_data: The detail. `view`, `center`, `radius` and `position` are
                required; `scale` is 2:1 and `label` the next free letter.

        Returns:
            dict[str, Any]: The new view.
        """
        input_data = normalize_input(input_data, CreateDetailViewInput)
        require_capability(
            adapter,
            "create_detail_view",
            "Active adapter does not support create_detail_view; no view was added.",
        )
        result = await adapter.create_detail_view(
            input_data.view,
            input_data.center,
            input_data.radius,
            input_data.position,
            input_data.scale,
            input_data.label,
        )
        require_success(result, "Failed to create detail view")
        data = result.data or {}
        return {
            "status": "success",
            "message": f"Added detail view {data.get('label')} of {input_data.view} "
            f"at {data.get('scale')}",
            "view": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def update_sheet_format(input_data: UpdateSheetFormatInput) -> dict[str, Any]:
        """Change the active drawing sheet's format and paper size, keeping every view.

        Give a format name SolidWorks has (e.g. "a2 - iso") or a .slddrt path; the paper
        size comes from the name unless given. The views are checked to be the same
        afterwards, and the title block keeps its values (they are properties). Needs:
        an open, active drawing. Changes: the sheet's format and size. Returns: `sheet`
        with `sheet_format` (the path), `paper_size`, `sheet_mm` and `views`.

        Args:
            input_data: The format. `sheet_format` is required; `paper_size` optional.

        Returns:
            dict[str, Any]: The sheet afterwards.
        """
        input_data = normalize_input(input_data, UpdateSheetFormatInput)
        require_capability(
            adapter,
            "update_sheet_format",
            "Active adapter does not support update_sheet_format; the sheet was "
            "not changed.",
        )
        result = await adapter.update_sheet_format(
            input_data.sheet_format, input_data.paper_size
        )
        require_success(result, "Failed to update sheet format")
        data = result.data or {}
        return {
            "status": "success",
            "message": f"Sheet is now {str(data.get('paper_size', '')).upper()} "
            f"({input_data.sheet_format})",
            "sheet": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def auto_dimension_view(input_data: AutoDimensionViewInput) -> dict[str, Any]:
        """Dimension one drawing view with SolidWorks' auto-dimension (baseline or chain).

        It adds reference dimensions measured from the view's edges; prefer
        `insert_model_dimensions`, which shows the model's own (driving) dimensions, and
        use this for a view that has none to bring in. Dimensions may overlap: run
        `arrange_dimensions` after. It fails when nothing is added. Needs: an open,
        active drawing with that view. Changes: adds dimensions. Returns: `data` with
        `added` and the counts before and after.

        Args:
            input_data: The view. `view` is required; `scheme` is baseline.

        Returns:
            dict[str, Any]: The dimensions added.
        """
        input_data = normalize_input(input_data, AutoDimensionViewInput)
        require_capability(
            adapter,
            "auto_dimension_view",
            "Active adapter does not support auto_dimension_view; no dimensions "
            "were added.",
        )
        result = await adapter.auto_dimension_view(input_data.view, input_data.scheme)
        require_success(result, "Failed to auto-dimension view")
        data = result.data or {}
        return {
            "status": "success",
            "message": f"Added {data.get('added')} dimension(s) to {input_data.view}",
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def check_drawing() -> dict[str, Any]:
        """Check the active drawing is complete before exporting it: views, dimensions, title block, BOM.

        Read-only. Findings: `view_off_sheet` (error), `view_not_dimensioned`
        (warning; isometric and detail views excepted), `title_block_field_empty`
        (warning: title, drawn by, revision), `dangling_annotation` (error) and
        `bom_missing` (warning, an assembly with no BOM). Each names the tool that
        fixes it. It checks completeness, not a drafting standard. Needs: an open,
        active drawing. Returns: `passed` (no errors), `errors`, `warnings`,
        `findings` (each `rule`, `severity`, `message`, maybe `view`), `views` and
        `report` (the facts read).
        """
        require_capability(
            adapter,
            "drawing_report",
            "Active adapter can't read a drawing; nothing was checked.",
        )
        result = await adapter.drawing_report()
        require_success(result, "Failed to read the drawing")
        report = result.data or {}
        verdict = check(report)
        return {
            "status": "success",
            "message": (
                "The drawing passed"
                if verdict["passed"]
                else f"The drawing has {verdict['errors']} error(s)"
            )
            + f", {verdict['warnings']} warning(s)",
            **verdict,
            "report": report,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_technical_drawing(
        input_data: DrawingCreationInput,
    ) -> dict[str, Any]:
        """Place a model's three standard views (front, top and side) on the active drawing sheet in one call.

        Use it to start a drawing's views quickly; for one view, use
        `create_drawing_view`, and to create and save a complete drawing from a model
        file, use `generate_drawing_package`. Views are laid out in `projection`
        (third-angle by default). Needs: an open, active drawing and the model file on
        disk. Changes: adds three views to the sheet. Returns: `data` with `views` (the
        names added), `model_path`, `projection` and the view counts.

        Args:
            input_data: The model to draw. Give `model_file`; `projection` is
                third_angle unless you pass first_angle.

        Returns:
            dict[str, Any]: The views added.
        """
        require_capability(
            adapter,
            "create_technical_drawing",
            "Active adapter does not support create_technical_drawing; "
            "no drawing was created.",
        )
        result = await adapter.create_technical_drawing(input_data.model_dump())
        require_success(result, "Failed to create technical drawing")
        return {
            "status": "success",
            "message": "Technical drawing created successfully",
            "data": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_drawing_view(input_data: DrawingViewInput) -> dict[str, Any]:
        """Place one named view of a part or assembly on the active drawing sheet; the same as `create_drawing_view`.

        Prefer `create_drawing_view`; this tool reaches the same code and is kept for
        existing callers. It only adds views: an `operation` other than "add" is
        refused. Needs: an open, active drawing and the model file on
        disk. Changes: adds a view to the sheet. Returns: `data` with the view's `name`,
        `orientation`, `position`, `scale` and the view counts.

        Args:
            input_data: The view. Give `model_file` and `view_type` (front, back, left, right, top, bottom, isometric (iso), trimetric, dimetric or current);
                `position` ([x, y] mm on the sheet) and `scale` have defaults.

        Returns:
            dict[str, Any]: The placed view.
        """
        if input_data.operation.strip().lower() != "add":
            raise InvalidInputError(
                f"operation '{input_data.operation}' is not supported: "
                "add_drawing_view only adds views, and the drawing was not changed."
            )
        require_capability(
            adapter,
            "add_drawing_view",
            "Active adapter does not support add_drawing_view; no view was added.",
        )
        result = await adapter.add_drawing_view(input_data.model_dump())
        require_success(result, "Failed to add drawing view")
        return {
            "status": "success",
            "message": "Drawing view added successfully",
            "data": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def update_title_block(input_data: UpdateTitleBlockInput) -> dict[str, Any]:
        """Change the active drawing's title-block fields: title, drawing number, drawn by, date, revision, company.

        Each field is written to the property the sheet format shows (the same mapping
        `generate_drawing_package` uses): the title is the drawn model's Description,
        the others the drawing's own properties. Fields not given keep their values.
        Needs: an open, active drawing of a model. Changes: those properties. Returns:
        `fields`, each with the `property` written, `on` (drawing or model) and `value`.

        Args:
            input_data: At least one of `title`, `drawing_number`, `drawn_by`,
                `drawn_date`, `revision`, `company`.

        Returns:
            dict[str, Any]: The fields written.
        """
        input_data = normalize_input(input_data, UpdateTitleBlockInput)
        require_capability(
            adapter,
            "update_title_block",
            "Active adapter does not support update_title_block; the title block "
            "was not changed.",
        )
        fields = input_data.fields()
        result = await adapter.update_title_block(fields)
        require_success(result, "Failed to update title block")
        return {
            "status": "success",
            "message": "Title block: "
            + ", ".join(f"{k} = {v}" for k, v in fields.items()),
            "fields": (result.data or {}).get("fields", {}),
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def list_drawing_views() -> dict[str, Any]:
        """List the views on the active drawing, by name.

        Use it before `auto_center_marks`, `insert_model_dimensions` or any tool that
        takes a view name. Needs: an open, active drawing. Read-only. Returns: `views`
        (names, sheet formats left out) and `count`.
        """
        result = await adapter.list_drawing_views()
        require_success(result, context="Failed to list drawing views")
        views = list(result.data or [])
        return {
            "status": "success",
            "message": (
                f"{len(views)} view(s): {', '.join(views)}"
                if views
                else "The drawing has no views"
            ),
            "views": views,
            "count": len(views),
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def auto_center_marks(
        input_data: AutoCenterMarksInput,
    ) -> dict[str, Any]:
        """Insert centre marks on the holes, slots and (optionally) fillets of one drawing view.

        Use it after placing views, before dimensioning holes. Adding none is still a
        success: the view may have no unmarked circular edges. Needs: an open drawing
        with the named view (see `list_drawing_views`). Changes: adds centre-mark
        annotations to the view. Returns: `data` with `center_marks_added` and the
        counts before and after.

        Args:
            input_data: The view. `view_name` is required; `mark_holes` and
                `mark_slots` default to on, `mark_fillets` to off.

        Returns:
            dict[str, Any]: The marks added.
        """
        require_capability(
            adapter,
            "auto_center_marks",
            "Active adapter does not support auto_center_marks; "
            "no centre marks were inserted.",
        )
        input_data = normalize_input(input_data, AutoCenterMarksInput)
        result = await adapter.auto_center_marks(
            input_data.view_name,
            input_data.mark_holes,
            input_data.mark_fillets,
            input_data.mark_slots,
        )
        require_success(result, context="Failed to insert centre marks")
        data = result.data if isinstance(result.data, dict) else {}
        added = data.get("center_marks_added")
        return {
            "status": "success",
            "message": (
                f"Inserted {added} centre mark(s) on {input_data.view_name}"
                if isinstance(added, int)
                else f"Ran centre-mark insertion on {input_data.view_name}"
            ),
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_balloons(input_data: AddBalloonsInput) -> dict[str, Any]:
        """Number the parts in a drawing view with balloons, one per part.

        Use it on an assembly drawing, with a BOM (`insert_bom_table`): the
        balloons carry the BOM's item numbers. Works best on an exploded view
        (`create_drawing_view` with `exploded`). Needs: an open drawing with a view
        of an assembly. Changes: adds balloons around the view. Returns:
        `balloons` with the `view`, `layout`, `count` and the `items` shown. A
        view that isn't there fails as `not_found`.

        Args:
            input_data: `view_name` is required; `layout` is `square`.

        Returns:
            dict[str, Any]: The balloons added.
        """
        input_data = normalize_input(input_data, AddBalloonsInput)
        require_capability(adapter, "add_balloons", "Active adapter can't add balloons")
        result = await adapter.add_balloons(input_data.view_name, input_data.layout)
        require_success(result, context="Failed to add balloons")
        count = (result.data or {}).get("count", 0)
        return {
            "status": "success",
            "message": f"Added {count} balloon(s) to {input_data.view_name}",
            "balloons": result.data,
            "execution_time": result.execution_time,
        }

    tool_count = 14  # Number of tools registered
    return tool_count

register_engineering_data_tools async

register_engineering_data_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]) -> int

Register engineering-data tools with FastMCP.

Parameters:

Name Type Description Default
mcp FastMCP

The MCP server.

required
adapter SolidWorksAdapter

Adapter used by every tool.

required
config dict[str, Any]

Server configuration (unused).

required

Returns:

Name Type Description
int int

Number of tools registered.

Source code in src/solidworks_mcp/tools/engineering_data.py
async def register_engineering_data_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]
) -> int:
    """Register engineering-data tools with FastMCP.

    Args:
        mcp (FastMCP): The MCP server.
        adapter (SolidWorksAdapter): Adapter used by every tool.
        config (dict[str, Any]): Server configuration (unused).

    Returns:
        int: Number of tools registered.
    """

    mcp = with_tool_boundary(mcp)

    def _supports(method: str) -> None:
        require_capability(
            adapter,
            method,
            f"The active adapter does not support {method}; nothing was done.",
        )

    @mcp.tool()
    async def list_model_dimensions(
        input_data: ListModelDimensionsInput | None = None,
    ) -> dict[str, Any]:
        """List every dimension in the active part or assembly with its full name and value.

        Use it to find a dimension in an existing model before changing it: each
        `full_name` (e.g. "D1@Sketch1") is the name `get_dimension` and `set_dimension`
        take. Each dimension is listed once, under the feature its full name names
        after the `@` (`D1@Plane1` under `Plane1`, never also under a cut sketched on
        that plane). Needs: an open part or assembly. Read-only. Returns: `data`, one
        entry per dimension with `full_name`, `feature`, `value` (document units),
        `system_value` (metres or radians) and `read_only`.

        Args:
            input_data: Nothing is required: give `feature_name` to list the
                dimensions that feature owns, or omit it for every dimension.

        Returns:
            dict[str, Any]: The dimensions.
        """
        input_data = normalize_input(input_data, ListModelDimensionsInput)
        _supports("list_model_dimensions")
        result = await adapter.list_model_dimensions(input_data.feature_name)
        require_success(result, context="Failed to list model dimensions")
        return _success(result, f"Listed {len(result.data or [])} dimension(s)")

    @mcp.tool()
    async def measure_entities(input_data: MeasureEntitiesInput) -> dict[str, Any]:
        """Measure faces, edges or vertices of the active model with SolidWorks' Measure tool.

        One face gives area and perimeter; one edge its length, and radius and diameter
        for an arc; two entities give the distance and angle between them. Use it to
        check a feature's size or position; for the whole model, use `verify_model`.
        Needs: an open part or assembly. Read-only. It clears the selection afterwards.
        Returns: `data` with `selected` and the values in metres, square metres and
        radians (`distance_m`, `angle_rad`, `length_m`, `radius_m`, `diameter_m`,
        `area_m2`, `perimeter_m`; for two entities `is_parallel`).

        Args:
            input_data: What to measure. `entities` (one or two) is required, each
                "face:x,y,z", "edge:x,y,z", "vertex:x,y,z" (a point on it, in metres)
                or a name like "Edge<1>".

        Returns:
            dict[str, Any]: The measurements.
        """
        input_data = normalize_input(input_data, MeasureEntitiesInput)
        _supports("measure_entities")
        result = await adapter.measure_entities(input_data.entities)
        require_success(result, context="Failed to measure")
        return _success(result, f"Measured {', '.join(input_data.entities)}")

    @mcp.tool()
    async def get_material(
        input_data: GetMaterialInput | None = None,
    ) -> dict[str, Any]:
        """Report the active part's material with its strength and stiffness, or any library material's.

        Use it to size a part (the elastic modulus and yield strength) or to compare
        materials before `set_material`, and before `get_mass_properties` (without a
        material SolidWorks uses a default density). Give `material` to read a library
        material without assigning it: no document is needed and none changes.
        Needs: an open, active part unless `material` is given (any other document is
        `wrong_document_type`). Read-only. Returns: `data` with `material` (null when
        none is assigned), `database`, `density_kg_m3`, `source` (part or library) and
        `properties`: `elastic_modulus`, `poissons_ratio`, `shear_modulus`,
        `tensile_strength`, `yield_strength`, `compressive_strength`, `density`,
        `thermal_expansion`, `thermal_conductivity`, `specific_heat`, each with
        `value` in SI units (Pa; divide by 1e6 for MPa), `unit` and `label`. A
        property the library doesn't hold for that material is left out.

        Args:
            input_data: Nothing is required: `configuration` defaults to the active
                one; `material` (and `database`) read a library material instead.

        Returns:
            dict[str, Any]: The material and its properties.
        """
        input_data = normalize_input(input_data, GetMaterialInput)
        _supports("get_material")
        result = await adapter.get_material(
            input_data.configuration, input_data.material, input_data.database
        )
        require_success(result, context="Failed to read the material")
        data = result.data or {}
        material = data.get("material")
        if not material:
            return _success(result, "No material assigned")
        modulus = data.get("properties", {}).get("elastic_modulus")
        stiffness = f", E = {modulus['value'] / 1e9:g} GPa" if modulus else ""
        return _success(result, f"Material: {material}{stiffness}")

    @mcp.tool()
    async def set_material(input_data: SetMaterialInput) -> dict[str, Any]:
        """Assign a library material to the active part, e.g. "AISI 1020" or "6061-T6 (SS)".

        The material sets the mass and what a title block's Material field shows. The
        name must match the library exactly; find it with `list_available_materials`.
        The material is read back, so success means it applied. Needs: an open, active
        part. Changes: the part's material. Returns: `data` with `material`, `database`
        and `configuration`.

        Args:
            input_data: The material. `material_name` is required; `database`
                ("SOLIDWORKS Materials") and `configuration` (all) have defaults.

        Returns:
            dict[str, Any]: The material assigned.
        """
        input_data = normalize_input(input_data, SetMaterialInput)
        _supports("set_material")
        result = await adapter.set_material(
            input_data.material_name, input_data.database, input_data.configuration
        )
        require_success(result, context="Failed to assign the material")
        return _success(result, f"Assigned {input_data.material_name}")

    @mcp.tool()
    async def list_available_materials(
        input_data: ListMaterialsInput | None = None,
    ) -> dict[str, Any]:
        """List the materials in the installed SolidWorks material libraries.

        Use it to find the exact name `set_material` needs; filter with `search` (e.g.
        "steel" or "6061"). It reads every .sldmat library SolidWorks is set to use.
        Needs: SolidWorks installed. Read-only. Returns: `data`, each material's `name`,
        `classification` and `database`; the message gives the total and the folders
        read, so an empty search can be told from a missing library.

        Args:
            input_data: Nothing is required: `search` filters by name or
                classification, not case-sensitive.

        Returns:
            dict[str, Any]: The materials.
        """
        input_data = normalize_input(input_data, ListMaterialsInput)
        _supports("list_available_materials")
        result = await adapter.list_available_materials(input_data.search)
        require_success(result, context="Failed to list materials")
        searched = result.metadata or {}
        found = len(result.data or [])
        message = f"Found {found} material(s)"
        if "total" in searched:
            message += (
                f" of {searched['total']} in {'; '.join(searched.get('folders', []))}"
            )
        return _success(result, message)

    @mcp.tool()
    async def get_custom_properties(
        input_data: CustomPropertiesInput | None = None,
    ) -> dict[str, Any]:
        """Read the active document's custom properties, such as PartNo, Description or Revision.

        File-level properties (the default) are what drawing title blocks and BOMs
        usually read; give `configuration` for that configuration's own. Needs: an open,
        active document. Read-only. Returns: `data` with `properties`, each name mapped
        to its `value` and `resolved` value (a $PRP link evaluated).

        Args:
            input_data: Nothing is required: `configuration` reads that
                configuration's properties instead of the file's.

        Returns:
            dict[str, Any]: The properties.
        """
        input_data = normalize_input(input_data, CustomPropertiesInput)
        _supports("get_custom_properties")
        result = await adapter.get_custom_properties(input_data.configuration)
        require_success(result, context="Failed to read custom properties")
        count = len((result.data or {}).get("properties", {}))
        return _success(result, f"Read {count} custom propert(ies)")

    @mcp.tool()
    async def set_custom_property(input_data: SetCustomPropertyInput) -> dict[str, Any]:
        """Create or overwrite one text custom property on the active document, e.g. PartNo or DrawnBy.

        Use it to fill what a title block or BOM reads. The value is read back, so
        success means it was stored. Needs: an open, active document. Changes: writes
        the property, replacing one of the same name (save the file to keep it).
        Returns: `data` with `name`, `value`, `resolved` and `configuration`.

        Args:
            input_data: The property. `name` and `value` (text, or a link such as
                $PRP:"SW-Mass") are required; `configuration` defaults to file-level.

        Returns:
            dict[str, Any]: The property stored.
        """
        input_data = normalize_input(input_data, SetCustomPropertyInput)
        _supports("set_custom_property")
        result = await adapter.set_custom_property(
            input_data.name, input_data.value, input_data.configuration
        )
        require_success(result, context="Failed to set the custom property")
        return _success(result, f"Set custom property {input_data.name}")

    @mcp.tool()
    async def export_flat_pattern(input_data: ExportFlatPatternInput) -> dict[str, Any]:
        """Export the active sheet-metal part's flat pattern to DXF or DWG, for laser or waterjet cutting.

        It writes the flat outline only, without bend lines or sketches. A part with no
        sheet-metal body is `not_found` and nothing is written. Needs: an open, active
        sheet-metal part. Changes: writes the file, replacing one already there.
        Returns: `data` with `file_path` and `size_bytes`.

        Args:
            input_data: The export. `file_path` (ending in .dxf or .dwg) is
                required.

        Returns:
            dict[str, Any]: The file written.
        """
        input_data = normalize_input(input_data, ExportFlatPatternInput)
        _supports("export_flat_pattern")
        result = await adapter.export_flat_pattern(input_data.file_path)
        require_success(result, context="Failed to export the flat pattern")
        written = (result.data or {}).get("file_path")
        return _success(result, f"Exported the flat pattern to {written}")

    return 8

register_equation_tools async

register_equation_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: Any) -> int

Register the equation tools with FastMCP.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config Any

Configuration values for the operation.

required

Returns:

Name Type Description
int int

How many tools were registered.

Source code in src/solidworks_mcp/tools/equations.py
async def register_equation_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: Any
) -> int:
    """Register the equation tools with FastMCP.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (Any): Configuration values for the operation.

    Returns:
        int: How many tools were registered.
    """
    mcp = with_tool_boundary(mcp)

    @mcp.tool()
    async def list_equations() -> dict[str, Any]:
        """List the active part's or assembly's equations and global variables, with their values.

        Use it before `set_equation` or `delete_equation`, to see which values
        drive which dimensions. Needs: an open part or assembly. Read-only.
        Returns: `equations`, each with `index`, `name`, `expression`, `kind`
        (`global_variable`, `dimension` or `other`) and `value` (solved, in the
        document's units; null when SolidWorks has none), and `count`.
        """
        require_capability(
            adapter, "list_equations", "Active adapter can't read equations"
        )
        result = await adapter.list_equations()
        require_success(result, "Failed to list equations")
        rows = result.data or []
        return {
            "status": "success",
            "equations": rows,
            "count": len(rows),
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def set_equation(input_data: SetEquationInput) -> dict[str, Any]:
        """Add or change an equation or global variable, so one value drives others (a parametric part).

        E.g. `Width` = `60mm`, then `D1@Sketch1` = `"Width" / 2`: changing `Width`
        later resizes the sketch. A `name` with `@` is a dimension of the active
        document (`list_model_dimensions`); any other is a global variable. An
        existing equation for `name` is changed, else one is added; the model
        is solved and rebuilt. Applies in all configurations unless
        `configurations` says otherwise. An expression SolidWorks can't use
        fails as `equation_refused`, changing nothing. Needs: an open part or
        assembly. Changes: the equation and what it drives. Returns:
        `equation` with `name`, `expression`, `kind`, `value` (document units),
        `action` (`added` or `changed`) and `configurations`.

        Args:
            input_data: `name` and `expression` are required; `configurations`
                defaults to `all`.

        Returns:
            dict[str, Any]: The equation as SolidWorks stored it.
        """
        input_data = normalize_input(input_data, SetEquationInput)
        require_capability(
            adapter, "set_equation", "Active adapter can't change equations"
        )
        result = await adapter.set_equation(
            input_data.name, input_data.expression, input_data.configurations
        )
        require_success(result, "Failed to set equation")
        data = result.data or {}
        return {
            "status": "success",
            "message": f"{str(data.get('action', 'set')).capitalize()} "
            f"{input_data.name} = {input_data.expression.strip()}",
            "equation": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def delete_equation(input_data: EquationNameInput) -> dict[str, Any]:
        """Delete an equation or global variable of the active part or assembly, by name.

        A dimension the equation drove keeps its last value; an equation that
        used a deleted global variable stops solving, so check
        `list_equations` afterwards. Needs: an open part or assembly with that
        equation. Changes: removes the equation. Returns: `deleted` and the
        `equations` left (names).

        Args:
            input_data: `name`, as `list_equations` gives it.

        Returns:
            dict[str, Any]: The deleted name and those left.
        """
        input_data = normalize_input(input_data, EquationNameInput)
        require_capability(
            adapter, "delete_equation", "Active adapter can't delete equations"
        )
        result = await adapter.delete_equation(input_data.name)
        require_success(result, "Failed to delete equation")
        data = result.data or {}
        return {
            "status": "success",
            "message": f"Deleted equation {data.get('deleted', input_data.name)}",
            "deleted": data.get("deleted"),
            "equations": data.get("equations", []),
            "execution_time": result.execution_time,
        }

    return 3

register_export_tools async

register_export_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]) -> int

Register export tools with FastMCP.

Registers comprehensive file export tools for SolidWorks automation supporting multiple industry-standard formats including neutral CAD formats, manufacturing formats, and documentation outputs.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config dict[str, Any]

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Example
from solidworks_mcp.tools.export import register_export_tools

tool_count = await register_export_tools(mcp, adapter, config)
print(f"Registered {tool_count} export tools")
Source code in src/solidworks_mcp/tools/export.py
async def register_export_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]
) -> int:
    """Register export tools with FastMCP.

    Registers comprehensive file export tools for SolidWorks automation supporting multiple
    industry-standard formats including neutral CAD formats, manufacturing formats, and
    documentation outputs.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (dict[str, Any]): Configuration values for the operation.

    Returns:
        int: The computed numeric result.

    Example:
                        ```python
                        from solidworks_mcp.tools.export import register_export_tools

                        tool_count = await register_export_tools(mcp, adapter, config)
                        print(f"Registered {tool_count} export tools")
                        ```
    """
    mcp = with_tool_boundary(mcp)
    tool_count = 0

    @mcp.tool()
    async def export_step(input_data: ExportFileInput) -> dict[str, Any]:
        """Write a STEP copy of the active document; it stays open on its own file.

        Use it to hand a model to another CAD system; it is the most widely read
        exchange format. SolidWorks picks the format from the file's extension, so end
        `file_path` in .step or .stp. Needs: an open, active part or assembly. Changes:
        writes the file, creating its folder and replacing a file already there; the
        open document is unchanged. Returns: `export` with `file_path` and `format`.

        Args:
            input_data: The export. `file_path` (ending in .step or .stp) is required;
                `format_type` is not needed: the file extension decides the
                format.

        Returns:
            dict[str, Any]: The file written.
        """
        input_data = _file_input(input_data, "step")
        file_path = input_data.file_path

        if hasattr(adapter, "export_step"):
            result = await adapter.export_step(input_data.model_dump())
        else:
            result = await adapter.export_file(file_path, "step")

        require_success(result, context="Failed to export STEP")
        if hasattr(adapter, "export_step"):
            return {
                "status": "success",
                "message": f"Exported to STEP format: {file_path}",
                "data": result.data,
                "execution_time": result.execution_time,
            }
        return {
            "status": "success",
            "message": f"Exported to STEP format: {file_path}",
            "export": {
                "file_path": file_path,
                "format": "STEP",
                "size_estimate": "Unknown",  # Would get actual file size
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def export_iges(input_data: ExportFileInput) -> dict[str, Any]:
        """Write a IGES copy of the active document; it stays open on its own file.

        Use it only when the receiver can't read STEP; prefer `export_step`. SolidWorks
        picks the format from the file's extension, so end `file_path` in .igs or .iges.
        Needs: an open, active part or assembly. Changes: writes the file, creating its
        folder and replacing a file already there; the open document is unchanged.
        Returns: `export` with `file_path` and `format`.

        Args:
            input_data: The export. `file_path` (ending in .igs or .iges) is required;
                `format_type` is not needed: the file extension decides the
                format.

        Returns:
            dict[str, Any]: The file written.
        """
        input_data = _file_input(input_data, "iges")
        if hasattr(adapter, "export_iges"):
            result = await adapter.export_iges(input_data.model_dump())
        else:
            result = await adapter.export_file(input_data.file_path, "iges")

        require_success(result, context="Failed to export IGES")
        if hasattr(adapter, "export_iges"):
            return {
                "status": "success",
                "message": f"Exported to IGES format: {input_data.file_path}",
                "data": result.data,
                "execution_time": result.execution_time,
            }
        return {
            "status": "success",
            "message": f"Exported to IGES format: {input_data.file_path}",
            "export": {
                "file_path": input_data.file_path,
                "format": "IGES",
                "size_estimate": "Unknown",
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def export_stl(input_data: ExportFileInput) -> dict[str, Any]:
        """Write a STL copy of the active document; it stays open on its own file.

        Use it for 3D printing. An assembly is exported whole, with lightweight
        components resolved first. SolidWorks picks the format from the file's
        extension, so end `file_path` in .stl. Needs: an open, active part or assembly.
        Changes: writes the file, creating its folder and replacing a file already
        there; the open document is unchanged. Returns: `export` with `file_path` and
        `format`, plus `warning` when SolidWorks wrote the file without its STL options
        (bodies not merged into one file).

        Args:
            input_data: The export. `file_path` (ending in .stl) is required;
                `format_type` is not needed: the file extension decides the
                format.

        Returns:
            dict[str, Any]: The file written.
        """
        input_data = _file_input(input_data, "stl")
        if hasattr(adapter, "export_stl"):
            result = await adapter.export_stl(input_data.model_dump())
        else:
            result = await adapter.export_file(input_data.file_path, "stl")

        require_success(result, context="Failed to export STL")
        if hasattr(adapter, "export_stl"):
            return {
                "status": "success",
                "message": f"Exported to STL format: {input_data.file_path}",
                "data": result.data,
                "execution_time": result.execution_time,
            }
        reply: dict[str, Any] = {
            "status": "success",
            "message": f"Exported to STL format: {input_data.file_path}",
            "export": {
                "file_path": input_data.file_path,
                "format": "STL",
                "use_case": "3D printing and rapid prototyping",
                "size_estimate": "Unknown",
            },
            "execution_time": result.execution_time,
        }
        # The adapter's data is a dict only when its fallback wrote the file
        # without the STL options.
        if isinstance(result.data, dict) and result.data.get("warning"):
            reply["warning"] = result.data["warning"]
        return reply

    @mcp.tool()
    async def export_pdf(input_data: ExportFileInput) -> dict[str, Any]:
        """Write a PDF copy of the active document; it stays open on its own file.

        Use it to share a drawing; `generate_drawing_package` (drawing toolset) already
        saves a PDF with the drawing it creates. SolidWorks picks the format from the file's extension,
        so end `file_path` in .pdf. Needs: an open, active drawing (a part or assembly
        gives a PDF of the view). Changes: writes the file, creating its folder and
        replacing a file already there; the open document is unchanged. Returns:
        `export` with `file_path` and `format`.

        Args:
            input_data: The export. `file_path` (ending in .pdf) is required;
                `format_type` is not needed: the file extension decides the
                format.

        Returns:
            dict[str, Any]: The file written.
        """
        input_data = _file_input(input_data, "pdf")
        if hasattr(adapter, "export_pdf"):
            result = await adapter.export_pdf(input_data.model_dump())
        else:
            result = await adapter.export_file(input_data.file_path, "pdf")

        require_success(result, context="Failed to export PDF")
        if hasattr(adapter, "export_pdf"):
            return {
                "status": "success",
                "message": f"Exported to PDF format: {input_data.file_path}",
                "data": result.data,
                "execution_time": result.execution_time,
            }
        return {
            "status": "success",
            "message": f"Exported to PDF format: {input_data.file_path}",
            "export": {
                "file_path": input_data.file_path,
                "format": "PDF",
                "use_case": "Documentation and sharing",
                "size_estimate": "Unknown",
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def export_dwg(input_data: ExportFileInput) -> dict[str, Any]:
        """Write a DWG copy of the active document; it stays open on its own file.

        Use it to hand a drawing to AutoCAD users. SolidWorks picks the format from the
        file's extension, so end `file_path` in .dwg. Needs: an open, active drawing.
        Changes: writes the file, creating its folder and replacing a file already
        there; the open document is unchanged. Returns: `export` with `file_path` and
        `format`.

        Args:
            input_data: The export. `file_path` (ending in .dwg) is required;
                `format_type` is not needed: the file extension decides the
                format.

        Returns:
            dict[str, Any]: The file written.
        """
        input_data = _file_input(input_data, "dwg")
        if hasattr(adapter, "export_dwg"):
            result = await adapter.export_dwg(input_data.model_dump())
        else:
            result = await adapter.export_file(input_data.file_path, "dwg")

        require_success(result, context="Failed to export DWG")
        if hasattr(adapter, "export_dwg"):
            return {
                "status": "success",
                "message": f"Exported to DWG format: {input_data.file_path}",
                "data": result.data,
                "execution_time": result.execution_time,
            }
        return {
            "status": "success",
            "message": f"Exported to DWG format: {input_data.file_path}",
            "export": {
                "file_path": input_data.file_path,
                "format": "DWG",
                "use_case": "AutoCAD compatibility",
                "size_estimate": "Unknown",
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def export_image(input_data: ExportImageInput) -> dict[str, Any]:
        """Save a picture of the active model in a standard view, as PNG or JPG, at a chosen size.

        Use it to show a result, or to check a model's shape after a change. The view is
        set first (front, back, left, right, top, bottom, isometric, dimetric,
        trimetric, or current to keep the user's view). Needs: an open, active document.
        Changes: writes the image file, and turns the model to the view asked for.
        Returns: `data` with `file_path`, `format`, `dimensions` (the size written),
        `requested_dimensions` and `view`.

        Args:
            input_data: The image. `file_path` (or `output_path`) is needed; the
                format follows its extension. `width`/`height` (or `resolution` as
                "1920x1080") and `view_orientation` (isometric) have defaults.

        Returns:
            dict[str, Any]: The image written.
        """
        input_data = normalize_input(input_data, ExportImageInput)
        if hasattr(adapter, "export_image"):
            result = await adapter.export_image(input_data.model_dump())
            require_success(result, "Failed to export image")
            return {
                "status": "success",
                "message": f"Exported image: {input_data.file_path}",
                "data": result.data,
                "execution_time": result.execution_time,
            }

        if hasattr(adapter, "export_file") and input_data.file_path:
            result = await adapter.export_file(  # type: ignore[assignment]
                input_data.file_path, input_data.format_type
            )
            require_success(result, "Failed to export image")
            return {
                "status": "success",
                "message": f"Exported image: {input_data.file_path}",
                "export": {
                    "file_path": input_data.file_path,
                    "format": input_data.format_type.upper(),
                    "dimensions": f"{input_data.width}x{input_data.height}",
                    "view": input_data.view_orientation,
                    "use_case": "Documentation and presentations",
                },
                "execution_time": result.execution_time,
            }

        raise UnsupportedOperationError(
            "Active adapter does not support export_image or "
            "export_file; no image was exported."
        )

    tool_count = 6  # Number of tools registered
    return tool_count

register_file_management_tools async

register_file_management_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]) -> int

Register file management tools with FastMCP.

Registers essential file operations for SolidWorks document management including save operations, file format conversions, and file property access. These tools provide fundamental document lifecycle management capabilities.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config dict[str, Any]

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Example
from solidworks_mcp.tools.file_management import register_file_management_tools

tool_count = await register_file_management_tools(mcp, adapter, config)
print(f"Registered {tool_count} file management tools")
            Note:
                File management tools require an active SolidWorks document.
                Save operations preserve the current document state and metadata.
Source code in src/solidworks_mcp/tools/file_management.py
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
async def register_file_management_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]
) -> int:
    """Register file management tools with FastMCP.

    Registers essential file operations for SolidWorks document management including save
    operations, file format conversions, and file property access. These tools provide
    fundamental document lifecycle management capabilities.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (dict[str, Any]): Configuration values for the operation.

    Returns:
        int: The computed numeric result.

    Example:
                        ```python
                        from solidworks_mcp.tools.file_management import register_file_management_tools

                        tool_count = await register_file_management_tools(mcp, adapter, config)
                        print(f"Registered {tool_count} file management tools")
                        ```

                    Note:
                        File management tools require an active SolidWorks document.
                        Save operations preserve the current document state and metadata.
    """
    mcp = with_tool_boundary(mcp)
    tool_count = 0

    def _coerce_input(model_cls: Any, payload: Any) -> Any:
        """Accept legacy dict payloads from compatibility wrapper as well as model instances.

        Args:
            model_cls (Any): The model cls value.
            payload (Any): The payload value.

        Returns:
            Any: The result produced by the operation.
        """
        return (
            payload
            if isinstance(payload, model_cls)
            else model_cls.model_validate(payload)
        )

    def _result_value(payload: Any, *keys: str, default: Any = None) -> Any:
        """Read a value from adapter result payloads that may be dicts or model objects.

        Args:
            payload (Any): The payload value.
            *keys (str): Additional positional arguments forwarded to the call.
            default (Any): Fallback value returned when the operation fails. Defaults to None.

        Returns:
            Any: The result produced by the operation.
        """
        if payload is None:
            return default

        if isinstance(payload, dict):
            for key in keys:
                if key in payload and payload[key] is not None:
                    return payload[key]
            return default

        for key in keys:
            value = getattr(payload, key, None)
            if value is not None:
                return value
        return default

    def _prepare_save_target(
        raw_path: str,
        *,
        required_extension: str,
        overwrite: bool,
        refuse_wrong_extension: bool = False,
    ) -> str:
        """Normalize and validate a save target before adapter/COM calls.

        Enforces pre-save guardrails used by save convenience tools so invalid paths are
        rejected consistently in both mock and real adapter modes. The helper performs:
        path trimming, extension normalization, parent-directory existence check,
        directory writability check, and overwrite policy enforcement.

        Args:
            raw_path (str): User-supplied path where the file should be saved.
            required_extension (str): Required extension for the target document type
                (for example, ``.sldprt`` or ``.sldasm``).
            overwrite (bool): Whether saving is allowed to replace an existing file.
            refuse_wrong_extension (bool): Refuse a path with another extension
                instead of changing it to ``required_extension``.

        Returns:
            str: The normalized save path.

        Raises:
            InvalidInputError: When the path is empty, has the wrong extension, its
                directory is missing or not writable, or it exists and overwrite is
                False.

        Example:
            ```python
            target = _prepare_save_target(
                r"C:/Exports/bracket.step",
                required_extension=".sldprt",
                overwrite=False,
            )

            # target == "C:/Exports/bracket.sldprt" when valid; raises
            # InvalidInputError when the directory is missing/not writable,
            # or when the target exists and overwrite is False.
            ```

        Note:
            Validation is intentionally executed before adapter calls to avoid opaque COM
            failures and to provide deterministic, user-friendly error messages.
        """
        file_path = raw_path.strip()
        if not file_path:
            raise InvalidInputError("Invalid file_path: path is empty or whitespace.")

        cleaned = file_path.strip()
        extension_name = required_extension.lstrip(".")
        if (
            cleaned.count(".") == 1
            and cleaned.startswith(".")
            and cleaned[1:].lower() == extension_name
        ):
            # Reject extension-only input such as ".sldprt".
            raise InvalidInputError(
                "Invalid file_path: missing base filename before extension."
            )

        target = Path(file_path)
        if target.suffix.lower() != required_extension.lower():
            if refuse_wrong_extension:
                raise InvalidInputError(
                    f"file_path must end in {required_extension} for the active "
                    f"document; got {target.name!r}. Nothing was saved."
                )
            # Normalize extension so save_part/save_assembly always use their canonical type.
            target = target.with_suffix(required_extension)

        target_dir = target.parent
        if not target_dir.exists():
            raise InvalidInputError(f"Target directory does not exist: {target_dir}")

        if not target_dir.is_dir():
            raise InvalidInputError(
                f"Target parent path is not a directory: {target_dir}"
            )

        if not os.access(target_dir, os.W_OK):
            raise InvalidInputError(f"Target directory is not writable: {target_dir}")

        if target.exists() and target.is_dir():
            raise InvalidInputError(
                f"Target path is a directory, expected file path: {target}"
            )

        if target.exists() and not overwrite:
            raise InvalidInputError(
                f"File already exists and overwrite=False: {target}. Set overwrite=True to replace it."
            )

        return str(target)

    async def _checked_new_path(raw_path: str, *, overwrite: bool) -> str:
        """Check a new save path against the active document's type.

        Args:
            raw_path: The path the caller gave.
            overwrite: Whether a file already there may be replaced.

        Returns:
            str: The path to save to.

        Raises:
            ToolOperationError: If the active document's type can't be read.
            InvalidInputError: If the path fails a check (see
                ``_prepare_save_target``).
        """
        info = await adapter.get_model_info()
        doc_type = (
            info.data.get("type")
            if info.is_success and isinstance(info.data, dict)
            else None
        )
        extension = _DOC_EXTENSIONS.get(str(doc_type))
        if extension is None:
            raise ToolOperationError(
                "Could not tell the active document's type "
                f"({info.error or doc_type!r}), so file_path was not checked and "
                "nothing was saved."
            )
        return _prepare_save_target(
            raw_path,
            required_extension=extension,
            overwrite=overwrite,
            refuse_wrong_extension=True,
        )

    def _get_attr_or_call(obj: Any, name: str, default: Any = None) -> Any:
        """Read COM values that may be exposed as either properties or methods."""
        candidate = getattr(obj, name, None)
        if candidate is None:
            return default
        if callable(candidate):
            try:
                value = candidate()
            except Exception:
                return default
            return default if value is None else value
        return candidate

    def _copy_with_collision_handling(source_path: Path, target_dir: Path) -> Path:
        """Copy a source file into target_dir, suffixing when name collisions occur."""
        destination = target_dir / source_path.name
        try:
            same_target = destination.exists() and source_path.resolve(
                strict=False
            ) == destination.resolve(strict=False)
        except Exception:
            same_target = False
        if same_target:
            return destination

        if not destination.exists():
            shutil.copy2(source_path, destination)
            return destination

        stem = source_path.stem
        suffix = source_path.suffix
        index = 1
        while True:
            candidate = target_dir / f"{stem}_{index}{suffix}"
            if not candidate.exists():
                shutil.copy2(source_path, candidate)
                return candidate
            index += 1

    def _copy_active_assembly_with_references(
        target_assembly_path: str,
    ) -> dict[str, Any]:
        """Copy active assembly and resolved dependencies into target folder."""
        current_model = getattr(adapter, "currentModel", None)
        if current_model is None:
            raise ToolOperationError("No active model available to copy references.")

        source_assembly_raw = _get_attr_or_call(current_model, "GetPathName")
        source_assembly = (
            Path(str(source_assembly_raw)) if source_assembly_raw else None
        )
        if source_assembly is None or not source_assembly.exists():
            raise ToolOperationError(
                "Active assembly must be saved before include_references=True can copy dependencies."
            )

        target_assembly = Path(target_assembly_path)
        target_dir = target_assembly.parent
        target_dir.mkdir(parents=True, exist_ok=True)

        dependency_callable = getattr(current_model, "GetDependencies2", None)
        if not callable(dependency_callable):
            raise ToolOperationError(
                "Adapter does not expose GetDependencies2; cannot copy assembly references in this mode."
            )

        try:
            raw_dependencies = dependency_callable(True, True, False)
        except Exception as exc:
            raise ToolOperationError(
                f"Failed to enumerate assembly dependencies: {exc}"
            ) from exc

        unique_sources: list[Path] = [source_assembly]
        for dep in dependency_paths(raw_dependencies):
            if dep not in unique_sources:
                unique_sources.append(dep)

        copied_files: list[str] = []
        missing_sources: list[str] = []

        for source in unique_sources:
            if not source.exists():
                missing_sources.append(str(source))
                continue

            if source.resolve(strict=False) == source_assembly.resolve(strict=False):
                shutil.copy2(source, target_assembly)
                copied_files.append(str(target_assembly))
            else:
                copied_target = _copy_with_collision_handling(source, target_dir)
                copied_files.append(str(copied_target))

        copied_files = sorted(copied_files)
        return {
            "status": "success",
            "message": f"Assembly and references copied to: {target_dir}",
            "file_path": str(target_assembly),
            "copied_file_count": len(copied_files),
            "copied_files": copied_files,
            "missing_source_files": missing_sources,
            "copy_method": "tool_dependency_copy",
        }

    @mcp.tool()
    async def save_file(input_data: SaveFileInput) -> dict[str, Any]:
        """Save the active part, assembly or drawing, to its own path or to a new path in its own format.

        Use it after changes you want to keep. A new `file_path` is checked before
        SolidWorks is asked: its folder must exist and be writable, it must end in
        the document's extension (.sldprt, .sldasm, .slddrw), and a file already there
        is kept unless `overwrite` is on. To write another format, use `save_as` or an
        `export_*` tool; to copy an assembly with its parts, `pack_and_go_assembly`.
        Needs: an open, active document, already saved once unless you give a path.
        Changes: writes the file. Returns: `file_path` (when given) and `data` from
        the save.

        Args:
            input_data: The save. Nothing is required: give `file_path` to save to a
                new path, or omit it to save in place; `overwrite` is off.

        Returns:
            dict[str, Any]: The save result.
        """
        input_data = _coerce_input(SaveFileInput, input_data)
        require_capability(
            adapter,
            "save_file",
            "Active adapter does not support save_file; the file was not saved.",
        )
        target = input_data.file_path
        if target is not None:
            target = await _checked_new_path(target, overwrite=input_data.overwrite)
        result = await adapter.save_file(target)
        require_success(result, "Failed to save file")
        return {
            "status": "success",
            "message": "File saved successfully",
            **({"file_path": target} if target is not None else {}),
            "data": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def save_as(input_data: SaveAsInput) -> dict[str, Any]:
        """Save the active document under a new path, as a SolidWorks file or in an exchange format such as STEP or IGES.

        For a SolidWorks format (solidworks, sldprt, sldasm, slddrw) it is File >
        Save As: the open document becomes the new file, and open assemblies that use
        it follow it to the new name. With `as_copy` it writes a copy instead and the
        original stays the open, referenced document. Any other `format_type` is an
        export: it writes a copy, and the open document keeps its own file. For one known format with its options, prefer
        `export_step`, `export_stl` and the other `export_*` tools. Needs: an open,
        active document. Changes: writes the file. A file already at `file_path` is
        kept and the call refused, unless `overwrite` is on. Returns: `file_path` and
        `format`; with `as_copy`, `current_path` and `kept_original`.

        Args:
            input_data: The target. `file_path` is required; `format_type` defaults
                to "solidworks", and `overwrite` and `as_copy` are off.

        Returns:
            dict[str, Any]: The path written.
        """
        input_data = _coerce_input(SaveAsInput, input_data)
        if Path(input_data.file_path).exists() and not input_data.overwrite:
            raise InvalidInputError(
                f"A file already exists at {input_data.file_path}; it was left "
                "untouched. Pass overwrite: true to replace it, or choose another path."
            )
        if hasattr(adapter, "save_file") and input_data.format_type.lower() in {
            "solidworks",
            "sldprt",
            "sldasm",
            "slddrw",
        }:
            if input_data.as_copy:
                copied = await adapter.save_copy(input_data.file_path)
                require_success(copied, "Failed to save a copy")
                return {
                    "status": "success",
                    "message": f"Copy saved as: {input_data.file_path}",
                    "file_path": input_data.file_path,
                    "format": input_data.format_type,
                    **(copied.data or {}),
                    "execution_time": copied.execution_time,
                }
            result = await adapter.save_file(input_data.file_path)
            require_success(result, "Failed to save file")
            return {
                "status": "success",
                "message": f"File saved as: {input_data.file_path}",
                "file_path": input_data.file_path,
                "format": input_data.format_type,
                "execution_time": result.execution_time,
            }

        require_capability(
            adapter,
            "export_file",
            "Active adapter does not support save_file or export_file; "
            f"no file was saved as {input_data.file_path}.",
        )
        result = await adapter.export_file(
            input_data.file_path,
            input_data.format_type,
        )
        require_success(result, "Failed to export file")
        return {
            "status": "success",
            "message": f"File exported as: {input_data.file_path}",
            "file_path": input_data.file_path,
            "format": input_data.format_type,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def save_as_template(input_data: SaveAsTemplateInput) -> dict[str, Any]:
        """Save the active part, assembly or drawing as a template, so new documents start from it.

        Use it after setting up a document once (units with `set_units`, a material,
        custom properties) to start the next one the same way: pass the template to
        `create_part`, `create_assembly` or `create_drawing` as `template`. A part
        saves as .prtdot, an assembly as .asmdot, a drawing as .drwdot. Give
        `file_path`, or `name` to save in SolidWorks' first template folder so it also
        shows in File > New. Afterwards SolidWorks has the template open in place of
        the document, as File > Save As does (a saved document's file is left as last
        saved); the reply's `warning` says so. Save from a part with no features when only its settings should carry
        over. The path is checked before SolidWorks is asked: the extension must
        match, the folder must exist, and a file already there is kept unless
        `overwrite` is on. Needs: an open, active document. Changes: writes the
        template. Returns: `data` with `file_path`, `document_type`,
        `in_template_folder`, `template_folders` and `document_became_template`.

        Args:
            input_data: `file_path` or `name` (one is required); `overwrite` is off.

        Returns:
            dict[str, Any]: The template written.
        """
        input_data = _coerce_input(SaveAsTemplateInput, input_data)
        require_capability(
            adapter,
            "save_as_template",
            "Active adapter does not support save_as_template; nothing was saved.",
        )
        found = await adapter.get_template_folders()
        require_success(found, "Failed to read SolidWorks' template folders")
        extension = (found.data or {}).get("extension")
        if extension is None:
            raise ToolOperationError(
                "No active model: open or create the document to save as a template."
            )
        raw = input_data.file_path
        if input_data.name is not None:
            folders = (found.data or {}).get("folders") or []
            if not folders:
                raise InvalidInputError(
                    "SolidWorks lists no document template folder (Options > File "
                    "Locations > Document Templates); give file_path instead."
                )
            stem = input_data.name
            if stem.lower().endswith(extension):
                stem = stem[: -len(extension)]
            raw = os.path.join(folders[0], stem + extension)
        target = _prepare_save_target(
            str(raw),
            required_extension=extension,
            overwrite=input_data.overwrite,
            refuse_wrong_extension=True,
        )
        result = await adapter.save_as_template(target)
        require_success(result, "Failed to save the template")
        data = result.data or {}
        reply: dict[str, Any] = {
            "status": "success",
            "message": f"Saved the {data.get('document_type', 'document')} template "
            f"{target}; start a new one from it with template: {target!r}",
            "file_path": target,
            "data": data,
            "execution_time": result.execution_time,
        }
        if data.get("document_became_template"):
            original = data.get("document_path")
            kept = (
                f"{original} is unchanged on disk, as last saved."
                if original
                else "The document had never been saved, so it now exists only "
                "as this template."
            )
            reply["warning"] = (
                f"SolidWorks now has the template {target} open in place of the "
                f"document, as File > Save As does, so save_file would change the "
                f"template. {kept} Start the next part with create_part and this "
                "template."
            )
        return reply

    @mcp.tool()
    async def get_file_properties() -> dict[str, Any]:
        """Report the active document's file details: name, path, size, dates, type, configuration and unsaved changes.

        Use it to check what is open and whether it has unsaved changes before saving or
        closing. For the model itself (rebuild status included), use `get_model_info`;
        for custom properties, use `get_custom_properties`. Needs: an open, active
        document. Read-only. Returns: `properties` with `file_name`, `file_path`,
        `file_size_bytes`, `created_date` and `modified_date` (Unix timestamps; empty
        for a document never saved), `document_type`, `configuration`, `is_dirty` and
        `feature_count`.
        """
        require_capability(
            adapter, "get_model_info", "Active adapter does not support model metadata"
        )

        result = await adapter.get_model_info()
        if not result.is_success or not isinstance(result.data, dict):
            raise ToolOperationError(result.error or "No active SolidWorks document")

        model_info = dict(result.data)
        raw_path = str(model_info.get("path") or "")
        file_path = Path(raw_path) if raw_path else None
        properties: dict[str, Any] = {
            "file_name": (
                file_path.name
                if file_path is not None
                else str(model_info.get("title") or "")
            ),
            "file_path": raw_path,
            "file_size_bytes": None,
            "created_date": None,
            "modified_date": None,
            "document_type": model_info.get("type"),
            "configuration": model_info.get("configuration"),
            "is_dirty": model_info.get("is_dirty"),
            "feature_count": model_info.get("feature_count"),
        }

        details = file_details(raw_path)
        properties.update(
            {
                "file_size_bytes": details["size_bytes"],
                "created_date": details["created"],
                "modified_date": details["modified"],
            }
        )

        return {
            "status": "success",
            "properties": properties,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def get_model_info() -> dict[str, Any]:
        """Report the active document's title, path, type, configuration, unsaved changes, feature count, rebuild status and file details.

        Use it first in a session, to see which document is active (it follows a switch
        made in the SolidWorks window). For every open document, use
        `list_open_documents`; for custom properties, `get_custom_properties`. Needs: an
        open document. Read-only. Returns: `model_info` with `title`, `path`, `type`,
        `configuration`, `is_dirty`, `feature_count`, `rebuild_status` (0 rebuilt, 1
        needs a rebuild), and the file's `size_bytes`, `created` and `modified` (Unix
        timestamps; null for a document never saved).
        """
        require_capability(
            adapter, "get_model_info", "Active adapter does not support get_model_info"
        )
        result = await adapter.get_model_info()
        require_success(result, "Failed to get model info")
        info = dict(result.data or {})
        info.update(file_details(str(info.get("path") or "")))
        return {
            "status": "success",
            "model_info": info,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def list_features(input_data: ListFeaturesInput) -> dict[str, Any]:
        """List the feature tree of the active part or assembly, in order.

        Use it before editing a model, to find a feature's exact name for
        `delete_feature`, `suppress_feature` or `rename_feature`. For an assembly it
        also lists each component's features, down to `max_assembly_depth` levels.
        Needs: an open part or assembly. Read-only. Returns: `features` (each with
        `name`, `type`, `suppressed` and, in an assembly, `component`), `count`, and
        `assembly_tree`, the same list grouped by component.

        Args:
            input_data: What to list. Nothing is required: `include_suppressed`
                (off) and `max_assembly_depth` (2) have defaults.

        Returns:
            dict[str, Any]: The feature list.
        """
        input_data = _coerce_input(ListFeaturesInput, input_data)
        require_capability(
            adapter, "list_features", "Active adapter does not support list_features"
        )
        result = await adapter.list_features(
            include_suppressed=input_data.include_suppressed,
            max_assembly_depth=input_data.max_assembly_depth,
        )
        require_success(result, "Failed to list features")
        return {
            "status": "success",
            "features": result.data or [],
            "count": len(result.data or []),
            # Additive, derived view of the same flat "features"
            # list above - grouped by component/sub-component
            # for callers that want the assembly structure
            # rather than a flat tag-and-scan. Does not change
            # the meaning of "features"/"count".
            "assembly_tree": build_component_tree(result.data),
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def get_selected_entities() -> dict[str, Any]:
        """Describe the faces, edges and vertices selected in the SolidWorks window right now.

        Use it when the user has clicked something and says "this face": it reads the
        selection instead of guessing from a screenshot. To select edges yourself, use
        `select_face_edges`. Needs: an open document. Read-only. Returns: `entities`
        (each with `index` and `type`; a face adds `bounding_box_mm`, `center_mm`,
        `area_mm2` and `normal` when flat, an edge `start_mm`, `end_mm` and
        straight-line `length_mm`, a vertex `point_mm`) and `count`.
        """
        require_capability(
            adapter,
            "get_selected_entities",
            "Active adapter does not support get_selected_entities",
        )
        result = await adapter.get_selected_entities()
        require_success(result, "Failed to get selected entities")
        return {
            "status": "success",
            "entities": result.data or [],
            "count": len(result.data or []),
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def classify_feature_tree(
        input_data: ClassifyFeatureTreeInput,
    ) -> dict[str, Any]:
        """Classify the active model's kind (part, sheet metal, assembly, drawing and so on) from its feature tree, and suggest how to work on it.

        Use it before rebuilding or editing an unfamiliar model, to pick a workflow. It
        reads the model unless you pass `model_info` and `features` you already have
        (from `get_model_info` and `list_features`). Needs: an open document, unless
        both are passed. Read-only. Returns: `classification` with `family`,
        `recommended_workflow`, `confidence`, `needs_vba`, `evidence`, `warnings`,
        `next_actions` and `feature_count`, plus the `model_info` and `features` used.

        Args:
            input_data: What to classify. Nothing is required: pass `model_info`
                and `features` to classify them without reading the model;
                `include_suppressed` is off by default.

        Returns:
            dict[str, Any]: The classification.
        """
        input_data = _coerce_input(ClassifyFeatureTreeInput, input_data)
        model_info = input_data.model_info
        features = input_data.features
        execution_time = 0.0

        # Reading the model can fail (nothing open); classifying an empty
        # model instead would give a confident wrong answer.
        if model_info is None:
            require_capability(
                adapter,
                "get_model_info",
                "Active adapter does not support get_model_info",
            )
            model_result = await adapter.get_model_info()
            require_success(
                model_result, context="Failed to read the model to classify"
            )
            model_info = model_result.data
            execution_time = max(execution_time, model_result.execution_time or 0.0)

        if features is None:
            require_capability(
                adapter,
                "list_features",
                "Active adapter does not support list_features",
            )
            feature_result = await adapter.list_features(
                include_suppressed=input_data.include_suppressed
            )
            require_success(
                feature_result, context="Failed to list features to classify"
            )
            features = feature_result.data or []
            execution_time = max(execution_time, feature_result.execution_time or 0.0)

        classification = classify_feature_tree_snapshot(model_info, features or [])  # type: ignore[arg-type]
        return {
            "status": "success",
            "classification": classification,
            "model_info": model_info or {},
            "features": features or [],
            "execution_time": execution_time,
        }

    @mcp.tool()
    async def list_configurations() -> dict[str, Any]:
        """List the names of the active model's configurations, and which one is active.

        Use it to see which configurations a part or assembly has before
        `activate_configuration` or `delete_configuration`. Needs: an open part or
        assembly. Read-only. Returns: `configurations` (names), `count` and
        `active` (null when it can't be read).
        """
        require_capability(
            adapter,
            "list_configurations",
            "Active adapter does not support list_configurations",
        )
        result = await adapter.list_configurations()
        require_success(result, "Failed to list configurations")
        active: str | None = None
        if callable(getattr(adapter, "active_configuration", None)):
            read = await adapter.active_configuration()
            active = read.data if read.is_success else None
        return {
            "status": "success",
            "configurations": result.data or [],
            "count": len(result.data or []),
            "active": active,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def load_part(input_data: LoadPartInput) -> dict[str, Any]:
        """Open a part (.sldprt) from disk and make it the active document.

        Use it to start work on an existing part; for any document type, use
        `open_model`, and to switch to a document that is already open, use
        `activate_document`. A path that isn't a .sldprt is refused. Needs: the file on
        disk. Changes: opens the part (no file is written). Returns: `model` with
        `name`, `type`, `path` and `configuration` (the one SolidWorks opened, or
        null when it can't be read).

        Args:
            input_data: The part. `file_path` (a .sldprt) is required.

        Returns:
            dict[str, Any]: The opened part.
        """
        input_data = _coerce_input(LoadPartInput, input_data)
        # Ensure file path ends with .sldprt
        file_path = input_data.file_path
        if not file_path.lower().endswith(".sldprt"):
            raise InvalidInputError(f"File must be a .sldprt part file: {file_path}")

        result = await adapter.open_model(file_path)
        require_success(result, context="Failed to load part")
        model = result.data
        title = _result_value(model, "title", "name", default=file_path)
        path = _result_value(model, "path", "file_path", default=file_path)
        configuration = _result_value(model, "configuration")
        return {
            "status": "success",
            "message": f"Loaded part: {title}",
            "model": {
                "name": title,
                "type": "Part",
                "path": path,
                "configuration": configuration,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def load_assembly(input_data: LoadAssemblyInput) -> dict[str, Any]:
        """Open an assembly (.sldasm) from disk and make it the active document.

        Use it to start work on an existing assembly; for any document type, use
        `open_model`, and to switch to a document that is already open, use
        `activate_document`. A path that isn't a .sldasm is refused. Needs: the file on
        disk. Changes: opens the assembly (no file is written). Returns: `model` with
        `name`, `type`, `path` and `configuration` (the one SolidWorks opened, or
        null when it can't be read).

        Args:
            input_data: The assembly. `file_path` (a .sldasm) is required.

        Returns:
            dict[str, Any]: The opened assembly.
        """
        input_data = _coerce_input(LoadAssemblyInput, input_data)
        # Ensure file path ends with .sldasm
        file_path = input_data.file_path
        if not file_path.lower().endswith(".sldasm"):
            raise InvalidInputError(
                f"File must be a .sldasm assembly file: {file_path}"
            )

        result = await adapter.open_model(file_path)
        require_success(result, context="Failed to load assembly")
        model = result.data
        title = _result_value(model, "title", "name", default=file_path)
        path = _result_value(model, "path", "file_path", default=file_path)
        configuration = _result_value(model, "configuration")
        return {
            "status": "success",
            "message": f"Loaded assembly: {title}",
            "model": {
                "name": title,
                "type": "Assembly",
                "path": path,
                "configuration": configuration,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def save_part(input_data: SavePartInput | None = None) -> dict[str, Any]:
        """Save the active part in place, or as a new .sldprt with the target path checked first.

        Before writing to a new path it checks the folder exists and is writable,
        changes the extension to .sldprt, and refuses an existing file unless
        `overwrite` is on. For other formats, use `save_as` or an `export_*` tool. Needs: an open, active part. Changes: writes
        the part file. Returns: `file_path` when saved to a new path.

        Args:
            input_data: The save. Nothing is required: give `file_path` for a new
                path or omit it to save in place; `overwrite` is off.

        Returns:
            dict[str, Any]: The save result.
        """
        if input_data is None:
            input_data = SavePartInput()
        else:
            input_data = _coerce_input(SavePartInput, input_data)

        # If file_path provided, use save_as; otherwise use regular save
        if input_data.file_path:
            file_path = _prepare_save_target(
                input_data.file_path,
                required_extension=".sldprt",
                overwrite=input_data.overwrite,
            )

            result = await adapter.save_file(file_path)
            require_success(result, context="Failed to save part")
            return {
                "status": "success",
                "message": f"Part saved as: {file_path}",
                "file_path": file_path,
                "execution_time": result.execution_time,
            }
        else:
            # Save to current location
            result = await adapter.save_file(None)
            require_success(result, context="Failed to save part")
            return {
                "status": "success",
                "message": "Part saved successfully",
                "execution_time": result.execution_time,
            }

    @mcp.tool()
    async def save_assembly(
        input_data: SaveAssemblyInput | None = None,
    ) -> dict[str, Any]:
        """Save the active assembly in place, or as a new .sldasm, optionally copying the parts it uses alongside.

        Before writing to a new path it checks the folder exists and is writable,
        changes the extension to .sldasm, and refuses an existing file unless
        `overwrite` is on. With `include_references`, it instead copies the assembly
        file as last saved, and the files it references, into the new folder; unsaved
        changes are left out and references are not updated. To copy an assembly with
        its referenced parts, use `pack_and_go_assembly`. Needs: an open, active assembly, saved once for
        `include_references`. Changes: writes the assembly file (and copies). Returns:
        `file_path`, plus `copied_files` and `missing_source_files` when copying
        references.

        Args:
            input_data: The save. Nothing is required: give `file_path` for a new
                path or omit it to save in place; `overwrite` and
                `include_references` are off.

        Returns:
            dict[str, Any]: The save result.
        """
        if input_data is None:
            input_data = SaveAssemblyInput()
        else:
            input_data = _coerce_input(SaveAssemblyInput, input_data)

        # If file_path provided, use save_as; otherwise use regular save
        if input_data.file_path:
            file_path = _prepare_save_target(
                input_data.file_path,
                required_extension=".sldasm",
                overwrite=input_data.overwrite,
            )

            if input_data.include_references:
                return _copy_active_assembly_with_references(file_path)

            result = await adapter.save_file(file_path)
            require_success(result, context="Failed to save assembly")
            return {
                "status": "success",
                "message": f"Assembly saved as: {file_path}",
                "file_path": file_path,
                "execution_time": result.execution_time,
            }
        else:
            # Save to current location
            result = await adapter.save_file(None)
            require_success(result, context="Failed to save assembly")
            return {
                "status": "success",
                "message": "Assembly saved successfully",
                "execution_time": result.execution_time,
            }

    @mcp.tool()
    async def pack_and_go_assembly(input_data: PackAndGoInput) -> dict[str, Any]:
        """Copy an assembly and every file it uses into one folder that opens on its own.

        Use it to hand over or archive an assembly. SolidWorks lists the files the
        assembly uses; each is copied under its own name, as saved on disk, so save
        first: a `warning` names any open file whose unsaved changes the copy doesn't
        have. The copy opens with its own parts, since SolidWorks looks in the
        assembly's folder first. It fails, copying nothing, when a file is missing,
        two files share a name, or `target_dir` holds the originals. It refuses a
        folder that already has files unless `overwrite` is on, and then replaces only
        the files it copies. It never closes or changes a document. It can add an
        isometric PNG preview. Needs: the .sldasm on disk. Changes: writes the copies
        into `target_dir`, creating it if needed. Returns: `source_assembly`,
        `target_dir`, `copied_files`, `source_files`, `unsaved_changes` and, with a
        preview, `preview_image`.

        Args:
            input_data: The copy. `source_path` (a .sldasm) and `target_dir` are
                required; `export_preview` and `overwrite` are off.

        Returns:
            dict[str, Any]: The files copied.
        """
        input_data = _coerce_input(PackAndGoInput, input_data)

        from pathlib import Path as _Path

        source = _Path(input_data.source_path)
        out_dir = _Path(input_data.target_dir)

        if not source.exists():
            raise ToolOperationError(f"Source assembly not found: {source}")

        if not source.suffix.lower() == ".sldasm":
            raise InvalidInputError(
                f"source_path must be a .sldasm file, got: {source.suffix}"
            )

        if not input_data.overwrite and out_dir.exists() and any(out_dir.iterdir()):
            raise InvalidInputError(
                f"target_dir already contains files: {out_dir}. "
                "Set overwrite=True to replace them."
            )

        result = await adapter.pack_and_go_assembly(
            source_path=str(source),
            target_dir=str(out_dir),
        )

        require_success(result)
        packed = result.data or {}

        report: dict[str, Any] = {
            "status": "success",
            "method": "dependency_copy",
            "execution_time": result.execution_time,
            **packed,
        }
        warnings: list[str] = []
        unsaved = packed.get("unsaved_changes") or []
        if unsaved:
            warnings.append(
                f"{', '.join(unsaved)} had unsaved changes, which are not in the "
                "copy; save, then pack again to include them."
            )

        if input_data.export_preview:
            preview_path = out_dir / "assembly_preview.png"
            img_result = await adapter.export_image(
                {
                    "file_path": str(preview_path),
                    "format_type": "png",
                    "width": 1600,
                    "height": 1000,
                    "view_orientation": "isometric",
                }
            )
            report["preview_image"] = (
                str(preview_path) if img_result.is_success else None
            )
            if not img_result.is_success:
                warnings.append(
                    f"The copy is complete, but the preview couldn't be written: "
                    f"{img_result.error}"
                )

        if warnings:
            report["warning"] = " ".join(warnings)
        return report

    @mcp.tool()
    async def list_open_documents() -> dict[str, Any]:
        """List every document open in SolidWorks and say which one is active.

        Use it before `activate_document`, or when tool calls seem to act on the wrong
        document. Needs: SolidWorks running. Read-only. Returns: `documents` (each with
        `title`, `path`, `type` and `is_active`) and `count`.
        """
        result = await adapter.list_open_documents()
        require_success(result, context="Failed to list open documents")
        docs = result.data if isinstance(result.data, list) else []
        active = next((d.get("title") for d in docs if d.get("is_active")), None)
        return {
            "status": "success",
            "message": (
                f"{len(docs)} document(s) open"
                + (f"; active: {active}" if active else "")
            ),
            "documents": docs,
            "count": len(docs),
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def activate_document(
        input_data: ActivateDocumentInput,
    ) -> dict[str, Any]:
        """Make an already-open document the active one, so the next tools act on it.

        Match it by window title, full path or file name (not case-sensitive); see
        `list_open_documents`. To open a file that isn't open yet, use `open_model`.
        Needs: the document open in SolidWorks. Changes: the active document. Returns:
        `data` with `activated` (the title) and `verified` (read back from SolidWorks;
        null when it couldn't be read). No match is an error that lists the open
        documents.

        Args:
            input_data: The document. `title_or_path` is required.

        Returns:
            dict[str, Any]: The activated document.
        """
        input_data = _coerce_input(ActivateDocumentInput, input_data)
        result = await adapter.activate_document(input_data.title_or_path)
        require_success(result, context="Failed to activate document")
        data = result.data if isinstance(result.data, dict) else {}
        activated = data.get("activated", input_data.title_or_path)
        message = f"Activated {activated}"
        if data.get("verified") is None:
            message += " (could not be read back to confirm)"
        return {
            "status": "success",
            "message": message,
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def save_body_as_part(
        input_data: SaveBodyAsPartInput,
    ) -> dict[str, Any]:
        """Save one solid body of the active multibody part as a new part file (Insert > Features > Save Bodies).

        Use it to split a multibody part into parts, e.g. after `create_extrusion`
        without merging. Needs: an open, active part with the named body and an existing
        target folder. Changes: adds a Save Bodies feature to the part and writes the
        new .sldprt; closes any new, unsaved assembly SolidWorks opens alongside.
        Returns: `data` with `body`, `file_path`, `feature`, `solid_bodies` (every
        body name, to correct a wrong one) and `closed_documents`.

        Args:
            input_data: The body. `body_name` and `file_path` (a .sldprt) are
                required.

        Returns:
            dict[str, Any]: The file written.
        """
        require_capability(
            adapter,
            "save_body_as_part",
            "Active adapter does not support save_body_as_part; no file was written.",
        )
        input_data = _coerce_input(SaveBodyAsPartInput, input_data)
        result = await adapter.save_body_as_part(
            input_data.body_name, input_data.file_path
        )
        require_success(result, context="Failed to save body as part")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": (
                f"Saved body {input_data.body_name} to "
                f"{data.get('file_path', input_data.file_path)}"
            ),
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_configuration(
        input_data: CreateConfigurationInput,
    ) -> dict[str, Any]:
        """Add a configuration to the active part or assembly, e.g. a long and a short version of one part.

        A configuration holds its own dimension values and suppressed features;
        give `parent` to derive it from another. The configuration that was active
        stays active unless `activate` is on. Needs: an open part or assembly.
        Changes: adds a configuration. Returns: `configuration` with `name`,
        `parent`, `active` and every name in `configurations`.

        Args:
            input_data: The configuration. `name` is required; `parent`,
                `description` and `activate` are optional.

        Returns:
            dict[str, Any]: The configuration added.
        """
        input_data = normalize_input(input_data, CreateConfigurationInput)
        require_capability(
            adapter, "create_configuration", "Active adapter can't add configurations"
        )
        result = await adapter.create_configuration(
            input_data.name,
            input_data.parent,
            input_data.description,
            input_data.activate,
        )
        require_success(result, "Failed to create configuration")
        return {
            "status": "success",
            "message": f"Added configuration {input_data.name}",
            "configuration": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def activate_configuration(
        input_data: ConfigurationNameInput,
    ) -> dict[str, Any]:
        """Make a configuration of the active model the active one, and rebuild.

        Dimensions and suppression then read and change in that configuration.
        Needs: an open part or assembly with that configuration. Changes: the
        active configuration. Returns: `active` and `previous`.

        Args:
            input_data: `name`, the configuration to show.

        Returns:
            dict[str, Any]: The active configuration.
        """
        input_data = normalize_input(input_data, ConfigurationNameInput)
        require_capability(
            adapter,
            "activate_configuration",
            "Active adapter can't switch configurations",
        )
        result = await adapter.activate_configuration(input_data.name)
        require_success(result, "Failed to activate configuration")
        data = result.data or {}
        return {
            "status": "success",
            "message": f"Activated configuration {input_data.name}",
            "active": data.get("active"),
            "previous": data.get("previous"),
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def delete_configuration(
        input_data: ConfigurationNameInput,
    ) -> dict[str, Any]:
        """Delete a configuration of the active model; not the active one or the only one.

        Activate another configuration first to delete the active one. Needs: an
        open part or assembly with that configuration. Changes: removes it.
        Returns: `deleted` and the `configurations` left.

        Args:
            input_data: `name`, the configuration to delete.

        Returns:
            dict[str, Any]: What was deleted.
        """
        input_data = normalize_input(input_data, ConfigurationNameInput)
        require_capability(
            adapter,
            "delete_configuration",
            "Active adapter can't delete configurations",
        )
        result = await adapter.delete_configuration(input_data.name)
        require_success(result, "Failed to delete configuration")
        data = result.data or {}
        return {
            "status": "success",
            "message": f"Deleted configuration {input_data.name}",
            "deleted": data.get("deleted"),
            "configurations": data.get("configurations", []),
            "execution_time": result.execution_time,
        }

    tool_count = 20  # Total number of registered tools in this module
    return tool_count

register_macro_recording_tools async

register_macro_recording_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: Any) -> int

Register macro recording and playback tools with FastMCP.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config Any

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Example

tool_count = await register_macro_recording_tools(mcp, adapter, config)

Source code in src/solidworks_mcp/tools/macro_recording.py
async def register_macro_recording_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: Any
) -> int:
    """Register macro recording and playback tools with FastMCP.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (Any): Configuration values for the operation.

    Returns:
        int: The computed numeric result.

    Example:
                        >>> tool_count = await register_macro_recording_tools(mcp, adapter, config)
    """
    mcp = with_tool_boundary(mcp)
    tool_count = 0

    @mcp.tool()
    async def execute_macro(input_data: MacroPlaybackInput) -> dict[str, Any]:
        """Run a SolidWorks macro (.swp, or legacy .swb), or VBA source text, in the running SolidWorks, calling its `main` procedure.

        Use it for a macro file, or VBA you wrote as `vba_source`. Source runs only
        if the user set SOLIDWORKS_MCP_ALLOW_VBA_SOURCE to restricted or full, else
        `vba_source_off`: ask the user, never set it yourself. Restricted source may
        call SolidWorks on the open documents only, else `vba_source_refused`,
        naming each line. The reply gives `source_file` and the `vba_source` run.
        The macro must not wait for the user: a dialog still open after
        `dialog_timeout` s (30) is answered with Cancel or its only button, and the
        run fails as `macro_waited_for_user`; a Yes/No prompt is left. It runs
        `repeat_count` times, `pause_between_runs` s apart, stopping at the first
        failure. Needs: SolidWorks running. Changes: whatever the macro does; it
        can't be undone from here. Returns: `runs_completed` and `data` from the
        last run (`macro_path`, `module_name`, `proc_name`, `errors`).

        Args:
            input_data: The macro to run. Give `macro_path` (or `macro_file`), or
                `vba_source`; `proc_name` (main), `repeat_count` (1),
                `pause_between_runs` (0 s) and `dialog_timeout` (30 s) have
                defaults.

        Returns:
            dict[str, Any]: The run's result.
        """
        input_data = normalize_input(input_data, MacroPlaybackInput)
        require_capability(
            adapter,
            "execute_macro",
            "Active adapter does not support execute_macro; "
            f"{input_data.macro_path} was not run, and no features "
            "were created.",
        )
        payload = input_data.model_dump()
        if input_data.vba_source is not None:
            payload["macro_path"] = str(_write_source(input_data.vba_source, config))
        total = input_data.repeat_count
        execution_time = 0.0
        result = None
        for run in range(total):
            if run and input_data.pause_between_runs:
                await asyncio.sleep(input_data.pause_between_runs)
            # require_capability above checked the adapter has it.
            result = await adapter.execute_macro(payload)  # type: ignore[attr-defined]
            if not result.is_success:
                raise ToolOperationError(
                    f"Run {run + 1} of {total} failed after {run} completed: "
                    f"{result.error or 'the macro reported an error'}",
                    execution_time=execution_time + (result.execution_time or 0.0),
                    data=result.data,
                )
            execution_time += result.execution_time or 0.0
        runs = "1 run" if total == 1 else f"{total} runs"
        reply: dict[str, Any] = {
            "status": "success",
            "message": f"Macro completed {runs}",
            "runs_completed": total,
            "data": result.data if result is not None else None,
            "execution_time": execution_time,
        }
        if input_data.vba_source is not None:
            reply["source_file"] = payload["macro_path"]
            reply["vba_source"] = input_data.vba_source
        return reply

    tool_count = 1  # execute_macro
    return tool_count

register_modeling_tools async

register_modeling_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]) -> int

Register modeling tools with FastMCP.

Registers comprehensive modeling tools for SolidWorks automation including model creation, feature creation, and model management operations.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config dict[str, Any]

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Example
from solidworks_mcp.tools.modeling import register_modeling_tools

tool_count = await register_modeling_tools(mcp, adapter, config)
print(f"Registered {tool_count} modeling tools")
Source code in src/solidworks_mcp/tools/modeling.py
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
async def register_modeling_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]
) -> int:
    """Register modeling tools with FastMCP.

    Registers comprehensive modeling tools for SolidWorks automation including model
    creation, feature creation, and model management operations.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (dict[str, Any]): Configuration values for the operation.

    Returns:
        int: The computed numeric result.

    Example:
                        ```python
                        from solidworks_mcp.tools.modeling import register_modeling_tools

                        tool_count = await register_modeling_tools(mcp, adapter, config)
                        print(f"Registered {tool_count} modeling tools")
                        ```
    """
    mcp = with_tool_boundary(mcp)
    tool_count = 0

    @mcp.tool()
    async def open_model(input_data: OpenModelInput) -> dict[str, Any]:
        """Open a part, assembly or drawing from disk and make it the active document.

        Use it to start work on an existing file. To switch to a document that is
        already open, use `activate_document`. Needs: the file on disk (a vault file
        must be checked out). Changes: opens the document; nothing is written. Returns:
        `model` with `title`, `type`, `path` and `configuration`.

        Args:
            input_data: The file. `file_path` (.sldprt, .sldasm or .slddrw) is
                required.

        Returns:
            dict[str, Any]: The opened document.
        """
        input_data = normalize_input(input_data, OpenModelInput)
        result = await adapter.open_model(input_data.file_path)

        require_success(result, context="Failed to open model")
        model = result.data
        title = _result_value(model, "title", "name", default=input_data.file_path)
        model_type = _result_value(model, "type", default="Part")
        path = _result_value(model, "path", "file_path", default=input_data.file_path)
        configuration = _result_value(model, "configuration", default="Default")
        return {
            "status": "success",
            "message": f"Opened {model_type}: {title}",
            "model": {
                "title": title,
                "name": title,
                "type": model_type,
                "path": path,
                "configuration": configuration,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_part(input_data: CreatePartInput) -> dict[str, Any]:
        """Create a new, empty part document and make it active, ready for `create_sketch`.

        Uses `template` (a .prtdot) or the default part template, then sets `units` if
        given, and assigns `material` if given. The part is not saved until
        `save_file`. Needs: SolidWorks running. Changes: opens a new part document.
        Returns: `part` with `name`, `units`, `template` and `material` (the one
        assigned, or null), plus a `warning` when the material couldn't be assigned;
        the part is created either way.

        Args:
            input_data: The part. `name` is required but only a label: the reply
                gives SolidWorks' own name (Part1, ...); `template`, `units` (mm, cm, m, in, ft) and
                `material` (a name from `list_available_materials`) are optional.

        Returns:
            dict[str, Any]: The new part.
        """
        input_data = normalize_input(input_data, CreatePartInput)
        result = await adapter.create_part(
            input_data.name, input_data.units, input_data.template
        )

        require_success(result, context="Failed to create part")
        model = result.data
        part_name = _result_value(model, "name", default=input_data.name)
        units = _result_value(model, "units", default=input_data.units or "mm")
        material = None
        warning = None
        if input_data.material:
            assigned = await adapter.set_material(input_data.material)
            if assigned.is_success:
                material = input_data.material
            else:
                warning = (
                    f"The part was created without a material: "
                    f"'{input_data.material}' could not be assigned "
                    f"({assigned.error}). Use list_available_materials, then "
                    "set_material."
                )
        reply: dict[str, Any] = {
            "status": "success",
            "message": f"Created new part: {part_name}",
            "part": {
                "name": part_name,
                "units": units,
                "material": material,
                "template": input_data.template,
            },
            "execution_time": result.execution_time,
        }
        if warning:
            reply["warning"] = warning
        return reply

    @mcp.tool()
    async def create_assembly(input_data: CreateAssemblyInput) -> dict[str, Any]:
        """Create a new, empty assembly document and make it active, ready for `insert_component`.

        Uses `template` (an .asmdot) or the default assembly template. It is not saved
        until `save_file`. Needs: SolidWorks running. Changes: opens a new assembly
        document. Returns: `assembly` with `name` and `template`. Then call
        `insert_component` for each part.

        Args:
            input_data: The assembly. `name` is required but only a label: the
                reply gives SolidWorks' own name (Assem1, ...); `template` has a
                default.

        Returns:
            dict[str, Any]: The new assembly.
        """
        input_data = normalize_input(input_data, CreateAssemblyInput)
        result = await adapter.create_assembly(input_data.name, input_data.template)

        require_success(result, context="Failed to create assembly")
        model = result.data
        assembly_name = _result_value(model, "name", default=input_data.name)
        return {
            "status": "success",
            "message": f"Created new assembly: {assembly_name}",
            "assembly": {
                "name": assembly_name,
                "template": input_data.template,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_drawing(input_data: CreateDrawingInput) -> dict[str, Any]:
        """Create a new, empty drawing document and make it active.

        Uses `template` (a .drwdot) or the default drawing template. Then place views
        with `create_drawing_view`; for a whole drawing from a model in one call, use
        `generate_drawing_package` instead (both in the drawing toolset). Needs: SolidWorks running. Changes: opens a
        new drawing document. Returns: `drawing` with `name` and `template`.

        Args:
            input_data: The drawing. `name` is required but only a label: the
                reply gives SolidWorks' own name (Draw1, ...); `template` has a
                default.

        Returns:
            dict[str, Any]: The new drawing.
        """
        input_data = normalize_input(input_data, CreateDrawingInput)
        result = await adapter.create_drawing(input_data.name, input_data.template)

        require_success(result, context="Failed to create drawing")
        model = result.data
        drawing_name = _result_value(model, "name", default=input_data.name)
        return {
            "status": "success",
            "message": f"Created new drawing: {drawing_name}",
            "drawing": {
                "name": drawing_name,
                "template": input_data.template,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def close_model(input_data: CloseModelInput) -> dict[str, Any]:
        """Close the active document, saving it first only if asked.

        Use it when you are done with a document. Unsaved changes are lost unless `save`
        is on. Needs: an open, active document. Changes: closes the document, and with
        `save` writes it first. Returns: `saved`, whether a save was asked for.

        Args:
            input_data: The close. Nothing is required: `save` is off.

        Returns:
            dict[str, Any]: The close result.
        """
        input_data = normalize_input(input_data, CloseModelInput)
        result = await adapter.close_model(input_data.save)

        require_success(result, context="Failed to close model")
        return {
            "status": "success",
            "message": "Model closed successfully",
            "saved": input_data.save,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_extrusion(input_data: CreateExtrusionInput) -> dict[str, Any]:
        """Extrude the sketch you just closed into a solid boss, added to the active part.

        Use it after `exit_sketch`; to remove material, use `create_cut_extrude`. It
        extrudes the sketch `sketch_name` names, or the one `exit_sketch` closed last;
        a name that isn't in the feature tree fails before anything is built. End
        conditions: Blind (default), MidPlane, ThroughAll, UpToNext; `both_directions`
        means MidPlane. With `thin_feature` and `thin_thickness` it makes a wall from the
        sketch's outline, and an open chain works (a bent plate from its centreline).
        Needs: an open part and a closed profile (open is fine when thin). Changes:
        adds a Boss-Extrude feature. Returns: `extrusion` (`name`, `sketch` when one
        was named, `depth`, the `end_condition` SolidWorks used, `both_directions`,
        and `thin` when thin) and
        `measured_change` (volume, bounding box and solid-body count before and after;
        left out when the model can't be read).

        Args:
            input_data: The extrusion. `depth` (mm) is required; `sketch_name`,
                `end_condition`, `both_directions`, `reverse_direction`,
                `draft_angle`, `thin_feature`/`thin_thickness` and `merge_result`
                have defaults.

        Returns:
            dict[str, Any]: The created feature.
        """
        input_data = normalize_input(input_data, CreateExtrusionInput)
        # Convert input to ExtrusionParameters
        params = ExtrusionParameters(
            depth=input_data.depth,
            draft_angle=input_data.draft_angle,
            reverse_direction=input_data.reverse_direction,
            both_directions=input_data.both_directions,
            thin_feature=input_data.thin_feature,
            thin_thickness=input_data.thin_thickness,
            thin_side=input_data.thin_side,
            end_condition=input_data.end_condition,
            merge_result=input_data.merge_result,
            feature_scope=False,
            auto_select=True,
            sketch_name=input_data.sketch_name,
        )

        before = await _read_summary(adapter)
        result = await adapter.create_extrusion(params)

        require_success(result, context="Failed to create extrusion")
        after = await _read_summary(adapter) if before is not None else None
        feature = result.data
        name = _result_value(feature, "feature_name", "name", default="Extrusion")
        applied = _result_value(feature, "parameters", default=None) or {}
        extrusion: dict[str, Any] = {
            "name": name,
            "sketch": applied.get("sketch_name"),
            "depth": input_data.depth,
            "end_condition": applied.get("end_condition")
            or ("MidPlane" if input_data.both_directions else input_data.end_condition),
            "both_directions": input_data.both_directions,
        }
        if input_data.thin_feature:
            extrusion["thin"] = {
                "thickness": input_data.thin_thickness,
                "side": input_data.thin_side,
            }
        reply = {
            "status": "success",
            "message": f"Created extrusion: {name}",
            "extrusion": extrusion,
            "execution_time": result.execution_time,
        }
        return _attach_measured_change(reply, before, after)

    @mcp.tool()
    async def create_revolve(input_data: CreateRevolveInput) -> dict[str, Any]:
        """Revolve a closed sketch around an axis, adding a solid to the active part.

        Use it for turned shapes (shafts, knobs). It revolves the sketch `sketch_name`
        names, or the one `exit_sketch` closed last, about `axis_entity`: the id
        `add_centerline` returned or a reference axis from `create_axis`. Left out,
        the centreline drawn in the sketch is the axis. A sketch or axis that isn't in the model fails
        before anything is built. Needs: an open part and a closed profile with a
        centreline or a reference axis. Changes: adds a Revolve feature. Returns:
        `revolve` (`name`, `sketch` and `axis_entity` when named, `angle`) and
        `measured_change` (volume, bounding box and solid-body count before and
        after; left out when the model can't be read).

        Args:
            input_data: The revolve. `angle` (degrees, 360 for a full turn) is
                required; `sketch_name`, `axis_entity`, `both_directions`,
                `reverse_direction`, `thin_feature` and `merge_result` have defaults.

        Returns:
            dict[str, Any]: The created feature.
        """
        input_data = normalize_input(input_data, CreateRevolveInput)
        # Convert input to RevolveParameters
        params = RevolveParameters(
            angle=input_data.angle,
            reverse_direction=input_data.reverse_direction,
            both_directions=input_data.both_directions,
            thin_feature=input_data.thin_feature,
            thin_thickness=input_data.thin_thickness,
            merge_result=input_data.merge_result,
            sketch_name=input_data.sketch_name,
            axis_entity=input_data.axis_entity,
        )

        before = await _read_summary(adapter)
        result = await adapter.create_revolve(params)

        require_success(result, context="Failed to create revolve")
        after = await _read_summary(adapter) if before is not None else None
        feature = result.data
        name = _result_value(feature, "feature_name", "name", default="Revolve")
        applied = _result_value(feature, "parameters", default=None) or {}
        reply = {
            "status": "success",
            "message": f"Created revolve: {name}",
            "revolve": {
                "name": name,
                "sketch": applied.get("sketch_name"),
                "axis_entity": applied.get("axis_entity"),
                "angle": input_data.angle,
            },
            "execution_time": result.execution_time,
        }
        return _attach_measured_change(reply, before, after)

    @mcp.tool()
    async def get_dimension(input_data: GetDimensionInput) -> dict[str, Any]:
        """Read one dimension of the active model by its full name, e.g. "D1@Sketch1".

        Find names with `list_model_dimensions`. It reads the active configuration's
        value; `activate_configuration` to read another. Needs: an open part or
        assembly. Read-only. Returns: `dimension` with `name`, `value` and `units`: "mm" for a
        length, "deg" for an angle, and null for a count (such as a pattern's
        instance count).

        Args:
            input_data: The dimension. Give `name` (or `dimension_name`), e.g.
                "D1@Boss-Extrude1".

        Returns:
            dict[str, Any]: The dimension's value.
        """
        input_data = normalize_input(input_data, GetDimensionInput)
        # The input's validator fills `name` (from `dimension_name` if need be).
        result = await adapter.get_dimension(cast(str, input_data.name))

        require_success(result, context="Failed to get dimension")
        reading = result.data or {}
        dimension_value = reading.get("value")
        dimension_units = reading.get("units")
        unit_text = f" {dimension_units}" if dimension_units else ""
        return {
            "status": "success",
            "message": f"Dimension {input_data.name} = {dimension_value}{unit_text}",
            "dimension": {
                "name": input_data.name,
                "value": dimension_value,
                "units": dimension_units,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def set_dimension(input_data: SetDimensionInput) -> dict[str, Any]:
        """Change one length dimension of the active model, in mm, and rebuild.

        Use it to resize an existing model; find the full name with
        `list_model_dimensions` and check the result with `rebuild_model` or
        `verify_model`. The value is always millimetres. It changes the active
        configuration only, unless `configurations` is "all" or a list of names.
        Needs: an open part or assembly. Changes: the dimension and the geometry it
        drives. Returns: `dimension_update` with `name`, `old_value`, `new_value`
        and `configurations` (the names it was set in).

        Args:
            input_data: The change. `value` (mm) is required, with `name` (or
                `dimension_name`) such as "D1@Sketch1"; `units` is not used.

        Returns:
            dict[str, Any]: The old and new values.
        """
        input_data = normalize_input(input_data, SetDimensionInput)
        result = await adapter.set_dimension(
            cast(str, input_data.name), input_data.value, input_data.configurations
        )

        require_success(result, context="Failed to set dimension")
        payload = result.data
        where = _configurations_named(payload)
        return {
            "status": "success",
            "message": f"Set dimension {input_data.name} = {input_data.value} mm"
            + (f" in {', '.join(where)}" if where else ""),
            "dimension_update": {
                "name": input_data.name,
                "old_value": _result_value(payload, "old_value"),
                "new_value": _result_value(
                    payload, "new_value", default=input_data.value
                ),
                "units": input_data.units or "mm",
                "configurations": where,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def set_units(input_data: SetUnitsInput) -> dict[str, Any]:
        """Set the active document's length units: mm, cm, m, in or ft.

        Use it on a new part before sketching if you want other than the template's
        units. Aliases such as "inch" are accepted. The setting is read back, so success
        means it applied. Tool inputs stay in mm whatever the document shows. Needs: an
        open, active document. Changes: the document's unit system. Returns: `data` with
        `unit_system`, the values applied and observed, and `verified`.

        Args:
            input_data: The units. `unit_system` (mm, cm, m, in or ft) is
                required.

        Returns:
            dict[str, Any]: The units set.
        """
        input_data = normalize_input(input_data, SetUnitsInput)
        result = await adapter.set_units(input_data.unit_system)
        require_success(result, context="Failed to set units")
        data = result.data if isinstance(result.data, dict) else {}
        applied = data.get("unit_system", input_data.unit_system)
        message = f"Set document units to {applied}"
        if data.get("verified") is None:
            message += " (could not be read back to confirm)"
        return {
            "status": "success",
            "message": message,
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_cut_extrude(input_data: CreateCutExtrudeInput) -> dict[str, Any]:
        """Cut material from the active part with a closed sketch profile: holes, slots, windows.

        Use it after `exit_sketch`. Pass `sketch_name` unless the sketch you just closed
        is the one: with no name only that sketch is used, and any doubt is an error
        naming the sketches found. A one-way cut that finds no material is retried the
        other way (`direction_flipped`). For a sketch inside the body, use
        ThroughAllBoth. Needs: an open part with a solid body. Changes: adds a
        Cut-Extrude feature. Returns: `cut_extrude` (`name`, `sketch_name`,
        `direction_flipped`) and `measured_change` (volume, bounding box and solid-body
        count before and after; left out when the model can't be read).

        Args:
            input_data: The cut. `depth` (mm) is required; `sketch_name`,
                `end_condition` (Blind, ThroughAll, ThroughAllBoth),
                `both_directions`, `reverse_direction` and `draft_angle` have
                defaults.

        Returns:
            dict[str, Any]: The created cut.
        """
        input_data = normalize_input(input_data, CreateCutExtrudeInput)
        params = ExtrusionParameters(
            depth=input_data.depth,
            draft_angle=input_data.draft_angle,
            reverse_direction=input_data.reverse_direction,
            both_directions=input_data.both_directions,
            thin_feature=False,
            thin_thickness=None,
            end_condition=input_data.end_condition,
            merge_result=False,
            feature_scope=False,
            auto_select=True,
            sketch_name=input_data.sketch_name,
        )
        before = await _read_summary(adapter)
        result = await adapter.create_cut_extrude(params)
        require_success(result, context="Failed to create cut-extrude")
        after = await _read_summary(adapter) if before is not None else None
        feature = result.data
        feature_params = _result_value(feature, "parameters", default={})
        sketch_used = (
            feature_params.get("sketch_name")
            if isinstance(feature_params, dict)
            else None
        ) or input_data.sketch_name
        flipped = bool(
            isinstance(feature_params, dict) and feature_params.get("direction_flipped")
        )
        name = _result_value(feature, "feature_name", "name", default="Cut-Extrude")
        message = f"Created cut-extrude: {name}"
        if flipped:
            message += (
                " (the requested direction found no material, so it cut the other way)"
            )
        reply = {
            "status": "success",
            "message": message,
            "cut_extrude": {
                "name": name,
                "depth": input_data.depth,
                "sketch_name": sketch_used,
                "direction_flipped": flipped,
            },
            "execution_time": result.execution_time,
        }
        return _attach_measured_change(reply, before, after)

    @mcp.tool()
    async def add_fillet(input_data: AddFilletInput) -> dict[str, Any]:
        """Round edges of the active part with a constant-radius fillet.

        Name the edges as "Edge<1>", a point "x,y,z" (metres) or "face:x,y,z" for every
        edge of a face; or leave `edge_names` empty to use the current selection (e.g.
        from `select_face_edges`). A fillet that leaves the volume unchanged is an
        error. Needs: an open part with a solid body. Changes: adds a Fillet feature.
        Returns: `fillet` with `name`, `radius` and `edges`.

        Args:
            input_data: The fillet. `radius` (mm) is required; `edge_names`
                defaults to the current selection.

        Returns:
            dict[str, Any]: The created fillet.
        """
        input_data = normalize_input(input_data, AddFilletInput)
        result = await adapter.add_fillet(input_data.radius, input_data.edge_names)
        require_success(result, context="Failed to add fillet")
        feature = result.data
        return {
            "status": "success",
            "message": f"Created fillet: {_result_value(feature, 'feature_name', 'name', default='Fillet')}",
            "fillet": {
                "name": _result_value(
                    feature, "feature_name", "name", default="Fillet"
                ),
                "radius": input_data.radius,
                "edges": input_data.edge_names,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_chamfer(input_data: AddChamferInput) -> dict[str, Any]:
        """Bevel edges of the active part with a chamfer: `distance` along one face at `angle` (45 by default).

        Name the edges as "Edge<1>", a point "x,y,z" (metres) or "face:x,y,z" for every
        edge of a face; or leave `edge_names` empty to use the current selection (e.g.
        from `select_face_edges`). At 45 degrees both legs equal `distance`; at another
        angle, `flip` swaps which face `distance` is measured on. Needs: an open part
        with a solid body. Changes: adds a Chamfer feature. Returns: `chamfer` with
        `name`, `distance`, `angle`, `flip` and `edges`.

        Args:
            input_data: The chamfer. `distance` (mm) is required; `edge_names`
                defaults to the current selection.

        Returns:
            dict[str, Any]: The created chamfer.
        """
        input_data = normalize_input(input_data, AddChamferInput)
        result = await adapter.add_chamfer(
            input_data.distance,
            input_data.edge_names,
            input_data.angle,
            input_data.flip,
        )
        require_success(result, context="Failed to add chamfer")
        feature = result.data
        return {
            "status": "success",
            "message": f"Created chamfer: {_result_value(feature, 'feature_name', 'name', default='Chamfer')}",
            "chamfer": {
                "name": _result_value(
                    feature, "feature_name", "name", default="Chamfer"
                ),
                "distance": input_data.distance,
                "angle": input_data.angle,
                "flip": input_data.flip,
                "edges": input_data.edge_names,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_sweep(input_data: CreateSweepInput) -> dict[str, Any]:
        """Sweep a closed profile sketch along a path sketch to make a solid (Insert > Boss/Base > Sweep).

        Draw the closed profile and the open path as two sketches first. The profile is
        the sketch you closed last that isn't the path. Needs: an open part with both
        sketches. Changes: adds a Sweep feature. Returns: `sweep` with `name`, `path`,
        `twist_along_path` and `twist_angle`.

        Args:
            input_data: The sweep. `path` (the path sketch's name) is required;
                `twist_along_path`, `twist_angle` (degrees) and `merge_result` have
                defaults.

        Returns:
            dict[str, Any]: The created sweep.
        """
        input_data = normalize_input(input_data, CreateSweepInput)
        params = SweepParameters(
            path=input_data.path,
            twist_along_path=input_data.twist_along_path,
            twist_angle=input_data.twist_angle,
            merge_result=input_data.merge_result,
        )
        result = await adapter.create_sweep(params)
        require_success(result, context="Failed to create sweep")
        feature = result.data
        return {
            "status": "success",
            "message": f"Created sweep: {_result_value(feature, 'feature_name', 'name', default='Sweep')}",
            "sweep": {
                "name": _result_value(feature, "feature_name", "name", default="Sweep"),
                "path": input_data.path,
                "twist_along_path": input_data.twist_along_path,
                "twist_angle": input_data.twist_angle,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_loft(input_data: CreateLoftInput) -> dict[str, Any]:
        """Loft a solid through two or more closed profile sketches, in order (Insert > Boss/Base > Loft).

        Use it for shapes that change section, such as a wing or a transition duct. Put
        each profile on its own plane (see `create_reference_plane`). Needs: an open
        part with the profile sketches. Changes: adds a Loft feature. Returns: `loft`
        with `name`, `profiles` and `guide_curves`.

        Args:
            input_data: The loft. `profiles` (sketch names, in order) is required;
                `guide_curves`, `start_tangent`, `end_tangent` and `merge_result`
                have defaults.

        Returns:
            dict[str, Any]: The created loft.
        """
        input_data = normalize_input(input_data, CreateLoftInput)
        params = LoftParameters(
            profiles=input_data.profiles,
            guide_curves=input_data.guide_curves,
            start_tangent=input_data.start_tangent,
            end_tangent=input_data.end_tangent,
            merge_result=input_data.merge_result,
        )
        result = await adapter.create_loft(params)
        require_success(result, context="Failed to create loft")
        feature = result.data
        return {
            "status": "success",
            "message": f"Created loft: {_result_value(feature, 'feature_name', 'name', default='Loft')}",
            "loft": {
                "name": _result_value(feature, "feature_name", "name", default="Loft"),
                "profiles": input_data.profiles,
                "guide_curves": input_data.guide_curves,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def insert_component(input_data: InsertComponentInput) -> dict[str, Any]:
        """Insert a part or sub-assembly file into the active assembly at a position in mm.

        Use it after `create_assembly`, then position components with `add_mate`. The
        file must contain solid geometry; an empty or missing file is an error. Needs:
        an open, active assembly and the file on disk. Changes: adds a component.
        Returns: `component` with its instance name (`component`, which `add_mate`
        takes), `file_path`, `position` and the counts before and after.

        Args:
            input_data: The component. `file_path` is required; `x`, `y`, `z` (mm
                from the assembly origin) default to 0.

        Returns:
            dict[str, Any]: The inserted component.
        """
        input_data = normalize_input(input_data, InsertComponentInput)
        result = await adapter.insert_component(
            input_data.file_path, input_data.x, input_data.y, input_data.z
        )
        require_success(result, context="Failed to insert component")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": f"Inserted {data.get('component', 'component')}",
            "component": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_mate(input_data: AddMateInput) -> dict[str, Any]:
        """Mate two components of the active assembly: coincident, concentric, parallel, perpendicular, tangent, distance or angle.

        Each side is a named reference plane or axis (`entity_a`, e.g. "Front Plane"),
        or a face given by a point on it (`face_a`, mm, in that part's own coordinates):
        a pin's round face and a hole's face make a concentric mate. Get instance names
        from `list_components`. Needs: an open assembly with both components. Changes:
        adds a mate and may move components. Returns: `mate` with `mate_type`,
        `components`, `entities`, `geometry_moved` and `moved_components`.

        Args:
            input_data: The mate. `component_a` and `component_b` are required;
                each side takes `entity_*` or `face_*` (default "Front Plane");
                `mate_type` (coincident), `alignment`, `distance` (mm) and `angle`
                (degrees) have defaults.

        Returns:
            dict[str, Any]: The mate added.
        """
        input_data = normalize_input(input_data, AddMateInput)
        result = await adapter.add_mate(
            input_data.component_a,
            input_data.component_b,
            input_data.entity_a or "Front Plane",
            input_data.entity_b or "Front Plane",
            input_data.mate_type,
            input_data.alignment,
            input_data.distance,
            input_data.angle,
            input_data.face_a,
            input_data.face_b,
        )
        require_success(result, context="Failed to add mate")
        data = result.data if isinstance(result.data, dict) else {}
        message = (
            f"Added {input_data.mate_type} mate between "
            f"{input_data.component_a} and {input_data.component_b}"
        )
        # SolidWorks accepts a mate whose constraint is already
        # satisfied and moves nothing. That is a real success, but
        # saying so plainly beats leaving the caller to infer it.
        if data.get("geometry_moved") is False:
            message += " (no component moved - the constraint was already satisfied)"
        return {
            "status": "success",
            "message": message,
            "mate": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def set_component_state(input_data: SetComponentStateInput) -> dict[str, Any]:
        """Fix or float an assembly component, make a sub-assembly flexible, or hide it or make it transparent.

        Float a part so mates can position it (the first part inserted is fixed); fix
        one to hold it still. Make a sub-assembly flexible so its own mates move in
        this assembly (a crank whose knob turns). Hide a component (`visible` false)
        or make it `transparent` to see the parts inside. Only what you pass changes:
        the component's suppression, configuration and other states are kept. Needs:
        an open assembly. Changes: the states you pass. Returns: `state` with
        `component`, `before` and `after` (`fixed`, `flexible`, `visible` and the
        properties kept), and `appearance` (`visible`, `transparent`) when you
        changed it.

        Args:
            input_data: The component and what to change: `fixed`, `flexible`,
                `visible`, `transparent`, any of them.

        Returns:
            dict[str, Any]: The state before and after.
        """
        input_data = normalize_input(input_data, SetComponentStateInput)
        data: dict[str, Any] = {"component": input_data.component}
        words = []
        execution_time = 0.0
        if input_data.fixed is not None or input_data.flexible is not None:
            result = await adapter.set_component_state(
                input_data.component, input_data.fixed, input_data.flexible
            )
            require_success(result, context="Failed to set the component state")
            data = result.data if isinstance(result.data, dict) else data
            execution_time = result.execution_time or 0.0
            after = data.get("after", {})
            if input_data.fixed is not None:
                words.append("fixed" if after.get("fixed") else "floating")
            if input_data.flexible is not None:
                words.append("flexible" if after.get("flexible") else "rigid")
        if input_data.visible is not None or input_data.transparent is not None:
            require_capability(
                adapter,
                "set_component_appearance",
                "Active adapter can't hide components or make them transparent",
            )
            shown = await adapter.set_component_appearance(
                input_data.component, input_data.visible, input_data.transparent
            )
            require_success(shown, context="Failed to set the component state")
            appearance = shown.data or {}
            data = {**data, "appearance": appearance}
            execution_time += shown.execution_time or 0.0
            if input_data.visible is not None:
                words.append("shown" if appearance.get("visible") else "hidden")
            if input_data.transparent is not None:
                words.append("transparent" if input_data.transparent else "opaque")
        return {
            "status": "success",
            "message": f"{input_data.component} is now {' and '.join(words)}",
            "state": data,
            "execution_time": execution_time,
        }

    @mcp.tool()
    async def list_components() -> dict[str, Any]:
        """List the top-level components of the active assembly by instance name, e.g. "bracket-1".

        Use it before `add_mate`, which takes these names. Needs: an open, active
        assembly. Read-only. Returns: `components` (instance names).
        """
        result = await adapter.list_components()
        require_success(result, context="Failed to list components")
        components = result.data if isinstance(result.data, list) else []
        return {
            "status": "success",
            "message": f"{len(components)} component(s) in the assembly",
            "components": components,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def delete_feature(input_data: DeleteFeatureInput) -> dict[str, Any]:
        """Delete a feature or sketch from the active model, along with the features that depend on it.

        Ask the user before you call it: deleting a sketch also deletes every feature
        built from it (a cut, a boss), and deleting a feature deletes everything that
        depends on it. Use `suppress_feature` instead when you might want it back. Find
        names and dependents with `list_features`. Needs: an open part or assembly.
        Changes: removes the feature and its dependents (`undo` can restore them).
        Returns: `data` with `deleted`, `removed_features` (every feature that went,
        dependents included), `features_before` and `features_after`; the message names
        the dependents, and a `warning` says so whenever more than the named feature
        went: tell the user.

        Args:
            input_data: The feature. `name` (e.g. "Fillet1") is required.

        Returns:
            dict[str, Any]: What was deleted.
        """
        input_data = normalize_input(input_data, DeleteFeatureInput)
        result = await adapter.delete_feature(input_data.name)
        require_success(result, context="Failed to delete feature")
        data = result.data if isinstance(result.data, dict) else {}
        removed = data.get("removed_features") or [input_data.name]
        dependents = [n for n in removed if n != input_data.name.split("@", 1)[0]]
        reply: dict[str, Any] = {
            "status": "success",
            "message": (
                f"Deleted {input_data.name}"
                + (
                    f" and its dependents: {', '.join(dependents)}"
                    if dependents
                    else ""
                )
            ),
            "data": data,
            "execution_time": result.execution_time,
        }
        if dependents:
            reply["warning"] = (
                f"Deleting {input_data.name} also deleted {', '.join(dependents)}. "
                "Tell the user; `undo` restores them."
            )
        return reply

    @mcp.tool()
    async def suppress_feature(input_data: SuppressFeatureInput) -> dict[str, Any]:
        """Suppress or unsuppress a feature of the active model; suppressed features stay in the tree but are not built.

        Use it to turn off a feature that breaks the rebuild without deleting it. It
        acts in the active configuration only, unless `configurations` is "all" or a
        list of names. Needs: an open part or assembly. Changes: the feature's
        suppression state, and its children's. Returns: `data` with `feature`,
        `requested`, `suppressed` (read back in the active configuration; null when
        unreadable), `was_suppressed` and `configurations` (where it applied).

        Args:
            input_data: The feature. `name` is required; `suppress` defaults to on
                (pass false to unsuppress).

        Returns:
            dict[str, Any]: The new state.
        """
        input_data = normalize_input(input_data, SuppressFeatureInput)
        result = await adapter.suppress_feature(
            input_data.name, input_data.suppress, input_data.configurations
        )
        require_success(result, context="Failed to suppress feature")
        data = result.data if isinstance(result.data, dict) else {}
        verb = "Suppressed" if input_data.suppress else "Unsuppressed"
        message = f"{verb} {input_data.name}"
        where = _configurations_named(data)
        if where:
            message += f" in {', '.join(where)}"
        if data.get("suppressed") is None:
            message += " (state could not be read back)"
        return {
            "status": "success",
            "message": message,
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def rename_feature(input_data: RenameFeatureInput) -> dict[str, Any]:
        """Rename a feature in the active model's feature tree.

        Use it to give features meaningful names (e.g. "MountingHoles"). The new name is
        read back: a name another feature already uses is an error. Renaming to the
        current name succeeds with no change. Needs: an open part or assembly. Changes:
        the feature's name. Returns: `data` with `old_name`, `new_name`, `renamed` and,
        when unchanged, `reason`.

        Args:
            input_data: The rename. `old_name` and `new_name` are required.

        Returns:
            dict[str, Any]: The rename.
        """
        input_data = normalize_input(input_data, RenameFeatureInput)
        result = await adapter.rename_feature(input_data.old_name, input_data.new_name)
        require_success(result, context="Failed to rename feature")
        data = result.data if isinstance(result.data, dict) else {}
        if data.get("renamed") is False:
            return {
                "status": "success",
                "message": (
                    f"{input_data.old_name} unchanged: "
                    + str(data.get("reason") or "no change")
                ),
                "data": data,
                "execution_time": result.execution_time,
            }
        return {
            "status": "success",
            "message": (
                f"Renamed {data.get('old_name', input_data.old_name)} to "
                f"{data.get('new_name', input_data.new_name)}"
            ),
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def select_feature(input_data: SelectFeatureInput) -> dict[str, Any]:
        """Select a feature, sketch, plane or component by name in the active document.

        Use it to set up a selection that the next SolidWorks operation acts on.
        `append` adds to the selection instead of replacing it. A name that can't be
        selected is an error. Needs: an open document. Changes: the selection only.
        Returns: `data` with `selected_name` and `selection_count`.

        Args:
            input_data: The selection. `feature_name` is required; `append` is off.

        Returns:
            dict[str, Any]: The selection.
        """
        input_data = normalize_input(input_data, SelectFeatureInput)
        result = await adapter.select_feature(
            input_data.feature_name, input_data.append
        )
        require_success(result)
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": (
                f"Selected {data.get('selected_name', input_data.feature_name)} "
                f"({data.get('selection_count', '?')} selected)"
            ),
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def select_face_edges(input_data: SelectFaceEdgesInput) -> dict[str, Any]:
        """Select every edge of one face of the active part, ready for `add_fillet` or `add_chamfer` with no edge names.

        Pick the face with one of `point` (a point on it, mm), `side` (top, bottom,
        front, back, right, left or +X..-Z; the furthest flat face that way) or `plane`
        (the face lying on that plane). `edge_type` narrows to linear or circular edges
        (hole rims). Needs: an open part. Changes: the selection only. Returns: `data`
        with `face`, `edges`, `selected_edges` and `selection_count`.

        Args:
            input_data: The face. Give exactly one of `point`, `side` or `plane`;
                `edge_type` (all) and `append` (off) have defaults.

        Returns:
            dict[str, Any]: The edges selected.
        """
        input_data = normalize_input(input_data, SelectFaceEdgesInput)
        result = await adapter.select_face_edges(
            input_data.point,
            input_data.side,
            input_data.plane,
            input_data.edge_type,
            input_data.append,
        )
        require_success(result)
        data = result.data if isinstance(result.data, dict) else {}
        kinds = sorted({e.get("curve", "other") for e in data.get("edges", [])})
        return {
            "status": "success",
            "message": (
                f"Selected {data.get('selected_edges', 0)} edge(s) "
                f"({', '.join(kinds)}) on the face at "
                f"{data.get('face', {}).get('center_mm')} mm"
            ),
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def rebuild_model(
        input_data: RebuildModelInput | None = None,
    ) -> dict[str, Any]:
        """Rebuild the active model and report each feature that fails, with the reason and a fix.

        Use it after `set_dimension` or other edits to confirm the model is sound.
        Failing features make the call an error whose `data.errors` names each one, its
        SolidWorks error, what it depends on and a `hint`; warnings alone still succeed.
        Needs: an open part or assembly. Changes: rebuilds the model. Returns: `data`
        with `rebuilt`, `errors`, `error_count`, `warning_count` and `summary`.

        Args:
            input_data: Nothing is required: `top_level_only` is off (a full
                rebuild).

        Returns:
            dict[str, Any]: The rebuild report.
        """
        input_data = normalize_input(input_data or {}, RebuildModelInput)
        result = require_success(
            await adapter.rebuild_model(input_data.top_level_only), with_data=True
        )
        data = result.data if isinstance(result.data, dict) else None
        response: dict[str, Any] = {
            "status": "success",
            "message": (data or {}).get("summary") or "Rebuilt",
            "execution_time": result.execution_time,
        }
        if data is not None:
            response["data"] = data
        return response

    @mcp.tool()
    async def undo(input_data: UndoInput | None = None) -> dict[str, Any]:
        """Undo the last operations in the active model (Edit > Undo).

        Use it to step back after a feature went wrong. SolidWorks accepts an undo with
        nothing to undo, so the reply says whether the feature tree changed. Needs: an
        open document. Changes: reverts the last `count` operations. Returns: `data`
        with `requested_steps`, `features_before`, `features_after` and `tree_changed`.

        Args:
            input_data: Nothing is required: `count` defaults to 1.

        Returns:
            dict[str, Any]: What changed.
        """
        input_data = normalize_input(input_data or UndoInput(), UndoInput)
        result = await adapter.undo(input_data.count)
        require_success(result, context="Failed to undo")
        data = result.data if isinstance(result.data, dict) else {}
        changed = data.get("tree_changed")
        if changed is False:
            message = (
                f"Undo of {input_data.count} step(s) left the feature "
                "tree unchanged - there was nothing to undo"
            )
        else:
            message = f"Undid {input_data.count} step(s)"
        return {
            "status": "success",
            "message": message,
            "data": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_reference_plane(
        input_data: CreateReferencePlaneInput,
    ) -> dict[str, Any]:
        """Add a reference plane offset from, at an angle to, or on a plane or flat face.

        Use it to sketch somewhere other than the three default planes: pass the new
        plane's name to `create_sketch`. Offset: `reference` and a non-zero `offset`
        (mm). Angle: `reference`, `angle` (degrees) and `axis`, an axis lying in the
        reference (make one with `create_axis`). Face: `face_point`, a point on a flat
        face (sloped or not), with an `offset` or 0 for a plane on the face. Needs: an
        open part. Changes: adds a plane feature. Returns: `plane` with `name` and the
        inputs used.

        Args:
            input_data: The plane. Exactly one of `reference` and `face_point`;
                then `offset`, or `angle` with `axis`; `flip` is off.

        Returns:
            dict[str, Any]: The new plane.
        """
        input_data = normalize_input(input_data, CreateReferencePlaneInput)
        result = await adapter.create_reference_plane(
            input_data.reference,
            input_data.offset,
            input_data.angle,
            input_data.flip,
            input_data.axis,
            input_data.face_point,
        )
        require_success(result, context="Failed to create reference plane")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": f"Created reference plane: {data.get('name', 'Plane')}",
            "plane": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_axis(input_data: CreateAxisInput) -> dict[str, Any]:
        """Add a reference axis along the model's x, y or z direction, through the origin.

        Use it before `pattern_circular`, which rotates features about an axis. It is
        built where two default planes meet. Needs: an open part. Changes: adds an axis
        feature. Returns: `axis` with `name` (e.g. "Axis1", for
        `create_reference_plane`'s `axis`), `reference`, `planes` and the feature counts.

        Args:
            input_data: The axis. `reference` ("x", "y" or "z") is required.

        Returns:
            dict[str, Any]: The new axis.
        """
        input_data = normalize_input(input_data, CreateAxisInput)
        result = await adapter.create_axis(input_data.reference)
        require_success(result, context="Failed to create axis")
        data = result.data if isinstance(result.data, dict) else {}
        axis_name = data.get("reference", input_data.reference)
        return {
            "status": "success",
            "message": f"Created {axis_name} axis",
            "axis": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_reference_point(
        input_data: CreateReferencePointInput,
    ) -> dict[str, Any]:
        """Add a reference point on an edge (at a distance or percentage along it) or at the centre of a face.

        Use it to mark a position for a later feature or measurement. Pick the edge or
        face with a point on it in mm. Needs: an open part with that edge or face.
        Changes: adds a point feature. Returns: `point` with `mode`, `at_mm`,
        `distance_mm` or `percent`, and the feature counts.

        Args:
            input_data: The point. `mode` (along_curve or face_center) and `x`,
                `y`, `z` (mm) are required; for along_curve give one of `distance`
                (mm) or `percent`.

        Returns:
            dict[str, Any]: The new point.
        """
        input_data = normalize_input(input_data, CreateReferencePointInput)
        result = await adapter.create_reference_point(
            input_data.mode,
            input_data.x,
            input_data.y,
            input_data.z,
            input_data.distance,
            input_data.percent,
        )
        require_success(result, context="Failed to create reference point")
        data = result.data if isinstance(result.data, dict) else {}
        where = (
            f"{data.get('percent')}% along a curve"
            if data.get("percent") is not None
            else (
                f"{data.get('distance_mm')} mm along a curve"
                if data.get("distance_mm") is not None
                else "a face centre"
            )
        )
        return {
            "status": "success",
            "message": f"Created a reference point at {where}",
            "point": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def mirror_feature(input_data: MirrorFeatureInput) -> dict[str, Any]:
        """Mirror solid bodies or features of the active part about a plane.

        Use it for symmetric parts: model one half, then mirror. Body mirroring (the
        default) is what works for lofts and sweeps; to mirror a cut, name the cut and
        set `mirror_bodies` off. It fails when the volume doesn't change, which means
        nothing was mirrored. Needs: an open part with the named bodies or features.
        Changes: adds a Mirror feature. Returns: `mirror` with `name`, `mirrored`,
        `mirror_plane`, `merge`, `mirror_bodies`, and `volume_before`,
        `volume_after` and `volume_change` (mm³, negative for a cut).

        Args:
            input_data: The mirror. `features` (at least one name) and
                `mirror_plane` (e.g. "Right Plane") are required; `merge` and
                `mirror_bodies` are on.

        Returns:
            dict[str, Any]: The mirror created.
        """
        input_data = normalize_input(input_data, MirrorFeatureInput)
        result = await adapter.mirror_feature(
            input_data.features,
            input_data.mirror_plane,
            input_data.merge,
            input_data.mirror_bodies,
        )
        require_success(result, context="Failed to mirror feature")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": f"Mirrored {', '.join(input_data.features)} "
            f"about {input_data.mirror_plane}",
            "mirror": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def pattern_circular(input_data: PatternCircularInput) -> dict[str, Any]:
        """Repeat features of the active part around an axis, e.g. a ring of bosses or ribs.

        Create the axis first with `create_axis`. Bosses and cuts both work (a ring of
        holes is a patterned cut). It fails when the volume doesn't change, which
        means no instance landed on the part. Needs: an open part with the named
        features and axis. Changes: adds a circular pattern feature. Returns:
        `pattern` with `name`, `patterned`, `axis`, `count`, `angle`,
        `equal_spacing`, and `volume_before`, `volume_after` and `volume_change`
        (mm³, negative for cuts).

        Args:
            input_data: The pattern. `features` (at least one), `axis` and `count`
                (2 or more, the original included) are required; `angle` (360) and
                `equal_spacing` (on) have defaults.

        Returns:
            dict[str, Any]: The pattern created.
        """
        input_data = normalize_input(input_data, PatternCircularInput)
        result = await adapter.pattern_circular(
            input_data.features,
            input_data.axis,
            input_data.count,
            input_data.angle,
            input_data.equal_spacing,
        )
        require_success(result, context="Failed to pattern feature")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": f"Patterned {', '.join(input_data.features)} "
            f"around {input_data.axis} ({input_data.count} instances)",
            "pattern": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def pattern_linear(input_data: PatternLinearInput) -> dict[str, Any]:
        """Repeat features of the active part in a row or a grid, e.g. a row of holes.

        Give the direction as a point on a straight edge (mm) or an axis name; add
        `direction2` for a grid. Bosses and cuts both work. SolidWorks picks which
        way an edge points; if instances land off the part, set `reverse` or
        `reverse2`. It fails when the volume doesn't change, which means no
        instance landed on the part. Needs: an open
        part with the named features. Changes: adds a linear pattern feature.
        Returns: `pattern` with `name`, `patterned`, `count`, `spacing_mm`, and
        `volume_before`, `volume_after` and `volume_change` (mm³).

        Args:
            input_data: The pattern. `features`, `direction`, `count` (2+) and
                `spacing` (mm) are required; `direction2`, `count2` and `spacing2`
                make a grid.

        Returns:
            dict[str, Any]: The pattern created.
        """
        input_data = normalize_input(input_data, PatternLinearInput)
        result = await adapter.pattern_linear(
            input_data.features,
            input_data.direction,
            input_data.count,
            input_data.spacing,
            input_data.direction2,
            input_data.count2,
            input_data.spacing2,
            input_data.reverse,
            input_data.reverse2,
        )
        require_success(result, context="Failed to pattern features")
        data = result.data if isinstance(result.data, dict) else {}
        grid = f" x {input_data.count2}" if input_data.direction2 is not None else ""
        return {
            "status": "success",
            "message": f"Patterned {', '.join(input_data.features)} "
            f"({input_data.count}{grid} instances)",
            "pattern": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_shell(input_data: CreateShellInput) -> dict[str, Any]:
        """Hollow the active part to a wall thickness, opening the faces picked.

        Use it for boxes, housings and cups: pick the face to open with a point on
        it (mm). It fails when the volume doesn't change. Needs: an open part with a
        solid body. Changes: adds a Shell feature. Returns: `shell` with `name`,
        `thickness_mm`, `faces_removed`, and `volume_before`, `volume_after` and
        `volume_change` (mm³).

        Args:
            input_data: The shell. `thickness` (mm) is required; `faces` lists a
                point on each face to remove; `outward` puts the wall outside.

        Returns:
            dict[str, Any]: The shell created.
        """
        input_data = normalize_input(input_data, CreateShellInput)
        result = await adapter.create_shell(
            input_data.thickness, input_data.faces, input_data.outward
        )
        require_success(result, context="Failed to shell the part")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": f"Shelled the part to {input_data.thickness} mm walls",
            "shell": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_draft(input_data: AddDraftInput) -> dict[str, Any]:
        """Draft faces of the active part by an angle, e.g. for a moulded part.

        Pick each face to draft and the neutral face it pivots on (usually the
        parting or bottom face) with a point on each (mm). It fails when the volume
        doesn't change. Needs: an open part. Changes: adds a Draft feature.
        Returns: `draft` with `name`, `angle`, `faces_drafted`, and
        `volume_before`, `volume_after` and `volume_change` (mm³).

        Args:
            input_data: The draft. `angle` (degrees, under 90), `faces` (at least
                one point) and `neutral_face` (a point) are required.

        Returns:
            dict[str, Any]: The draft created.
        """
        input_data = normalize_input(input_data, AddDraftInput)
        result = await adapter.add_draft(
            input_data.angle,
            input_data.faces,
            input_data.neutral_face,
            input_data.reverse,
        )
        require_success(result, context="Failed to draft faces")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": f"Drafted {len(input_data.faces)} face(s) by {input_data.angle} degrees",
            "draft": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_rib(input_data: CreateRibInput) -> dict[str, Any]:
        """Add a rib from a sketched line, stiffening a wall or the inside of a shell.

        Sketch an open line where the rib goes (it can stop short of the walls; the
        rib extends to meet them), close the sketch, then call this with its name.
        It fails when the volume doesn't change. Needs: an open part and the rib
        sketch. Changes: adds a Rib feature. Returns: `rib` with `name`, `sketch`,
        `thickness_mm`, and `volume_before`, `volume_after` and `volume_change` (mm³).

        Args:
            input_data: The rib. `sketch` and `thickness` (mm) are required;
                `both_sides` is on.

        Returns:
            dict[str, Any]: The rib created.
        """
        input_data = normalize_input(input_data, CreateRibInput)
        result = await adapter.create_rib(
            input_data.sketch,
            input_data.thickness,
            input_data.both_sides,
            input_data.flip_material,
            input_data.normal_to_sketch,
        )
        require_success(result, context="Failed to create rib")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": f"Added a {input_data.thickness} mm rib from {input_data.sketch}",
            "rib": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_base_flange(input_data: CreateBaseFlangeInput) -> dict[str, Any]:
        """Start a sheet-metal part: a base flange from a sketch, with its thickness, bend radius and K-factor.

        A closed sketch (a rectangle) makes a flat plate; an open one (a line or an L)
        makes a bent profile extruded by `depth`. Use it as a sheet-metal part's
        first feature, then `add_edge_flange`, `create_sketched_bend` and
        `set_flat_pattern`; export the blank with `export_flat_pattern`. The values
        are read back from the feature. Needs: an open part with the profile sketch
        closed. Changes: adds a Base-Flange feature and makes the part sheet metal.
        Returns: `flange` with `name`, `thickness_mm`, `bend_radius_mm`, `k_factor`
        and `volume_after` (mm³).

        Args:
            input_data: The flange. `thickness` and `bend_radius` (mm) are required;
                `k_factor` is 0.5; `depth` only for an open profile.

        Returns:
            dict[str, Any]: The base flange.
        """
        input_data = normalize_input(input_data, CreateBaseFlangeInput)
        result = await adapter.create_base_flange(
            input_data.thickness,
            input_data.bend_radius,
            input_data.k_factor,
            input_data.depth,
            input_data.sketch,
            input_data.reverse,
        )
        require_success(result, context="Failed to create the base flange")
        data = result.data if isinstance(result.data, dict) else {}
        thickness = data.get("thickness_mm", input_data.thickness)
        return {
            "status": "success",
            "message": f"Made a {thickness:g} mm sheet-metal base flange from "
            f"{data.get('sketch', 'the sketch')}",
            "flange": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_edge_flange(input_data: AddEdgeFlangeInput) -> dict[str, Any]:
        """Add a flange to one edge of a sheet-metal part, at a length and angle.

        Pick the edge with a point on it in mm (`edge_point`), e.g. on the top
        face's long edge. `length` is measured from that edge, so on a 2 mm sheet a
        20 mm flange stands 22 mm above the bottom face. It fails when the volume
        doesn't change. Needs: a sheet-metal part (`create_base_flange` first).
        Changes: adds an Edge-Flange feature. Returns: `flange` with `name`,
        `edge_length_mm` (to check the pick) and `volume_change` (mm³).

        Args:
            input_data: The flange. `edge_point` ([x, y, z] mm) and `length` (mm)
                are required; `angle` is 90 and the bend radius the sheet's.

        Returns:
            dict[str, Any]: The edge flange.
        """
        input_data = normalize_input(input_data, AddEdgeFlangeInput)
        result = await adapter.add_edge_flange(
            input_data.edge_point,
            input_data.length,
            input_data.angle,
            input_data.bend_radius,
            input_data.flip,
        )
        require_success(result, context="Failed to add the edge flange")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": f"Added a {input_data.length:g} mm edge flange at "
            f"{input_data.angle:g}° on the {data.get('edge_length_mm')} mm edge",
            "flange": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_sketched_bend(
        input_data: CreateSketchedBendInput | None = None,
    ) -> dict[str, Any]:
        """Bend a sheet-metal part along a line sketched on one of its faces.

        Sketch a line across a face of the sheet (`create_sketch` with `face_point`,
        then `add_line`), close the sketch, then call this. SolidWorks chooses which
        side of the line stays put; the reply's `bounding_box_mm` shows which side
        moved, and `reverse` bends the other way. Needs: a sheet-metal part and the
        bend-line sketch. Changes: adds a Sketched Bend feature. Returns: `bend`
        with `name` and `bounding_box_mm`.

        Args:
            input_data: Nothing is required: `angle` is 90, the bend radius the
                sheet's, and the sketch the last closed one.

        Returns:
            dict[str, Any]: The bend.
        """
        input_data = normalize_input(input_data, CreateSketchedBendInput)
        result = await adapter.create_sketched_bend(
            input_data.angle,
            input_data.bend_radius,
            input_data.sketch,
            input_data.reverse,
        )
        require_success(result, context="Failed to create the sketched bend")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": f"Bent the sheet {input_data.angle:g}° along "
            f"{data.get('sketch', 'the sketch')}",
            "bend": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def set_flat_pattern(
        input_data: SetFlatPatternInput | None = None,
    ) -> dict[str, Any]:
        """Flatten a sheet-metal part to its blank, or fold it back, and measure it.

        Flattened, `bounding_box_mm` is the blank to cut: check it against the
        part's size and the bend allowance before `export_flat_pattern` writes the
        DXF. Fold it back before adding more features. Needs: a sheet-metal part.
        Changes: unsuppresses (flat) or suppresses (folded) the Flat-Pattern
        feature. Returns: `flat`, `feature` and `bounding_box_mm`.

        Args:
            input_data: `flat` is true (flatten); false folds the part back.

        Returns:
            dict[str, Any]: The state and the part's size in it.
        """
        input_data = normalize_input(input_data, SetFlatPatternInput)
        result = await adapter.set_flat_pattern(input_data.flat)
        require_success(result, context="Failed to change the flat pattern")
        data = result.data if isinstance(result.data, dict) else {}
        size = (data.get("bounding_box_mm") or {}).get("size")
        state = "Flattened: the blank is" if input_data.flat else "Folded: the part is"
        return {
            "status": "success",
            "message": f"{state} {size} mm",
            **data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def list_hole_sizes(input_data: ListHoleSizesInput) -> dict[str, Any]:
        """List the Hole Wizard's hole types and sizes for a standard, or one size's dimensions.

        Read from SolidWorks' installed Hole Wizard library, so the sizes are the
        ones `create_hole` accepts; it works without SolidWorks running. Give
        `hole_type` for every hole type of that kind with its sizes, add `fastener`
        for one type only, or `size` for its dimensions (counterbore diameter and
        depth, head diameter, pitch: mm, inches for ansi_inch). Needs: SolidWorks'
        Toolbox library on this machine (or `SOLIDWORKS_MCP_HOLE_LIBRARY`). Changes:
        nothing. Returns: `library` with `standard`, `hole_type`, `units` and either
        `hole_types` (each `name` and `sizes`) or `fastener` and `dimensions`.

        Args:
            input_data: What to list. `hole_type` is required; `standard` is iso.

        Returns:
            dict[str, Any]: The hole types and sizes, or one size's dimensions.
        """
        input_data = normalize_input(input_data, ListHoleSizesInput)
        result = await adapter.list_hole_sizes(
            input_data.standard,
            input_data.hole_type,
            input_data.fastener,
            input_data.size,
        )
        require_success(result, context="Failed to read the Hole Wizard library")
        data = result.data if isinstance(result.data, dict) else {}
        if "dimensions" in data:
            message = f"{data['fastener']} {data['dimensions']['size']} dimensions"
        else:
            names = ", ".join(t["name"] for t in data.get("hole_types", []))
            message = (
                f"{input_data.hole_type} hole types ({input_data.standard}): {names}"
            )
        return {
            "status": "success",
            "message": message,
            "library": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def create_hole(input_data: CreateHoleInput) -> dict[str, Any]:
        """Add a standard Hole Wizard hole (simple, counterbored, countersunk or tapped) at a point on a flat face.

        Use it instead of sketching a circle and cutting: the hole is sized from the
        standard, so it fits the fastener and a drawing shows its callout. Several
        holes on one face: give the rest as `positions` (one feature, a hole at each
        point); a regular grid can also use `pattern_linear`. Sizes come from the
        Hole Wizard library (`list_hole_sizes`): an unknown size is refused, with the
        nearest ones, before anything is made. A simple hole can't be through all in
        SolidWorks, so it is made blind through the part (`end_condition_used`, with
        a warning). It fails when the volume doesn't change. Needs: an open part with
        a flat face at `face_point`. Changes: adds a hole feature. Returns: `hole`
        with `name`, `hole_count`, `hole_type`, `standard`, `fastener` (the hole type
        used), `size`, `end_condition`, `end_condition_used`, and `volume_before`,
        `volume_after` and `volume_change` (mm³).

        Args:
            input_data: The hole. `face_point`, `hole_type` and `size` are
                required; `standard` is iso and `end_condition` through_all (blind
                needs `depth`).

        Returns:
            dict[str, Any]: The hole created.
        """
        input_data = normalize_input(input_data, CreateHoleInput)
        result = await adapter.create_hole(
            input_data.face_point,
            input_data.hole_type,
            input_data.standard,
            input_data.size,
            input_data.end_condition,
            input_data.depth,
            input_data.positions,
            input_data.fastener,
        )
        require_success(result, context="Failed to create hole")
        data = result.data if isinstance(result.data, dict) else {}
        count = int(data.get("hole_count", 1))
        kind = f"{data.get('size', input_data.size)} {input_data.hole_type}"
        return {
            "status": "success",
            "message": (
                f"Added a {kind} hole" if count == 1 else f"Added {count} {kind} holes"
            ),
            "hole": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_cosmetic_thread(input_data: AddCosmeticThreadInput) -> dict[str, Any]:
        """Add a cosmetic thread to a hole or shaft, so drawings show the thread callout.

        Pick the circular edge where the thread starts with a point on it (mm). The
        thread is drawn, not modelled, so the volume must not change; it fails if it
        does. Needs: an open part with a round edge at `edge_point`. Changes: adds a
        Cosmetic Thread feature. Returns: `thread` with `name`, `size`,
        `diameter_mm`, `end_type` and `depth_mm`.

        Args:
            input_data: The thread. `edge_point` and `size` are required;
                `diameter` comes from a metric size; blind (the default) needs
                `depth`.

        Returns:
            dict[str, Any]: The thread added.
        """
        input_data = normalize_input(input_data, AddCosmeticThreadInput)
        result = await adapter.add_cosmetic_thread(
            input_data.edge_point,
            input_data.size,
            cast(float, input_data.diameter),
            input_data.end_type,
            input_data.depth,
            input_data.standard,
        )
        require_success(result, context="Failed to add cosmetic thread")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": f"Added a {input_data.size} cosmetic thread",
            "thread": data,
            "execution_time": result.execution_time,
        }

    # Counted from the registry rather than hand-maintained. The literal that
    # used to live here had drifted: it read 15 while 16 tools were actually
    # registered, so the function misreported its own work and the test
    # enshrined the wrong number.
    tool_count = len(await mcp.list_tools())
    return tool_count

register_server_admin_tools async

register_server_admin_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]) -> int

Register the server-administration tools with FastMCP.

Parameters:

Name Type Description Default
mcp FastMCP

The MCP server.

required
adapter SolidWorksAdapter

Adapter used by every tool.

required
config dict[str, Any]

Server configuration (unused).

required

Returns:

Name Type Description
int int

Number of tools registered.

Source code in src/solidworks_mcp/tools/server_admin.py
async def register_server_admin_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]
) -> int:
    """Register the server-administration tools with FastMCP.

    Args:
        mcp (FastMCP): The MCP server.
        adapter (SolidWorksAdapter): Adapter used by every tool.
        config (dict[str, Any]): Server configuration (unused).

    Returns:
        int: Number of tools registered.
    """
    mcp = with_tool_boundary(mcp)

    @mcp.tool()
    async def reset_circuit_breaker(
        input_data: ResetCircuitBreakerInput | None = None,
    ) -> dict[str, Any]:
        """Let calls through again to operations the server paused after repeated failures.

        Use it when a tool returns `error_type: circuit_open` and you have fixed what
        made the calls fail (a missing sketch, a wrong reference): without a reset, the
        operation stays paused until its timeout. Resetting does not fix the cause; a
        call that fails again counts towards a new pause. Changes: only the server's
        own state; no SolidWorks document is touched. Returns: `data` with `reset`, the
        operations that were paused (empty when none were).

        Args:
            input_data: Optional: `operation` resets only that operation; leave it out
                to reset every one.

        Returns:
            dict[str, Any]: The operations that were reset.
        """
        input_data = normalize_input(input_data, ResetCircuitBreakerInput)
        result = await adapter.reset_circuit_breakers(input_data.operation)
        require_success(result, context="Failed to reset the circuit breaker")
        names = list((result.data or {}).get("reset", []))
        message = f"Reset {', '.join(names)}" if names else "No operation was paused"
        return {"status": "success", "message": message, "data": {"reset": names}}

    return 1

register_sketching_tools async

register_sketching_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]) -> int

Register sketching tools with FastMCP.

Registers comprehensive sketching tools for SolidWorks automation including geometry creation, constraints, dimensions, and pattern operations.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config dict[str, Any]

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Example
from solidworks_mcp.tools.sketching import register_sketching_tools

tool_count = await register_sketching_tools(mcp, adapter, config)
print(f"Registered {tool_count} sketching tools")
Source code in src/solidworks_mcp/tools/sketching.py
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
async def register_sketching_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: dict[str, Any]
) -> int:
    """Register sketching tools with FastMCP.

    Registers comprehensive sketching tools for SolidWorks automation including geometry
    creation, constraints, dimensions, and pattern operations.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (dict[str, Any]): Configuration values for the operation.

    Returns:
        int: The computed numeric result.

    Example:
                        ```python
                        from solidworks_mcp.tools.sketching import register_sketching_tools

                        tool_count = await register_sketching_tools(mcp, adapter, config)
                        print(f"Registered {tool_count} sketching tools")
                        ```
    """
    mcp = with_tool_boundary(mcp)
    tool_count = 0
    # SolidWorks won't rename a sketch while it is open for editing (it
    # accepts the name, but the tree keeps the old one), so the name
    # create_sketch was asked for is given when exit_sketch closes it:
    # SolidWorks' name -> the name asked for.
    names_on_exit: dict[str, str] = {}

    async def _honour_construction(entity_id: Any, construction: bool) -> None:
        """Make a new entity construction geometry when ``construction`` asks.

        The drawing calls themselves draw normal geometry on SolidWorks, so a
        tool's ``construction`` flag is applied afterwards, by id.
        """
        if not construction or not entity_id:
            return
        made = await adapter.set_construction_geometry([str(entity_id)], True)
        require_success(made, context="Failed to make it construction geometry")

    @mcp.tool()
    async def create_sketch(input_data: CreateSketchInput) -> dict[str, Any]:
        """Open a new sketch on a plane or a flat face of the active part, ready for lines, circles and other sketch entities.

        Use it first in every feature: sketch, draw, `exit_sketch`, then
        `create_extrusion` or `create_cut_extrude`. Planes: Front, Top, Right (or XY,
        XZ, YZ), or a plane made by `create_reference_plane`. Or give `face_point`, a
        point in mm on a flat face of the body: the sketch opens on that face and moves
        with it when the model changes. Coordinates in the sketch are mm from the
        origin. Needs: an open part. Changes: adds a sketch and opens it for editing;
        give `sketch_name` and `exit_sketch` names it that when it closes it (SolidWorks
        won't rename an open sketch). Returns: `sketch` with `name` (the name in the
        feature tree now) and `plane` (or `face_point`), plus `name_on_exit` when you
        gave `sketch_name`. A `face_point` not on a flat face fails with
        `invalid_input`.

        Args:
            input_data: The sketch. Exactly one of `plane` and `face_point`;
                `sketch_name` names it.

        Returns:
            dict[str, Any]: The new sketch.
        """
        input_data = normalize_input(input_data, CreateSketchInput)
        face_point = (
            (
                float(input_data.face_point[0]),
                float(input_data.face_point[1]),
                float(input_data.face_point[2]),
            )
            if input_data.face_point is not None
            else None
        )
        if face_point is None:
            result = await adapter.create_sketch(input_data.plane)
            where = f"{input_data.plane} plane"
        else:
            result = await adapter.create_sketch(None, face_point)
            where = f"the face at {list(face_point)} mm"

        require_success(result, context="Failed to create sketch")
        sketch_name = (
            result.data.get("sketch_name")
            if isinstance(result.data, dict)
            else result.data
        )
        sketch: dict[str, Any] = {"name": sketch_name, "plane": input_data.plane}
        if face_point is not None:
            sketch["face_point"] = list(face_point)
        reply: dict[str, Any] = {
            "status": "success",
            "message": f"Created sketch '{sketch_name}' on {where}",
            "sketch": sketch,
            "execution_time": result.execution_time,
        }
        names_on_exit.clear()  # a sketch left open is abandoned with its name
        wanted = (input_data.sketch_name or "").strip()
        if wanted and wanted != sketch_name:
            names_on_exit[str(sketch_name)] = wanted
            reply["sketch"]["name_on_exit"] = wanted
            reply["message"] += f"; exit_sketch names it '{wanted}'"
        return reply

    @mcp.tool()
    async def add_line(input_data: AddLineInput) -> dict[str, Any]:
        """Draw a straight line between two points in the open sketch, in mm.

        Use it for profiles made of straight edges; for a closed box, use
        `add_rectangle`, and for a revolve axis, `add_centerline`. Needs: an open sketch
        (`create_sketch`). Changes: adds a line (a construction line with
        `construction`). Returns: `line` with `id`, `start`, `end` and `length`. Its
        `id` is what `add_sketch_constraint` and `add_sketch_dimension` take.

        Args:
            input_data: The line. Give `x1`, `y1`, `x2`, `y2` (mm; or `start_x`,
                `start_y`, `end_x`, `end_y`); `construction` is off.

        Returns:
            dict[str, Any]: The line added.
        """
        input_data = normalize_input(input_data, AddLineInput)
        if hasattr(adapter, "add_sketch_line"):
            result = await adapter.add_sketch_line(
                input_data.x1,
                input_data.y1,
                input_data.x2,
                input_data.y2,
                input_data.construction,
            )
        else:
            result = await adapter.add_line(
                input_data.x1,  # type: ignore[arg-type]
                input_data.y1,  # type: ignore[arg-type]
                input_data.x2,  # type: ignore[arg-type]
                input_data.y2,  # type: ignore[arg-type]
            )

        require_success(result, context="Failed to add line")
        line_id = (
            result.data.get("entity_id") or result.data.get("id")
            if isinstance(result.data, dict)
            else result.data
        )
        await _honour_construction(line_id, input_data.construction)
        x1, y1 = input_data.x1 or 0.0, input_data.y1 or 0.0
        x2, y2 = input_data.x2 or 0.0, input_data.y2 or 0.0
        return {
            "status": "success",
            "message": f"Added line from ({x1}, {y1}) to ({x2}, {y2})",
            "line": {
                "id": line_id,
                "start": {"x": x1, "y": y1},
                "end": {"x": x2, "y": y2},
                "construction": input_data.construction,
                "length": ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_circle(input_data: AddCircleInput) -> dict[str, Any]:
        """Draw a circle in the open sketch from its centre and radius, in mm.

        Use it for holes and round bosses. Needs: an open sketch (`create_sketch`).
        Changes: adds a circle (a construction circle with `construction`). Returns:
        `circle` with `id`, `center`, `radius` and `diameter`. Its `id` is what
        `add_sketch_constraint` and `add_sketch_dimension` take.

        Args:
            input_data: The circle. `center_x`, `center_y` and `radius` (mm) are
                required; `construction` is off.

        Returns:
            dict[str, Any]: The circle added.
        """
        input_data = normalize_input(input_data, AddCircleInput)
        if hasattr(adapter, "add_sketch_circle"):
            result = await adapter.add_sketch_circle(
                input_data.center_x,
                input_data.center_y,
                input_data.radius,
                input_data.construction,
            )
        else:
            result = await adapter.add_circle(
                input_data.center_x, input_data.center_y, input_data.radius
            )

        require_success(result, context="Failed to add circle")
        circle_id = (
            result.data.get("entity_id")
            if isinstance(result.data, dict)
            else result.data
        )
        await _honour_construction(circle_id, input_data.construction)
        return {
            "status": "success",
            "message": f"Added circle at ({input_data.center_x}, {input_data.center_y}) with radius {input_data.radius}mm",
            "circle": {
                "id": circle_id,
                "center": {"x": input_data.center_x, "y": input_data.center_y},
                "radius": input_data.radius,
                "construction": input_data.construction,
                "diameter": input_data.radius * 2,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_rectangle(input_data: AddRectangleInput) -> dict[str, Any]:
        """Draw a closed, axis-aligned rectangle in the open sketch from two opposite corners, in mm.

        Use it for blocks, plates and rectangular cut-outs. Needs: an open sketch
        (`create_sketch`). Changes: adds four connected lines. Returns: `rectangle` with
        `id`, `corner1`, `corner2`, `width`, `height` and `area`. Its `id` is what
        `add_sketch_constraint` and `add_sketch_dimension` take.

        Args:
            input_data: The rectangle. Give `x1`, `y1`, `x2`, `y2` (mm; or
                `corner1_x` ... `corner2_y`); `construction` is off.

        Returns:
            dict[str, Any]: The rectangle added.
        """
        input_data = normalize_input(input_data, AddRectangleInput)
        if input_data.from_center:
            result = await adapter.add_rectangle(
                input_data.x1,  # type: ignore[arg-type]
                input_data.y1,  # type: ignore[arg-type]
                input_data.x2,  # type: ignore[arg-type]
                input_data.y2,  # type: ignore[arg-type]
                True,
            )
        elif hasattr(adapter, "add_sketch_rectangle"):
            result = await adapter.add_sketch_rectangle(
                input_data.x1,
                input_data.y1,
                input_data.x2,
                input_data.y2,
                input_data.construction,
            )
        else:
            result = await adapter.add_rectangle(
                input_data.x1,  # type: ignore[arg-type]
                input_data.y1,  # type: ignore[arg-type]
                input_data.x2,  # type: ignore[arg-type]
                input_data.y2,  # type: ignore[arg-type]
            )

        require_success(result, context="Failed to add rectangle")
        rect_id = (
            result.data.get("entity_id")
            if isinstance(result.data, dict)
            else result.data
        )
        await _honour_construction(rect_id, input_data.construction)
        rx1, ry1 = input_data.x1 or 0.0, input_data.y1 or 0.0
        rx2, ry2 = input_data.x2 or 0.0, input_data.y2 or 0.0
        if input_data.from_center:
            # x1, y1 is the centre: the real corners mirror x2, y2 through it.
            rx1, ry1 = 2 * rx1 - rx2, 2 * ry1 - ry2
        width = abs(rx2 - rx1)
        height = abs(ry2 - ry1)

        return {
            "status": "success",
            "message": f"Added rectangle from ({rx1}, {ry1}) to ({rx2}, {ry2})",
            "rectangle": {
                "id": rect_id,
                "from_center": input_data.from_center,
                "corner1": {"x": rx1, "y": ry1},
                "corner2": {"x": rx2, "y": ry2},
                "construction": input_data.construction,
                "width": width,
                "height": height,
                "area": width * height,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def exit_sketch() -> dict[str, Any]:
        """Close the open sketch so a feature can use it.

        Call it after drawing and before `create_extrusion`, `create_revolve` or
        `create_cut_extrude`; those use the sketch closed last. Needs: an open sketch.
        Changes: leaves sketch editing; the sketch stays in the feature tree, renamed
        to the `sketch_name` `create_sketch` was given. Returns: a message, plus, after
        a `sketch_name`, `sketch` with `name` (the name in the feature tree, which later
        tools take as `sketch_name`) and a `warning` when the name couldn't be given
        (another feature has it).
        """
        result = await adapter.exit_sketch()

        require_success(result, context="Failed to exit sketch")
        reply: dict[str, Any] = {
            "status": "success",
            "message": "Exited sketch editing mode",
            "execution_time": result.execution_time,
        }
        if names_on_exit:
            current, wanted = names_on_exit.popitem()
            renamed = await adapter.rename_feature(current, wanted)
            name = current
            if renamed.is_success:
                name = (renamed.data or {}).get("new_name") or wanted
            else:
                reply["warning"] = (
                    f"The sketch keeps SolidWorks' name '{current}': it could "
                    f"not be named '{wanted}' ({renamed.error})."
                )
            reply["sketch"] = {"name": name}
        return reply

    @mcp.tool()
    async def check_sketch_fully_defined(
        input_data: CheckSketchDefinitionInput | None = None,
    ) -> dict[str, Any]:
        """Check whether a sketch is fully defined, under-defined or over-defined.

        Use it after adding relations and dimensions, to see if the sketch still has
        free geometry. Needs: an open part with the sketch. Read-only. Returns: `sketch`
        with `sketch_name`, `is_fully_defined` (null when unknown), `definition_state`
        and where the answer came from (`source`).

        Args:
            input_data: Nothing is required: give `sketch_name`, or omit it for the
                open or last sketch.

        Returns:
            dict[str, Any]: The definition state.
        """
        normalized = normalize_input(
            input_data or {},
            CheckSketchDefinitionInput,
        )
        result = await adapter.check_sketch_fully_defined(normalized.sketch_name)

        require_success(result, context="Failed to inspect sketch definition")

        payload = result.data or {}
        state = payload.get("definition_state", "unknown")
        return {
            "status": "success",
            "message": f"Sketch definition state: {state}",
            "sketch": payload,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_arc(input_data: AddArcInput) -> dict[str, Any]:
        """Draw a circular arc in the open sketch: from its centre, through three points, or tangent.

        Use it for rounded ends and curved edges of a profile. `kind` `center` (the
        default) takes the centre, start and end, counter-clockwise; `three_point`
        takes start, end and a point the arc passes through (`through_x`,
        `through_y`); `tangent` starts at the end of an existing entity (`start`) and
        leaves tangent to it. Needs: an open sketch (`create_sketch`). Changes: adds an
        arc. Returns: `arc` with `id`, `kind`, `center`, `start_point` and
        `end_point`. Its `id` is what `add_sketch_constraint` and
        `add_sketch_dimension` take.

        Args:
            input_data: The arc. `start_x`, `start_y`, `end_x` and `end_y` (mm) are
                required; `center_x`, `center_y` for `center`; `through_x`,
                `through_y` for `three_point`.

        Returns:
            dict[str, Any]: The arc added.
        """
        input_data = normalize_input(input_data, AddArcInput)
        through = (
            (input_data.through_x, input_data.through_y)
            if input_data.through_x is not None and input_data.through_y is not None
            else None
        )
        result = await adapter.add_arc(
            input_data.center_x or 0.0,
            input_data.center_y or 0.0,
            input_data.start_x,
            input_data.start_y,
            input_data.end_x,
            input_data.end_y,
            input_data.kind,
            through,
        )

        require_success(result, context="Failed to add arc")
        arc_id = result.data
        center = (
            {"x": input_data.center_x, "y": input_data.center_y}
            if input_data.kind == "center"
            else None
        )
        return {
            "status": "success",
            "message": f"Added a {input_data.kind} arc from "
            f"({input_data.start_x}, {input_data.start_y}) to "
            f"({input_data.end_x}, {input_data.end_y})",
            "arc": {
                "id": arc_id,
                "kind": input_data.kind,
                "center": center,
                "through": (
                    {"x": through[0], "y": through[1]} if through is not None else None
                ),
                "start_point": {
                    "x": input_data.start_x,
                    "y": input_data.start_y,
                },
                "end_point": {"x": input_data.end_x, "y": input_data.end_y},
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_spline(input_data: AddSplineInput) -> dict[str, Any]:
        """Draw a smooth spline through a list of points in the open sketch, in mm.

        Use it for free-form curves such as an aerofoil or a handle profile. Needs: an
        open sketch (`create_sketch`). Changes: adds a spline. Returns: `spline` with
        `id`, `control_points` and `point_count`.

        Args:
            input_data: The spline. `points` (a list of {"x", "y"} in mm) is
                required.

        Returns:
            dict[str, Any]: The spline added.
        """
        result = await adapter.add_spline(input_data.points)

        require_success(result, context="Failed to add spline")
        spline_id = result.data
        return {
            "status": "success",
            "message": f"Added spline with {len(input_data.points)} control points",
            "spline": {
                "id": spline_id,
                "control_points": input_data.points,
                "point_count": len(input_data.points),
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_centerline(input_data: AddLineInput) -> dict[str, Any]:
        """Draw a centreline (construction line) in the open sketch, in mm.

        Use it as the axis for `create_revolve`, or as the line for `sketch_mirror` and
        symmetric relations. It is never part of the profile. Needs: an open sketch
        (`create_sketch`). Changes: adds a centreline. Returns: `centerline` with `id`,
        `start_point` and `end_point`.

        Args:
            input_data: The line. Give `x1`, `y1`, `x2`, `y2` (mm; or `start_x`,
                `start_y`, `end_x`, `end_y`); `construction` is not used.

        Returns:
            dict[str, Any]: The centreline added.
        """
        input_data = normalize_input(input_data, AddLineInput)

        result = await adapter.add_centerline(
            input_data.x1,  # type: ignore[arg-type]
            input_data.y1,  # type: ignore[arg-type]
            input_data.x2,  # type: ignore[arg-type]
            input_data.y2,  # type: ignore[arg-type]
        )

        require_success(result, context="Failed to add centerline")
        centerline_id = result.data
        return {
            "status": "success",
            "message": f"Added centerline from ({input_data.x1}, {input_data.y1}) to ({input_data.x2}, {input_data.y2})",
            "centerline": {
                "id": centerline_id,
                "start_point": {"x": input_data.x1, "y": input_data.y1},
                "end_point": {"x": input_data.x2, "y": input_data.y2},
                "type": "construction",
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_polygon(input_data: dict[str, Any]) -> dict[str, Any]:
        """Draw a regular polygon (e.g. a hexagon) in the open sketch from its centre, radius and number of sides.

        Use it for nuts, hex bosses and pockets. Needs: an open sketch
        (`create_sketch`). Changes: adds the polygon's sides. Returns: `polygon` with
        `id`, `center`, `radius`, `sides` and `angle`. A `diameter` dimension
        (`add_sketch_dimension`) on its `id` sizes it by the circle drawn inside it.

        Args:
            input_data: A mapping: `center_x`, `center_y` (mm, default 0), `radius`
                (mm, circumscribed, default 10), `sides` (default 6) and `angle`
                (degrees the first corner is turned from +x, default 0).

        Returns:
            dict[str, Any]: The polygon added.
        """
        center_x = input_data.get("center_x", 0.0)
        center_y = input_data.get("center_y", 0.0)
        radius = input_data.get("radius", 10.0)
        sides = input_data.get("sides", 6)
        angle = float(input_data.get("angle", 0.0))

        result = await adapter.add_polygon(center_x, center_y, radius, sides, angle)

        require_success(result, context="Failed to add polygon")
        polygon_id = result.data
        return {
            "status": "success",
            "message": f"Added {sides}-sided polygon at ({center_x}, {center_y}) with radius {radius}mm",
            "polygon": {
                "id": polygon_id,
                "center": {"x": center_x, "y": center_y},
                "radius": radius,
                "sides": sides,
                "angle": angle,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_ellipse(input_data: dict[str, Any]) -> dict[str, Any]:
        """Draw an ellipse in the open sketch from its centre and axis lengths, in mm.

        Use it for oval holes and bosses. Needs: an open sketch (`create_sketch`).
        Changes: adds an ellipse. Returns: `ellipse` with `id`, `center`, `major_axis`,
        `minor_axis` and `angle`.

        Args:
            input_data: A mapping: `center_x`, `center_y` (mm, default 0),
                `major_axis` (default 20) and `minor_axis` (default 10), in mm, and
                `angle`, the major axis's turn from +x in degrees (default 0).

        Returns:
            dict[str, Any]: The ellipse added.
        """
        center_x = input_data.get("center_x", 0.0)
        center_y = input_data.get("center_y", 0.0)
        major_axis = input_data.get("major_axis", 20.0)
        minor_axis = input_data.get("minor_axis", 10.0)
        angle = input_data.get("angle", 0.0)

        result = await adapter.add_ellipse(
            center_x, center_y, major_axis, minor_axis, angle
        )

        require_success(result, context="Failed to add ellipse")
        ellipse_id = result.data
        return {
            "status": "success",
            "message": f"Added ellipse at ({center_x}, {center_y}) with axes {major_axis}x{minor_axis}mm",
            "ellipse": {
                "id": ellipse_id,
                "center": {"x": center_x, "y": center_y},
                "major_axis": major_axis,
                "minor_axis": minor_axis,
                "angle": angle,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_sketch_text(input_data: AddSketchTextInput) -> dict[str, Any]:
        """Write text in the open sketch, at a point or centred on one, to deboss or emboss it.

        Use it for lettering, then cut (`create_cut_extrude`) or extrude the sketch.
        `center_at` (a model point in mm) puts the text's middle there; otherwise it
        starts at `x`, `y`. `$PRP:"name"` in the text shows a custom property;
        one holding a dimension's name (`"D1@Sketch3"`, set with `set_custom_property`)
        makes the text follow that dimension. To read from a face's own side, such as
        an underside, open the sketch on that face (`create_sketch` `face_point`).
        Needs: an open sketch (`create_sketch`). Changes: adds text. Returns: the
        text's `id`, the `text` as SolidWorks shows it (links resolved) and its
        `height`.

        Args:
            input_data: The text. `text` is required; `x`, `y` (mm), `height` (3.5
                mm) and `center_at` have defaults.

        Returns:
            dict[str, Any]: The text added.
        """
        input_data = normalize_input(input_data, AddSketchTextInput)
        result = await adapter.add_sketch_text(
            input_data.text,
            input_data.x,
            input_data.y,
            input_data.height,
            input_data.center_at,
        )
        require_success(result, context="Failed to add sketch text")
        added: dict[str, Any] = result.data or {}
        return {
            "status": "success",
            "message": f"Added text {added['text']!r}, {added['height']} mm high",
            "sketch_text": {
                **added,
                "center_at": input_data.center_at,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def set_sketch_text(input_data: SetSketchTextInput) -> dict[str, Any]:
        """Rewrite the text in a sketch and rebuild, so debossed lettering shows it.

        Use it after a change the text should follow: SolidWorks doesn't redraw sketch
        text on rebuild, not even a `$PRP:` link to a dimension an equation changed.
        Give the same `$PRP:"name"` text to re-read the link, or new text. The sketch
        is opened, its text rewritten, closed, and the model rebuilt, so a cut made
        from it changes too. Needs: a part with that sketch, holding text from
        `add_sketch_text`. Changes: the sketch's text. Returns: the `sketch`, each
        text `before` and `after` (links resolved).

        Args:
            input_data: `sketch_name` and `text`, both required.

        Returns:
            dict[str, Any]: The text before and after.
        """
        input_data = normalize_input(input_data, SetSketchTextInput)
        result = await adapter.set_sketch_text(input_data.sketch_name, input_data.text)
        require_success(result, context="Failed to rewrite the sketch text")
        changed: dict[str, Any] = result.data or {}
        return {
            "status": "success",
            "message": f"{input_data.sketch_name} now reads {changed.get('after')}",
            "sketch_text": changed,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_sketch_constraint(input_data: AddRelationInput) -> dict[str, Any]:
        """Add a geometric relation between sketch entities: horizontal, vertical, parallel, perpendicular, tangent, coincident, concentric, equal, symmetric, collinear or fix.

        Use it to hold design intent, then check with `check_sketch_fully_defined`.
        Entities are the `id`s the add_* tools returned. Needs: an open sketch
        (`create_sketch`). Changes: adds the relation. Returns: `constraint` with `id`,
        `type` and the entities.

        Args:
            input_data: The relation. `entity1` and `relation_type` are required;
                `entity2` when the relation needs two, and `entity3` (the
                centreline) only for symmetric.

        Returns:
            dict[str, Any]: The relation added.
        """
        input_data = normalize_input(input_data, AddRelationInput)
        result = await adapter.add_sketch_constraint(
            input_data.entity1,
            input_data.entity2,
            input_data.relation_type,
            input_data.entity3,
        )

        require_success(result, context="Failed to add constraint")
        constraint_id = result.data
        return {
            "status": "success",
            "message": f"Added {input_data.relation_type} constraint between {input_data.entity1} and {input_data.entity2}",
            "constraint": {
                "id": constraint_id,
                "type": input_data.relation_type,
                "entity1": input_data.entity1,
                "entity2": input_data.entity2,
                "entity3": input_data.entity3,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_sketch_dimension(input_data: AddDimensionInput) -> dict[str, Any]:
        """Add a driving dimension to sketch entities: linear, angular, radial, diameter or an ellipse axis.

        Use it to fix a size so the sketch is fully defined and can be changed later
        with `set_dimension`. Entities are the `id`s the add_* tools returned; two
        connected lines for angular, a circle or arc for radial or diameter, an
        ellipse for `major_axis` or `minor_axis` (`value` is the whole axis; the
        dimension runs from the centre, so it shows half). No dialog opens. Needs: an open sketch (`create_sketch`). Changes: adds the dimension and
        resizes the geometry to `value`. Returns: `dimension` with `id`, `type`, `value`
        and the entities.

        Args:
            input_data: The dimension. `entity1` and `value` (mm, or degrees for
                angular) are required; `entity2` and `dimension_type` (linear) have
                defaults.

        Returns:
            dict[str, Any]: The dimension added.
        """
        input_data = normalize_input(input_data, AddDimensionInput)
        result = await adapter.add_sketch_dimension(
            input_data.entity1,
            input_data.entity2,
            input_data.dimension_type,
            input_data.value,
        )

        require_success(result, context="Failed to add dimension")
        dimension_id = result.data
        return {
            "status": "success",
            "message": f"Added {input_data.dimension_type} dimension of {input_data.value}mm to {input_data.entity1}",
            "dimension": {
                "id": dimension_id,
                "type": input_data.dimension_type,
                "value": input_data.value,
                "entity1": input_data.entity1,
                "entity2": input_data.entity2,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def sketch_linear_pattern(input_data: dict[str, Any]) -> dict[str, Any]:
        """Repeat sketch entities in a straight line at a fixed spacing.

        Use it for rows of holes or slots before one cut. Needs: an open sketch
        (`create_sketch`). Changes: adds copies of the entities. Returns: `pattern` with
        `id`, `entities`, `count`, `spacing` and `direction`.

        Args:
            input_data: A mapping: `entities` (ids to repeat), `direction_x`/
                `direction_y` (default 1, 0), `spacing` (mm, default 10) and
                `count` (instances, default 3).

        Returns:
            dict[str, Any]: The pattern added.
        """
        entities = input_data.get("entities", [])
        direction_x = input_data.get("direction_x", 1.0)
        direction_y = input_data.get("direction_y", 0.0)
        spacing = input_data.get("spacing", 10.0)
        count = input_data.get("count", 3)

        result = await adapter.sketch_linear_pattern(
            entities, direction_x, direction_y, spacing, count
        )

        require_success(result, context="Failed to create linear pattern")
        pattern_id = result.data
        return {
            "status": "success",
            "message": f"Created linear pattern with {count} instances, spacing {spacing}mm",
            "pattern": {
                "id": pattern_id,
                "type": "linear",
                "entities": entities,
                "count": count,
                "spacing": spacing,
                "direction": {"x": direction_x, "y": direction_y},
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def sketch_circular_pattern(input_data: dict[str, Any]) -> dict[str, Any]:
        """Repeat sketch entities around the sketch origin, e.g. a bolt circle.

        The centre is always the sketch origin, so draw the first entity at the radius
        you want from it. Then cut once with `create_cut_extrude`. Needs: an open sketch
        (`create_sketch`). Changes: adds copies of the entities. Returns: `pattern` with
        `id`, `entities`, `count` and `angle`.

        Args:
            input_data: A mapping: `entities` (ids to repeat), `count` (instances,
                default 6) and `angle` (degrees spread over, default 360).

        Returns:
            dict[str, Any]: The pattern added.
        """
        entities = input_data.get("entities", [])
        angle = input_data.get("angle", 360.0)
        count = input_data.get("count", 6)

        result = await adapter.sketch_circular_pattern(entities, angle, count)

        require_success(result, context="Failed to create circular pattern")
        pattern_id = result.data
        return {
            "status": "success",
            "message": (
                f"Created circular pattern with {count} instances "
                "around the sketch origin"
            ),
            "pattern": {
                "id": pattern_id,
                "type": "circular",
                "entities": entities,
                "count": count,
                "center": {"x": 0.0, "y": 0.0},
                "angle": angle,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def sketch_mirror(input_data: dict[str, Any]) -> dict[str, Any]:
        """Mirror sketch entities about a centreline in the open sketch.

        Use it for symmetric profiles: draw one half, add a centreline with
        `add_centerline`, then mirror. Needs: an open sketch (`create_sketch`). Changes:
        adds mirrored copies. Returns: `mirror` with `id`, `entities` and `mirror_line`.

        Args:
            input_data: A mapping: `entities` (ids to mirror) and `mirror_line`
                (the centreline's id).

        Returns:
            dict[str, Any]: The mirror added.
        """
        entities = input_data.get("entities", [])
        mirror_line = input_data.get("mirror_line", "")

        result = await adapter.sketch_mirror(entities, mirror_line)

        require_success(result, context="Failed to mirror entities")
        mirror_id = result.data
        return {
            "status": "success",
            "message": f"Mirrored {len(entities)} entities about {mirror_line}",
            "mirror": {
                "id": mirror_id,
                "entities": entities,
                "mirror_line": mirror_line,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def sketch_offset(input_data: dict[str, Any]) -> dict[str, Any]:
        """Offset sketch entities by a distance in mm, outward or inward, e.g. for a wall or a clearance.

        Needs: an open sketch (`create_sketch`). Changes: adds the offset entities.
        Returns: `offset` with `id`, `entities`, `distance` and `direction` (outward or
        inward).

        Args:
            input_data: A mapping: `entities` (ids to offset), `offset_distance`
                (mm, default 5) and `reverse_direction` (inward; default off).

        Returns:
            dict[str, Any]: The offset added.
        """
        entities = input_data.get("entities", [])
        offset_distance = input_data.get("offset_distance", 5.0)
        reverse_direction = input_data.get("reverse_direction", False)

        result = await adapter.sketch_offset(
            entities, offset_distance, reverse_direction
        )

        require_success(result, context="Failed to create offset")
        offset_id = result.data
        direction = "outward" if not reverse_direction else "inward"
        return {
            "status": "success",
            "message": f"Created offset of {len(entities)} entities by {offset_distance}mm ({direction})",
            "offset": {
                "id": offset_id,
                "entities": entities,
                "distance": offset_distance,
                "direction": direction,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def sketch_convert_entities(
        input_data: SketchConvertEntitiesInput,
    ) -> dict[str, Any]:
        """Copy model edges, or a face's whole outline, into the open sketch (Convert Entities).

        Use it to sketch on an existing edge exactly, e.g. to cut a pocket that
        follows a face, instead of re-drawing it at guessed coordinates. Pick edges
        or a face with a point on each (mm). Needs: an open sketch
        (`create_sketch`) and a part with those edges. Changes: adds the copied
        entities, tied to the edges. Returns: `converted` with `entities` (ids for
        the other sketch tools), `count` and `construction`.

        Args:
            input_data: The edges. Give `edge_points`, `face_point`, or both;
                `construction` makes the copies construction geometry.

        Returns:
            dict[str, Any]: The entities added.
        """
        input_data = normalize_input(input_data, SketchConvertEntitiesInput)
        result = await adapter.sketch_convert_entities(
            input_data.edge_points or None,
            input_data.face_point,
            input_data.construction,
        )
        require_success(result, context="Failed to convert entities")
        data = result.data if isinstance(result.data, dict) else {}
        return {
            "status": "success",
            "message": f"Copied {data.get('count', 0)} edge(s) into the sketch",
            "converted": data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_sketch_fillet(input_data: AddSketchFilletInput) -> dict[str, Any]:
        """Round corners of the open sketch to a radius, picking each corner by its point.

        Use it for rounded corners of a 2D profile, such as a cut's outline; round a
        solid's edges with `add_fillet` instead, last in the feature tree. Give
        each corner's `[x, y]` in sketch mm, where two entities meet (for a rectangle,
        its corners). Needs: an open sketch (`create_sketch`) with entities meeting at
        each corner. Changes: each corner becomes an arc and the entities either side
        are trimmed back to it. Returns: `fillet` with one `id` per corner, the
        `corners` and the `radius`. A corner with no sketch point fails as
        `invalid_input` and nothing changes.

        Args:
            input_data: `corners` (a list of `[x, y]` mm) and `radius` (mm).

        Returns:
            dict[str, Any]: The fillets added.
        """
        input_data = normalize_input(input_data, AddSketchFilletInput)
        result = await adapter.add_sketch_fillet(input_data.corners, input_data.radius)
        require_success(result, context="Failed to round the sketch corners")
        made: dict[str, Any] = result.data or {}
        return {
            "status": "success",
            "message": f"Rounded {len(input_data.corners)} corner(s) to "
            f"R{input_data.radius:g} mm",
            "fillet": {**made, "radius": input_data.radius},
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_sketch_chamfer(input_data: AddSketchChamferInput) -> dict[str, Any]:
        """Bevel corners of the open sketch, picking each corner by its point.

        Use it for bevelled corners of a 2D profile; bevel a solid's edges with
        `add_chamfer` instead, last in the feature tree. `distance` alone cuts the same
        distance along both sides; add `distance2` for a different second side, or
        `angle` (degrees) for a distance and an angle. Needs: an open sketch with
        entities meeting at each corner (`[x, y]` in sketch mm). Changes: each corner
        becomes a line and the entities either side are trimmed back to it. Returns:
        `chamfer` with one `id` per corner, the `corners` and the sizes. A corner
        with no sketch point fails as `invalid_input` and nothing changes.

        Args:
            input_data: `corners` and `distance` (mm) are required; `distance2` (mm)
                or `angle` (degrees), not both.

        Returns:
            dict[str, Any]: The chamfers added.
        """
        input_data = normalize_input(input_data, AddSketchChamferInput)
        result = await adapter.add_sketch_chamfer(
            input_data.corners,
            input_data.distance,
            input_data.distance2,
            input_data.angle,
        )
        require_success(result, context="Failed to bevel the sketch corners")
        made: dict[str, Any] = result.data or {}
        return {
            "status": "success",
            "message": f"Bevelled {len(input_data.corners)} corner(s)",
            "chamfer": {
                **made,
                "distance": input_data.distance,
                "distance2": input_data.distance2,
                "angle": input_data.angle,
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def add_slot(input_data: AddSlotInput) -> dict[str, Any]:
        """Draw a slot in the open sketch: straight between two centres, or along an arc.

        Use it for adjustment slots and keyways, then cut it (`create_cut_extrude`).
        A straight slot takes its two end centres, `start` and `end`. An arc slot
        (`kind` `arc`) takes the `center` of the arc it follows and `start` and `end`
        on that arc, counter-clockwise. Needs: an open sketch (`create_sketch`).
        Changes: adds one closed slot. Returns: `slot` with its `id`, the points, the
        `width` and its `area` (mm²).

        Args:
            input_data: `width` (mm), `start` and `end` (`[x, y]` mm) are required;
                `kind` is `straight`; `center` for an arc slot.

        Returns:
            dict[str, Any]: The slot added.
        """
        input_data = normalize_input(input_data, AddSlotInput)
        result = await adapter.add_slot(
            input_data.kind,
            input_data.width,
            input_data.start,
            input_data.end,
            input_data.center,
        )
        require_success(result, context="Failed to draw the slot")
        area = slot_area(
            input_data.kind,
            input_data.width,
            input_data.start,
            input_data.end,
            input_data.center,
        )
        return {
            "status": "success",
            "message": f"Drew a {input_data.kind} slot {input_data.width:g} mm wide",
            "slot": {
                "id": result.data,
                "kind": input_data.kind,
                "start": input_data.start,
                "end": input_data.end,
                "center": input_data.center,
                "width": input_data.width,
                "area": round(area, 4),
            },
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def sketch_trim(input_data: SketchPointInput) -> dict[str, Any]:
        """Trim away the piece of a sketch entity at a point, back to where it meets others.

        Use it to clean up overlapping shapes into one profile, such as the arc inside
        a keyhole. `point` (`[x, y]` sketch mm) is on the piece to remove; an entity
        that meets nothing is removed whole. Needs: an open sketch with an entity at
        the point. Changes: removes that piece. Returns: `trim` with the `point` and
        the sketch's `segments_before` and `segments_after`. A trimmed entity's id may
        no longer name a whole segment. Nothing at the point fails as `invalid_input`.

        Args:
            input_data: `point`, `[x, y]` in sketch mm.

        Returns:
            dict[str, Any]: The trim.
        """
        input_data = normalize_input(input_data, SketchPointInput)
        result = await adapter.sketch_trim(input_data.point)
        require_success(result, context="Failed to trim the sketch")
        return {
            "status": "success",
            "message": f"Trimmed at {input_data.point} mm",
            "trim": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def sketch_extend(input_data: SketchPointInput) -> dict[str, Any]:
        """Lengthen a sketch entity from its end nearest a point until it meets the next entity.

        Use it to close a gap, such as lines that stop short of a circle. `point`
        (`[x, y]` sketch mm) is on the entity, near the end to lengthen. Needs: an
        open sketch with an entity at the point and another in its way. Changes: that
        end moves to the next entity. Returns: `extend` with the `point` and the
        segment counts. Nothing at the point, or nothing to reach, fails as
        `invalid_input`.

        Args:
            input_data: `point`, `[x, y]` in sketch mm.

        Returns:
            dict[str, Any]: The extension.
        """
        input_data = normalize_input(input_data, SketchPointInput)
        result = await adapter.sketch_extend(input_data.point)
        require_success(result, context="Failed to extend the sketch entity")
        return {
            "status": "success",
            "message": f"Extended the entity at {input_data.point} mm",
            "extend": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def set_construction_geometry(
        input_data: SetConstructionGeometryInput,
    ) -> dict[str, Any]:
        """Make sketch entities construction geometry, or normal again, by id.

        Use it for reference lines and circles a feature should ignore, such as a
        line to dimension from or a bolt circle. Needs: an open sketch and ids the
        sketch tools returned. Changes: each entity's construction setting (setting it
        to what it already is changes nothing). Returns: `construction_geometry` with
        each id's state. An unknown id fails as `invalid_input` and nothing changes.

        Args:
            input_data: `entities` (ids) is required; `construction` is on.

        Returns:
            dict[str, Any]: The entities' states.
        """
        input_data = normalize_input(input_data, SetConstructionGeometryInput)
        result = await adapter.set_construction_geometry(
            input_data.entities, input_data.construction
        )
        require_success(result, context="Failed to set construction geometry")
        kind = "construction" if input_data.construction else "normal"
        return {
            "status": "success",
            "message": f"{len(input_data.entities)} entity(ies) now {kind} geometry",
            "construction_geometry": result.data,
            "execution_time": result.execution_time,
        }

    @mcp.tool()
    async def sketch_tutorial_simple_hole() -> dict[str, Any]:
        """Demonstrate the sketch steps: draw a 5 mm circle at the origin on the Top plane and close the sketch.

        It is a fixed example; for a real hole, use `create_sketch`, `add_circle`,
        `exit_sketch` and `create_cut_extrude`. Needs: an open part. Changes: adds a
        sketch with one circle. Returns: `steps_completed`, `next_step` (cut the hole
        with `create_cut_extrude`) and `suggested_cut`, that call's input.
        """
        steps_completed = []

        # Step 1: Create sketch
        sketch_result = await adapter.create_sketch("Top")
        require_success(sketch_result, context="Failed to create sketch")
        sketch_name = (
            sketch_result.data.get("sketch_name")
            if isinstance(sketch_result.data, dict)
            else sketch_result.data
        )
        steps_completed.append(f"Created sketch: {sketch_name}")

        # Step 2: Add circle
        circle_result = await adapter.add_circle(0, 0, 2.5)
        require_success(circle_result, context="Failed to add circle")
        steps_completed.append(f"Added 5mm diameter circle: {circle_result.data}")

        # Step 3: Exit sketch
        exit_result = await adapter.exit_sketch()
        require_success(exit_result, context="Failed to exit sketch")
        steps_completed.append("Exited sketch")

        return {
            "status": "success",
            "message": "Tutorial completed: Simple hole sketch created",
            "steps_completed": steps_completed,
            "next_step": (
                "Cut the hole through the part with create_cut_extrude, using "
                "suggested_cut as its input"
            ),
            "suggested_cut": {
                "sketch_name": sketch_name,
                "depth": 10.0,
                "end_condition": "ThroughAll",
            },
        }

    tool_count = 6  # Legacy count expected by tests
    return tool_count

register_template_management_tools async

register_template_management_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: Any) -> int

Register template management tools with FastMCP.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config Any

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Example

tool_count = await register_template_management_tools(mcp, adapter, config)

Source code in src/solidworks_mcp/tools/template_management.py
async def register_template_management_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: Any
) -> int:
    """Register template management tools with FastMCP.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (Any): Configuration values for the operation.

    Returns:
        int: The computed numeric result.

    Example:
                        >>> tool_count = await register_template_management_tools(mcp, adapter, config)
    """
    mcp = with_tool_boundary(mcp)
    tool_count = 0

    @mcp.tool()
    async def compare_templates(input_data: TemplateComparisonInput) -> dict[str, Any]:
        """Compare two template files on disk: whether they are byte-identical, and how their sizes differ.

        Use it to tell whether two templates are the same file. It does not read the
        templates' settings, so it can't say which properties or dimensions differ.
        Needs: both files on disk. Read-only. Returns: `comparison` with `file_1` and
        `file_2` (size, modified time, SHA-256), `identical` and `size_delta_bytes`.

        Args:
            input_data: The two templates: `template1_path` and `template2_path`,
                both required.

        Returns:
            dict[str, Any]: The comparison.
        """
        file_1 = file_fact_snapshot(input_data.template1_path)
        file_2 = file_fact_snapshot(input_data.template2_path)

        missing = [
            path
            for path, snapshot in (
                (input_data.template1_path, file_1),
                (input_data.template2_path, file_2),
            )
            if snapshot is None
        ]
        if missing:
            raise ToolOperationError(
                "Cannot compare templates; file(s) not found on "
                f"disk: {', '.join(missing)}. No template-property "
                "diff was performed."
            )

        assert file_1 is not None
        assert file_2 is not None
        identical = file_1["sha256"] == file_2["sha256"]

        return {
            "status": "success",
            "message": (
                "Template files are byte-identical"
                if identical
                else "Template files differ on disk"
            ),
            "comparison": {
                "file_1": file_1,
                "file_2": file_2,
                "identical": identical,
                "size_delta_bytes": file_2["size_bytes"] - file_1["size_bytes"],
            },
            "note": (
                "This is a filesystem-level comparison only; template "
                "properties, dimensions, and formatting were not "
                "analyzed."
            ),
        }

    tool_count = 1  # compare_templates
    return tool_count

register_tools async

register_tools(mcp: FastMCP, adapter: Any, config: Any) -> int

Register all SolidWorks MCP tools.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter Any

Adapter instance used for the operation.

required
config Any

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Source code in src/solidworks_mcp/tools/__init__.py
async def register_tools(mcp: FastMCP, adapter: Any, config: Any) -> int:
    """Register all SolidWorks MCP tools.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (Any): Adapter instance used for the operation.
        config (Any): Configuration values for the operation.

    Returns:
        int: The computed numeric result.
    """
    logger.info("Registering SolidWorks MCP tools...")

    # Register tool categories
    await register_modeling_tools(mcp, adapter, config)
    await register_equation_tools(mcp, adapter, config)
    await register_assembly_tools(mcp, adapter, config)
    await register_sketching_tools(mcp, adapter, config)
    await register_drawing_tools(mcp, adapter, config)
    await register_drawing_analysis_tools(mcp, adapter, config)
    await register_drawing_automation_tools(mcp, adapter, config)
    await register_engineering_data_tools(mcp, adapter, config)
    await register_analysis_tools(mcp, adapter, config)
    await register_export_tools(mcp, adapter, config)
    await register_automation_tools(mcp, adapter, config)
    await register_file_management_tools(mcp, adapter, config)
    await register_vba_generation_tools(mcp, adapter, config)
    await register_template_management_tools(mcp, adapter, config)
    await register_macro_recording_tools(mcp, adapter, config)
    await register_docs_discovery_tools(mcp, adapter, config)
    await register_server_admin_tools(mcp, adapter, config)

    tool_count = len(await mcp.list_tools())

    logger.info(f"Registered {tool_count} SolidWorks tools")
    return tool_count

register_vba_generation_tools async

register_vba_generation_tools(mcp: FastMCP, adapter: SolidWorksAdapter, config: Any) -> int

Register VBA generation tools with FastMCP.

Parameters:

Name Type Description Default
mcp FastMCP

The mcp value.

required
adapter SolidWorksAdapter

Adapter instance used for the operation.

required
config Any

Configuration values for the operation.

required

Returns:

Name Type Description
int int

The computed numeric result.

Example

tool_count = await register_vba_generation_tools(mcp, adapter, config)

Source code in src/solidworks_mcp/tools/vba_generation.py
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
async def register_vba_generation_tools(
    mcp: FastMCP, adapter: SolidWorksAdapter, config: Any
) -> int:
    """Register VBA generation tools with FastMCP.

    Args:
        mcp (FastMCP): The mcp value.
        adapter (SolidWorksAdapter): Adapter instance used for the operation.
        config (Any): Configuration values for the operation.

    Returns:
        int: The computed numeric result.

    Example:
                        >>> tool_count = await register_vba_generation_tools(mcp, adapter, config)
    """
    mcp = with_tool_boundary(mcp)
    tool_count = 0

    @mcp.tool()
    async def generate_vba_extrusion(input_data: VBAExtrusionInput) -> dict[str, Any]:
        """Write a VBA macro that makes an extrusion with the full FeatureExtrusion options: both directions, draft, thin wall, end conditions.

        Use it when `create_extrusion` doesn't cover an option you need, or to hand the
        step to someone as a macro. The macro comes back as text for the SolidWorks VBA
        editor (Alt+F11); `execute_macro` runs only binary .swp files, so it can't run
        this directly. Read-only. Nothing runs in SolidWorks. Returns: `vba_code`, the
        `parameters` used and `usage_instructions`.

        Args:
            input_data: The extrusion. `depth` (mm) is required; everything else
                (`sketch_name`, draft, thin-wall and end-condition options) has a
                default.

        Returns:
            dict[str, Any]: The macro text.
        """
        if hasattr(adapter, "generate_vba_extrusion"):
            result = await adapter.generate_vba_extrusion(input_data.model_dump())
            require_success(result, "Failed to generate VBA code")
            return {
                "status": "success",
                "message": "Generated VBA code for advanced extrusion",
                "data": result.data,
                "execution_time": result.execution_time,
            }

        vba_code = f"""Sub CreateAdvancedExtrusion()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeatMgr As SldWorks.FeatureManager
Dim swFeat As SldWorks.Feature
Dim myFeature As Object

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

If swModel Is Nothing Then
    MsgBox "No active document"
    Exit Sub
End If

Set swFeatMgr = swModel.FeatureManager

' Advanced extrusion with full parameter control
myFeature = swFeatMgr.FeatureExtruThin2( _
    {input_data.depth / 1000.0}, _  ' Depth (meters)
    {input_data.depth2 / 1000.0}, _ ' Depth2 (meters)
    {str(input_data.both_directions).lower()}, _ ' BothDirections
    {input_data.draft_angle * 3.14159 / 180.0}, _ ' DraftAngle (radians)
    {0}, _ ' DraftAngle2
    {str(input_data.draft_outward).lower()}, _ ' DraftOutward
    {str(input_data.draft_outward).lower()}, _ ' DraftOutward2
    {str(input_data.merge_result).lower()}, _ ' MergeResult
    {str(input_data.feature_scope).lower()}, _ ' FeatureScope
    {str(input_data.auto_select).lower()}, _ ' AutoSelect
    {input_data.thin_thickness / 1000.0}, _ ' Thickness (meters)
    {input_data.thin_thickness / 1000.0}, _ ' Thickness2
    False, _ ' ReverseOffset
    {str(input_data.both_directions).lower()}, _ ' BothDirectionThin
    {str(input_data.cap_ends).lower()}, _ ' CapEnds
    0, _ ' EndCondition
    0 _ ' EndCondition2
)

If myFeature Is Nothing Then
    MsgBox "Failed to create extrusion"
Else
    MsgBox "Advanced extrusion created successfully"
End If

End Sub"""

        return {
            "status": "success",
            "message": "Generated VBA code for advanced extrusion",
            "vba_code": vba_code,
            "parameters": {
                "depth": input_data.depth,
                "both_directions": input_data.both_directions,
                "thin_feature": input_data.thin_feature,
                "parameter_count": 17,
                "complexity": "complex",
            },
            "usage_instructions": [
                "1. Open SolidWorks VBA editor (Tools > Macro > Edit)",
                "2. Create new module and paste the generated code",
                "3. Ensure you have an active sketch selected",
                "4. Run the macro (F5)",
            ],
        }

    @mcp.tool()
    async def generate_vba_revolve(input_data: VBARevolveInput) -> dict[str, Any]:
        """Write a VBA macro that revolves the selected sketch, with both-direction and thin-wall options.

        Use it when `create_revolve` doesn't cover an option you need. The macro comes
        back as text for the SolidWorks VBA editor (Alt+F11); `execute_macro` runs only
        binary .swp files, so it can't run this directly. Read-only. Nothing runs in
        SolidWorks. Returns: `vba_code`, the `parameters` used and `usage_instructions`.

        Args:
            input_data: The revolve. Give `angle` in degrees (it defaults to 0, a
                revolve of nothing); `sketch_name`, `axis_reference`, both-direction
                and thin-wall options have defaults.

        Returns:
            dict[str, Any]: The macro text.
        """
        if hasattr(adapter, "generate_vba_revolve"):
            result = await adapter.generate_vba_revolve(input_data.model_dump())
            require_success(result, "Failed to generate VBA code")
            return {
                "status": "success",
                "message": "Generated VBA code for advanced revolve",
                "data": result.data,
                "execution_time": result.execution_time,
            }

        vba_code = f"""Sub CreateAdvancedRevolve()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeatMgr As SldWorks.FeatureManager
Dim myFeature As Object

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swFeatMgr = swModel.FeatureManager

' Advanced revolve with thin-wall options
myFeature = swFeatMgr.FeatureRevolve2( _
    True, _ ' SingleDir
    {str(input_data.both_directions).lower()}, _ ' IsSolid
    False, _ ' IsThin
    False, _ ' ReverseDir
    3, _ ' Type (swEndCondBlind)
    3, _ ' Type2
    {(input_data.angle or 0.0) * 3.14159 / 180.0}, _ ' Angle (radians)
    {(input_data.angle2 or 0.0) * 3.14159 / 180.0}, _ ' Angle2
    {str(input_data.merge_result).lower()}, _ ' MergeResult
    False, _ ' FeatureScope
    False, _ ' Auto
    True, _ ' AssemblyFeatureScope
    0, _ ' AutoSelectComponents
    False, _ ' PropagateFeatureToParts
    True, _ ' CreateSelectionSet
    False, _ ' CurveToOrderUse
    False _ ' UseMachinedSurface
)

If myFeature Is Nothing Then
    MsgBox "Failed to create revolve"
Else
    MsgBox "Advanced revolve created successfully"
End If

End Sub"""

        return {
            "status": "success",
            "message": "Generated VBA code for advanced revolve",
            "vba_code": vba_code,
            "parameters": {
                "angle": input_data.angle,
                "both_directions": input_data.both_directions,
                "thin_feature": input_data.thin_feature,
                "parameter_count": 16,
                "complexity": "complex",
            },
            "usage_instructions": [
                "1. Create a sketch profile and axis of revolution",
                "2. Select the sketch and axis",
                "3. Run the generated VBA macro",
                "4. Check the revolve feature in the feature tree",
            ],
        }

    @mcp.tool()
    async def generate_vba_assembly_insert(
        input_data: VBAAssemblyInput,
    ) -> dict[str, Any]:
        """Write a VBA macro that inserts a component into the active assembly at a point.

        To insert a component directly, use `insert_component`. The macro comes back as
        text for the SolidWorks VBA editor (Alt+F11); `execute_macro` runs only binary
        .swp files, so it can't run this directly. Read-only. Nothing runs in
        SolidWorks. Returns: `vba_code`, the `parameters` used and `usage_instructions`.

        Args:
            input_data: The component. Give `component_path` (the .sldprt or
                .sldasm); `insertion_point` and `rotation` default to the origin.

        Returns:
            dict[str, Any]: The macro text.
        """
        if hasattr(adapter, "generate_vba_assembly_insert"):
            result = await adapter.generate_vba_assembly_insert(input_data.model_dump())
            require_success(result, "Failed to generate VBA code")
            return {
                "status": "success",
                "message": "Generated VBA code for component insertion",
                "data": result.data,
                "execution_time": result.execution_time,
            }

        vba_code = f'''Sub InsertComponent()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel

' Insert component at specified location
Set swComp = swAssy.AddComponent5( _
    "{input_data.component_path}", _
    0, _ ' Configuration
    "", _ ' Config name
    False, _ ' LoadModel
    "", _ ' ReferenceName
    {input_data.insertion_point[0] / 1000.0}, _ ' X position (meters)
    {input_data.insertion_point[1] / 1000.0}, _ ' Y position (meters)
    {input_data.insertion_point[2] / 1000.0} _ ' Z position (meters)
)

If swComp Is Nothing Then
    MsgBox "Failed to insert component"
Else
    MsgBox "Component inserted successfully: " & swComp.Name2
End If

End Sub'''

        return {
            "status": "success",
            "message": "Generated VBA code for component insertion",
            "vba_code": vba_code,
            "parameters": {
                "component_path": input_data.component_path,
                "insertion_point": input_data.insertion_point,
                "operation_type": input_data.operation_type,
            },
            "usage_instructions": [
                "1. Open the target assembly document",
                "2. Ensure the component file path exists",
                "3. Run the macro to insert the component",
                "4. Add mates as needed for positioning",
            ],
        }

    @mcp.tool()
    async def generate_vba_drawing_views(input_data: VBADrawingInput) -> dict[str, Any]:
        """Write a VBA macro that places a model's standard views (front, top, right) on the active drawing.

        To make a drawing directly, use `generate_drawing_package` or
        `create_drawing_view`. The macro comes back as text for the SolidWorks VBA
        editor (Alt+F11); `execute_macro` runs only binary .swp files, so it can't run
        this directly. Read-only. Nothing runs in SolidWorks. Returns: `vba_code`, the
        `parameters` used and `usage_instructions`.

        Args:
            input_data: The views. Give `model_path` (the model to draw);
                `scale`, `sheet_format` and the view layout have defaults. The
                drawing must be open and active when the macro runs.

        Returns:
            dict[str, Any]: The macro text.
        """
        if hasattr(adapter, "generate_vba_drawing_views"):
            result = await adapter.generate_vba_drawing_views(input_data.model_dump())
            require_success(result, "Failed to generate VBA code")
            return {
                "status": "success",
                "message": "Generated VBA code for drawing views",
                "data": result.data,
                "execution_time": result.execution_time,
            }

        vba_code = f'''Sub CreateDrawingViews()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swSheet As SldWorks.Sheet

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel

If swDraw Is Nothing Then
    MsgBox "No active drawing document"
    Exit Sub
End If

Set swSheet = swDraw.GetCurrentSheet

' Create standard 3 view drawing
' Front view
Set swView = swDraw.CreateDrawViewFromModelView3( _
    "{input_data.model_path}", _
    "*Front", _
    0.1, _ ' X position
    0.15, _ ' Y position
    0 _ ' Z position
)

swView.ScaleRatio = Array({input_data.scale}, 1)

' Top view
Set swView = swDraw.CreateDrawViewFromModelView3( _
    "{input_data.model_path}", _
    "*Top", _
    0.1, _
    0.25, _
    0 _
)

swView.ScaleRatio = Array({input_data.scale}, 1)

' Right view
Set swView = swDraw.CreateDrawViewFromModelView3( _
    "{input_data.model_path}", _
    "*Right", _
    0.25, _
    0.15, _
    0 _
)

swView.ScaleRatio = Array({input_data.scale}, 1)

MsgBox "Drawing views created successfully"

End Sub'''

        return {
            "status": "success",
            "message": "Generated VBA code for drawing views",
            "vba_code": vba_code,
            "parameters": {
                "model_path": input_data.model_path,
                "scale": input_data.scale,
                "sheet_format": input_data.sheet_format,
            },
            "usage_instructions": [
                "1. Create a new drawing document",
                "2. Verify the 3D model path is correct",
                "3. Run the macro to create standard views",
                "4. Adjust view positions as needed",
            ],
        }

    @mcp.tool()
    async def generate_vba_batch_export(input_data: VBABatchInput) -> dict[str, Any]:
        """Write a VBA macro that opens every matching file in a folder and saves a STEP copy of each to another folder.

        To export one file directly, use `export_step`. The macro comes back as text for
        the SolidWorks VBA editor (Alt+F11); `execute_macro` runs only binary .swp
        files, so it can't run this directly. Read-only. Nothing runs in SolidWorks.
        Returns: `vba_code`, the `parameters` used and `usage_instructions`.

        Args:
            input_data: The batch. Give `operation_type` (e.g. "export"),
                `file_pattern` (e.g. "*.sldprt"), `source_folder` and
                `target_folder`; `recursive` defaults to on.

        Returns:
            dict[str, Any]: The macro text.
        """
        if hasattr(adapter, "generate_vba_batch_export"):
            result = await adapter.generate_vba_batch_export(input_data.model_dump())
            require_success(result, "Failed to generate VBA code")
            return {
                "status": "success",
                "message": f"Generated VBA code for batch {input_data.operation_type}",
                "data": result.data,
                "execution_time": result.execution_time,
            }

        vba_code = f'''Sub BatchExport()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim fso As FileSystemObject
Dim folder As Folder
Dim file As file
Dim filePath As String
Dim exportPath As String
Dim processedCount As Integer

Set swApp = Application.SldWorks
Set fso = New FileSystemObject
Set folder = fso.GetFolder("{input_data.source_folder}")

processedCount = 0

' Process all files matching pattern
For Each file In folder.Files
    If LCase(file.Name) Like LCase("{input_data.file_pattern}") Then
        filePath = file.Path

        ' Open the file
        Set swModel = swApp.OpenDoc6(filePath, 1, 1, "", 0, 0)

        If Not swModel Is Nothing Then
            ' Generate export path
            exportPath = "{input_data.target_folder}" & "\\" & _
                fso.GetBaseName(file.Name) & ".step"

            ' Export as STEP
            swModel.SaveAs2 exportPath, 0, True, False

            ' Close the model
            swApp.CloseDoc swModel.GetTitle

            processedCount = processedCount + 1
        End If
    End If
Next file

MsgBox "Batch export completed. Processed " & processedCount & " files."

End Sub'''

        return {
            "status": "success",
            "message": f"Generated VBA code for batch {input_data.operation_type}",
            "vba_code": vba_code,
            "parameters": {
                "operation_type": input_data.operation_type,
                "file_pattern": input_data.file_pattern,
                "source_folder": input_data.source_folder,
                "target_folder": input_data.target_folder,
                "recursive": input_data.recursive,
            },
            "usage_instructions": [
                "1. Verify source and target folder paths exist",
                "2. Close any open documents to avoid conflicts",
                "3. Run the macro - it will process all matching files",
                "4. Check the target folder for exported files",
            ],
        }

    @mcp.tool()
    async def generate_vba_part_modeling(input_data: dict[str, Any]) -> dict[str, Any]:
        """Write a VBA macro for a shell or a fillet on the active part.

        Use it for a shell, which no direct tool makes yet; for a fillet, `add_fillet`
        works directly. The macro comes back as text for the SolidWorks VBA editor
        (Alt+F11); `execute_macro` runs only binary .swp files, so it can't run this
        directly. Read-only. Nothing runs in SolidWorks. Returns: `vba_code`, the
        `parameters` used and `usage_instructions`.

        Args:
            input_data: A mapping with `operation`, "shell" (the default, with
                `thickness` in mm, default 2) or "fillet" (with `radius` in mm,
                default 5). Any other operation is refused.

        Returns:
            dict[str, Any]: The macro text.
        """
        operation = input_data.get("operation", "shell")
        thickness = input_data.get("thickness", 2.0)

        if operation == "shell":
            vba_code = f"""Sub CreateShell()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeatMgr As SldWorks.FeatureManager
Dim swFeat As SldWorks.Feature

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swFeatMgr = swModel.FeatureManager

' Create shell feature
Set swFeat = swFeatMgr.FeatureShell2( _
    {thickness / 1000.0}, _ ' Thickness (meters)
    False, _ ' OutwardThickness
    False, _ ' MultiThickness
    False, _ ' ShowPreview
    False, _ ' MultipleFaceDef
    False _ ' MultipleThicknessDef
)

If swFeat Is Nothing Then
    MsgBox "Failed to create shell"
Else
    MsgBox "Shell created successfully with thickness: {thickness}mm"
End If

End Sub"""
        elif operation == "fillet":
            radius = input_data.get("radius", 5.0)
            vba_code = f"""Sub CreateFillet()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeatMgr As SldWorks.FeatureManager

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swFeatMgr = swModel.FeatureManager

' Create fillet feature
swFeatMgr.FeatureFillet4( _
    {radius / 1000.0}, _ ' Radius (meters)
    0, _ ' SecondRadius
    0, _ ' RollOnOff
    0, _ ' RollFirstRadius
    0, _ ' RollSecondRadius
    0, _ ' RollSmoothTransition
    0, _ ' ConicRho
    0, _ ' SetbackDistance
    False, _ ' AssemblyFeatureScope
    False, _ ' AutoSelectComponents
    False, _ ' PropagateFeatureToParts
    False, _ ' CreateSelectionSet
    False, _ ' EnableSolidPreview
    0 _ ' CornerType
)

swModel.ClearSelection2 True
MsgBox "Fillet created with radius: {radius}mm"

End Sub"""
        else:
            raise InvalidInputError(f"Unsupported operation: {operation}")

        return {
            "status": "success",
            "message": f"Generated VBA code for {operation} operation",
            "vba_code": vba_code,
            "parameters": input_data,
            "usage_instructions": [
                "1. Select the appropriate faces/edges",
                "2. Run the generated VBA macro",
                "3. Verify the feature was created correctly",
            ],
        }

    @mcp.tool()
    async def generate_vba_assembly_mates(input_data: dict[str, Any]) -> dict[str, Any]:
        """Write a VBA macro that adds a concentric, distance, parallel, perpendicular or coincident mate between two selected faces or edges.

        To add a mate directly, use `add_mate`. The macro comes back as text for the
        SolidWorks VBA editor (Alt+F11); `execute_macro` runs only binary .swp files, so
        it can't run this directly. The faces must be selected in SolidWorks before the
        macro runs. Read-only. Nothing runs in SolidWorks. Returns: `vba_code`, the
        `parameters` used and `usage_instructions`.

        Args:
            input_data: A mapping with `mate_type` (default "concentric"),
                `component1` and `component2` (names, for the comments), and
                `distance` in mm for a distance mate.

        Returns:
            dict[str, Any]: The macro text.
        """
        mate_type = input_data.get("mate_type", "concentric")
        component1 = input_data.get("component1", "Part1")
        component2 = input_data.get("component2", "Part2")
        distance = input_data.get("distance", 0.0)

        mate_type_map = {
            "concentric": "swMateType_CONCENTRIC",
            "distance": "swMateType_DISTANCE",
            "parallel": "swMateType_PARALLEL",
            "perpendicular": "swMateType_PERPENDICULAR",
            "coincident": "swMateType_COINCIDENT",
        }

        sw_mate_type = mate_type_map.get(mate_type, "swMateType_CONCENTRIC")

        vba_code = f"""Sub CreateAssemblyMate()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swMateFeats As Variant
Dim swMate As SldWorks.Feature

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel

' Clear existing selections
swModel.ClearSelection2 True

' Note: Entity selection needs to be done before running this macro
' Select appropriate faces/edges on {component1} and {component2}

' Create {mate_type} mate
swMateFeats = swAssy.AddMate5( _
    {sw_mate_type}, _ ' Mate type
    swMateAlign_ALIGNED, _ ' Alignment
    False, _ ' Flip
    {distance / 1000.0}, _ ' Distance (meters)
    0, _ ' Distance2
    0, _ ' Angle
    0, _ ' Angle2
    False, _ ' LockRotation
    0, _ ' WidthMateOption
    0, _ ' For Positioning Only
    0 _ ' LockToSketch
)

If IsEmpty(swMateFeats) Then
    MsgBox "Failed to create mate. Check entity selection."
Else
    Set swMate = swMateFeats(0)
    MsgBox "Mate created: " & swMate.Name
End If

End Sub"""

        return {
            "status": "success",
            "message": f"Generated VBA code for {mate_type} mate",
            "vba_code": vba_code,
            "parameters": {
                "mate_type": mate_type,
                "component1": component1,
                "component2": component2,
                "distance": distance,
            },
            "usage_instructions": [
                "1. Open the assembly document",
                "2. Select the two faces/edges to mate",
                "3. Run the generated macro",
                "4. Verify the mate was created in the feature tree",
            ],
        }

    @mcp.tool()
    async def generate_vba_drawing_dimensions(
        input_data: dict[str, Any],
    ) -> dict[str, Any]:
        """Write a VBA macro that adds a dimension to the selected drawing entities and sets its precision.

        To bring the model's dimensions into a drawing directly, use
        `insert_model_dimensions`. The macro comes back as text for the SolidWorks VBA
        editor (Alt+F11); `execute_macro` runs only binary .swp files, so it can't run
        this directly. The entities must be selected before the macro runs. Read-only.
        Nothing runs in SolidWorks. Returns: `vba_code`, the `parameters` used and
        `usage_instructions`.

        Args:
            input_data: A mapping with `dimension_type` ("linear", the default,
                "radial", "angular" or "diameter") and `precision` (decimal places,
                default 2).

        Returns:
            dict[str, Any]: The macro text.
        """
        dimension_type = input_data.get("dimension_type", "linear")
        precision = input_data.get("precision", 2)

        vba_code = f'''Sub CreateDrawingDimensions()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swDisp As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel

' Get active drawing view
Set swView = swDraw.GetFirstView ' Sheet
Set swView = swView.GetNextView ' First drawing view

If swView Is Nothing Then
    MsgBox "No drawing view found"
    Exit Sub
End If

swModel.ClearSelection2 True

' Note: Select appropriate entities before running
' For linear dimensions: select two edges or points
' For radial: select arc or circle
' For angular: select two lines

Select Case "{dimension_type}"
    Case "linear"
        Set swDisp = swModel.AddDimension2(0.05, 0.05, 0)
    Case "radial"
        Set swDisp = swModel.AddDimension2(0.05, 0.05, 0)
    Case "angular"
        Set swDisp = swModel.AddDimension2(0.05, 0.05, 0)
    Case "diameter"
        Set swDisp = swModel.AddDimension2(0.05, 0.05, 0)
End Select

If Not swDisp Is Nothing Then
    Set swDim = swDisp.GetDimension2(0)
    ' Set precision
    swDim.SetPrecision2 swLinear, {precision}
    MsgBox "Dimension created with {precision} decimal places"
Else
    MsgBox "Failed to create dimension. Check entity selection."
End If

End Sub'''

        return {
            "status": "success",
            "message": f"Generated VBA code for {dimension_type} dimension",
            "vba_code": vba_code,
            "parameters": {
                "dimension_type": dimension_type,
                "precision": precision,
            },
            "usage_instructions": [
                "1. Open a drawing with views",
                "2. Select two edges, points, or appropriate entities",
                "3. Run the macro to create dimension",
                "4. Adjust dimension position as needed",
            ],
        }

    @mcp.tool()
    async def generate_vba_file_operations(
        input_data: dict[str, Any],
    ) -> dict[str, Any]:
        """Write a VBA macro that sets a custom property in every configuration, or runs Pack and Go on the active document.

        To set a property directly, use `set_custom_property`; for Pack and Go, use
        `pack_and_go_assembly`. The macro comes back as text for the SolidWorks VBA
        editor (Alt+F11); `execute_macro` runs only binary .swp files, so it can't run
        this directly. Read-only. Nothing runs in SolidWorks. Returns: `vba_code`, the
        `parameters` used and `usage_instructions`.

        Args:
            input_data: A mapping with `operation`: "custom_properties" (the
                default, with `property_name` and `property_value`) or
                "pack_and_go" (with `target_folder`). Any other operation is
                refused.

        Returns:
            dict[str, Any]: The macro text.
        """
        operation = input_data.get("operation", "custom_properties")

        if operation == "custom_properties":
            property_name = input_data.get("property_name", "Material")
            property_value = input_data.get("property_value", "Steel")

            vba_code = f'''Sub SetCustomProperties()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim configNames As Variant
Dim i As Integer

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

' Get configuration names
configNames = swModel.GetConfigurationNames

' Set property for all configurations
For i = 0 To UBound(configNames)
    Set swCustPropMgr = swModel.Extension.CustomPropertyManager(configNames(i))

    swCustPropMgr.Add3 "{property_name}", swCustomInfoText, _
        "{property_value}", swCustomPropertyAddOption_ReplaceValue
Next i

' Also set at file level
Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
swCustPropMgr.Add3 "{property_name}", swCustomInfoText, _
    "{property_value}", swCustomPropertyAddOption_ReplaceValue

MsgBox "Custom property '{property_name}' set to '{property_value}'"

End Sub'''

        elif operation == "pack_and_go":
            target_folder = input_data.get("target_folder", "C:\\PackAndGo")

            vba_code = f'''Sub PackAndGo()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPackAndGo As SldWorks.PackAndGo
Dim pgStatus As Long
Dim pgWarnings As Long

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

Set swPackAndGo = swApp.GetPackAndGo

' Set Pack and Go settings
swPackAndGo.IncludeDrawings = True
swPackAndGo.IncludeSimulationResults = False
swPackAndGo.IncludeToolboxComponents = False
swPackAndGo.FlattenToSingleFolder = True

' Set destination folder
swPackAndGo.SetSaveToName swModel.GetTitle, "{target_folder}"

' Execute Pack and Go
swPackAndGo.PackAndGo2 pgStatus, pgWarnings

If pgStatus = 0 Then
    MsgBox "Pack and Go completed successfully"
Else
    MsgBox "Pack and Go failed with status: " & pgStatus
End If

End Sub'''
        else:
            raise InvalidInputError(f"Unsupported operation: {operation}")

        return {
            "status": "success",
            "message": f"Generated VBA code for {operation}",
            "vba_code": vba_code,
            "parameters": input_data,
            "usage_instructions": [
                "1. Open the target document",
                "2. Run the generated macro",
                "3. Check results in properties or file system",
            ],
        }

    @mcp.tool()
    async def generate_vba_macro_recorder(input_data: dict[str, Any]) -> dict[str, Any]:
        """Write a VBA starting point: a macro skeleton to fill in, or a note on starting and stopping SolidWorks' macro recorder.

        Use "create_template" for an empty macro with the usual SolidWorks setup.
        Recording itself is done in SolidWorks (Tools > Macro > Record). The macro comes
        back as text for the SolidWorks VBA editor (Alt+F11); `execute_macro` runs only
        binary .swp files, so it can't run this directly. Read-only. Nothing runs in
        SolidWorks. Returns: `vba_code`, the `parameters` used and `usage_instructions`.

        Args:
            input_data: A mapping with `operation`: "start_recording" (the
                default), "stop_recording" or "create_template" (with
                `macro_name`). Any other operation is refused.

        Returns:
            dict[str, Any]: The macro text.
        """
        operation = input_data.get("operation", "start_recording")

        if operation == "start_recording":
            vba_code = """Sub StartMacroRecording()
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks

' Enable macro recording
swApp.UnloadAddIn "SldWorks.Addin.Utilities.MacroRecorder"
swApp.LoadAddIn "SldWorks.Addin.Utilities.MacroRecorder"

' Note: Use Tools > Macro > Record in SolidWorks interface
' This code prepares the environment for recording

MsgBox "Macro recording environment prepared. Use Tools > Macro > Record to start."

End Sub"""

        elif operation == "stop_recording":
            vba_code = """Sub StopMacroRecording()
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks

' Note: Use Tools > Macro > Stop Record in SolidWorks interface
' This code helps manage the recording session

MsgBox "Stop recording using Tools > Macro > Stop Record"

End Sub"""

        elif operation == "create_template":
            macro_name = input_data.get("macro_name", "CustomMacro")
            vba_code = f"""Sub {macro_name}Template()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
' Add more variables as needed

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

' TODO: Add your custom automation code here
' This template provides the basic SolidWorks API structure

' Error handling
If swModel Is Nothing Then
    MsgBox "No active document"
    Exit Sub
End If

' Your automation logic goes here...

MsgBox "Custom macro template ready for customization"

End Sub"""
        else:
            raise InvalidInputError(f"Unsupported operation: {operation}")

        return {
            "status": "success",
            "message": f"Generated VBA code for macro {operation}",
            "vba_code": vba_code,
            "parameters": input_data,
            "usage_instructions": [
                "1. Open SolidWorks VBA editor (Alt+F11)",
                "2. Create new module and paste code",
                "3. Customize as needed for your workflow",
            ],
        }

    # Core VBA generation tools
    tool_count = 10
    return tool_count