Labsco
sdiehl logo

Symbolic Algebra MCP Server

โ˜… 75

from sdiehl

Perform symbolic mathematics and computer algebra using the SymPy library.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedFreeAdvanced setup
Sympy MCP Logo

Symbolic Algebra MCP Server

Sympy-MCP is a Model Context Protocol server for allowing LLMs to autonomously perform symbolic mathematics and computer algebra. It exposes numerous tools from SymPy's core functionality to MCP clients for manipulating mathematical expressions and equations.

Why?

Language models are absolutely abysmal at symbolic manipulation. They hallucinate variables, make up random constants, permute terms and generally make a mess. But we have computer algebra systems specifically built for symbolic manipulation, so we can use tool-calling to orchestrate a sequence of transforms so that the symbolic kernel does all the heavy lifting.

While you can certainly have an LLM generate Mathematica or Python code, if you want to use the LLM as an agent or on-the-fly calculator, it's a better experience to use the MCP server and expose the symbolic tools directly.

The server exposes a subset of symbolic mathematics capabilities including algebraic equation solving, integration and differentiation, vector calculus, tensor calculus for general relativity, and both ordinary and partial differential equations.

For example, you can ask it in natural language to solve a differential equation:

Solve the damped harmonic oscillator with forcing term: the mass-spring-damper system described by the differential equation where m is mass, c is the damping coefficient, k is the spring constant, and F(t) is an external force.

$$ m\frac{d^2x}{dt^2} + c\frac{dx}{dt} + kx = F(t) $$

Or involving general relativity:

Compute the trace of the Ricci tensor $R_{\mu\nu}$ using the inverse metric $g^{\mu\nu}$ for Anti-de Sitter spacetime to determine its constant scalar curvature $R$.

Available Tools

The sympy-mcp server provides the following tools for symbolic mathematics:

ToolTool IDDescription
Variable IntroductionintroIntroduces a variable with specified assumptions and stores it
Multiple Variablesintro_manyIntroduces multiple variables with specified assumptions simultaneously
Expression Parserintroduce_expressionParses an expression string using available local variables and stores it
LaTeX Printerprint_latex_expressionPrints a stored expression in LaTeX format, along with variable assumptions
Algebraic Solversolve_algebraicallySolves an equation algebraically for a given variable over a given domain
Linear Solversolve_linear_systemSolves a system of linear equations
Nonlinear Solversolve_nonlinear_systemSolves a system of nonlinear equations
Function Variableintroduce_functionIntroduces a function variable for use in differential equations
ODE Solverdsolve_odeSolves an ordinary differential equation
PDE Solverpdsolve_pdeSolves a partial differential equation
Standard Metriccreate_predefined_metricCreates a predefined spacetime metric (e.g. Schwarzschild, Kerr, Minkowski)
Metric Searchsearch_predefined_metricsSearches available predefined metrics
Tensor Calculatorcalculate_tensorCalculates tensors from a metric (Ricci, Einstein, Weyl tensors)
Custom Metriccreate_custom_metricCreates a custom metric tensor from provided components and symbols
Tensor LaTeXprint_latex_tensorPrints a stored tensor expression in LaTeX format
Simplifiersimplify_expressionSimplifies a mathematical expression using SymPy's canonicalize function
Substitutionsubstitute_expressionSubstitutes a variable with an expression in another expression
Integrationintegrate_expressionIntegrates an expression with respect to a variable
Differentiationdifferentiate_expressionDifferentiates an expression with respect to a variable
Coordinatescreate_coordinate_systemCreates a 3D coordinate system for vector calculus operations
Vector Fieldcreate_vector_fieldCreates a vector field in the specified coordinate system
Curlcalculate_curlCalculates the curl of a vector field
Divergencecalculate_divergenceCalculates the divergence of a vector field
Gradientcalculate_gradientCalculates the gradient of a scalar field
Unit Converterconvert_to_unitsConverts a quantity to given target units
Unit Simplifierquantity_simplify_unitsSimplifies a quantity with units
Matrix Creatorcreate_matrixCreates a SymPy matrix from the provided data
Determinantmatrix_determinantCalculates the determinant of a matrix
Matrix Inversematrix_inverseCalculates the inverse of a matrix
Eigenvaluesmatrix_eigenvaluesCalculates the eigenvalues of a matrix
Eigenvectorsmatrix_eigenvectorsCalculates the eigenvectors of a matrix

