use-get-tools.ts


Overview

The use-get-tools.ts file provides custom React hooks for extracting specific pieces of data related to "tools" and "MCP IDs" from an agent form context within a React application. These hooks facilitate the retrieval of tool component names and MCP identifiers from nested form data, enabling components to access this information in a clean, reusable, and memoized manner.

The file leverages React's useContext and useMemo hooks, along with Lodash's get utility function, to safely and efficiently access deeply nested properties from a shared context (AgentFormContext). This approach helps prevent runtime errors caused by missing or undefined data and optimizes performance by memoizing the extracted values.


File Contents


Detailed Explanations

1. useGetAgentToolNames()

Purpose

Extracts and returns the list of component names for all tools defined in the current agent form context.

Signature

function useGetAgentToolNames(): { toolNames: string[] }

Parameters

This hook takes no parameters directly. It internally consumes the AgentFormContext to access the agent form data.

Return Value

An object containing:

Implementation Details

Usage Example

import { useGetAgentToolNames } from './use-get-tools';

function ToolList() {
  const { toolNames } = useGetAgentToolNames();

  return (
    <ul>
      {toolNames.map(name => (
        <li key={name}>{name}</li>
      ))}
    </ul>
  );
}

2. useGetAgentMCPIds()

Purpose

Extracts and returns the list of MCP IDs from the current agent form context.

Signature

function useGetAgentMCPIds(): { mcpIds: string[] }

Parameters

This hook takes no parameters directly. It consumes AgentFormContext internally.

Return Value

An object containing:

Implementation Details

Usage Example

import { useGetAgentMCPIds } from './use-get-tools';

function MCPList() {
  const { mcpIds } = useGetAgentMCPIds();

  return (
    <div>
      {mcpIds.map(id => (
        <p key={id}>MCP ID: {id}</p>
      ))}
    </div>
  );
}

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Flowchart Diagram: Hook Workflow and Data Flow

flowchart TD
    A[React Component] -->|calls| B[useGetAgentToolNames]
    A -->|calls| C[useGetAgentMCPIds]
    B --> D[useContext(AgentFormContext)]
    C --> D
    D --> E[agent form node data]
    E --> F{Tools array exists?}
    F -- Yes --> G[Extract component_name from each tool]
    F -- No --> H[Return empty array]
    E --> I{MCP array exists?}
    I -- Yes --> J[Extract mcp_id from each MCP]
    I -- No --> K[Return empty array]
    G --> L[Return toolNames array]
    J --> M[Return mcpIds array]

Summary

The use-get-tools.ts file is a utility module that provides two React hooks for efficiently accessing specific parts of an agent form's data — the component names of tools and the MCP IDs. By leveraging React context, Lodash's safe property access, and memoization, it offers a robust and reusable solution to extract and provide this data to React components within the application.


End of Documentation for use-get-tools.ts