The tools.py module#
Summary#
Check the status of Mechanical initialization. |
|
Check if Mechanical is installed on the system. |
|
Execute a Python script inside Mechanical. |
|
Launch a new Mechanical instance. |
|
Connect to an existing Mechanical instance. |
|
Disconnect from the connected Mechanical instance. |
|
List all Mechanical instances running on the local machine. |
|
List files in the Mechanical working directory. |
|
Upload a file to the Mechanical instance. |
|
Download a file from the Mechanical instance. |
|
Clear the Mechanical database. |
|
Return the working directory of the connected Mechanical instance. |
|
Get information about the current model in Mechanical. |
|
Capture a screenshot of the current Mechanical view. |
|
Execute Python code in the persistent Python session for data processing. |
|
Create a custom plot using Matplotlib in the persistent Python session. |
|
Save the current Mechanical project. |
|
Open an existing Mechanical project file. |
|
Solve the specified analysis in Mechanical. |
|
Export results from the solved analysis. |
|
Return recent log entries from the connected Mechanical instance. |
Description#
List of tools in PyMechanical-MCP.
Module detail#
- tools.check_mechanical_status(ctx: fastmcp.server.Context) str#
Check the status of Mechanical initialization.
This tool retrieves comprehensive information from the connected Mechanical instance including version, project directory, and connection status.
- Parameters:
- ctx
Context MCP context containing the server session and application context.
- ctx
- Returns:
strJSON string containing comprehensive Mechanical status information including:
connection: Basic connection information (version, project_directory, is_alive)
product_info: Product version and build information
Returns an error message if Mechanical is not available or has exited.
- tools.check_mechanical_installed(ctx: fastmcp.server.Context) str#
Check if Mechanical is installed on the system.
This tool checks whether Ansys Mechanical is properly installed and available on the system by inspecting standard installation paths.
Note
When PyMechanical-MCP runs inside a Docker container, the host filesystem is not accessible. In this case, the tool probes the configured gRPC endpoint instead and reports whether a Mechanical server is reachable.
- Returns:
strStatus message indicating whether Mechanical is installed or reachable.
- tools.run_python_script(ctx: fastmcp.server.Context, script: str | None = None, file_path: str | None = None) str#
Execute a Python script inside Mechanical.
This tool runs a Python script block inside the connected Mechanical instance using Mechanical’s scripting API. The script has access to Mechanical’s
ExtAPI,DataModel,Model,Tree, andGraphicsentry points.Provide either an inline
scriptstring or afile_pathpointing to a local Python file. Whenfile_pathis given, its contents are read and executed inside Mechanical.- Parameters:
- ctx
Context MCP context containing the server session and application context.
- script
str,optional The Python script to execute inside Mechanical. Required when
file_pathis not provided.- file_path
str,optional Path to a local Python script file whose contents are executed inside Mechanical. Takes precedence over
scriptwhen both are provided.
- ctx
- Returns:
strScript execution result. Returns the string value of the last executed statement, or an empty string if the value cannot be returned as a string.
- async tools.launch_mechanical(ctx: fastmcp.server.Context, exec_file: str | None = None, port: int | None = None, batch: bool = True, version: str | None = None, transport_mode: str | None = None) str#
Launch a new Mechanical instance.
This tool starts a new Mechanical instance using PyMechanical’s
launch_mechanicalfunction. The launched instance will be automatically connected and stored in the context for subsequent operations. Once connected, additional tools become available to interact with it (includingrun_python_script,screenshot, andsave_project).- Parameters:
- ctx
Context MCP context containing the server session and application context.
- exec_file
str, default:None path to the Mechanical executable. If
None, PyMechanical attempts to find the executable automatically.- port
int, default:None gRPC port for Mechanical to listen on. If
None,10000is used.- batchbool, default:
True Whether to launch Mechanical in batch mode.
- version
str, default:None Mechanical version to run (such as “252” for 2025 R2). If
None, the latest installed version is used.- transport_mode
str, default:None gRPC transport mode for the launched instance. Options are
auto,insecure,mtls, andwnua. WhenNoneorauto, PyMechanical applies the platform default.
- ctx
- Returns:
strLaunch status message with Mechanical version and connection information.
- async tools.connect_to_mechanical(ctx: fastmcp.server.Context, port: int = 10000, ip: str = '127.0.0.1', transport_mode: str | None = None) str#
Connect to an existing Mechanical instance.
This tool establishes a connection to a running Mechanical instance using the provided port and IP address. The connection is stored for subsequent operations and can be closed using the
disconnect_from_mechanicaltool. Once connected, additional tools become available to interact with it (includingrun_python_script,screenshot, andsave_project).- Parameters:
- ctx
Context MCP context containing the server session and application context.
- port
int, default: 10000 gRPC port where Mechanical is listening.
- ip
str, default: “127.0.0.1” IP address where Mechanical is running.
- transport_mode
str, default:None gRPC transport mode. Options are
"auto","insecure","mtls", and"wnua". WhenNoneor"auto", the mode is determined automatically. On Windows, it defers to PyMechanical’s default ("wnua"). On Linux/Docker, it uses"mtls"if certificates are found. Otherwise it uses"insecure".
- ctx
- Returns:
strConnection status message with Mechanical version information.
- async tools.disconnect_from_mechanical(ctx: fastmcp.server.Context) str#
Disconnect from the connected Mechanical instance.
This tool closes the connection to the Mechanical instance and releases the associated resources.
- Parameters:
- ctx
Context MCP context containing the server session and application context.
- ctx
- Returns:
strDisconnection status message.
- tools.list_mechanical_instances() str#
List all Mechanical instances running on the local machine.
This tool discovers Mechanical instances running on the machine by scanning for active gRPC servers and their associated metadata.
Note
When the MCP server runs inside a Docker container, host process scanning is not available. In this case, the tool probes the configured gRPC endpoint and reports its reachability.
- Returns:
strFormatted table of Mechanical instances, or reachability status when running in Docker.
- tools.list_files(ctx: fastmcp.server.Context) str#
List files in the Mechanical working directory.
This tool lists all files in the working directory of the connected Mechanical instance.
- Parameters:
- ctx
Context MCP context containing the server session and application context.
- ctx
- Returns:
strList of files in the working directory.
- tools.upload_file(ctx: fastmcp.server.Context, file_path: str) str#
Upload a file to the Mechanical instance.
This tool uploads a local file to the Mechanical instance’s working directory.
- tools.download_file(ctx: fastmcp.server.Context, file_name: str, target_dir: str | None = None) str#
Download a file from the Mechanical instance.
This tool downloads a file from the Mechanical instance’s working directory to the local filesystem.
- Parameters:
- Returns:
strDownload status message with local file path.
- tools.clear_mechanical(ctx: fastmcp.server.Context) str#
Clear the Mechanical database.
This tool clears the current database in the Mechanical instance, providing a fresh start for a new analysis.
- Parameters:
- ctx
Context MCP context containing the server session and application context.
- ctx
- Returns:
strClear status message.
- tools.mechanical_working_directory() str#
Return the working directory of the connected Mechanical instance.
This resource provides the absolute path to the active project directory used by the connected Mechanical instance. The value is resolved lazily each time the resource is read.
- Returns:
strAbsolute path to the Mechanical project directory, or a message indicating that Mechanical is not connected.
- tools.get_model_info(ctx: fastmcp.server.Context) str#
Get information about the current model in Mechanical.
This tool retrieves information about the current model including geometry, mesh, and analysis settings by executing a script inside Mechanical.
- Parameters:
- ctx
Context MCP context containing the server session and application context.
- ctx
- Returns:
strJSON string containing model information.
- tools.screenshot(ctx: fastmcp.server.Context, view_type: str = 'model') list[mcp.types.TextContent | mcp.types.ImageContent]#
Capture a screenshot of the current Mechanical view.
This tool captures the current graphics view from Mechanical as an image. It can capture model views, mesh views, or result contour plots.
- Parameters:
- ctx
Context MCP context containing the server session and application context.
- view_type
str, default:"model" Type of view to capture. Options are
"model","mesh", and"result".
- ctx
- Returns:
list[TextContent|ImageContent]A list containing:
TextContent with the screenshot file path
ImageContent with the base64-encoded image data
- tools.run_python_code(ctx: fastmcp.server.Context, code: str, timeout: int = 60) str#
Execute Python code in the persistent Python session for data processing.
IMPORTANT: This tool runs in a separate Python process and does NOT have access to the Mechanical gRPC connection. Use this tool for:
Custom data processing and analysis (NumPy, Pandas, and SciPy)
Creating Matplotlib plots and visualizations
Postprocessing results exported from Mechanical
General Python computations
For code that needs to run inside Mechanical (such as IronPython with
ExtAPIorModel), use the ‘run_python_script’ tool instead.- Parameters:
- ctx
Context MCP context containing the server session and application context.
- code
str Python code to execute. You should use standard Python libraries. This tool does NOT have access to Mechanical’s
ExtAPI,Model, or other internal objects.- timeout
int, default: 60 Maximum time in seconds to allow for code execution.
- ctx
- Returns:
strExecution result or error message. Returns JSON for structured output.
- tools.create_custom_plot(ctx: fastmcp.server.Context, plot_code: str, plot_type: str = 'matplotlib', timeout: int = 60) list[mcp.types.TextContent | mcp.types.ImageContent] | str#
Create a custom plot using Matplotlib in the persistent Python session.
IMPORTANT: This tool runs in a separate Python process and does not have access to the Mechanical gRPC connection. Use this to:
Visualize data that was exported/saved from Mechanical.
Create plots from result files (such as CSV and TXT).
Perform postprocessing visualizations.
For plots that need data directly from Mechanical, first use
run_python_scriptto export the data to a file. Then use this tool to create the visualization.- Parameters:
- ctx
Context MCP context containing the server session and application context.
- plot_code
str Python code to create the plot. You should use
matplotlib.pyplot. For Matplotlib, the code should create the figure or plot but not callplt.show(). Use thesave_matplotlib_plot()helper function to return the plot.- plot_type
str, default:"matplotlib" Type of plot. Options are
"matplotlib"and"pyvista".- timeout
int, default: 60 Maximum time in seconds for plot generation.
- ctx
- Returns:
list[TextContent|ImageContent]A list containing:
TextContent with the plot creation status message
ImageContent with the base64-encoded image data if successful
Or, a JSON string with error details if failed.
- tools.save_project(ctx: fastmcp.server.Context, file_path: str | None = None) str#
Save the current Mechanical project.
This tool saves the current project to disk. If
file_pathis provided, the project is saved to that location (Save As). Otherwise, the project is saved in place.- Parameters:
- Returns:
strSave status message with the project file path.
- tools.open_project(ctx: fastmcp.server.Context, file_path: str) str#
Open an existing Mechanical project file.
This tool opens a MECHDB project file in the connected Mechanical instance. Any unsaved work in the current project is lost.
- tools.solve_analysis(ctx: fastmcp.server.Context, analysis_index: int = 0, wait: bool = True) str#
Solve the specified analysis in Mechanical.
This tool runs the solver for the specified analysis and returns the solution status. It checks for common issues and provides diagnostic information if the solve fails.
- Parameters:
- Returns:
strJSON string with solution status, timing, and result summary.
- tools.export_results(ctx: fastmcp.server.Context, result_type: str = 'all', export_format: str = 'png', output_dir: str | None = None) str#
Export results from the solved analysis.
This tool exports result images (contour plots) and/or data from the current solution. It can export individual result types or all available results at once.
- Parameters:
- ctx
Context MCP context containing the server session and application context.
- result_type
str, default:"all" - Type of result to export. Options are
"all","deformation","stress", "strain", or a specific result object name.
- Type of result to export. Options are
- export_format
str, default:"png" Export format. Use
"png"for images,"txt"for text data, or"both"for images and text.- output_dir
str, default:None Directory to save exported files. If
None, uses the project directory.
- ctx
- Returns:
strJSON string with export status and file paths.
- tools.get_mechanical_logs(ctx: fastmcp.server.Context, source: str = 'messages', tail_lines: int = 200, contains: str | None = None, max_chars: int = 40000) str#
Return recent log entries from the connected Mechanical instance.
This tool provides visibility into Mechanical session activity by reading either the application message list or solver output log file.
- Parameters:
- ctx
Context MCP context containing the server session and application context.
- source
str,optional Log source to read. Options are:
"messages"(default): Application messages (errors, warnings, info) fromExtAPI.Application.Messages."solve_log": Raw solver transcript fromsolve.outin the project directory.
- tail_lines
int, default: 200 Number of most recent lines to return.
- contains
str,optional Case-insensitive substring filter applied to each line.
- max_chars
int, default: 40000 Maximum character count for the returned log text.
- ctx
- Returns:
strJSON string with log metadata and selected log text.
- tools.REQUIRES_MECHANICAL_TAG = 'requires_mechanical'#
- tools.logger#