Skip to main content
Glama
nbiish
by nbiish

area

Calculate the area under a curve between two points using numerical integration. Input a mathematical expression and integration bounds to compute the enclosed region.

Instructions

Calculate the area under a curve between two points

Input Schema

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

Implementation Reference

  • Handler function for the 'area' tool. It invokes the riemannSum helper with the trapezoid method to approximate the area under the curve (definite integral).
    async ({ expression, start, end, n = 1000 }) => {
      return riemannSum(expression, 'x', start, end, n, 'trapezoid');
    }
  • Input and output schema for the 'area' tool. Defines parameters: expression (string), start/end (numbers), optional n (subintervals). Outputs a number.
    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(),
  • index.js:131-146 (registration)
    Registration of the 'area' tool via ai.defineTool, including name, description, schema, and inline handler.
    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');
      }
    );
  • riemannSum helper function that performs numerical integration using left, right, midpoint, or trapezoid Riemann sums. Used by the 'area' handler with 'trapezoid' method.
    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