By default variables are predefined with assumptions (similar to how the symbols() function works in SymPy). Unless otherwise specified the defaut assumptions is that a variable is complex, commutative, term over the complex field $\mathbb{C}$.

PropertyValue
commutativetrue
complextrue
finitetrue
infinitefalse

HTTP Transport (Streamable HTTP / SSE)

The server supports MCP over HTTP using the streamable-http transport introduced in MCP spec 2025-03-26. This replaces the legacy SSE transport and exposes a single /mcp endpoint that clients connect to over HTTP.

This is the recommended transport when running the server as a standalone process or in a container, because it allows any HTTP-capable MCP client to connect without needing to launch the server as a subprocess.

# Run locally with HTTP transport
uv run python server.py --transport streamable-http

# Override host/port
uv run python server.py --transport streamable-http --mcp-host 127.0.0.1 --mcp-port 9000

The legacy --transport sse flag is still supported for backward compatibility.

A /healthcheck endpoint is also exposed that runs a full MCP protocol round-trip (initialize โ†’ tools/list โ†’ session teardown) and returns {"status": "ok", "tool_count": N}.

Example Interaction 1 : Differential Equations

Here's an example of how the tool-calling works in a conversation with an AI assistant using this MCP server:

User: Solve the damped harmonic oscillator with forcing term: the mass-spring-damper system described by the differential equation where m is mass, c is the damping coefficient, k is the spring constant, and F(t) is an external force.

$$ m\frac{d^2x}{dt^2} + c\frac{dx}{dt} + kx = F(t) $$

Assistant: (Internal tool call) intro_many to introduce variables:

{
  "variables": [
    {"var_name": "t", "pos_assumptions": ["real"], "neg_assumptions": []},
    {"var_name": "m", "pos_assumptions": ["real", "positive"], "neg_assumptions": []},
    {"var_name": "c", "pos_assumptions": ["real", "positive"], "neg_assumptions": []},
    {"var_name": "k", "pos_assumptions": ["real", "positive"], "neg_assumptions": []}
  ]
}

Assistant: (Internal tool call) introduce_function to define x(t):

{
  "func_name": "x"
}

Assistant: (Internal tool call) introduce_expression to create the differential equation:

{
  "expr_str": "Eq(m*Derivative(x(t), t, t) + c*Derivative(x(t), t) + k*x(t), F(t))"
}

Assistant: (Internal tool call) print_latex_expression to display the equation nicely:

{
  "expr_key": "expr_12"
}

Result:

$$ c \frac{d}{d t} x{\left(t \right)} + k x{\left(t \right)} + m \frac{d^{2}}{d t^{2}} x{\left(t \right)} = F{\left(t \right)} $$

Assistant: (Internal tool call) introduce_function to define F(t):

{
  "func_name": "F"
}

Assistant: (Internal tool call) dsolve_ode to solve the differential equation:

{
  "expr_key": "expr_12",
  "func_name": "x"
}

Result:

$$ x{\left(t \right)} = C_{1} e^{\frac{t \left(- c + \sqrt{c^{2} - 4 k m}\right)}{2 m}} + C_{2} e^{- \frac{t \left(c + \sqrt{c^{2} - 4 k m}\right)}{2 m}} + \cdots $$

Example Interaction 2: General Relativity

User: Compute the trace of the Ricci tensor $R_{\mu\nu}$ using the inverse metric $g^{\mu\nu}$ for Anti-de Sitter spacetime to determine its constant scalar curvature $R$.

