solve
Solve equations for specific variables by inputting expressions and variables. Part of the MCP Calc Tools server, enabling AI assistants to handle complex mathematical calculations directly.
Instructions
Solve an equation for a variable
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes | Equation to solve (e.g., "x^2 = 4") | |
| variable | Yes | Variable to solve for |
Implementation Reference
- index.js:350-364 (handler)The handler function for the 'solve' tool. It processes the input equation by splitting on '=', parsing into a mathjs node as left - right = 0, solves for the variable using math.solve, maps solutions to strings, or returns error.async ({ expression, variable }) => { try { // Convert equation to standard form (expression = 0) const sides = expression.split('=').map(s => s.trim()); if (sides.length !== 2) { throw new Error('Invalid equation format. Use "expression = value"'); } const equationNode = math.parse(`${sides[0]}-(${sides[1]})`); const solutions = math.solve(equationNode, variable); return solutions.map(sol => sol.toString()); } catch (e) { return [`Error: ${e.message}`]; } }
- index.js:341-349 (schema)Schema definition for the 'solve' tool including name, description, input schema (expression and variable strings), and output as array of strings.{ name: 'solve', description: 'Solve an equation for a variable', inputSchema: z.object({ expression: z.string().describe('Equation to solve (e.g., "x^2 = 4")'), variable: z.string().describe('Variable to solve for') }), outputSchema: z.array(z.string()), },
- index.js:340-365 (registration)Registration of the 'solve' tool using ai.defineTool, providing schema and inline handler implementation.ai.defineTool( { name: 'solve', description: 'Solve an equation for a variable', inputSchema: z.object({ expression: z.string().describe('Equation to solve (e.g., "x^2 = 4")'), variable: z.string().describe('Variable to solve for') }), outputSchema: z.array(z.string()), }, async ({ expression, variable }) => { try { // Convert equation to standard form (expression = 0) const sides = expression.split('=').map(s => s.trim()); if (sides.length !== 2) { throw new Error('Invalid equation format. Use "expression = value"'); } const equationNode = math.parse(`${sides[0]}-(${sides[1]})`); const solutions = math.solve(equationNode, variable); return solutions.map(sol => sol.toString()); } catch (e) { return [`Error: ${e.message}`]; } } );