The tools.py module#
Summary#
Execute Python code in the persistent Python session with automatic rule generation. |
|
Create a custom plot using Matplotlib or PyVista in the persistent Python session. |
Description#
Common tool implementations for PyAnsys MCP servers.
This module provides reusable tool functions that product-specific MCP servers can use or extend. These are not registered tools themselves, but functions that can be called from product-specific tool implementations.
Module detail#
- async tools.execute_python_code(ctx: fastmcp.Context, code: str, timeout: int = 60, no_output_timeout: float | None = None) str#
Execute Python code in the persistent Python session with automatic rule generation.
This function should be used for custom Python code execution. When code execution fails, it automatically generates a rule using LLM analysis to prevent similar errors in the future.
- Parameters:
- ctx
Context MCP context containing server session and application context.
- code
str Python code to execute.
- timeout
int, default: 60 Maximum time in seconds to allow for code execution.
- no_output_timeout
floatorNone, default:None Maximum number of seconds to wait without receiving any output before stopping collection. Useful for code that produces no output for extended periods. If greater than
timeout,timeoutis raised to match it, sinceno_output_timeoutcan never exceed the overall execution timeout. IfNone, the persistent session’s default is used.
- ctx
- Returns:
strExecution result or error message. Returns JSON for structured output compatible with both stdio and http transports.
Examples
Execute simple Python code:
code = ''' result = sum([i**2 for i in range(10)]) print(f"Sum of squares: {result}") ''' await execute_python_code(ctx, code)
Execute code with automatic rule generation on failure:
code = "result = 1/0" # This will fail await execute_python_code(ctx, code)
Automatically adds rule like:
{"Division Operations": ["Do not divide by zero"]}
- tools.create_custom_plot(ctx: fastmcp.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 or PyVista in the persistent Python session.
- Parameters:
- ctx
Context MCP context containing server session and application context.
- plot_code
str Python code for creating the plot. You should use
matplotlib.pyplotor PyVista. For Matplotlib, the code should create the figure/plot but NOT callplt.show(). Use thesave_matplotlib_plot() or ``save_plot()helper functions to return the plot.- plot_type
str, default: “matplotlib” Type of plot. Options are
"matplotlib"or"pyvista".- timeout
int, default: 60 Maximum time in seconds for plot generation.
- ctx
- Returns:
list[TextContent|ImageContent]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.
Examples
Create a custom Matplotlib line plot:
plot_code = ''' import matplotlib.pyplot as plt import numpy as np # Extract data from MAPDL displacements = mapdl.get_array("NODE", item1="U", it1num="Y") # Create custom plot plt.figure(figsize=(10, 6)) plt.plot(displacements) plt.xlabel("Node Number") plt.ylabel("Displacement (m)") plt.title("Custom Displacement Plot") plt.grid(True) # Save and return result = save_matplotlib_plot(dpi=150) print(result) ''' create_custom_plot(ctx, plot_code, plot_type="matplotlib")