Assistant: (Internal tool call) create_predefined_metric to create the Anti-de Sitter metric:

{
  "metric_name": "AntiDeSitter"
}

Assistant: (Internal tool call) calculate_tensor to calculate the Ricci tensor:

{
  "metric_key": "metric_AntiDeSitter",
  "tensor_type": "RICCI_TENSOR",
  "simplify_result": true
}

Assistant: (Internal tool call) print_latex_tensor to display the Ricci tensor:

{
  "tensor_key": "riccitensor_metric_AntiDeSitter"
}

Result:

$$ \left[\begin{matrix}3 & 0 & 0 & 0\\0 & - 3 \cos^{2}{\left(t \right)} & 0 & 0\\0 & 0 & - 3 \cos^{2}{\left(t \right)} \sinh^{2}{\left(\chi \right)} & 0\\0 & 0 & 0 & - 3 \sin^{2}{\left(\theta \right)} \cos^{2}{\left(t \right)} \sinh^{2}{\left(\chi \right)}\end{matrix}\right] $$

Assistant: (Internal tool call) calculate_tensor to calculate the Ricci scalar:

{
  "metric_key": "metric_AntiDeSitter",
  "tensor_type": "RICCI_SCALAR",
  "simplify_result": true
}

Assistant: (Internal tool call) print_latex_tensor to display the Ricci scalar:

{
  "tensor_key": "ricciscalar_metric_AntiDeSitter"
}

Result:

$$ -12 $$

Example Interaction 3: Coupled ODE System (Fluid Dynamics)

This example demonstrates solving a coupled system of ODEs and verifying the solution against an algebraic steady-state analysis โ€” a task where LLMs typically hallucinate without a CAS to ground each step.

User:

Use the sympy-mcp tools to solve this symbolically โ€” do not compute by hand.

Two cylindrical tanks are connected by a pipe. Tank 1 has cross-sectional area Aโ‚ = 2 mยฒ and receives a constant inflow of Q = 0.5 mยณ/s. Water drains from Tank 1 into Tank 2 through a pipe with flow rate proportional to the height difference: qโ‚โ‚‚ = kยท(hโ‚ - hโ‚‚) where k = 0.3 mยฒ/s. Tank 2 has cross-sectional area Aโ‚‚ = 1 mยฒ and drains to the outside at rate qโ‚‚ = kยทhโ‚‚ with the same k.

Set up and solve the coupled system of ODEs for the water heights hโ‚(t) and hโ‚‚(t), starting from empty tanks (hโ‚(0) = 0, hโ‚‚(0) = 0). Then find the steady-state heights as t โ†’ โˆž by solving the equilibrium equations algebraically, and verify they match the long-term solution of the ODEs.

Assistant: (Internal tool chain)

  1. intro_many โ€” introduce t, k, A1, A2, Q with real/positive assumptions
  2. introduce_function ร— 2 โ€” introduce h1(t) and h2(t) as unknown functions
  3. introduce_expression ร— 2 โ€” encode the mass-balance ODEs:

$$A_1 \frac{dh_1}{dt} = Q - k(h_1 - h_2), \quad A_2 \frac{dh_2}{dt} = k(h_1 - h_2) - k h_2$$

  1. substitute_expression โ€” substitute numeric values for k, A1, A2, Q
  2. dsolve_ode ร— 2 โ€” solve the coupled system; apply initial conditions via substitute_expression
  3. introduce_expression ร— 2 โ€” encode equilibrium equations (derivatives set to zero)
  4. solve_linear_system โ€” solve the 2ร—2 algebraic system for h1*, h2*
  5. print_latex_expression โ€” display both the time-domain solution and the steady-state values

Security Disclaimer

This server runs on your computer and gives the language model access to run Python logic. Notably it uses Sympy's parse_expr to parse mathematical expressions, which is uses eval under the hood, effectively allowing arbitrary code execution. By running the server, you are trusting the code that Claude generates. Running in the Docker image is slightly safer, but it's still a good idea to review the code before running it.