Skip to content

Function: defineTool()

Makaio Framework


Makaio Framework / tools-core / defineTool

defineTool<TInput, TOutput>(config): ToolDefinition<TInput, TOutput>

Defined in: ../../../tools/core/src/define-tool.ts:83

Creates a ToolDefinition from a configuration object.

Provides a clean builder API for defining tools with full type inference. The returned ToolDefinition can be used directly or registered with a toolset.

TInput extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>

Zod schema type for input validation

TOutput extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>

Zod schema type for output validation

DefineToolConfig<TInput, TOutput>

Tool configuration

ToolDefinition<TInput, TOutput>

Fully typed ToolDefinition

import { z } from 'zod';
import { defineTool, toolSuccess, toolError, ToolErrorCodes } from '@makaio/tools-core';
const ReadFileInputSchema = z.object({
path: z.string().describe('File path to read'),
encoding: z.enum(['utf-8', 'base64']).default('utf-8'),
});
const ReadFileOutputSchema = z.object({
content: z.string(),
size: z.number(),
});
const readFileTool = defineTool({
name: 'readFile',
description: 'Reads a file from the filesystem',
annotations: { readOnly: true },
inputSchema: ReadFileInputSchema,
outputSchema: ReadFileOutputSchema,
execute: async (input, context) => {
const fullPath = path.resolve(context.cwd, input.path);
try {
const content = await fs.readFile(fullPath, input.encoding);
const stats = await fs.stat(fullPath);
return toolSuccess({ content, size: stats.size });
} catch (err) {
return toolError(
ToolErrorCodes.RESOURCE_NOT_FOUND,
`Failed to read file: ${fullPath}`
);
}
},
});