|
| 1 | +package mcptools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + |
| 8 | + "github.com/redhat-appstudio/tssc-cli/pkg/config" |
| 9 | + "github.com/redhat-appstudio/tssc-cli/pkg/constants" |
| 10 | + "github.com/redhat-appstudio/tssc-cli/pkg/deployer" |
| 11 | + "github.com/redhat-appstudio/tssc-cli/pkg/flags" |
| 12 | + "github.com/redhat-appstudio/tssc-cli/pkg/installer" |
| 13 | + "github.com/redhat-appstudio/tssc-cli/pkg/k8s" |
| 14 | + "github.com/redhat-appstudio/tssc-cli/pkg/resolver" |
| 15 | + |
| 16 | + "github.com/mark3labs/mcp-go/mcp" |
| 17 | + "github.com/mark3labs/mcp-go/server" |
| 18 | +) |
| 19 | + |
| 20 | +// NotesTool a MCP tool to provide connection instructions for products. These |
| 21 | +// products must be completely deployed before its "NOTES.txt" is generated. |
| 22 | +type NotesTool struct { |
| 23 | + logger *slog.Logger // application logger |
| 24 | + flags *flags.Flags // global flags |
| 25 | + kube *k8s.Kube // kubernetes client |
| 26 | + cm *config.ConfigMapManager // cluster configuration |
| 27 | + tb *resolver.TopologyBuilder // topology builder |
| 28 | + job *installer.Job // cluster deployment job |
| 29 | +} |
| 30 | + |
| 31 | +var _ Interface = &NotesTool{} |
| 32 | + |
| 33 | +const ( |
| 34 | + // NotesToolName retrieves the connection instruction for a product. |
| 35 | + NotesToolName = constants.AppName + "_notes" |
| 36 | +) |
| 37 | + |
| 38 | +// notesHandler retrieves the Helm chart notes (NOTES.txt) for a specified Red Hat |
| 39 | +// product. It ensures the product name is provided, checks if the cluster |
| 40 | +// installation is in a "completed" phase, and then uses a Helm client to fetch |
| 41 | +// and return the notes. |
| 42 | +func (n *NotesTool) notesHandler( |
| 43 | + ctx context.Context, |
| 44 | + ctr mcp.CallToolRequest, |
| 45 | +) (*mcp.CallToolResult, error) { |
| 46 | + // Ensure the user has provided the product name. |
| 47 | + name := ctr.GetString(NameArg, "") |
| 48 | + if name == "" { |
| 49 | + return mcp.NewToolResultError(` |
| 50 | + You must inform the Red Hat product name`, |
| 51 | + ), nil |
| 52 | + } |
| 53 | + |
| 54 | + // Check if the cluster is ready. If not, provide instructions on how to |
| 55 | + // proceed. The installer must be on "completed" status. |
| 56 | + phase, err := getInstallerPhase(ctx, n.cm, n.tb, n.job) |
| 57 | + currentStatus := fmt.Sprintf(` |
| 58 | +# Current Status: %q |
| 59 | +
|
| 60 | +The cluster is not ready, use the tool %q to check the overall status and general |
| 61 | +directions on how to proceed.`, |
| 62 | + phase, StatusToolName, |
| 63 | + ) |
| 64 | + if err != nil { |
| 65 | + return mcp.NewToolResultText(fmt.Sprintf(`%s |
| 66 | +
|
| 67 | +Inspecting the cluster returned the following error: |
| 68 | +
|
| 69 | +> %s`, |
| 70 | + currentStatus, err.Error(), |
| 71 | + )), nil |
| 72 | + } |
| 73 | + if phase != CompletedPhase { |
| 74 | + return mcp.NewToolResultText(currentStatus), nil |
| 75 | + } |
| 76 | + |
| 77 | + dep, err := n.tb.GetCollection().GetProductDependency(name) |
| 78 | + if err != nil { |
| 79 | + return mcp.NewToolResultErrorFromErr( |
| 80 | + fmt.Sprintf(` |
| 81 | +Unable to find the dependency for the informed product name %q`, |
| 82 | + name, |
| 83 | + ), |
| 84 | + err, |
| 85 | + ), nil |
| 86 | + } |
| 87 | + |
| 88 | + hc, err := deployer.NewHelm( |
| 89 | + n.logger, n.flags, n.kube, dep.Namespace(), dep.Chart()) |
| 90 | + if err != nil { |
| 91 | + return mcp.NewToolResultErrorFromErr( |
| 92 | + fmt.Sprintf(` |
| 93 | +Error trying to instantiate a Helm client for the chart %q on namespace %q.`, |
| 94 | + dep.Chart().Name(), |
| 95 | + dep.Namespace(), |
| 96 | + ), |
| 97 | + err, |
| 98 | + ), nil |
| 99 | + } |
| 100 | + |
| 101 | + notes, err := hc.GetNotes() |
| 102 | + if err != nil { |
| 103 | + return mcp.NewToolResultErrorFromErr( |
| 104 | + fmt.Sprintf(` |
| 105 | +Unable to get "NOTES.txt" for the chart %q on namespace %q.`, |
| 106 | + dep.Chart().Name(), |
| 107 | + dep.Namespace(), |
| 108 | + ), |
| 109 | + err, |
| 110 | + ), nil |
| 111 | + } |
| 112 | + |
| 113 | + return mcp.NewToolResultText(notes), nil |
| 114 | +} |
| 115 | + |
| 116 | +func (n *NotesTool) Init(s *server.MCPServer) { |
| 117 | + s.AddTools([]server.ServerTool{{ |
| 118 | + Tool: mcp.NewTool( |
| 119 | + NotesToolName, |
| 120 | + mcp.WithDescription(` |
| 121 | +Retrieve the service notes, the initial coordinates to utilize services deployed |
| 122 | +by this installer, from the informed product name.`, |
| 123 | + ), |
| 124 | + mcp.WithString( |
| 125 | + NameArg, |
| 126 | + mcp.Description(` |
| 127 | +The name of the Red Hat product to retrieve connection information.`, |
| 128 | + ), |
| 129 | + ), |
| 130 | + ), |
| 131 | + Handler: n.notesHandler, |
| 132 | + }}...) |
| 133 | +} |
| 134 | + |
| 135 | +func NewNotesTool( |
| 136 | + logger *slog.Logger, |
| 137 | + f *flags.Flags, |
| 138 | + kube *k8s.Kube, |
| 139 | + cm *config.ConfigMapManager, |
| 140 | + tb *resolver.TopologyBuilder, |
| 141 | +) *NotesTool { |
| 142 | + return &NotesTool{ |
| 143 | + logger: logger, |
| 144 | + flags: f, |
| 145 | + kube: kube, |
| 146 | + cm: cm, |
| 147 | + tb: tb, |
| 148 | + job: installer.NewJob(kube), |
| 149 | + } |
| 150 | +} |
0 commit comments