calculator_tool
Perform diverse math calculations including expressions, functions, statistics, geometry, financial math, logic operations, number theory, combinatorics, probability, set theory, and complex numbers with precise results.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| calculation_type | Yes | The type of calculation to perform. | |
| combinatorics_operation | No | The type of combinatorics operation. | |
| complex_a_imaginary | No | The imaginary part of the first complex number. | |
| complex_a_real | No | The real part of the first complex number. | |
| complex_b_imaginary | No | The imaginary part of the second complex number (if needed). | |
| complex_b_real | No | The real part of the second complex number (if needed). | |
| complex_number_operation | No | The type of complex number operation. | |
| data_points | No | The array of data points. | |
| data_set_x | No | The dataset X for bivariate statistics. | |
| data_set_y | No | The dataset Y for bivariate statistics. | |
| expression | No | The mathematical expression to calculate. | |
| financial_math_operation | No | The type of financial math calculation to perform. | |
| function_argument | No | The argument value for the function. | |
| function_name | No | The name of the function to calculate. | |
| geometry_operation | No | The type of geometric calculation to perform. | |
| height | No | The height. | |
| length | No | The length. | |
| logic_operation | No | The type of logic operation to perform. | |
| n_compounding_periods | No | The number of compounding periods. | |
| n_value | No | The n value. | |
| number_a | No | The first number. | |
| number_b | No | The second number (if needed). | |
| number_theory_modulus | No | The modulus for modular exponentiation. | |
| number_theory_operation | No | The type of number theory operation. | |
| operand_a | No | The first operand. | |
| operand_b | No | The second operand. | |
| precision_level | No | Calculation precision level. | |
| principal | No | The principal amount. | |
| probability_a | No | The probability of event A (between 0 and 1). | |
| probability_a_given_b | No | The conditional probability of A given B (between 0 and 1). | |
| probability_b | No | The probability of event B (between 0 and 1). | |
| probability_b_given_a | No | The conditional probability of B given A (between 0 and 1). | |
| probability_operation | No | The type of probability calculation. | |
| r_value | No | The r value (if needed). | |
| radius | No | The radius of the circle. | |
| rate | No | The interest rate (percentage). | |
| set_a | No | The first set. | |
| set_b | No | The second set. | |
| set_theory_operation | No | The type of set theory operation. | |
| side | No | The side length of the cube. | |
| statistics_operation | No | The type of statistical calculation to perform. | |
| time | No | The time (in years). | |
| width | No | The width. |
Implementation Reference
- src/tools/calculator_tool.ts:283-342 (handler)Main handler function that processes the tool request, dispatches to appropriate helper based on calculation_type, handles precision with Decimal.js, and returns formatted result or error.export default async function (request: any) { try { const { calculation_type, ...params } = request.params.arguments; const precisionLevel = params.precision_level || 64; Decimal.set({ precision: precisionLevel }); let result; switch (calculation_type) { case "evaluate_expression": if (!params.expression) throw new Error("An expression is required."); result = evaluate(params.expression); break; case "calculate_statistics": result = handleStatistics(params); break; case "perform_geometry": result = handleGeometry(params); break; case "perform_financial_math": result = handleFinancialMath(params); break; case "perform_logic_operations": result = handleLogicOperations(params); break; case "perform_number_theory": result = handleNumberTheory(params); break; case "perform_combinatorics": result = handleCombinatorics(params); break; case "calculate_probability": result = handleProbability(params); break; case "perform_set_theory": result = handleSetTheory(params); break; case "perform_complex_number": result = handleComplexNumber(params); break; default: throw new Error(`Unsupported calculation type: ${calculation_type}`); } return { content: [{ type: "text", text: JSON.stringify({ result }, null, 2), }], }; } catch (error) { return { content: [{ type: "text", text: JSON.stringify({ error: error instanceof Error ? error.message : String(error) }, null, 2), }], isError: true, }; } }
- src/tools/calculator_tool.ts:5-71 (schema)Input schema for the calculator_tool defining all possible calculation types and their parameters.export const schema = { name: "calculator_tool", description: "A powerful universal calculator supporting basic expressions, statistics, geometry, finance, logic, number theory, combinatorics, probability, set theory, and complex numbers.", type: "object", properties: { calculation_type: { type: "string", enum: [ "evaluate_expression", "calculate_statistics", "perform_geometry", "perform_financial_math", "perform_logic_operations", "perform_number_theory", "perform_combinatorics", "calculate_probability", "perform_set_theory", "perform_complex_number" ], description: "The type of calculation to perform.", }, // Common params expression: { type: "string", description: "The mathematical expression to evaluate (for 'evaluate_expression')." }, // Statistics params statistics_operation: { type: "string", enum: ["mean", "median", "mode", "standard_deviation", "variance", "correlation", "regression"] }, data_points: { type: "array", items: { type: "number" } }, data_set_x: { type: "array", items: { type: "number" } }, data_set_y: { type: "array", items: { type: "number" } }, // Geometry params geometry_operation: { type: "string", enum: ["area_circle", "area_rectangle", "volume_cube"] }, radius: { type: "number" }, length: { type: "number" }, width: { type: "number" }, side: { type: "number" }, // Financial params financial_math_operation: { type: "string", enum: ["simple_interest", "compound_interest", "present_value", "future_value"] }, principal: { type: "number" }, rate: { type: "number", description: "Interest rate as a percentage (e.g., 5 for 5%)." }, time: { type: "number", description: "Time in years." }, n_compounding_periods: { type: "integer" }, future_value: { type: "number" }, // Logic params logic_operation: { type: "string", enum: ["AND", "OR", "NOT", "XOR"] }, operand_a: { type: "boolean" }, operand_b: { type: "boolean" }, // Number Theory params number_theory_operation: { type: "string", enum: ["gcd", "lcm", "prime_factorization", "is_prime", "modular_exponentiation"] }, number_a: { type: "integer" }, number_b: { type: "integer" }, number_theory_modulus: { type: "integer" }, // Combinatorics params combinatorics_operation: { type: "string", enum: ["permutation", "combination", "factorial", "binomial_coefficient"] }, n_value: { type: "integer" }, r_value: { type: "integer" }, // Probability params probability_operation: { type: "string", enum: ["probability_event", "conditional_probability", "bayes_theorem"] }, probability_a: { type: "number" }, probability_b: { type: "number" }, probability_a_given_b: { type: "number" }, probability_b_given_a: { type: "number" }, // Set Theory params set_theory_operation: { type: "string", enum: ["union", "intersection", "difference", "symmetric_difference", "is_subset"] }, set_a: { type: "array", items: { "anyOf": [{ "type": "number" }, { "type": "string" }] } }, set_b: { type: "array", items: { "anyOf": [{ "type": "number" }, { "type": "string" }] } }, // Complex Number params complex_number_operation: { type: "string", enum: ["add", "subtract", "multiply", "divide", "modulus", "argument", "conjugate"] }, complex_a: { type: "string", description: "First complex number (e.g., '3 + 4i')." }, complex_b: { type: "string", description: "Second complex number (e.g., '1 - 2i')." }, // Global params precision_level: { type: "number", enum: [32, 64, 128], default: 64 }, }, required: ["calculation_type"] };
- src/handler/ToolHandler.ts:91-139 (registration)Dynamic tool registration function that scans src/tools directory, imports each tool file (including calculator_tool.ts), extracts schema and handler using filename as name, and registers in global handlers.export async function loadTools(reload: boolean = false): Promise<{ [key: string]: (request: ToolRequest) => Promise<ToolResponse> }> { // 如果是初始加载且已加载,则直接返回 if (!reload && isLoaded) return; // 如果是重新加载,则重置状态 if (reload) { for (const tool of tools) { await tool?.destroy?.(); delete handlers[tool.name]; } tools.length = 0; isLoaded = false; } // 获取所有工具文件 const toolFiles = fs.readdirSync(toolsDir).filter(file => file.endsWith('.js') || file.endsWith('.ts')); // 加载每个工具 for (const file of toolFiles) { const toolPath = path.join(toolsDir, file); try { // 如果是重新加载,清除模块缓存 if (reload) clearModuleCache(toolPath); // 导入模块,重新加载时添加时间戳防止缓存 const importPath = 'file://' + toolPath + (reload ? `?update=${Date.now()}` : ''); const { default: tool, schema, destroy } = await import(importPath); const toolName = path.parse(toolPath).name; // 注册工具 tools.push({ name: toolName, description: tool.description, inputSchema: schema, destroy: destroy }); // 注册处理函数 handlers[toolName] = async (request: ToolRequest) => { return await tool(request); }; } catch (error) { console.error(`Failed to ${reload ? 'reload' : 'load'} tool ${file}:`, error); } } isLoaded = true; if (reload) console.log(`Successfully reloaded ${tools.length} tools`); return handlers; }