MCP compliance, safety and math fixes; English tool docs; symbolic dsolve
Fork of https://github.com/thinkitpossible/CalcMCP with: - tools/list now publishes inputSchema (upstream used non-standard 'params' key, leaving clients with no parameter information) - tool descriptions and parameter docs rewritten in English with examples - JSON-RPC notifications are no longer answered; tool failures set isError - raw eval() of tool arguments removed (json.loads/ast.literal_eval for matrices, sympy.lambdify for the numeric ODE right-hand side) - complex eigenvalues serialize as 'a+bi' strings instead of crashing - solve accepts '=' equations; ttest accepts {"popmean": ...} params - new symbolic_tool operation 'dsolve' for symbolic ODE solutions Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
135
mcp_server.py
135
mcp_server.py
@@ -58,55 +58,85 @@ class MCPCalculatorServer:
|
||||
# Consolidated tool definitions (3 tools)
|
||||
tool_definitions = {
|
||||
"symbolic_tool": {
|
||||
"description": "统一符号计算入口:simplify/expand/factor/derivative/integral/limit/solve/taylor/matrix",
|
||||
"description": (
|
||||
"Exact symbolic math (SymPy). Set operation to one of: simplify, expand, factor, "
|
||||
"derivative, integral, limit, solve, dsolve, taylor, matrix. "
|
||||
"Write expressions in Python/SymPy syntax, e.g. 'x**2 * exp(-x)'; use 'oo' for infinity. "
|
||||
"integral: optional lower_limit/upper_limit for definite integrals. "
|
||||
"solve: pass the equation, '=' is allowed (e.g. 'x**2 = 4' or 'x**3 - 6*x**2 + 11*x - 6'). "
|
||||
"dsolve: solves an ODE symbolically — write the unknown function as y(x), "
|
||||
"e.g. equation='Derivative(y(x), x, 2) + y(x) = 0'. "
|
||||
"taylor: expression + variable + point + order. "
|
||||
"matrix: matrix_data (JSON) with variable=determinant|inverse|rank|trace. "
|
||||
"Results are exact (e.g. '2', 'E', 'C1*sin(x) + C2*cos(x)')."
|
||||
),
|
||||
"params": [
|
||||
{"name": "operation", "type": "string", "description": "操作类型,如 derivative, integral, solve 等"},
|
||||
{"name": "expression", "type": "string", "description": "表达式", "optional": True},
|
||||
{"name": "variable", "type": "string", "description": "变量", "optional": True},
|
||||
{"name": "order", "type": "integer", "description": "阶数(导数/泰勒)", "optional": True},
|
||||
{"name": "lower_limit", "type": "string", "description": "积分下限", "optional": True},
|
||||
{"name": "upper_limit", "type": "string", "description": "积分上限", "optional": True},
|
||||
{"name": "equation", "type": "string", "description": "方程", "optional": True},
|
||||
{"name": "point", "type": "string", "description": "极限或泰勒展开点", "optional": True},
|
||||
{"name": "matrix_data", "type": "string", "description": "符号矩阵(JSON)", "optional": True}
|
||||
{"name": "operation", "type": "string", "description": "One of: simplify, expand, factor, derivative, integral, limit, solve, dsolve, taylor, matrix"},
|
||||
{"name": "expression", "type": "string", "description": "Expression in SymPy syntax, e.g. 'sin(x)/x'", "optional": True},
|
||||
{"name": "variable", "type": "string", "description": "Variable name, e.g. 'x' (for matrix: the operation name)", "optional": True},
|
||||
{"name": "order", "type": "integer", "description": "Order for derivative/taylor (default 1 / 5)", "optional": True},
|
||||
{"name": "lower_limit", "type": "string", "description": "Lower bound for definite integral, e.g. '0'", "optional": True},
|
||||
{"name": "upper_limit", "type": "string", "description": "Upper bound for definite integral, e.g. 'oo'", "optional": True},
|
||||
{"name": "equation", "type": "string", "description": "Equation for solve/dsolve; '=' allowed", "optional": True},
|
||||
{"name": "point", "type": "string", "description": "Point for limit/taylor, e.g. '0' or 'oo'", "optional": True},
|
||||
{"name": "matrix_data", "type": "string", "description": "Matrix as JSON, e.g. '[[1,2],[3,4]]'", "optional": True}
|
||||
]
|
||||
},
|
||||
"numpy_tool": {
|
||||
"description": "统一 NumPy 入口:数组归约、线性代数、三角、多项式",
|
||||
"description": (
|
||||
"Numeric arrays and linear algebra (NumPy). Set operation to one of: "
|
||||
"sum, mean, std, max, min (array_data, optional axis); "
|
||||
"eigenvalues, eigenvectors, determinant, inverse, solve, norm, rank, trace, "
|
||||
"svd, qr, cholesky, matmul, dot, hadamard (matrix_a, and matrix_b where two matrices are needed); "
|
||||
"sin, cos, tan, arcsin, arccos, arctan, sinh, cosh, tanh (values, optional use_degrees); "
|
||||
"poly_eval, poly_derivative, poly_integral (coefficients highest-degree first, optional x_values). "
|
||||
"All array/matrix inputs are JSON strings, e.g. matrix_a='[[2,1],[1,2]]'. Results are numeric."
|
||||
),
|
||||
"params": [
|
||||
{"name": "operation", "type": "string", "description": "操作类型,如 eigenvalues, sum, poly_eval 等"},
|
||||
{"name": "array_data", "type": "string", "description": "数组数据(JSON)", "optional": True},
|
||||
{"name": "axis", "type": "string", "description": "轴参数", "optional": True},
|
||||
{"name": "matrix_a", "type": "string", "description": "矩阵A(JSON)", "optional": True},
|
||||
{"name": "matrix_b", "type": "string", "description": "矩阵B(JSON)", "optional": True},
|
||||
{"name": "values", "type": "string", "description": "三角函数输入(JSON)", "optional": True},
|
||||
{"name": "use_degrees", "type": "boolean", "description": "角度制", "optional": True},
|
||||
{"name": "coefficients", "type": "string", "description": "多项式系数(JSON)", "optional": True},
|
||||
{"name": "x_values", "type": "string", "description": "多项式自变量数组(JSON)", "optional": True}
|
||||
{"name": "operation", "type": "string", "description": "Operation name, e.g. 'eigenvalues', 'matmul', 'mean', 'poly_eval'"},
|
||||
{"name": "array_data", "type": "string", "description": "Array as JSON, e.g. '[1,2,3]' or '[[1,2],[3,4]]'", "optional": True},
|
||||
{"name": "axis", "type": "string", "description": "Axis for reductions: '0' or '1'", "optional": True},
|
||||
{"name": "matrix_a", "type": "string", "description": "First matrix as JSON", "optional": True},
|
||||
{"name": "matrix_b", "type": "string", "description": "Second matrix as JSON (solve/matmul/dot/hadamard)", "optional": True},
|
||||
{"name": "values", "type": "string", "description": "Input values for trig functions as JSON, e.g. '[0, 90, 180]'", "optional": True},
|
||||
{"name": "use_degrees", "type": "boolean", "description": "Treat trig inputs as degrees (default radians)", "optional": True},
|
||||
{"name": "coefficients", "type": "string", "description": "Polynomial coefficients as JSON, highest degree first", "optional": True},
|
||||
{"name": "x_values", "type": "string", "description": "x values for poly_eval as JSON", "optional": True}
|
||||
]
|
||||
},
|
||||
"scipy_tool": {
|
||||
"description": "统一 SciPy 入口:积分、优化、插值、特殊函数、ODE、统计、FFT、特征分解",
|
||||
"description": (
|
||||
"Numerical methods (SciPy). Set operation to one of: "
|
||||
"integrate (numeric definite integral: expression, variable, lower, upper); "
|
||||
"optimize_minimize, optimize_root (expression, initial_value, optional method); "
|
||||
"interpolate_linear, interpolate_cubic, interpolate_spline (x_data, y_data, x_eval); "
|
||||
"special (function name + parameters JSON, e.g. gamma, besselj); "
|
||||
"solve_ode (NUMERIC simulation of dy/dt = f(t,y): expression e.g. '-2*y + t', "
|
||||
"initial_conditions, t_values; for exact general solutions use symbolic_tool with operation='dsolve'); "
|
||||
"statistics ops mean, std, describe, ttest, pearsonr (function + data, optional params); "
|
||||
"fft, rfft (data_series or data); matrix_eigensystem (matrix_a). "
|
||||
"All list inputs are JSON strings. Results are numeric."
|
||||
),
|
||||
"params": [
|
||||
{"name": "operation", "type": "string", "description": "操作类型,如 integrate, optimize_minimize, fft 等"},
|
||||
{"name": "expression", "type": "string", "description": "表达式/函数", "optional": True},
|
||||
{"name": "variable", "type": "string", "description": "变量", "optional": True},
|
||||
{"name": "lower", "type": "string", "description": "积分下限", "optional": True},
|
||||
{"name": "upper", "type": "string", "description": "积分上限", "optional": True},
|
||||
{"name": "initial_value", "type": "string", "description": "初值/起点", "optional": True},
|
||||
{"name": "method", "type": "string", "description": "算法方法", "optional": True},
|
||||
{"name": "x_data", "type": "string", "description": "插值 x 数据(JSON)", "optional": True},
|
||||
{"name": "y_data", "type": "string", "description": "插值 y 数据(JSON)", "optional": True},
|
||||
{"name": "x_eval", "type": "string", "description": "插值求值点(JSON)", "optional": True},
|
||||
{"name": "function", "type": "string", "description": "特殊函数名/统计操作名", "optional": True},
|
||||
{"name": "parameters", "type": "string", "description": "特殊函数参数(JSON)", "optional": True},
|
||||
{"name": "initial_conditions", "type": "string", "description": "ODE 初值(JSON)", "optional": True},
|
||||
{"name": "t_values", "type": "string", "description": "ODE 时间点(JSON)", "optional": True},
|
||||
{"name": "variables", "type": "string", "description": "ODE 变量名", "optional": True},
|
||||
{"name": "data", "type": "string", "description": "统计或 FFT 数据(JSON)", "optional": True},
|
||||
{"name": "params", "type": "string", "description": "统计附加参数(JSON)", "optional": True},
|
||||
{"name": "data_series", "type": "string", "description": "FFT 数据(JSON)", "optional": True},
|
||||
{"name": "matrix_a", "type": "string", "description": "矩阵(JSON)用于特征分解", "optional": True}
|
||||
{"name": "operation", "type": "string", "description": "Operation name, e.g. 'integrate', 'optimize_minimize', 'solve_ode', 'fft'"},
|
||||
{"name": "expression", "type": "string", "description": "Expression/function of the variable, e.g. 'x**2 * exp(-x)'", "optional": True},
|
||||
{"name": "variable", "type": "string", "description": "Variable name, e.g. 'x'", "optional": True},
|
||||
{"name": "lower", "type": "string", "description": "Lower integration bound (number)", "optional": True},
|
||||
{"name": "upper", "type": "string", "description": "Upper integration bound (number or 'oo')", "optional": True},
|
||||
{"name": "initial_value", "type": "string", "description": "Starting point for optimization/root finding", "optional": True},
|
||||
{"name": "method", "type": "string", "description": "Algorithm, e.g. 'BFGS', 'brentq'", "optional": True},
|
||||
{"name": "x_data", "type": "string", "description": "Interpolation x data as JSON", "optional": True},
|
||||
{"name": "y_data", "type": "string", "description": "Interpolation y data as JSON", "optional": True},
|
||||
{"name": "x_eval", "type": "string", "description": "Points to evaluate the interpolant at, JSON", "optional": True},
|
||||
{"name": "function", "type": "string", "description": "Special-function or statistics operation name", "optional": True},
|
||||
{"name": "parameters", "type": "string", "description": "Special-function parameters as JSON", "optional": True},
|
||||
{"name": "initial_conditions", "type": "string", "description": "ODE initial value(s) as JSON, e.g. '[1.0]'", "optional": True},
|
||||
{"name": "t_values", "type": "string", "description": "ODE time points as JSON, e.g. '[0, 0.1, 0.2]'", "optional": True},
|
||||
{"name": "variables", "type": "string", "description": "ODE dependent variable name, default 'y'", "optional": True},
|
||||
{"name": "data", "type": "string", "description": "Statistics/FFT data as JSON", "optional": True},
|
||||
{"name": "params", "type": "string", "description": "Extra statistics parameter as JSON: for ttest the population mean ('2.3' or '{\"popmean\": 2.3}'), for pearsonr the second data series", "optional": True},
|
||||
{"name": "data_series", "type": "string", "description": "FFT data as JSON", "optional": True},
|
||||
{"name": "matrix_a", "type": "string", "description": "Matrix as JSON for matrix_eigensystem", "optional": True}
|
||||
]
|
||||
},
|
||||
}
|
||||
@@ -116,7 +146,7 @@ class MCPCalculatorServer:
|
||||
tools.append({
|
||||
"name": tool_name,
|
||||
"description": definition["description"],
|
||||
"params": {
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
param["name"]: {
|
||||
@@ -170,7 +200,11 @@ class MCPCalculatorServer:
|
||||
method = request_dict.get("method")
|
||||
params = request_dict.get("params", {})
|
||||
req_id = request_dict.get("id")
|
||||
|
||||
|
||||
# JSON-RPC notifications (no id) must not be answered
|
||||
if req_id is None and isinstance(method, str) and method.startswith("notifications/"):
|
||||
return None
|
||||
|
||||
# 处理特殊方法
|
||||
if method == "initialize":
|
||||
return {
|
||||
@@ -200,7 +234,10 @@ class MCPCalculatorServer:
|
||||
tool_params = params.get("arguments", {})
|
||||
|
||||
result = self.call_tool(tool_name, tool_params)
|
||||
|
||||
|
||||
is_error = isinstance(result, str) and result.startswith(
|
||||
("Error", "Parameter Error", "Execution Error")
|
||||
)
|
||||
return {
|
||||
"jsonrpc": "2.0",
|
||||
"result": {
|
||||
@@ -209,7 +246,8 @@ class MCPCalculatorServer:
|
||||
"type": "text",
|
||||
"text": result
|
||||
}
|
||||
]
|
||||
],
|
||||
"isError": is_error
|
||||
},
|
||||
"id": req_id
|
||||
}
|
||||
@@ -252,10 +290,11 @@ class MCPCalculatorServer:
|
||||
try:
|
||||
request = json.loads(line)
|
||||
response = self.handle_rpc_request(request)
|
||||
|
||||
# 写入 JSON-RPC 响应到标准输出
|
||||
sys.stdout.write(json.dumps(response, ensure_ascii=False) + '\n')
|
||||
sys.stdout.flush()
|
||||
|
||||
# Notifications return None — nothing must be written back
|
||||
if response is not None:
|
||||
sys.stdout.write(json.dumps(response, ensure_ascii=False) + '\n')
|
||||
sys.stdout.flush()
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
# 发送 JSON 解析错误
|
||||
|
||||
Reference in New Issue
Block a user