index.tsx


Overview

index.tsx defines a React functional component named MCPForm that renders a user interface form for selecting tools associated with a "MCP server." The form displays server metadata and dynamically lists tool options as checkboxes, allowing users to select or deselect them. The component leverages several custom hooks and UI components to fetch data, maintain form state, and handle user interactions smoothly while ensuring type safety and validation through zod and react-hook-form.

Key functionalities include:


Detailed Explanation

Imports


FormSchema (Zod Schema)

const FormSchema = z.object({
  items: z.array(z.string()),
});

MCPForm Component

function MCPForm() { ... }

Description

Main React component rendering the MCP server information and a form with selectable MCP tools.

Internal Workflow and Logic

  1. State and Data Fetching:

    • Retrieves the currently clicked tool ID from the global useGraphStore.

    • Uses useGetMcpServer to fetch server data associated with this tool ID.

    • Uses useValues to get default values for the form fields.

  2. Form Setup:

    • Initializes react-hook-form with defaultValues and validation using zodResolver.

    • Uses useWatchFormChange to monitor form state changes (likely for syncing or side effects).

  3. Rendering:

    • Wraps the form in a custom Form component that integrates with react-hook-form.

    • Displays the MCP server's name and URL inside a styled Card.

    • Iterates over the MCP tools available in data.variables.tools:

      • For each tool, renders an MCPCard wrapping a Checkbox.

      • The checkbox is controlled by the form state; selecting/deselecting updates the form's items array.

    • Displays form validation messages if any.

  4. Form Submission:

    • The form's onSubmit handler prevents default submission behavior (no actual submission logic in this file).

Parameters

Return Value

Usage Example

import MCPForm from './index';

function App() {
  return (
    <div>
      <h1>MCP Server Tool Selection</h1>
      <MCPForm />
    </div>
  );
}

Important Implementation Details


Interaction with Other System Parts


Visual Diagram

componentDiagram
    component MCPForm {
        +useGraphStore() : clickedToolId
        +useGetMcpServer(clickedToolId) : data
        +useValues() : defaultValues
        +useForm({defaultValues, resolver}) : form
        +useWatchFormChange(form)

        MCPForm --> Form
        MCPForm --> Card
        MCPForm --> FormField (items)
        FormField --> MCPCard
        MCPCard --> Checkbox
    }

    component useGraphStore
    component useGetMcpServer
    component useValues
    component useWatchFormChange
    component MCPCard
    component Form
    component Card
    component Checkbox

    MCPForm --> useGraphStore
    MCPForm --> useGetMcpServer
    MCPForm --> useValues
    MCPForm --> useWatchFormChange
    MCPForm --> MCPCard
    MCPForm --> Form
    MCPForm --> Card
    MCPForm --> Checkbox

Summary

index.tsx implements a memoized React component MCPForm that dynamically renders a validated form for selecting tools associated with a fetched MCP server. It integrates with global state and custom hooks to fetch server info, manage form data, and synchronize changes. The component is built with reusable UI components and provides a clean, user-friendly interface for tool selection in the context of the broader application.