Skip to main content
Glama

solve

Solve equations for specific variables using symbolic mathematics. Input an equation and target variable to get the solution.

Instructions

Solve an equation for a variable

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
expressionYesEquation to solve (e.g., "x^2 = 4")
variableYesVariable to solve for

Implementation Reference

  • Handler function that processes the input equation by normalizing it to zero form, parses it using math.parse, solves for the specified variable using math.solve, converts solutions to strings, and handles errors.
    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}`]; } }
  • Schema definition for the 'solve' tool: input requires 'expression' (equation string) and 'variable' (string to solve for); output is an array of solution 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 name, description, input/output schemas, 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}`]; } } );

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/nbiish/mcp-calc-tools'

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