Skip to main content
Glama

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
NameRequiredDescriptionDefault
calculation_typeYesThe type of calculation to perform.
combinatorics_operationNoThe type of combinatorics operation.
complex_a_imaginaryNoThe imaginary part of the first complex number.
complex_a_realNoThe real part of the first complex number.
complex_b_imaginaryNoThe imaginary part of the second complex number (if needed).
complex_b_realNoThe real part of the second complex number (if needed).
complex_number_operationNoThe type of complex number operation.
data_pointsNoThe array of data points.
data_set_xNoThe dataset X for bivariate statistics.
data_set_yNoThe dataset Y for bivariate statistics.
expressionNoThe mathematical expression to calculate.
financial_math_operationNoThe type of financial math calculation to perform.
function_argumentNoThe argument value for the function.
function_nameNoThe name of the function to calculate.
geometry_operationNoThe type of geometric calculation to perform.
heightNoThe height.
lengthNoThe length.
logic_operationNoThe type of logic operation to perform.
n_compounding_periodsNoThe number of compounding periods.
n_valueNoThe n value.
number_aNoThe first number.
number_bNoThe second number (if needed).
number_theory_modulusNoThe modulus for modular exponentiation.
number_theory_operationNoThe type of number theory operation.
operand_aNoThe first operand.
operand_bNoThe second operand.
precision_levelNoCalculation precision level.
principalNoThe principal amount.
probability_aNoThe probability of event A (between 0 and 1).
probability_a_given_bNoThe conditional probability of A given B (between 0 and 1).
probability_bNoThe probability of event B (between 0 and 1).
probability_b_given_aNoThe conditional probability of B given A (between 0 and 1).
probability_operationNoThe type of probability calculation.
r_valueNoThe r value (if needed).
radiusNoThe radius of the circle.
rateNoThe interest rate (percentage).
set_aNoThe first set.
set_bNoThe second set.
set_theory_operationNoThe type of set theory operation.
sideNoThe side length of the cube.
statistics_operationNoThe type of statistical calculation to perform.
timeNoThe time (in years).
widthNoThe width.

Implementation Reference

  • 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, }; } }
  • 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"] };
  • 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; }

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/xiaoguomeiyitian/ToolBox'

If you have feedback or need assistance with the MCP directory API, please join our Discord server