/ hooks / useMergedTools.ts
useMergedTools.ts
 1  // biome-ignore-all assist/source/organizeImports: ANT-ONLY import markers must not be reordered
 2  import { useMemo } from 'react'
 3  import type { Tools, ToolPermissionContext } from '../Tool.js'
 4  import { assembleToolPool } from '../tools.js'
 5  import { useAppState } from '../state/AppState.js'
 6  import { mergeAndFilterTools } from '../utils/toolPool.js'
 7  
 8  /**
 9   * React hook that assembles the full tool pool for the REPL.
10   *
11   * Uses assembleToolPool() (the shared pure function used by both REPL and runAgent)
12   * to combine built-in tools with MCP tools, applying deny rules and deduplication.
13   * Any extra initialTools are merged on top.
14   *
15   * @param initialTools - Extra tools to include (built-in + startup MCP from props).
16   *   These are merged with the assembled pool and take precedence in deduplication.
17   * @param mcpTools - MCP tools discovered dynamically (from mcp state)
18   * @param toolPermissionContext - Permission context for filtering
19   */
20  export function useMergedTools(
21    initialTools: Tools,
22    mcpTools: Tools,
23    toolPermissionContext: ToolPermissionContext,
24  ): Tools {
25    let replBridgeEnabled = false
26    let replBridgeOutboundOnly = false
27    return useMemo(() => {
28      // assembleToolPool is the shared function that both REPL and runAgent use.
29      // It handles: getTools() + MCP deny-rule filtering + dedup + MCP CLI exclusion.
30      const assembled = assembleToolPool(toolPermissionContext, mcpTools)
31  
32      return mergeAndFilterTools(
33        initialTools,
34        assembled,
35        toolPermissionContext.mode,
36      )
37    }, [
38      initialTools,
39      mcpTools,
40      toolPermissionContext,
41      replBridgeEnabled,
42      replBridgeOutboundOnly,
43    ])
44  }