返回 DeepSeek-Reasonix
mcp_manager_view.go
根目录 / internal / cli / mcp_manager_view.go
1 package cli
2
3 // mcp_manager_view.go renders the /mcp manager overlay and its display strings.
4
5 import (
6 "fmt"
7 "strings"
8
9 "reasonix/internal/config"
10 "reasonix/internal/mcpdiag"
11 )
12
13 func (m chatTUI) renderMCPManager() string {
14 if m.mcp == nil {
15 return ""
16 }
17 return m.mcp.render(m.width)
18 }
19
20 func (p *mcpManager) render(width int) string {
21 w := max(viewWidth(width), 40)
22 switch p.stage {
23 case mcpStageDetail:
24 return managerContentPanelStyle(w).Render(p.renderDetail(w))
25 case mcpStageTools:
26 return managerContentPanelStyle(w).Render(p.renderTools(w))
27 case mcpStageLogs:
28 return managerContentPanelStyle(w).Render(p.renderLogs(w))
29 case mcpStageConfirmRemove:
30 return managerContentPanelStyle(w).Render(p.renderConfirmRemove(w))
31 case mcpStageConfirmClearAuth:
32 return managerContentPanelStyle(w).Render(p.renderConfirmClearAuth(w))
33 default:
34 return managerContentPanelStyle(w).Render(p.renderList(w))
35 }
36 }
37
38 func (p *mcpManager) footerHint() string {
39 switch p.stage {
40 case mcpStageDetail:
41 return "↑/↓ navigate · r refresh · Enter to select · Esc to back"
42 case mcpStageTools, mcpStageLogs:
43 return "Esc to back"
44 case mcpStageConfirmRemove, mcpStageConfirmClearAuth:
45 return "Enter to select · y confirm · n cancel · Esc to back"
46 default:
47 if len(p.snapshot.servers) == 0 {
48 return "↑/↓ navigate · Enter to confirm · Esc to cancel"
49 }
50 return "↑/↓ navigate · r refresh · Enter for details · Esc to close"
51 }
52 }
53
54 func (p *mcpManager) renderList(width int) string {
55 var b strings.Builder
56 fmt.Fprintf(&b, "%s\n", viewHeader("Manage MCP servers"))
57 fmt.Fprintf(&b, "%s\n\n", viewMeta(fmt.Sprintf("%d servers", len(p.snapshot.servers))))
58 if p.snapshot.err != "" {
59 fmt.Fprintf(&b, "%s\n\n", yellow("config: "+p.snapshot.err))
60 }
61 if len(p.snapshot.servers) == 0 {
62 b.WriteString(viewMeta("No MCP servers configured. Use /mcp add <name> ... to add one.") + "\n\n")
63 return b.String()
64 }
65 start, end := visibleRange(len(p.snapshot.servers), p.sel, mcpListMaxRows)
66 if start > 0 {
67 fmt.Fprintf(&b, "%s\n", viewMeta(fmt.Sprintf("↑ %d more above", start)))
68 }
69 lastGroup := ""
70 for i := start; i < end; i++ {
71 s := p.snapshot.servers[i]
72 group := mcpServerGroupLabel(s)
73 if group != lastGroup {
74 if lastGroup != "" {
75 b.WriteByte('\n')
76 }
77 header := group
78 if group == "Global MCPs" && p.snapshot.configPath != "" {
79 header += " (" + p.snapshot.configPath + ")"
80 }
81 fmt.Fprintf(&b, " %s\n", bold(header))
82 lastGroup = group
83 }
84 b.WriteString(p.renderListRow(i, s, width) + "\n")
85 }
86 if end < len(p.snapshot.servers) {
87 fmt.Fprintf(&b, "%s\n", viewMeta(fmt.Sprintf("↓ %d more below", len(p.snapshot.servers)-end)))
88 }
89 return strings.TrimRight(b.String(), "\n")
90 }
91
92 func (p *mcpManager) renderListRow(i int, s mcpServerView, width int) string {
93 prefix := " "
94 if i == p.sel {
95 prefix = accent(" › ")
96 }
97 nameWidth := min(28, max(12, width/3))
98 name := compactMiddle(s.Name, nameWidth)
99 status := mcpStatusLabel(s)
100 meta := fmt.Sprintf("%s · %s", status, countText(s.Tools, "tool"))
101 if s.Prompts > 0 {
102 meta += " · " + countText(s.Prompts, "prompt")
103 }
104 if s.Resources > 0 {
105 meta += " · " + countText(s.Resources, "resource")
106 }
107 if s.Transport != "" {
108 meta = s.Transport + " · " + meta
109 }
110 used := visibleWidth(prefix) + nameWidth + 3
111 meta = viewCompactText(meta, viewBudget(width, used))
112 name = padRight(name, nameWidth)
113 if i == p.sel {
114 name = bold(name)
115 }
116 return fmt.Sprintf("%s%s · %s", prefix, name, viewMeta(meta))
117 }
118
119 func (p *mcpManager) renderDetail(width int) string {
120 v, ok := p.selectedServer()
121 if !ok {
122 return "MCP server not found\n\n" + dim("Esc to back")
123 }
124 actions := mcpActionsFor(v, p.snapshot.configPath)
125 if p.action >= len(actions) {
126 p.action = max(0, len(actions)-1)
127 }
128 var b strings.Builder
129 fmt.Fprintf(&b, "%s MCP Server\n\n", bold(titleText(v.Name)))
130 writeMCPDetailField(&b, "Status", mcpStatusLabel(v))
131 if auth := mcpAuthLabel(v); auth != "" {
132 writeMCPDetailField(&b, "Auth", auth)
133 }
134 writeMCPDetailField(&b, "Transport", fallbackText(v.Transport, "unknown"))
135 if v.BuiltIn {
136 writeMCPDetailField(&b, "Config location", "built-in")
137 } else if v.Source == config.MCPSourceProjectMCPJSON {
138 writeMCPDetailField(&b, "Config location", "current project .mcp.json")
139 } else if v.Source == config.MCPSourceProjectConfig {
140 writeMCPDetailField(&b, "Config location", "current project reasonix.toml")
141 } else {
142 loc := fallbackText(p.snapshot.configPath, "not saved")
143 if loc != "not saved" {
144 loc = viewCompactPath(loc, viewBudget(width, 18))
145 }
146 writeMCPDetailField(&b, "Config location", loc)
147 }
148 writeMCPDetailField(&b, "Capabilities", mcpCapabilitiesText(v))
149 writeMCPDetailField(&b, "Tools", countText(v.Tools, "tool"))
150 if line := mcpCommandLine(v); line != "" {
151 writeMCPDetailField(&b, mcpCommandLabel(v), viewCompactText(line, viewBudget(width, 18)))
152 }
153 if len(v.EnvKeys) > 0 {
154 writeMCPDetailField(&b, "Env", strings.Join(v.EnvKeys, ", "))
155 }
156 if v.Error != "" {
157 writeMCPDetailField(&b, "Error", viewCompactText(v.Error, viewBudget(width, 18)))
158 }
159 b.WriteByte('\n')
160 if len(actions) == 0 {
161 b.WriteString(viewMeta("No actions available.") + "\n")
162 } else {
163 for i, a := range actions {
164 b.WriteString(rowLine(i == p.action, i+1, "", a.label, false) + "\n")
165 }
166 }
167 return strings.TrimRight(b.String(), "\n")
168 }
169
170 func (p *mcpManager) renderTools(width int) string {
171 v, ok := p.selectedServer()
172 if !ok {
173 return "MCP server not found\n\n" + dim("Esc to back")
174 }
175 var b strings.Builder
176 fmt.Fprintf(&b, "%s tools\n\n", bold(v.Name))
177 if len(v.ToolList) == 0 {
178 b.WriteString(viewMeta("Current connection did not return tool details.") + "\n")
179 } else {
180 limit := min(len(v.ToolList), mcpToolMaxRows)
181 for _, t := range v.ToolList[:limit] {
182 desc := t.Description
183 if t.SchemaError != "" {
184 desc = "unavailable: " + t.SchemaError
185 }
186 desc = viewCompactText(desc, viewBudget(width, 24))
187 fmt.Fprintf(&b, " %-20s %s\n", t.Name, viewMeta(desc))
188 }
189 if extra := len(v.ToolList) - limit; extra > 0 {
190 fmt.Fprintf(&b, "%s\n", viewMore(extra, "tools"))
191 }
192 }
193 return strings.TrimRight(b.String(), "\n")
194 }
195
196 func (p *mcpManager) renderLogs(width int) string {
197 v, ok := p.selectedServer()
198 if !ok {
199 return "MCP server not found\n\n" + dim("Esc to back")
200 }
201 var b strings.Builder
202 fmt.Fprintf(&b, "%s logs\n\n", bold(v.Name))
203 if strings.TrimSpace(v.Error) == "" {
204 b.WriteString(viewMeta("No failure log recorded for this MCP.") + "\n")
205 } else {
206 b.WriteString(viewProtectLines(v.Error, viewBudget(width, 2)) + "\n")
207 }
208 return strings.TrimRight(b.String(), "\n")
209 }
210
211 func (p *mcpManager) renderConfirmRemove(width int) string {
212 v, ok := p.selectedServer()
213 if !ok {
214 return "MCP server not found\n\n" + dim("Esc to back")
215 }
216 var b strings.Builder
217 fmt.Fprintf(&b, "Remove MCP server %q?\n", v.Name)
218 b.WriteString(viewMeta("This removes it from Reasonix config. It cannot be undone from this panel.") + "\n\n")
219 b.WriteString(rowLine(p.confirm == 0, 1, "", "Confirm remove", false) + "\n")
220 b.WriteString(rowLine(p.confirm == 1, 2, "", "Cancel", false) + "\n")
221 return strings.TrimRight(b.String(), "\n")
222 }
223
224 func (p *mcpManager) renderConfirmClearAuth(width int) string {
225 v, ok := p.selectedServer()
226 if !ok {
227 return "MCP server not found\n\n" + dim("Esc to back")
228 }
229 var b strings.Builder
230 fmt.Fprintf(&b, "Clear authentication for MCP server %q?\n", v.Name)
231 hint := "This removes local auth-like headers, environment values, and URL tokens; the server stays in config."
232 b.WriteString(viewMeta(viewCompactText(hint, viewBudget(width, 0))) + "\n\n")
233 b.WriteString(rowLine(p.confirm == 0, 1, "", "Confirm clear authentication", false) + "\n")
234 b.WriteString(rowLine(p.confirm == 1, 2, "", "Cancel", false) + "\n")
235 return strings.TrimRight(b.String(), "\n")
236 }
237
238 func mcpActionsFor(v mcpServerView, configPath string) []mcpActionItem {
239 var out []mcpActionItem
240 managed := v.BuiltIn || v.Source == config.MCPSourcePluginPackage
241 if v.Tools > 0 || len(v.ToolList) > 0 {
242 out = append(out, mcpActionItem{mcpActionViewTools, "View tools"})
243 }
244 if v.Status == "failed" {
245 if mcpAuthStatus(v) == mcpdiag.AuthRequired {
246 out = append(out, mcpActionItem{mcpActionAuth, "Authenticate"})
247 } else {
248 out = append(out, mcpActionItem{mcpActionConnect, "Retry"})
249 }
250 if mcpCanClearAuth(v) {
251 out = append(out, mcpActionItem{mcpActionClearAuth, "Clear authentication"})
252 }
253 out = appendMCPFailureSecondaryActions(out, v, configPath)
254 return out
255 }
256 switch v.Status {
257 case "connected":
258 out = append(out, mcpActionItem{mcpActionConnect, "Reconnect"})
259 case "deferred":
260 out = append(out, mcpActionItem{mcpActionConnect, "Reconnect"})
261 case "disabled":
262 out = append(out, mcpActionItem{mcpActionConnect, "Enable and connect"})
263 }
264 out = appendMCPConfigActions(out, v, configPath)
265 if mcpCanClearAuth(v) {
266 out = append(out, mcpActionItem{mcpActionClearAuth, "Clear authentication"})
267 }
268 if v.Status != "disabled" {
269 out = append(out, mcpActionItem{mcpActionDisable, "Disable for this session"})
270 }
271 if !managed {
272 out = append(out, mcpActionItem{mcpActionRemove, "Remove server"})
273 }
274 return out
275 }
276
277 func appendMCPFailureSecondaryActions(out []mcpActionItem, v mcpServerView, configPath string) []mcpActionItem {
278 managed := v.BuiltIn || v.Source == config.MCPSourcePluginPackage
279 if strings.TrimSpace(v.Error) != "" {
280 out = append(out, mcpActionItem{mcpActionLogs, "View logs"})
281 }
282 out = appendMCPConfigActions(out, v, configPath)
283 if v.Status != "disabled" {
284 out = append(out, mcpActionItem{mcpActionDisable, "Disable for this session"})
285 }
286 if !managed {
287 out = append(out, mcpActionItem{mcpActionRemove, "Remove server"})
288 }
289 return out
290 }
291
292 func appendMCPConfigActions(out []mcpActionItem, v mcpServerView, configPath string) []mcpActionItem {
293 if v.Configured {
294 path := mcpConfigPathForView(v, configPath)
295 if !v.BuiltIn && v.Source != config.MCPSourcePluginPackage && path != "" {
296 out = append(out, mcpActionItem{mcpActionEdit, "Edit config"})
297 }
298 }
299 return out
300 }
301
302 func mcpConfigPathForView(v mcpServerView, fallback string) string {
303 if path := strings.TrimSpace(v.ConfigPath); path != "" {
304 return path
305 }
306 return strings.TrimSpace(fallback)
307 }
308
309 func writeMCPDetailField(b *strings.Builder, label, value string) {
310 if strings.TrimSpace(value) == "" {
311 return
312 }
313 fmt.Fprintf(b, "%-16s %s\n", label+":", value)
314 }
315
316 func mcpStatusLabel(v mcpServerView) string {
317 switch {
318 case v.Status == "connected":
319 return green("✓ connected")
320 case v.Status == "failed" && mcpAuthStatus(v) == mcpdiag.AuthRequired:
321 return yellow("⚠ needs authentication")
322 case v.Status == "failed":
323 return red("✕ failed")
324 case v.Status == "deferred":
325 return "○ preparing in background"
326 case v.Status == "initializing":
327 return "◌ connecting..."
328 case v.Status == "disabled":
329 return "○ disabled"
330 default:
331 return viewMeta("unknown")
332 }
333 }
334
335 func mcpServerGroupLabel(v mcpServerView) string {
336 switch mcpServerGroupRank(v) {
337 case 0:
338 return "Managed MCPs"
339 case 1:
340 return "Project MCPs"
341 default:
342 return "Global MCPs"
343 }
344 }
345
346 func mcpAuthLabel(v mcpServerView) string {
347 switch {
348 case v.Status == "connected":
349 return green("✓ authenticated")
350 case mcpAuthStatus(v) == mcpdiag.AuthRequired:
351 return red("✕ not authenticated")
352 case mcpAuthStatus(v) == mcpdiag.AuthPossible:
353 return yellow("may need authorization")
354 default:
355 return ""
356 }
357 }
358
359 func mcpCapabilitiesText(v mcpServerView) string {
360 var caps []string
361 if v.HasTools || v.Tools > 0 {
362 caps = append(caps, "tools")
363 }
364 if v.Prompts > 0 {
365 caps = append(caps, "prompts")
366 }
367 if v.Resources > 0 {
368 caps = append(caps, "resources")
369 }
370 if len(caps) == 0 {
371 return "none"
372 }
373 return strings.Join(caps, ", ")
374 }
375
376 func mcpCommandLabel(v mcpServerView) string {
377 if v.Transport == "http" || v.Transport == "sse" {
378 return "URL"
379 }
380 return "Command"
381 }
382
383 func mcpCommandLine(v mcpServerView) string {
384 if v.Transport == "http" || v.Transport == "sse" {
385 return strings.TrimSpace(v.URL)
386 }
387 return strings.TrimSpace(v.Command + " " + strings.Join(v.Args, " "))
388 }
389
389 lines GO