|
| 1 | +package harness |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/harness/harness-mcp/client" |
| 8 | + "github.com/harness/harness-mcp/client/dto" |
| 9 | + "github.com/harness/harness-mcp/cmd/harness-mcp-server/config" |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | + "github.com/mark3labs/mcp-go/server" |
| 12 | +) |
| 13 | + |
| 14 | +// ListExperimentsTool creates a tool for listing the experiments |
| 15 | +func ListExperimentsTool(config *config.Config, client *client.ChaosService) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 16 | + return mcp.NewTool("chaos_experiments_list", |
| 17 | + mcp.WithDescription("List the chaos experiments"), |
| 18 | + WithScope(config, false), |
| 19 | + WithPagination(), |
| 20 | + ), |
| 21 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 22 | + scope, err := fetchScope(config, request, false) |
| 23 | + if err != nil { |
| 24 | + return mcp.NewToolResultError(err.Error()), nil |
| 25 | + } |
| 26 | + |
| 27 | + page, size, err := fetchPagination(request) |
| 28 | + if err != nil { |
| 29 | + return mcp.NewToolResultError(err.Error()), nil |
| 30 | + } |
| 31 | + |
| 32 | + pagination := &dto.PaginationOptions{ |
| 33 | + Page: page, |
| 34 | + Size: size, |
| 35 | + } |
| 36 | + |
| 37 | + data, err := client.ListExperiments(ctx, scope, pagination) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("failed to list experiments: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + r, err := json.Marshal(data) |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("failed to marshal list experiment response: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + return mcp.NewToolResultText(string(r)), nil |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// GetExperimentsTool creates a tool to get the experiment details |
| 52 | +func GetExperimentsTool(config *config.Config, client *client.ChaosService) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 53 | + return mcp.NewTool("chaos_experiment_describe", |
| 54 | + mcp.WithDescription("Retrieves information about chaos experiment, allowing users to get an overview and detailed insights for each experiment"), |
| 55 | + WithScope(config, false), |
| 56 | + ), |
| 57 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 58 | + scope, err := fetchScope(config, request, false) |
| 59 | + if err != nil { |
| 60 | + return mcp.NewToolResultError(err.Error()), nil |
| 61 | + } |
| 62 | + |
| 63 | + experimentID, err := requiredParam[string](request, "experimentID") |
| 64 | + if err != nil { |
| 65 | + return mcp.NewToolResultError(err.Error()), nil |
| 66 | + } |
| 67 | + |
| 68 | + data, err := client.GetExperiment(ctx, scope, experimentID) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("failed to get experiment: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + r, err := json.Marshal(data) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("failed to marshal get experiment response: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + return mcp.NewToolResultText(string(r)), nil |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// GetExperimentRunsTool creates a tool to get the experiment run details |
| 83 | +func GetExperimentRunsTool(config *config.Config, client *client.ChaosService) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 84 | + return mcp.NewTool("chaos_experiment_run_result", |
| 85 | + mcp.WithDescription("Retrieves run result of chaos experiment runs, helping to describe and summarize the details of each experiment run"), |
| 86 | + WithScope(config, false), |
| 87 | + ), |
| 88 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 89 | + scope, err := fetchScope(config, request, false) |
| 90 | + if err != nil { |
| 91 | + return mcp.NewToolResultError(err.Error()), nil |
| 92 | + } |
| 93 | + |
| 94 | + experimentID, err := requiredParam[string](request, "experimentID") |
| 95 | + if err != nil { |
| 96 | + return mcp.NewToolResultError(err.Error()), nil |
| 97 | + } |
| 98 | + |
| 99 | + experimentRunID, err := requiredParam[string](request, "experimentRunId") |
| 100 | + if err != nil { |
| 101 | + return mcp.NewToolResultError(err.Error()), nil |
| 102 | + } |
| 103 | + |
| 104 | + data, err := client.GetExperimentRun(ctx, scope, experimentID, experimentRunID) |
| 105 | + if err != nil { |
| 106 | + return nil, fmt.Errorf("failed to get experiment run: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + r, err := json.Marshal(data) |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("failed to marshal get experiment run response: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + return mcp.NewToolResultText(string(r)), nil |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// RunExperimentTool creates a tool to run the experiment |
| 119 | +func RunExperimentTool(config *config.Config, client *client.ChaosService) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 120 | + return mcp.NewTool("chaos_experiment_run", |
| 121 | + mcp.WithDescription("Run the chaos experiment"), |
| 122 | + WithScope(config, false), |
| 123 | + ), |
| 124 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 125 | + scope, err := fetchScope(config, request, false) |
| 126 | + if err != nil { |
| 127 | + return mcp.NewToolResultError(err.Error()), nil |
| 128 | + } |
| 129 | + |
| 130 | + experimentID, err := requiredParam[string](request, "experimentID") |
| 131 | + if err != nil { |
| 132 | + return mcp.NewToolResultError(err.Error()), nil |
| 133 | + } |
| 134 | + |
| 135 | + data, err := client.RunExperiment(ctx, scope, experimentID) |
| 136 | + if err != nil { |
| 137 | + return nil, fmt.Errorf("failed to run experiment: %w", err) |
| 138 | + } |
| 139 | + |
| 140 | + r, err := json.Marshal(data) |
| 141 | + if err != nil { |
| 142 | + return nil, fmt.Errorf("failed to marshal run experiment response: %w", err) |
| 143 | + } |
| 144 | + |
| 145 | + return mcp.NewToolResultText(string(r)), nil |
| 146 | + } |
| 147 | +} |
0 commit comments