Skip to main content
Glama

area

Compute the area under a curve defined by a mathematical expression between specified start and end points, using numerical integration for accurate results.

Instructions

Calculate the area under a curve between two points

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
endYes
expressionYes
nNoNumber of subintervals (default: 1000)
startYes

Implementation Reference

  • index.js:131-146 (registration)
    Registration of the 'area' tool using ai.defineTool, which includes the name, description, input/output schemas, and the handler function.
    ai.defineTool( { name: 'area', description: 'Calculate the area under a curve between two points', inputSchema: z.object({ expression: z.string(), start: z.number(), end: z.number(), n: z.number().optional().describe('Number of subintervals (default: 1000)') }), outputSchema: z.number(), }, async ({ expression, start, end, n = 1000 }) => { return riemannSum(expression, 'x', start, end, n, 'trapezoid'); } );
  • The handler function for the 'area' tool, which calls the riemannSum helper with trapezoid method to approximate the definite integral.
    async ({ expression, start, end, n = 1000 }) => { return riemannSum(expression, 'x', start, end, n, 'trapezoid'); }
  • Input schema defines parameters for the mathematical expression, integration limits (start, end), and optional number of subintervals. Output is a number representing the area.
    inputSchema: z.object({ expression: z.string(), start: z.number(), end: z.number(), n: z.number().optional().describe('Number of subintervals (default: 1000)') }), outputSchema: z.number(),
  • riemannSum helper function that implements various Riemann sum approximations (left, right, midpoint, trapezoid), used by the 'area' tool.
    const riemannSum = (expr, variable, a, b, n, method = 'midpoint') => { try { const deltaX = (b - a) / n; let sum = 0; const node = math.parse(expr); const scope = {}; if (method === 'left' || method === 'right') { const offset = method === 'right' ? 1 : 0; for (let i = 0; i < n; i++) { const x = a + (i + offset) * deltaX; scope[variable] = x; sum += math.evaluate(node, scope) * deltaX; } } else if (method === 'midpoint') { for (let i = 0; i < n; i++) { const x = a + (i + 0.5) * deltaX; scope[variable] = x; sum += math.evaluate(node, scope) * deltaX; } } else if (method === 'trapezoid') { for (let i = 0; i <= n; i++) { const x = a + i * deltaX; scope[variable] = x; const coef = (i === 0 || i === n) ? 0.5 : 1; sum += coef * math.evaluate(node, scope) * deltaX; } } return sum; } 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