# All files / src/components/personas McpToolSelector.tsx

88.46% Statements 46/52
66.66% Branches 10/15
94.73% Functions 18/19
88% Lines 44/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194

    8x
    8x
    8x

    576x
    576x
    160x

    576x

    8x
    344x
    160x

    8x

    57x
    57x

    57x

    4x

    4x
    4x

    4x

    4x

    57x

    2x

    2x
    2x
    18x

    18x

    2x

    57x

    13x

    13x

    57x

    57x

    228x

    13x

    21x

    1140x
    1512x

    1140x
    354x

    791x
    786x

    2x

    2733x

    2733x

    4x

     /**
     * A categorized multiselect for choosing which MCP tools a persona can access.
     *
     * Pure presentational component — receives data and callbacks as props.
     */

    import { useState, useMemo, useCallback, type JSX } from "react";
    import {
      ALL_MCP_TOOL_NAMES,
      DEFAULT_SCOPED_MCP_TOOLS,
      WORKER_MCP_TOOLS,
      ORCHESTRATOR_MCP_TOOLS,
      ADMIN_MCP_TOOLS,
    } from "@grackle-ai/common";
    import styles from "./McpToolSelector.module.scss";

    /** Tool groups derived from the naming convention (prefix before first underscore). */
    const TOOL_GROUPS: { group: string; tools: string[] }[] = (() => {
      const grouped = new Map<string, string[]>();
      for (const name of ALL_MCP_TOOL_NAMES) {
        // Most tools use "group_action" naming; "get_version_status" is a special case
        const group = name === "get_version_status" ? "version" : name.split("_")[0];
        if (!grouped.has(group)) {
          grouped.set(group, []);
        }
        grouped.get(group)!.push(name);
      }
      // Sort groups alphabetically, tools within each group alphabetically
      return [...grouped.entries()]
        .sort(([a], [b]) => a.localeCompare(b))
        .map(([group, tools]) => ({ group, tools: tools.sort() }));
    })();

    /** Preset definitions for quick selection. */
    const PRESETS = [
      { name: "default", label: "Default", tools: DEFAULT_SCOPED_MCP_TOOLS },
      { name: "worker", label: "Worker", tools: WORKER_MCP_TOOLS },
      { name: "orchestrator", label: "Orchestrator", tools: ORCHESTRATOR_MCP_TOOLS },
      { name: "admin", label: "Admin", tools: ADMIN_MCP_TOOLS },
    ] as const;

    export interface McpToolSelectorProps {
      /** Currently selected tool names. Empty = default (shown as hint, not checked). */
      selectedTools: string[];
      /** Callback when selection changes. */
      onChange: (tools: string[]) => void;
      /** Whether the component is disabled. */
      disabled?: boolean;
    }

    export function McpToolSelector({
      selectedTools,
      onChange,
      disabled,
    }: McpToolSelectorProps): JSX.Element {
      const [filter, setFilter] = useState("");
      const selectedSet = useMemo(() => new Set(selectedTools), [selectedTools]);

      const toggleTool = useCallback(
        (tool: string) => {
          Iif (disabled) {
            return;
          }
          const next = new Set(selectedSet);
          Iif (next.has(tool)) {
            next.delete(tool);
          } else {
            next.add(tool);
          }
          onChange([...next].sort());
        },
        [selectedSet, onChange, disabled],
      );

      const toggleGroup = useCallback(
        (tools: string[], allSelected: boolean) => {
          Iif (disabled) {
            return;
          }
          const next = new Set(selectedSet);
          for (const t of tools) {
            Iif (allSelected) {
              next.delete(t);
            } else {
              next.add(t);
            }
          }
          onChange([...next].sort());
        },
        [selectedSet, onChange, disabled],
      );

      const applyPreset = useCallback(
        (tools: readonly string[]) => {
          Iif (disabled) {
            return;
          }
          onChange([...tools].sort());
        },
        [onChange, disabled],
      );

      const lowerFilter = filter.toLowerCase();

      return (
        <div className={styles.container} data-testid="mcp-tool-selector">
          <div className={styles.header}>
            <span className={styles.count}>
              {selectedTools.length === 0
                ? `Using default (${DEFAULT_SCOPED_MCP_TOOLS.length} tools)`
                : `${selectedTools.length} of ${ALL_MCP_TOOL_NAMES.size} tools selected`}
            </span>
          </div>

          <div className={styles.presets} data-testid="mcp-tool-presets">
            {PRESETS.map((preset) => (
              <button
                key={preset.name}
                type="button"
                className={styles.presetButton}
                disabled={disabled}
                onClick={() => applyPreset(preset.tools)}
                data-testid={`preset-${preset.name}`}
              >
                {preset.label}
              </button>
            ))}
            <button
              type="button"
              className={styles.presetButton}
              disabled={disabled}
              onClick={() => onChange([])}
              data-testid="preset-clear"
            >
              Clear
            </button>
          </div>

          <input
            type="text"
            className={styles.filterInput}
            placeholder="Filter tools..."
            value={filter}
            onChange={(e) => setFilter(e.target.value)}
            disabled={disabled}
            data-testid="mcp-tool-filter"
          />

          <div className={styles.groups}>
            {TOOL_GROUPS.map(({ group, tools }) => {
              const visibleTools = lowerFilter
                ? tools.filter((t) => t.toLowerCase().includes(lowerFilter))
                : tools;
              if (visibleTools.length === 0) {
                return null;
              }
              const allSelected = visibleTools.every((t) => selectedSet.has(t));
              return (
                <div key={group} className={styles.group} data-testid={`tool-group-${group}`}>
                  <label className={styles.groupHeader}>
                    <input
                      type="checkbox"
                      checked={allSelected}
                      onChange={() => toggleGroup(visibleTools, allSelected)}
                      disabled={disabled}
                      data-testid={`group-toggle-${group}`}
                    />
                    <span className={styles.groupName}>{group}</span>
                    <span className={styles.groupCount}>
                      ({visibleTools.filter((t) => selectedSet.has(t)).length}/{visibleTools.length})
                    </span>
                  </label>
                  <div className={styles.toolList}>
                    {visibleTools.map((tool) => (
                      <label key={tool} className={styles.toolItem}>
                        <input
                          type="checkbox"
                          checked={selectedSet.has(tool)}
                          onChange={() => toggleTool(tool)}
                          disabled={disabled}
                          data-testid={`tool-${tool}`}
                        />
                        <span className={styles.toolName}>{tool}</span>
                      </label>
                    ))}
                  </div>
                </div>
              );
            })}
          </div>
        </div>
      );
    }