Skip to main content

Using El Form with AI agents

Give the agent a concrete acceptance test. For a team invitation, the person entering the form must supply an email address and choose an allowed role. The application must receive the chosen role when the form succeeds. A useful result has three parts: a component that compiles, feedback for an invalid invitation, and a test that captures the submitted invitation.

Discover the installed tools​

Connect the local server using the MCP setup guide. Ask the client to list its tools and prompts. Servers with the verification tools offer the prompt el_form_build_and_verify. Pass this task to the prompt:

Build a dark invitation form for an email and a role. Allow owner and member. Show a field error when the email is invalid. Submit a valid invitation to a callback. Verify the payload.

In a client without prompts, request the agent-workflow topic using el_form_get_topic. All calls in this walkthrough work over stdio.

Generate the component​

Call el_form_scaffold_form with this argument:

{
"api": "autoform",
"theme": "dark",
"includeTest": true,
"fields": [
{ "name": "email", "type": "email", "label": "Work email" },
{ "name": "role", "type": "select", "label": "Role", "options": ["owner", "member"] }
]
}

In an SDK client, read response.structuredContent. The text content contains a fenced source preview. For example, with a connected JavaScript client, the arguments above and a new component directory:

import { writeFile } from "node:fs/promises";

const response = await client.callTool({ name: "el_form_scaffold_form", arguments: args });
if (response.isError || !response.structuredContent?.verification) {
throw new Error("Form generation failed or the server did not return a test.");
}
const { code, schema, verification } = response.structuredContent;
await writeFile(verification.moduleFileName, code, { flag: "wx" });
await writeFile(verification.testFileName, verification.testSource, { flag: "wx" });

The generated test imports GeneratedForm.tsx from the same directory. Run it with Vitest, jsdom and React Testing Library. The component exports schema and GeneratedForm. Keep the portable JSON schema from the tool result for the sample checks below. Select "api": "useform" and omit theme when the project supplies its own controls.

In the consumer, import the packages used by the generated file. An AutoForm scaffold needs React, Zod and el-form-react-components. Give the generated component an application callback:

import { GeneratedForm, schema } from "./GeneratedForm";
import type { z } from "zod";

type Invitation = z.output<typeof schema>;
export function Invitations({ invite }: { invite: (data: Invitation) => Promise<void> }) {
return <GeneratedForm onSubmit={invite} />;
}
Try the form generator

Generate a form

Edit the field list to update the component and its input schema.

Fields validated

3 fields pass the MCP field contract. The component accepts an application-provided onSubmit callback.

Generated TSX

AutoForm · dark

import { AutoForm } from "el-form-react-components";
import "el-form-react-components/styles.css";
import { z } from "zod";

export const generatedFormSchema = z.object({
  "workEmail": z.string().email("Enter a valid email"),
  "role": z.enum(["owner", "member"] as const),
  "notes": z.string().transform(value => value === "" ? undefined : value).optional(),
});
export { generatedFormSchema as schema };

export interface GeneratedFormProps {
  onSubmit: (data: z.output<typeof generatedFormSchema>) => void | Promise<void>;
}

export function GeneratedForm({ onSubmit }: GeneratedFormProps) {
  return (
    <AutoForm
      schema={generatedFormSchema}
      initialValues={{
        "workEmail": "",
        "role": "owner",
        "notes": "",
      }}
      fields={[
        { name: "workEmail", label: "Work email", type: "email" },
        { name: "role", label: "Role", type: "select", options: [{"value":"owner","label":"owner"},{"value":"member","label":"member"}] },
        { name: "notes", label: "Notes", type: "textarea", required: false },
      ]}
      theme="dark"
      noValidate
      onSubmit={onSubmit}
    />
  );
}
Input JSON Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "workEmail": {
      "type": "string",
      "minLength": 1,
      "format": "email",
      "title": "Work email"
    },
    "role": {
      "type": "string",
      "enum": [
        "owner",
        "member"
      ],
      "title": "Role"
    },
    "notes": {
      "anyOf": [
        {
          "type": "string",
          "maxLength": 0
        },
        {
          "type": "string",
          "minLength": 1
        }
      ],
      "title": "Notes"
    }
  },
  "required": [
    "workEmail",
    "role"
  ],
  "$comment": "Generated Zod object schemas strip unknown keys on parse."
}
Check sample data
Submission test

Open Check sample data in the generator to validate a JSON example. A failure lists the field, rule and suggested correction. Edit the example and run the same check again.

Open the form generator in the playground

Check two samples and repair one​

Call el_form_validate_schema with the complete returned schema in the schema argument and the following object in sample:

{ "email": "sam@example.org", "role": "viewer" }

Expect valid: false, stage: "sample", and an issue with path: ["role"]. Pass its issues array to el_form_explain_validation_error. The tool accepts the same array you just received. It also accepts an El Form error map, for example {"errors":{"role":"Choose a role"}}.

Here the acceptance test allows only owner and member. Change the example role to "member" and run the schema check again. Expect valid: true and an empty issues array. If the product also needs viewer access, change the scaffold's options and regenerate the component and its schema together.

Verify the React submission​

Run the consuming project's TypeScript checker. With Vitest and React Testing Library, this example verifies the invalid path and the callback. Use jsdom for the test.

import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { expect, it, vi } from "vitest";
import { GeneratedForm } from "./GeneratedForm";

it("requires a valid email before it delivers the invitation", async () => {
const invite = vi.fn();
render(<GeneratedForm onSubmit={invite} />);
const email = screen.getByRole("textbox", { name: /work email/i });
const role = screen.getByRole("combobox", { name: /role/i });
const submit = screen.getByRole("button", { name: /submit/i });

fireEvent.change(role, { target: { value: "member" } });
fireEvent.change(email, { target: { value: "invalid" } });
fireEvent.click(submit);
await waitFor(() => expect(email.getAttribute("aria-invalid")).toBe("true"));
expect(invite).not.toHaveBeenCalled();

fireEvent.change(email, { target: { value: "sam@example.org" } });
fireEvent.click(submit);
await waitFor(() => expect(invite).toHaveBeenCalledWith({
email: "sam@example.org", role: "member"
}));
});

If the application adds a transform, check its result in this last assertion. For example, a seats field edited as "3" can submit the number 3. A JSON Schema sample check cannot execute your Zod transform or confirm that the callback ran. Schema inputs and outputs

Report the generated API and theme, which samples passed or failed, and the actual callback payload. A reviewer can reproduce that behavior with the same sample. What a generated form test proves

Build a form with repeated rows​

The scaffold accepts flat field descriptions. For an order with editable line items, request the field-arrays topic and build a custom form with useFieldArray. Use a stable row ID for the React key and a dotted index in the registered field name. Explain errors with the path array, for example ["lines", 0, "quantity"]. Verify that a failed row gets focus, a repaired quantity submits as a number and a moved row keeps its draft. Parsed line items example