|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + hawkconfig "github.com/GrayCodeAI/hawk/internal/config" |
| 10 | + "github.com/GrayCodeAI/hawk/internal/tool" |
| 11 | +) |
| 12 | + |
| 13 | +// This file holds the tool-registry construction used by the chat TUI: |
| 14 | +// the essential/optional tool sets and the registry builder that wires in |
| 15 | +// MCP servers and CLI tool filters. Split out of chat.go for clarity. |
| 16 | + |
| 17 | +func essentialTools() []tool.Tool { |
| 18 | + // Core tools needed for basic agent operation - always loaded at startup |
| 19 | + return []tool.Tool{ |
| 20 | + tool.BashTool{}, |
| 21 | + tool.FileReadTool{}, |
| 22 | + tool.FileWriteTool{}, |
| 23 | + tool.FileEditTool{}, |
| 24 | + tool.StructuredEditTool{}, |
| 25 | + tool.LSTool{}, |
| 26 | + tool.GlobTool{}, |
| 27 | + tool.GrepTool{}, |
| 28 | + tool.WebFetchTool{}, |
| 29 | + tool.WebSearchTool{}, |
| 30 | + tool.ToolSearchTool{}, |
| 31 | + tool.SkillTool{}, |
| 32 | + tool.AgentTool{}, |
| 33 | + tool.AskUserQuestionTool{}, |
| 34 | + tool.TodoWriteTool{}, |
| 35 | + tool.TaskOutputTool{}, |
| 36 | + tool.TaskStopTool{}, |
| 37 | + tool.LSPTool{}, |
| 38 | + tool.MultiEditTool{}, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func optionalTools() []tool.Tool { |
| 43 | + // Specialized tools that can be lazy-loaded on demand |
| 44 | + return []tool.Tool{ |
| 45 | + tool.EnterPlanModeTool{}, |
| 46 | + tool.ExitPlanModeTool{}, |
| 47 | + tool.NotebookEditTool{}, |
| 48 | + tool.EnterWorktreeTool{}, |
| 49 | + tool.ExitWorktreeTool{}, |
| 50 | + tool.ListMcpResourcesTool{}, |
| 51 | + tool.ReadMcpResourceTool{}, |
| 52 | + tool.ConfigTool{}, |
| 53 | + tool.BriefTool{}, |
| 54 | + tool.TaskCreateTool{}, |
| 55 | + tool.TaskGetTool{}, |
| 56 | + tool.TaskListTool{}, |
| 57 | + tool.TaskUpdateTool{}, |
| 58 | + tool.SleepTool{}, |
| 59 | + tool.CronCreateTool{}, |
| 60 | + tool.CronDeleteTool{}, |
| 61 | + tool.CronListTool{}, |
| 62 | + tool.VerifyPlanExecutionTool{}, |
| 63 | + tool.WorkflowTool{}, |
| 64 | + tool.McpAuthTool{}, |
| 65 | + tool.DiagnosticsTool{}, |
| 66 | + tool.CodeSearchTool{}, |
| 67 | + tool.CoreMemoryAppendTool{}, |
| 68 | + tool.CoreMemoryReplaceTool{}, |
| 69 | + tool.CoreMemoryRethinkTool{}, |
| 70 | + tool.DownloadTool{}, |
| 71 | + tool.AgenticFetchTool{}, |
| 72 | + tool.ImpactTool{}, |
| 73 | + tool.GitHistoryTool{}, |
| 74 | + tool.CodeGraphTool{}, |
| 75 | + tool.NilAwayTool{}, |
| 76 | + tool.ReviveTool{}, |
| 77 | + tool.MCPLanguageServerTool{}, |
| 78 | + tool.SQLTool{}, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func defaultRegistry(settings hawkconfig.Settings) (*tool.Registry, error) { |
| 83 | + // Load essential tools first for fast startup |
| 84 | + tools := essentialTools() |
| 85 | + if tool.IsPowerShellAvailable() { |
| 86 | + tools = append(tools, tool.PowerShellTool{}) |
| 87 | + } |
| 88 | + // Detect project-level MCP servers (supply chain attack vector). |
| 89 | + // Project .hawk/settings.json can be committed to a repo and define |
| 90 | + // arbitrary commands that execute on clone. Gate behind --allow-project-mcp. |
| 91 | + projectMCPServers := hawkconfig.ProjectMCPServers() |
| 92 | + projectMCPNames := make(map[string]bool, len(projectMCPServers)) |
| 93 | + for _, cfg := range projectMCPServers { |
| 94 | + if cfg.Name != "" { |
| 95 | + projectMCPNames[cfg.Name] = true |
| 96 | + } |
| 97 | + } |
| 98 | + for _, cfg := range settings.MCPServers { |
| 99 | + if cfg.Name == "" || cfg.Command == "" { |
| 100 | + continue |
| 101 | + } |
| 102 | + if projectMCPNames[cfg.Name] && !allowProjectMCP { |
| 103 | + fmt.Fprintf(os.Stderr, "hawk: skipping project-level MCP server %q (defined in .hawk/settings.json); use --allow-project-mcp to enable\n", cfg.Name) |
| 104 | + continue |
| 105 | + } |
| 106 | + mcpTools, err := tool.LoadMCPTools(context.Background(), cfg.Name, cfg.Command, cfg.Args...) |
| 107 | + if err != nil { |
| 108 | + continue |
| 109 | + } |
| 110 | + tools = append(tools, mcpTools...) |
| 111 | + } |
| 112 | + // Load MCP server tools |
| 113 | + for _, cmd := range mcpServers { |
| 114 | + parts := strings.Fields(cmd) |
| 115 | + if len(parts) == 0 { |
| 116 | + continue |
| 117 | + } |
| 118 | + name := parts[0] |
| 119 | + mcpTools, err := tool.LoadMCPTools(context.Background(), name, parts[0], parts[1:]...) |
| 120 | + if err != nil { |
| 121 | + // MCP server failed to connect — skip silently, will show in /doctor |
| 122 | + continue |
| 123 | + } |
| 124 | + tools = append(tools, mcpTools...) |
| 125 | + } |
| 126 | + |
| 127 | + filtered, err := filterAvailableTools( |
| 128 | + tools, |
| 129 | + toolsFlagSet, |
| 130 | + parseToolListFromCLI(toolsFlag), |
| 131 | + parseToolListFromCLI(disallowedToolsFlag), |
| 132 | + ) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + registry := tool.NewRegistry(filtered...) |
| 137 | + |
| 138 | + // Lazy-load optional tools in background |
| 139 | + go func() { |
| 140 | + for _, t := range optionalTools() { |
| 141 | + _ = registry.Register(t) |
| 142 | + } |
| 143 | + }() |
| 144 | + |
| 145 | + return registry, nil |
| 146 | +} |
| 147 | + |
| 148 | +func allTools() []tool.Tool { |
| 149 | + t := essentialTools() |
| 150 | + t = append(t, optionalTools()...) |
| 151 | + return t |
| 152 | +} |
0 commit comments