sympy_coeff
Retrieve the coefficient of a variable raised to a given power in a symbolic expression.
Instructions
Get coefficient.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expr | Yes | Expression | |
| x | Yes | Variable | |
| n | No | Power of x |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/mcp_sympy/tools.py:1704-1720 (handler)The `sympy_coeff` tool handler function. It is decorated with @mcp.tool(), takes expr (string expression), x (variable), and n (power, default 1) as parameters, converts the expression to a SymPy object via _sympify() helper, calls .coeff() on it, and returns the coefficient as a string.
@mcp.tool() def sympy_coeff(expr: str, x: str, n: int = 1) -> str: """Get coefficient. Args: expr: Expression x: Variable n: Power of x Returns: Coefficient as string Example: >>> sympy_coeff("3*x**2 + 2*x + 1", "x", 2) "3" """ return str(_sympify(expr).coeff(_sympify(x), n)) - src/mcp_sympy/tools.py:114-116 (helper)The _sympify() helper function used by sympy_coeff to convert a string expression into a SymPy Basic object so .coeff() can be called on it.
def _sympify(expr: str) -> sympy.Basic: """Convert string expression to SymPy object.""" return sympy.sympify(expr) - src/mcp_sympy/tools.py:119-119 (registration)The FastMCP instance (mcp) that registers the tool via the @mcp.tool() decorator on line 1704.
mcp = fastmcp.FastMCP("mcp-sympy") - src/mcp_sympy/tools.py:1-5 (helper)Module imports, including fastmcp and sympy, used by the sympy_coeff handler.
"""MCP tools that expose SymPy's symbolic mathematics functionality.""" import fastmcp import sympy from sympy import (