sympy_latex
Convert a SymPy expression string into LaTeX format for mathematical typesetting.
Instructions
Convert expression to LaTeX.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expr | Yes | SymPy expression string |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/mcp_sympy/tools.py:809-823 (handler)The main handler function for the sympy_latex tool. It is decorated with @mcp.tool(), takes a SymPy expression string as input, converts it to a SymPy object via _sympify(), and returns the LaTeX string representation using sympy's latex() function.
@mcp.tool() def sympy_latex(expr: str) -> str: """Convert expression to LaTeX. Args: expr: SymPy expression string Returns: LaTeX string Example: >>> sympy_latex("x**2 + sin(y)") "x^{2} + \\sin{\\left(y\\right)}" """ return str(latex(_sympify(expr))) - src/mcp_sympy/tools.py:809-809 (registration)The @mcp.tool() decorator on line 809 registers the sympy_latex function as a tool with the FastMCP server instance named 'mcp' (defined on line 119).
@mcp.tool() - src/mcp_sympy/tools.py:114-116 (helper)The _sympify() helper function is used by sympy_latex to convert the input string expression into a SymPy Basic object, which is then passed to latex().
def _sympify(expr: str) -> sympy.Basic: """Convert string expression to SymPy object.""" return sympy.sympify(expr) - src/mcp_sympy/tools.py:79-79 (schema)The 'latex' function is imported from sympy at the top of the file, providing the LaTeX conversion capability used in the sympy_latex handler.
latex, - tests/test_tools.py:140-143 (handler)Test case for the sympy_latex tool, verifying that the LaTeX output contains expected formatting like 'x^{2}'.
def test_latex(self): """Test LaTeX output.""" result = tools.sympy_latex("x**2 + 1") assert "x^{2}" in result