mpl_mcp_eqn_chart
Plot mathematical equations to visualize functions and relationships between variables. Generate charts with customizable axes, gridlines, and legends for analysis and presentation.
Instructions
Plots mathematical equations
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| equations | Yes | ||
| x_min | No | ||
| x_max | No | ||
| num_points | No | ||
| title | No | Equation Plot | |
| xlabel | No | x | |
| ylabel | No | y | |
| grid | No | ||
| legend | No | ||
| figsize | No | ||
| linewidth | No | ||
| linestyle | No | - | |
| alpha | No | ||
| dpi | No | ||
| save | No |
Implementation Reference
- fmcp/mpl_mcp/core/eqn_chart.py:15-125 (handler)The eqn_chart function is the main handler for the mpl_mcp_eqn_chart tool, plotting mathematical equations using matplotlib and returning a PNG image.
def eqn_chart( equations: Union[str, List[str]], x_min: Union[float, int] = -10.0, x_max: Union[float, int] = 10.0, num_points: Union[int, float] = 1000, title: str = "Equation Plot", xlabel: str = "x", ylabel: str = "y", grid: bool = True, legend: bool = True, figsize: List[Union[int, float]] = [10, 6], linewidth: Union[float, int] = 2.0, linestyle: str = "-", alpha: Union[float, int] = 1.0, dpi: Union[int, float] = 200, save: bool = False, ) -> Image: """ Plot one or more mathematical equations on the same chart. Args: equations: Single equation string or list of equation strings (e.g., "x**2" or ["x**2", "sin(x)"]) x_min: Minimum x-value for the plot x_max: Maximum x-value for the plot num_points: Number of points to generate for the plot title: Title of the plot xlabel: Label for the x-axis ylabel: Label for the y-axis grid: Whether to show grid lines legend: Whether to show legend figsize: Figure size (width, height) in inches linewidth: Width of the plot lines linestyle: Style of the plot lines alpha: Transparency of the plot lines dpi: Dots per inch for the output image save: If True, save the figure to a buffer Returns: Image: The image object containing the plot """ # Create a config instance to allow arbitrary types _ = PlotConfig() # Rest of your function remains the same if isinstance(equations, str): equations = [equations] fig, ax = plt.subplots(figsize=(float(figsize[0]), float(figsize[1])), dpi=int(dpi)) x = np.linspace(float(x_min), float(x_max), int(num_points)) for eq in equations: eq_py = eq.replace("^", "**") eq_py = re.sub(r"(\W|^)([a-z]+)\(", r"\1np.\2(", eq_py) try: y = eval( eq_py, { "x": x, "np": np, "sin": np.sin, "cos": np.cos, "tan": np.tan, "exp": np.exp, "log": np.log, "sqrt": np.sqrt, "pi": np.pi, "e": np.e, }, ) # Convert numpy arrays to lists for Pydantic compatibility if isinstance(y, np.ndarray): y = y.tolist() ax.plot( x, y, label=f"y = {eq}", linewidth=float(linewidth), linestyle=linestyle, alpha=float(alpha), ) except Exception as e: print(f"Error plotting equation '{eq}': {str(e)}") continue ax.set_title(title) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.grid(grid) if (len(equations) > 1 or legend) and any(ax.lines): ax.legend() plt.tight_layout() # Always save to buffer for the return value buf = io.BytesIO() plt.savefig(buf, format="png", dpi=int(dpi)) plt.close() buf.seek(0) # If save is True, also save to a file if save: with open("equation_plot.png", "wb") as f: f.write(buf.getvalue()) buf.seek(0) # Reset buffer position after reading return Image(data=buf.read(), format="png") - fmcp/mpl_mcp/server.py:24-24 (registration)The eqn_chart function is registered as a tool in the FastMCP server named mpl_mcp, likely resulting in the tool name mpl_mcp_eqn_chart.
mpl_mcp.tool(eqn_chart, description="Plots mathematical equations") - fmcp/mpl_mcp/server.py:7-7 (registration)Import of the eqn_chart handler into the server module for registration.
from .core.eqn_chart import eqn_chart