The helpers.py module#

Summary#

server_child_pids

Return the process IDs of this server’s direct child processes.

launched_mechanical_processes

Return Mechanical processes this server started after baseline was taken.

stop_processes

Terminate processes, then kill any that are still running after timeout.

resolve_transport_mode

Determine the gRPC transport mode and certificate directory to use.

list_instances

List running Mechanical instances on the system.

get_info

Get information from the Mechanical instance.

Description#

Helper functions for PyMechanical-MCP.

Module detail#

helpers.server_child_pids() → frozenset[int]#

Return the process IDs of this server’s direct child processes.

helpers.launched_mechanical_processes(baseline: frozenset[int]) → list[Any]#

Return Mechanical processes this server started after baseline was taken.

PyMechanical starts Mechanical as a direct child process with -grpc on its command line, so only such children (and their descendants) qualify. A user’s own Mechanical is never a child of this server.

helpers.stop_processes(processes: list[Any], timeout: float = 10.0) → None#

Terminate processes, then kill any that are still running after timeout.

helpers.resolve_transport_mode(transport_mode: str | None = None, certs_dir: str | None = None) → tuple[str | None, str | None]#

Determine the gRPC transport mode and certificate directory to use.

The resolution strategy follows:

  1. Explicit override: If the caller (CLI flag, environment variable, or tool parameter) supplies a concrete mode (insecure, mtls, or wnua), honor it unconditionally.

  2. Auto-detect (transport_mode is None or "auto"):
    • On Windows: Return None so that PyMechanical applies its own platform default (wnua).

    • On Linux / Docker: Return "mtls" plus the resolved certificate directory when mTLS certificate files are found. Otherwise, return "insecure" (the only mode that can succeed without certificates). WNUA is Windows-only.

Parameters:
transport_modestr | None

Transport mode requested by the user. Options are "auto" (default), "insecure", "mtls", and "wnua". None is treated identically to "auto".

certs_dirstr | None

Explicit path to a directory containing ca.crt, client.crt, and client.key for mTLS.

Returns:
tuple[str | None, str | None]

(resolved_transport_mode, resolved_certs_dir)

resolved_transport_mode is None when the caller should let PyMechanical choose (for example, Windows auto-detect). resolved_certs_dir is None unless mTLS is selected and certificates were located.

Raises:
ValueError

If transport_mode is not one of the accepted values.

helpers.list_instances(instances: bool = False, long: bool = False, cmd: bool = False, location: bool = False) → str#

List running Mechanical instances on the system.

This function scans all running processes to identify Ansys Mechanical instances that are using gRPC communication. It returns a formatted table with information about each discovered instance.

Parameters:
instancesbool, default: False

If True, only show main Mechanical instances (exclude child processes). If False, show all Mechanical-related processes.

longbool, default: False

If True, enable verbose output including command line and working directory. This automatically sets both cmd and location to True.

cmdbool, default: False

If True, include the command-line arguments in the output table.

locationbool, default: False

If True, include the working directory path in the output table.

Returns:
str

A formatted table string containing information about Mechanical instances. The table includes columns for process name, status, gRPC port, PID, and optionally the command line and working directory based on the parameters.

Notes

  • This function identifies Mechanical processes by looking for AnsysWBU or mechanical in the process name or command line.

  • Only processes with status RUNNING, IDLE, or SLEEPING are considered valid.

helpers.get_info(mechanical: ansys.mechanical.core.Mechanical) → dict[str, Any]#

Get information from the Mechanical instance.

Parameters:
mechanicalMechanical

Mechanical instance.

Returns:
dict[str, str | dict[str, Any]]

Dictionary containing Mechanical information.

helpers.VALID_TRANSPORT_MODES#
helpers.logger#