use-get-tools.ts

Overview

The use-get-tools.ts file provides two custom React hooks, useGetAgentToolNames and useGetAgentMCPIds, designed to extract and memoize specific pieces of data from a React context related to an agent form. These hooks abstract the logic required to retrieve lists of tool component names and MCP (presumably "Managed Control Points" or similar domain entities) IDs from the form data held within the AgentFormContext.

This file facilitates clean, reusable access to nested context data in a React application, improving component modularity and performance by using memoization (useMemo) and safe data access (lodash.get).


Detailed Explanation

Imports


Hook: useGetAgentToolNames()

Purpose

Retrieves an array of tool component names from the agent form context. It reads the tools array from data.form.tools and extracts the component_name property from each tool.

Implementation Details

Parameters

Returns

Usage Example

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

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

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

Hook: useGetAgentMCPIds()

Purpose

Retrieves an array of MCP IDs from the agent form context. It reads the MCP array from data.form.mcp and extracts the mcp_id property from each entry.

Implementation Details

Parameters

Returns

Usage Example

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

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

  return (
    <ul>
      {mcpIds.map((id) => (
        <li key={id}>{id}</li>
      ))}
    </ul>
  );
}

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Flowchart Diagram

flowchart TD
    A[AgentFormContext] -->|provides| B[useGetAgentToolNames]
    A -->|provides| C[useGetAgentMCPIds]

    B --> D[Access node.data.form.tools]
    D --> E[Extract component_name array]
    E --> F[Return { toolNames }]

    C --> G[Access node.data.form.mcp]
    G --> H[Extract mcp_id array]
    H --> I[Return { mcpIds }]

Summary

The use-get-tools.ts file contains two focused hooks that simplify extracting and memoizing specific data from a complex nested context in a React application. By encapsulating this logic, the file enhances maintainability and encourages consistent data access patterns for agent form tools and MCP IDs. This modular approach also aids in testing and future refactoring.