1package model
2
3import (
4 "fmt"
5 "strings"
6
7 "charm.land/lipgloss/v2"
8 "github.com/charmbracelet/crush/internal/agent/tools/mcp"
9 "github.com/charmbracelet/crush/internal/ui/common"
10 "github.com/charmbracelet/crush/internal/ui/styles"
11)
12
13// mcpInfo renders the MCP status section showing active MCP clients and their
14// tool/prompt counts.
15func (m *UI) mcpInfo(width, maxItems int, isSection bool) string {
16 var mcps []mcp.ClientInfo
17 t := m.com.Styles
18
19 for _, mcp := range m.com.Config().MCP.Sorted() {
20 if state, ok := m.mcpStates[mcp.Name]; ok {
21 mcps = append(mcps, state)
22 }
23 }
24
25 title := t.Subtle.Render("MCPs")
26 if isSection {
27 title = common.Section(t, title, width)
28 }
29 list := t.Subtle.Render("None")
30 if len(mcps) > 0 {
31 list = mcpList(t, mcps, width, maxItems)
32 }
33
34 return lipgloss.NewStyle().Width(width).Render(fmt.Sprintf("%s\n\n%s", title, list))
35}
36
37// mcpCounts formats tool and prompt counts for display.
38func mcpCounts(t *styles.Styles, counts mcp.Counts) string {
39 parts := []string{}
40 if counts.Tools > 0 {
41 parts = append(parts, t.Subtle.Render(fmt.Sprintf("%d tools", counts.Tools)))
42 }
43 if counts.Prompts > 0 {
44 parts = append(parts, t.Subtle.Render(fmt.Sprintf("%d prompts", counts.Prompts)))
45 }
46 return strings.Join(parts, " ")
47}
48
49// mcpList renders a list of MCP clients with their status and counts,
50// truncating to maxItems if needed.
51func mcpList(t *styles.Styles, mcps []mcp.ClientInfo, width, maxItems int) string {
52 var renderedMcps []string
53
54 for _, m := range mcps {
55 var icon string
56 title := m.Name
57 var description string
58 var extraContent string
59
60 switch m.State {
61 case mcp.StateStarting:
62 icon = t.ItemBusyIcon.String()
63 description = t.Subtle.Render("starting...")
64 case mcp.StateConnected:
65 icon = t.ItemOnlineIcon.String()
66 extraContent = mcpCounts(t, m.Counts)
67 case mcp.StateError:
68 icon = t.ItemErrorIcon.String()
69 description = t.Subtle.Render("error")
70 if m.Error != nil {
71 description = t.Subtle.Render(fmt.Sprintf("error: %s", m.Error.Error()))
72 }
73 case mcp.StateDisabled:
74 icon = t.ItemOfflineIcon.Foreground(t.Muted.GetBackground()).String()
75 description = t.Subtle.Render("disabled")
76 default:
77 icon = t.ItemOfflineIcon.String()
78 }
79
80 renderedMcps = append(renderedMcps, common.Status(t, common.StatusOpts{
81 Icon: icon,
82 Title: title,
83 Description: description,
84 ExtraContent: extraContent,
85 }, width))
86 }
87
88 if len(renderedMcps) > maxItems {
89 visibleItems := renderedMcps[:maxItems-1]
90 remaining := len(renderedMcps) - maxItems
91 visibleItems = append(visibleItems, t.Subtle.Render(fmt.Sprintf("β¦and %d more", remaining)))
92 return lipgloss.JoinVertical(lipgloss.Left, visibleItems...)
93 }
94 return lipgloss.JoinVertical(lipgloss.Left, renderedMcps...)
95}