edit-mcp-form.tsx
Overview
edit-mcp-form.tsx is a React functional component file designed for rendering and managing a form to edit or create an MCP (presumably a "Managed Connection Point" or similar entity) within a client-side React application. This form leverages react-hook-form for form state management and validation, and zod for schema-based validation. It supports fields such as MCP name, URL, server type, and an optional authorization token.
The form integrates with internationalization (i18next) for localized placeholder and validation messages. It also uses custom UI components for form layout and inputs and allows notifying parent components about field changes through a callback mechanism.
Detailed Explanation
Enumerations
ServerType
export enum ServerType {
SSE = 'sse',
StreamableHttp = 'streamable-http',
}
Purpose: Defines the two allowed server types for MCP connections.
Values:
SSE: Server-Sent Events.StreamableHttp: Streamable HTTP connections.
Constants
FormId
export const FormId = 'EditMcpForm';
Purpose: String identifier used as the HTML form's
idattribute for accessibility or other DOM operations.
ServerTypeOptions
const ServerTypeOptions = buildOptions(ServerType);
Purpose: Converts the
ServerTypeenum into an options array compatible with theRAGFlowSelectdropdown component via the utility functionbuildOptions.
Functions
useBuildFormSchema
export function useBuildFormSchema()
Purpose: Builds and returns a Zod validation schema object for the MCP form fields.
Returns: A
zodschema object validating:name: required string, 1-64 characters, allowing alphanumeric, underscores, and dashes.url: required valid URL string.server_type: required non-empty string.authorization_token: optional string.
Details:
Uses
i18next'stfunction for localized error messages and placeholders.Ensures trimmed inputs to avoid whitespace errors.
Usage Example:
const schema = useBuildFormSchema();
Components
EditMcpForm
export function EditMcpForm({
form,
onOk,
setFieldChanged,
}: IModalProps<any> & {
form: UseFormReturn<any>;
setFieldChanged: Dispatch<SetStateAction<boolean>>;
})
Purpose: Renders an MCP editing form with inputs for name, URL, server type, and authorization token.
Props:
form: An instance ofUseFormReturnfromreact-hook-form, controlling the form state.onOk: Optional callback invoked on form submission, receiving the validated form data.setFieldChanged: React state setter function to notify when any field has changed.
Returns: JSX element rendering the controlled form.
Behavior:
Uses
FormFieldcomponents to bind form controls to thereact-hook-formcontroller.Each field displays label, input/select, and validation messages.
On input changes, triggers
setFieldChanged(true)to indicate unsaved changes.On submit, invokes
onOkwith the validated form data.Uses
useBuildFormSchemainternally to infer the data shape for submission.
Usage Example:
import { useForm } from 'react-hook-form';
const form = useForm({ defaultValues: { name: '', url: '', server_type: '', authorization_token: '' } });
function handleOk(data) {
console.log('Submitted MCP data:', data);
}
function MyComponent() {
const [fieldChanged, setFieldChanged] = React.useState(false);
return (
<EditMcpForm form={form} onOk={handleOk} setFieldChanged={setFieldChanged} />
);
}
Implementation Details
Form Validation: Uses
zodschemas combined withreact-hook-formto declaratively validate inputs. Integration withi18nextallows error and placeholder messages to be localized.Form Inputs: Custom UI components (
Input,RAGFlowSelect, etc.) provide consistent styling and behavior across the app.Field Change Tracking: The
setFieldChangedcallback is triggered on every input change to inform parent components that the form has been modified, enabling UX features like disabling navigation or showing unsaved changes warnings.Code Editor Loader: The Monaco Editor loader is configured at the top, suggesting potential future use or integration with code editing features, though not directly used in this file.
Strict Typing: The form leverages strong typing with
zodandreact-hook-formto ensure runtime and compile-time validation consistency.ServerTypeOptions Generation: The utility
buildOptionsconverts enum values into dropdown-friendly options automatically, minimizing manual error.
Interactions with Other Parts of the Application
UI Components: Relies on shared UI components under
@/components/ui/*for form layout (Form,FormControl,FormField, etc.) and inputs (Input,RAGFlowSelect).Form Utilities: Uses
buildOptionsfrom@/utils/formto convert enums into select options.Internationalization: Uses
useTranslationfromreact-i18nextfor localization.Form State Management: Requires a parent to provide
react-hook-form'sUseFormReturnto manage form state externally.Modal Interface: Uses
IModalProps(generic modal props interface) to standardize modal form behavior, indicating this form is likely rendered within a modal dialog.Monaco Editor: Configures Monaco Editor loader, possibly due to integration or shared configuration but not used directly here.
Visual Diagram
componentDiagram
component EditMcpForm {
+form: UseFormReturn
+onOk(data: MCPData)
+setFieldChanged(flag: boolean)
+onSubmit(data: MCPData)
}
component useBuildFormSchema {
+returns: z.Schema<MCPData>
}
component ServerType {
<<enum>>
+SSE
+StreamableHttp
}
component ServerTypeOptions {
+options: SelectOption[]
}
EditMcpForm --> useBuildFormSchema : uses
EditMcpForm --> ServerTypeOptions : uses
EditMcpForm --> "Form UI Components" : renders
EditMcpForm --> "react-hook-form" : manages state
useBuildFormSchema --> "i18next" : uses for localization
Summary
edit-mcp-form.tsx provides a robust, localized, and type-safe form component to create or edit MCP entities. It integrates modern React form management techniques with declarative schema validation and modular UI components. The form is designed to be embedded within modals and notify parent components about changes, fitting seamlessly into a larger React application architecture.