divide
Perform division operations by calculating the quotient of two numbers, handling division by zero errors with proper error management.
Instructions
Divide a by b (a / b)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| a | Yes | ||
| b | Yes |
Implementation Reference
- src/index.ts:176-191 (handler)The handler function for the 'divide' tool. It validates that b is not zero, computes the quotient a / b, and returns a textual response with the result or an error.async ({ a, b }) => { if (b === 0) { return { content: [ { type: "text", text: `Error: Division by zero is not allowed.` } ], isError: true }; } const quotient = a / b; return { content: [ { type: "text", text: `The quotient of ${a} divided by ${b} is ${quotient}` } ] }; }
- src/index.ts:174-174 (schema)The input schema for the 'divide' tool, defining parameters 'a' and 'b' as numbers using Zod.inputSchema: { a: z.number(), b: z.number() },
- src/index.ts:169-192 (registration)The registration of the 'divide' tool using server.registerTool, including name, metadata (title, description, schema), and handler function.server.registerTool( "divide", { title: "Division tool", description: "Divide a by b (a / b)", inputSchema: { a: z.number(), b: z.number() }, }, async ({ a, b }) => { if (b === 0) { return { content: [ { type: "text", text: `Error: Division by zero is not allowed.` } ], isError: true }; } const quotient = a / b; return { content: [ { type: "text", text: `The quotient of ${a} divided by ${b} is ${quotient}` } ] }; } );