11 "github.com/charmbracelet/crush/internal/csync"
12 "github.com/modelcontextprotocol/go-sdk/mcp"
17// ToolResult represents the result of running an MCP tool.
18type ToolResult struct {
25var allTools = csync.NewMap[string, []*Tool]()
27// Tools returns all available MCP tools.
28func Tools() iter.Seq2[string, []*Tool] {
29 return allTools.Seq2()
32// RunTool runs an MCP tool with the given input parameters.
33func RunTool(ctx context.Context, name, toolName string, input string) (ToolResult, error) {
34 var args map[string]any
35 if err := json.Unmarshal([]byte(input), &args); err != nil {
36 return ToolResult{}, fmt.Errorf("error parsing parameters: %s", err)
39 c, err := getOrRenewClient(ctx, name)
41 return ToolResult{}, err
43 result, err := c.CallTool(ctx, &mcp.CallToolParams{
48 return ToolResult{}, err
51 if len(result.Content) == 0 {
52 return ToolResult{Type: "text", Content: ""}, nil
55 var textParts []string
57 var imageMimeType string
59 var audioMimeType string
61 for _, v := range result.Content {
62 switch content := v.(type) {
63 case *mcp.TextContent:
64 textParts = append(textParts, content.Text)
65 case *mcp.ImageContent:
67 imageData = content.Data
68 imageMimeType = content.MIMEType
70 case *mcp.AudioContent:
72 audioData = content.Data
73 audioMimeType = content.MIMEType
76 textParts = append(textParts, fmt.Sprintf("%v", v))
80 textContent := strings.Join(textParts, "\n")
82 // MCP SDK returns Data as already base64-encoded, so we use it directly.
88 MediaType: imageMimeType,
97 MediaType: audioMimeType,
103 Content: textContent,
107// RefreshTools gets the updated list of tools from the MCP and updates the
109func RefreshTools(ctx context.Context, name string) {
110 session, ok := sessions.Get(name)
112 slog.Warn("refresh tools: no session", "name", name)
116 tools, err := getTools(ctx, session)
118 updateState(name, StateError, err, nil, Counts{})
122 updateTools(name, tools)
124 prev, _ := states.Get(name)
125 prev.Counts.Tools = len(tools)
126 updateState(name, StateConnected, nil, session, prev.Counts)
129func getTools(ctx context.Context, session *mcp.ClientSession) ([]*Tool, error) {
130 // Always call ListTools to get the actual available tools.
131 // The InitializeResult Capabilities.Tools field may be an empty object {},
132 // which is valid per MCP spec, but we still need to call ListTools to discover tools.
133 result, err := session.ListTools(ctx, &mcp.ListToolsParams{})
137 return result.Tools, nil
140func updateTools(name string, tools []*Tool) {
145 allTools.Set(name, tools)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list