ElicitationField
Defines a single field in an elicitation form.
Field Schema
interface ElicitationField {
name: string;
label: string;
type: FieldType;
description?: string;
required?: boolean;
defaultValue?: any;
options?: Array<{ label: string; value: any }>;
validation?: FieldValidation;
placeholder?: string;
helpText?: string;
}
Unique field identifier (used as key in response object)
Human-readable field label
Field type (see Field Types below)
Additional description text
Whether the field is required
Default value for the field
options
Array<{label: string, value: any}>
Options for select/multiselect fields
Validation rules (see Validation below)
Placeholder text for input fields
Help text displayed below the field
Field Types
text
Single-line text input.
{
name: 'username',
label: 'Username',
type: 'text',
required: true,
placeholder: 'Enter username',
validation: {
minLength: 3,
maxLength: 20,
pattern: '^[a-zA-Z0-9_]+$',
},
}
textarea
Multi-line text input.
{
name: 'description',
label: 'Description',
type: 'textarea',
placeholder: 'Enter description',
helpText: 'Provide a detailed description',
validation: {
maxLength: 500,
},
}
number
Numeric input.
{
name: 'age',
label: 'Age',
type: 'number',
required: true,
defaultValue: 18,
validation: {
min: 0,
max: 120,
},
}
boolean
Checkbox input.
{
name: 'subscribe',
label: 'Subscribe to newsletter',
type: 'boolean',
defaultValue: false,
}
select
Dropdown selection (single value).
{
name: 'country',
label: 'Country',
type: 'select',
required: true,
options: [
{ label: 'United States', value: 'US' },
{ label: 'Canada', value: 'CA' },
{ label: 'United Kingdom', value: 'GB' },
],
}
multiselect
Multiple selection.
{
name: 'interests',
label: 'Interests',
type: 'multiselect',
options: [
{ label: 'Technology', value: 'tech' },
{ label: 'Sports', value: 'sports' },
{ label: 'Music', value: 'music' },
{ label: 'Travel', value: 'travel' },
],
}
email
Email input with validation.
{
name: 'email',
label: 'Email Address',
type: 'email',
required: true,
placeholder: '[email protected]',
validation: {
errorMessage: 'Please enter a valid email address',
},
}
url
URL input with validation.
{
name: 'website',
label: 'Website',
type: 'url',
placeholder: 'https://example.com',
validation: {
errorMessage: 'Please enter a valid URL',
},
}
date
Date picker.
{
name: 'birthdate',
label: 'Date of Birth',
type: 'date',
required: true,
}
FieldValidation
Validation rules for form fields.
interface FieldValidation {
min?: number;
max?: number;
minLength?: number;
maxLength?: number;
pattern?: string;
customValidator?: (value: any) => boolean | string;
errorMessage?: string;
}
Minimum value (for number fields)
Maximum value (for number fields)
Minimum length (for text fields)
Maximum length (for text fields)
Regular expression pattern
customValidator
(value: any) => boolean | string
Custom validation function. Return true for valid, false or error message string for invalid.
Custom error message to display when validation fails
Validation Examples
Text Length Validation
{
name: 'password',
label: 'Password',
type: 'text',
required: true,
validation: {
minLength: 8,
maxLength: 100,
errorMessage: 'Password must be between 8-100 characters',
},
}
Pattern Validation
{
name: 'phone',
label: 'Phone Number',
type: 'text',
required: true,
placeholder: '(555) 123-4567',
validation: {
pattern: '^\\(\\d{3}\\) \\d{3}-\\d{4}$',
errorMessage: 'Phone must be in format: (555) 123-4567',
},
}
Range Validation
{
name: 'quantity',
label: 'Quantity',
type: 'number',
required: true,
validation: {
min: 1,
max: 100,
errorMessage: 'Quantity must be between 1 and 100',
},
}
Custom Validation
{
name: 'couponCode',
label: 'Coupon Code',
type: 'text',
validation: {
customValidator: (value) => {
if (!value) return true; // Optional field
return value.toUpperCase().startsWith('SAVE') || 'Coupon must start with SAVE';
},
},
}
ValidationBuilder
Fluent API for building validation rules.
import { validation } from '@leanmcp/elicitation';
const passwordValidation = validation()
.minLength(8)
.maxLength(100)
.pattern('^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)')
.errorMessage('Password must contain uppercase, lowercase, and number')
.build();
Methods
customValidator(fn: (value: any) => boolean | string)
Add custom validator function
errorMessage(message: string)
Set error message
Build the validation object
Usage Example
import { ElicitationFormBuilder, validation } from '@leanmcp/elicitation';
const form = new ElicitationFormBuilder()
.title('User Registration')
.addTextField('username', 'Username', {
required: true,
validation: validation()
.minLength(3)
.maxLength(20)
.pattern('^[a-zA-Z0-9_]+$')
.errorMessage('Username must be 3-20 alphanumeric characters or underscore')
.build(),
})
.addTextField('password', 'Password', {
required: true,
validation: validation()
.minLength(8)
.customValidator((value) => {
if (!/[A-Z]/.test(value)) return 'Must contain uppercase letter';
if (!/[a-z]/.test(value)) return 'Must contain lowercase letter';
if (!/\d/.test(value)) return 'Must contain number';
return true;
})
.build(),
})
.build();
Fluent API for building forms.
Methods
description(description: string)
Set form description
condition(fn: (args: any) => boolean)
Set condition for when elicitation should occur
addTextField(name, label, options?)
Add text field
addTextAreaField(name, label, options?)
Add textarea field
addNumberField(name, label, options?)
Add number field
addBooleanField(name, label, options?)
Add boolean field
addSelectField(name, label, options, fieldOptions?)
Add select field
addMultiSelectField(name, label, options, fieldOptions?)
Add multiselect field
addEmailField(name, label, options?)
Add email field
addUrlField(name, label, options?)
Add URL field
addDateField(name, label, options?)
Add date field
addCustomField(field: ElicitationField)
Add custom field with full control
Build the final configuration
Complete Example
import { ElicitationFormBuilder, validation } from '@leanmcp/elicitation';
import { Tool } from '@leanmcp/core';
import { Elicitation } from '@leanmcp/elicitation';
@Tool({ description: 'Create event' })
@Elicitation({
builder: () => {
return new ElicitationFormBuilder()
.title('Create Event')
.description('Fill out the event details')
.addTextField('title', 'Event Title', {
required: true,
validation: validation().minLength(5).maxLength(100).build(),
})
.addTextAreaField('description', 'Description', {
required: true,
placeholder: 'Describe your event...',
})
.addDateField('date', 'Event Date', {
required: true,
})
.addSelectField('category', 'Category', [
{ label: 'Conference', value: 'conference' },
{ label: 'Workshop', value: 'workshop' },
{ label: 'Webinar', value: 'webinar' },
])
.addNumberField('maxAttendees', 'Max Attendees', {
defaultValue: 50,
validation: validation().min(1).max(1000).build(),
})
.addBooleanField('requiresRegistration', 'Requires Registration', {
defaultValue: true,
})
.build();
},
})
async createEvent(args: CreateEventInput) {
// Implementation
return { eventId: '123', ...args };
}