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
IAgentForm: Type interface representing the structure of the agent form data.get(from lodash): Utility function for safely accessing nested object properties.useContext,useMemo(from React): Hooks for consuming React context and memoizing computations.AgentFormContext: React context providing access to the agent form's state.
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
Uses
useContextto access the current context value (node).Uses
lodash.getto safely accessnode.data.form.tools, defaulting to an empty array if not present.Maps over the tools array to return only the
component_namevalues.Uses
useMemoto memoize the result and only recompute whennodechanges.
Parameters
None.
Returns
An object with a single property:
toolNames:string[]— array of tool component names.
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
Uses
useContextto get the current context value (node).Uses
lodash.getto safely accessnode.data.form.mcp, defaulting to an empty array if not present.Maps over the MCP array to return only the
mcp_idvalues.Uses
useMemoto memoize the output and only recompute whennodechanges.
Parameters
None.
Returns
An object with a single property:
mcpIds:(string | number)[]— array of MCP IDs (type inferred fromIAgentForm['mcp']).
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
Context Dependency: Both hooks rely on the presence of
AgentFormContextand expect the context value (node) to have a nested structure conforming tonode.data.form.Safe Access with
lodash.get: This prevents runtime errors if any path in the access chain is undefined, returning an empty array as a fallback.Memoization: Using
useMemoensures the returned arrays are only recomputed when thenodechanges, optimizing performance by preventing unnecessary renders.Type Safety: The hooks use TypeScript interfaces (
IAgentForm) to ensure the data being accessed and returned matches expected shapes.
Interaction with Other Parts of the System
AgentFormContext: The central piece connecting these hooks to the application state. This context presumably holds the current state of an agent form, including tools and MCP data.
Components Using These Hooks: Any React component that needs to display or manipulate the list of tools or MCPs can import and use these hooks to get up-to-date data directly from the context.
Data Source: The context data likely originates from a form management system or API responses loaded earlier in the application lifecycle.
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.