The visualization_tools.py module#

Summary#

read_image_in_base64

Read an image file and return its contents as a base64-encoded string.

visualize

Open a 3D view of the active design or save an image when requested.

Description#

MCP visualization tools for PyAnsys Geometry-MCP.

Tools that render the active design or read the resulting images.

Each function decorated with @app.tool() is exposed as an MCP tool. The ctx parameter provides access to the application context (and thus to the live product instance and command history).

How to add a tool#

  1. Define a Python function with type-annotated parameters.

  2. Add a NumPy-style docstring. The summary line becomes the tool description. visible to the AI assistant.

  3. Decorate it with @app.tool().

  4. Access the product instance:

    app_context = get_geometry_ctx(ctx)
    instance = app_context.product_instance
    

Notes#

Import this module in __main__.py (already done) to register all tools with the server before it starts.

Module detail#

visualization_tools.read_image_in_base64(ctx: fastmcp.server.Context, image_path: str) → dict[str, str]#

Read an image file and return its contents as a base64-encoded string.

Parameters#

ctxContext

MCP request context (injected automatically).

image_pathstr

Path to the image file to read.

Returns#

dict of str

A dictionary with a single key “base64” containing the base64-encoded contents of the image, or an error message if reading fails.

visualization_tools.visualize(ctx: fastmcp.server.Context, path: str | None = None, save: bool = False, use_service_colors: bool = False) → str#

Open a 3D view of the active design or save an image when requested.

Parameters#

ctxContext

MCP request context (injected automatically).

pathstr, default: None

Path to save the image to. Providing a path saves the image there.

savebool, default: False

Whether to save an image using a timestamped filename if no path is provided.

use_service_colorsbool, default: False

Whether to plot using service colors. Set to True when the design’s colors convey essential information or the user requests a full-color plot.

Returns#

str

Result message, or an error message if the operation fails.

visualization_tools.logger#