Spaces:
Running
Running
kabudadada commited on
Commit ·
b2a7907
1
Parent(s): ce4dde2
feat(sympy tools): accept string inputs, sympify internally; return JSON-serializable results
Browse files
sympy/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -13,67 +13,117 @@ from sympy.solvers import solve, linsolve
|
|
| 13 |
from sympy.integrals import integrate
|
| 14 |
from sympy.polys import Poly, factor
|
| 15 |
from sympy.functions import sin, cos, exp
|
|
|
|
| 16 |
mcp = FastMCP("sympy_service")
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
@mcp.tool
|
| 54 |
-
def
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
|
|
|
|
| 13 |
from sympy.integrals import integrate
|
| 14 |
from sympy.polys import Poly, factor
|
| 15 |
from sympy.functions import sin, cos, exp
|
| 16 |
+
from sympy import sympify
|
| 17 |
mcp = FastMCP("sympy_service")
|
| 18 |
|
| 19 |
+
def _ser(x):
|
| 20 |
+
if isinstance(x, Basic):
|
| 21 |
+
return str(x)
|
| 22 |
+
if isinstance(x, (list, tuple)):
|
| 23 |
+
return [_ser(i) for i in x]
|
| 24 |
+
if isinstance(x, set):
|
| 25 |
+
return [_ser(i) for i in x]
|
| 26 |
+
if isinstance(x, dict):
|
| 27 |
+
return {k: _ser(v) for k, v in x.items()}
|
| 28 |
+
return x
|
| 29 |
+
|
| 30 |
+
@mcp.tool(name="create_symbol")
|
| 31 |
+
def create_symbol(name: str):
|
| 32 |
+
try:
|
| 33 |
+
res = symbols(name)
|
| 34 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 37 |
+
|
| 38 |
+
@mcp.tool(name="expand_expression")
|
| 39 |
+
def expand_expression(expr: str):
|
| 40 |
+
try:
|
| 41 |
+
res = expand(sympify(expr))
|
| 42 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 45 |
+
|
| 46 |
+
@mcp.tool(name="simplify_expression")
|
| 47 |
+
def simplify_expression(expr: str):
|
| 48 |
+
try:
|
| 49 |
+
res = simplify(sympify(expr))
|
| 50 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 53 |
+
|
| 54 |
+
@mcp.tool(name="solve_equation")
|
| 55 |
+
def solve_equation(equation: str, variable: str):
|
| 56 |
+
try:
|
| 57 |
+
res = solve(sympify(equation), symbols(variable))
|
| 58 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 61 |
+
|
| 62 |
+
@mcp.tool(name="solve_linear_system")
|
| 63 |
+
def solve_linear_system(system: list, variables: list):
|
| 64 |
+
try:
|
| 65 |
+
eqs = [sympify(e) for e in system]
|
| 66 |
+
vars_sym = [symbols(v) for v in variables]
|
| 67 |
+
res = linsolve(eqs, vars_sym)
|
| 68 |
+
return {"success": True, "result": _ser(list(res)), "error": None}
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 71 |
+
|
| 72 |
+
@mcp.tool(name="differentiate")
|
| 73 |
+
def differentiate(expr: str, variable: str):
|
| 74 |
+
try:
|
| 75 |
+
res = diff(sympify(expr), symbols(variable))
|
| 76 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 79 |
+
|
| 80 |
+
@mcp.tool(name="integrate_expression")
|
| 81 |
+
def integrate_expression(expr: str, variable: str):
|
| 82 |
+
try:
|
| 83 |
+
res = integrate(sympify(expr), symbols(variable))
|
| 84 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 87 |
+
|
| 88 |
+
@mcp.tool(name="create_polynomial")
|
| 89 |
+
def create_polynomial(coeffs: list, variable: str):
|
| 90 |
+
try:
|
| 91 |
+
res = Poly(coeffs, symbols(variable))
|
| 92 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 93 |
+
except Exception as e:
|
| 94 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 95 |
+
|
| 96 |
+
@mcp.tool(name="factor_polynomial")
|
| 97 |
+
def factor_polynomial(poly: str):
|
| 98 |
+
try:
|
| 99 |
+
res = factor(sympify(poly))
|
| 100 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 101 |
+
except Exception as e:
|
| 102 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 103 |
+
|
| 104 |
+
@mcp.tool(name="calculate_sin")
|
| 105 |
+
def calculate_sin(angle: str):
|
| 106 |
+
try:
|
| 107 |
+
res = sin(sympify(angle))
|
| 108 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 109 |
+
except Exception as e:
|
| 110 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 111 |
+
|
| 112 |
+
@mcp.tool(name="calculate_cos")
|
| 113 |
+
def calculate_cos(angle: str):
|
| 114 |
+
try:
|
| 115 |
+
res = cos(sympify(angle))
|
| 116 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 117 |
+
except Exception as e:
|
| 118 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 119 |
+
|
| 120 |
+
@mcp.tool(name="calculate_exp")
|
| 121 |
+
def calculate_exp(value: str):
|
| 122 |
+
try:
|
| 123 |
+
res = exp(sympify(value))
|
| 124 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 125 |
+
except Exception as e:
|
| 126 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 127 |
|
| 128 |
|
| 129 |
|