Skip to content

Function: executeCrudOperation()

Makaio Framework


Makaio Framework / tools-core / executeCrudOperation

executeCrudOperation<TInput, TOutput>(input, context, entityName, handlers): Promise<ToolResult<TOutput>>

Defined in: ../../../tools/core/src/tool-utils.ts:127

Executes a CRUD tool operation with session validation and operation routing.

This helper consolidates the common pattern of:

  1. Validating sessionId from context
  2. Routing to operation-specific handlers based on discriminated union

The input type must be a discriminated union with an op field. Handler functions are provided in a map keyed by operation name.

Type parameters:

  • TInput: Discriminated union input type with op field
  • TOutput: Tool output type

TInput extends object

TOutput

TInput

The tool input with operation discriminator

ToolExecutionContext

Tool execution context

string

Entity name for validation error messages (e.g., ‘task’, ‘artifact’)

OperationHandlers<TInput, TOutput>

Map of operation names to handler functions

Promise<ToolResult<TOutput>>

Tool result from the appropriate operation handler

export const myTool = defineTool({
name: 'my_tool',
inputSchema: z.discriminatedUnion('op', [
z.object({ op: z.literal('create'), data: z.string() }),
z.object({ op: z.literal('list') }),
]),
execute: async (input, context) => {
return executeCrudOperation(input, context, 'item', {
create: async (input, sessionId, context) => {
// Handle create...
},
list: async (input, sessionId, context) => {
// Handle list...
},
});
},
});