tool2agent API Documentation - v0.0.0
    Preparing search index...

    Function toolBuilder

    • Creates a builder for tool definitions.

      Use .field(...) for each dynamic field in the InputSchema, and then call .build() to get the tool. The builder ensures type safety by requiring all dynamic fields to be configured before building.

      The execute function receives fully validated input (after all dynamic fields pass their validation) and must return a ToolCallResult:

      • For success: { ok: true, ...output } (spread output directly if OutputSchema is a record, otherwise use { value: output })
      • For failure: { ok: false, validationResults: {...}, problems: [...], feedback: [...], instructions: [...], ... } with structured feedback

      Type Parameters

      • InputSchema extends ZodObject<
            Readonly<
                {
                    [k: string]: $ZodType<
                        unknown,
                        unknown,
                        $ZodTypeInternals<unknown, unknown>,
                    >;
                },
            >,
            $strip,
        >
      • OutputSchema extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>
      • DynamicFields extends string

      Parameters

      • toolParams: ToolBuilderParams<InputSchema, OutputSchema, DynamicFields>

        The parameters for the tool builder.

        Parameters for creating a tool builder.

        • Optionaldescription?: string

          Optional description of the tool for the LLM.

        • dynamicFields: readonly DynamicFields[]

          Array of field names from InputSchema that are dynamic (will be made optional at runtime).

        • execute: (
              input: z.infer<InputSchema>,
          ) => Promise<ToolCallResult<z.infer<InputSchema>, z.infer<OutputSchema>>>

          Function that executes the tool after all dynamic fields have passed validation.

          The input parameter is fully validated and matches the InputSchema (all required fields are present).

          Must return a ToolCallResult that indicates success or failure:

          • Success: { ok: true, ...output } where output matches OutputSchema (if OutputSchema is a record, spread it directly; otherwise use { value: output })
          • Failure: { ok: false, validationResults: {...}, problems: [...], feedback: [...], instructions: [...], ... } with structured feedback
        • inputSchema: InputSchema

          The Zod schema defining the tool's input structure.

        • outputSchema: OutputSchema

          The Zod schema defining the tool's output structure.

      Returns BuilderApi<output<InputSchema>, output<OutputSchema>, never, DynamicFields>

      A builder object with .field() and .build() methods.

      const tool = toolBuilder({
      inputSchema: z.object({ name: z.string(), age: z.number() }),
      outputSchema: z.object({ greeting: z.string() }),
      dynamicFields: ['name', 'age'],
      execute: async (input) => {
      return { ok: true, greeting: `Hello, ${input.name}!` };
      },
      })
      .field('name', { ... })
      .field('age', { ... })
      .build();