Skip to content

Function: defineTool()

ts
function defineTool<TContext>(name, config): GroupedToolBuilder<TContext>;

Defined in: packages/core/src/core/builder/defineTool.ts:220

Define a tool using a high-level JSON-like config.

This is the recommended entry point for most developers. The framework handles all Zod schema creation, validation, and MCP protocol details internally.

Type Parameters

Type ParameterDefault type
TContextvoid

Parameters

ParameterType
namestring
configToolConfig<TContext>

Returns

GroupedToolBuilder<TContext>

Example

typescript
const echo = defineTool('echo', {
    actions: {
        say: {
            params: { message: 'string' },
            handler: async (ctx, args) => success(args.message),
        },
    },
});

// Tool with shared params + groups + middleware
const platform = defineTool<AppContext>('platform', {
    description: 'Platform management',
    tags: ['admin'],
    shared: { workspace_id: { type: 'string', description: 'Workspace ID' } },
    middleware: [requireAuth],
    groups: {
        users: {
            description: 'User management',
            actions: {
                list: { readOnly: true, handler: listUsers },
                ban:  { destructive: true, params: { user_id: 'string' }, handler: banUser },
            },
        },
    },
});

// Register normally
const registry = new ToolRegistry<AppContext>();
registry.register(platform);

See