sqrt
Calculate the square root of a number with results rounded to two decimal places for precise mathematical computations.
Instructions
Calculate the square root of a number (rounded to two decimals)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| x | Yes |
Implementation Reference
- src/index.ts:39-55 (handler)The handler function that computes the square root of the input 'x' using Math.sqrt, returns an error for negative inputs, rounds the result to two decimal places, and formats the response as MCP content.async ({ x }) => { if (x < 0) { return { content: [ { type: "text", text: `Error: Square root of negative number is not allowed.` } ], isError: true }; } const result = Math.sqrt(x); const rounded = Math.round(result * 100) / 100; return { content: [ { type: "text", text: `The square root of ${x} is ${rounded}` } ] }; }
- src/index.ts:34-38 (schema)Defines the input schema for the 'sqrt' tool using Zod: expects a single number parameter 'x'. Includes title and description.{ title: "Square Root tool", description: "Calculate the square root of a number (rounded to two decimals)", inputSchema: { x: z.number() }, },
- src/index.ts:32-56 (registration)Registers the 'sqrt' tool with the MCP server, providing the tool name, schema/metadata, and handler function.server.registerTool( "sqrt", { title: "Square Root tool", description: "Calculate the square root of a number (rounded to two decimals)", inputSchema: { x: z.number() }, }, async ({ x }) => { if (x < 0) { return { content: [ { type: "text", text: `Error: Square root of negative number is not allowed.` } ], isError: true }; } const result = Math.sqrt(x); const rounded = Math.round(result * 100) / 100; return { content: [ { type: "text", text: `The square root of ${x} is ${rounded}` } ] }; } );