Skip to content

Function: createTool()

ts
function createTool<TContext, TName>(name): GroupedToolBuilder<TContext, Record<string, never>, TName>;

Defined in: packages/core/src/core/builder/GroupedToolBuilder.ts:138

Create a new grouped tool builder.

This is the recommended entry point for building MCP tools. Equivalent to new GroupedToolBuilder<TContext>(name) but more concise and idiomatic.

Type Parameters

Type ParameterDefault typeDescription
TContextvoidApplication context type passed to every handler. Use void (default) if your handlers don't need context.
TName extends stringstring-

Parameters

ParameterTypeDescription
nameTNameTool name as it appears in the MCP tools/list response. Must be unique across all registered tools.

Returns

GroupedToolBuilder<TContext, Record<string, never>, TName>

A new GroupedToolBuilder configured with the given name.

Example

typescript
// Simple tool (no context)
const echo = createTool('echo')
    .action({
        name: 'say',
        schema: z.object({ message: z.string() }),
        handler: async (_ctx, args) => success(args.message),
    });

// With application context
const users = createTool<AppContext>('users')
    .description('User management')
    .use(requireAuth)
    .action({
        name: 'list',
        readOnly: true,
        handler: async (ctx, _args) => success(await ctx.db.users.findMany()),
    });

// With hierarchical groups
const platform = createTool<AppContext>('platform')
    .tags('core')
    .group('users', 'User management', g => {
        g.action({ name: 'list', readOnly: true, handler: listUsers });
    })
    .group('billing', 'Billing operations', g => {
        g.action({ name: 'refund', destructive: true, schema: refundSchema, handler: issueRefund });
    });

See