Skip to main content
Glama

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

TableJSON Schema
NameRequiredDescriptionDefault
equationsYes
x_minNo
x_maxNo
num_pointsNo
titleNoEquation Plot
xlabelNox
ylabelNoy
gridNo
legendNo
figsizeNo
linewidthNo
linestyleNo-
alphaNo
dpiNo
saveNo

Implementation Reference

  • 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")
  • 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")
  • Import of the eqn_chart handler into the server module for registration.
    from .core.eqn_chart import eqn_chart

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/abhiphile/fermat-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server