|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/harness/harness-mcp/client" |
| 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 | +// ListFMEWorkspacesTool creates a tool for listing FME workspaces |
| 15 | +func ListFMEWorkspacesTool(config *config.Config, fmeService *client.FMEService) (mcp.Tool, server.ToolHandlerFunc) { |
| 16 | + return mcp.NewTool("list_fme_workspaces", |
| 17 | + mcp.WithDescription("List Feature Management & Experimentation (FME) workspaces."), |
| 18 | + ), |
| 19 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 20 | + workspaces, err := fmeService.ListWorkspaces(ctx) |
| 21 | + if err != nil { |
| 22 | + return mcp.NewToolResultError(fmt.Sprintf("failed to list FME workspaces: %v", err)), nil |
| 23 | + } |
| 24 | + |
| 25 | + responseBytes, err := json.Marshal(workspaces) |
| 26 | + if err != nil { |
| 27 | + return nil, fmt.Errorf("failed to marshal workspaces: %w", err) |
| 28 | + } |
| 29 | + |
| 30 | + return mcp.NewToolResultText(string(responseBytes)), nil |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// ListFMEEnvironmentsTool creates a tool for listing FME environments for a specific workspace |
| 35 | +func ListFMEEnvironmentsTool(config *config.Config, fmeService *client.FMEService) (mcp.Tool, server.ToolHandlerFunc) { |
| 36 | + return mcp.NewTool("list_fme_environments", |
| 37 | + mcp.WithDescription("List Feature Management & Experimentation (FME) environments for a specific workspace."), |
| 38 | + mcp.WithString("ws_id", |
| 39 | + mcp.Required(), |
| 40 | + mcp.Description("The workspace ID to list environments for"), |
| 41 | + ), |
| 42 | + ), |
| 43 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 44 | + wsID, err := RequiredParam[string](request, "ws_id") |
| 45 | + if err != nil { |
| 46 | + return mcp.NewToolResultError(err.Error()), nil |
| 47 | + } |
| 48 | + |
| 49 | + environments, err := fmeService.ListEnvironments(ctx, wsID) |
| 50 | + if err != nil { |
| 51 | + return mcp.NewToolResultError(fmt.Sprintf("failed to list FME environments: %v", err)), nil |
| 52 | + } |
| 53 | + |
| 54 | + responseBytes, err := json.Marshal(environments) |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("failed to marshal environments: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + return mcp.NewToolResultText(string(responseBytes)), nil |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// ListFMEFeatureFlagsTool creates a tool for listing FME feature flags for a specific workspace |
| 64 | +func ListFMEFeatureFlagsTool(config *config.Config, fmeService *client.FMEService) (mcp.Tool, server.ToolHandlerFunc) { |
| 65 | + return mcp.NewTool("list_fme_feature_flags", |
| 66 | + mcp.WithDescription("List Feature Management & Experimentation (FME) feature flags for a specific workspace."), |
| 67 | + mcp.WithString("ws_id", |
| 68 | + mcp.Required(), |
| 69 | + mcp.Description("The workspace ID to list feature flags for"), |
| 70 | + ), |
| 71 | + ), |
| 72 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 73 | + wsID, err := RequiredParam[string](request, "ws_id") |
| 74 | + if err != nil { |
| 75 | + return mcp.NewToolResultError(err.Error()), nil |
| 76 | + } |
| 77 | + |
| 78 | + featureFlags, err := fmeService.ListFeatureFlags(ctx, wsID) |
| 79 | + if err != nil { |
| 80 | + return mcp.NewToolResultError(fmt.Sprintf("failed to list FME feature flags: %v", err)), nil |
| 81 | + } |
| 82 | + |
| 83 | + responseBytes, err := json.Marshal(featureFlags) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("failed to marshal feature flags: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + return mcp.NewToolResultText(string(responseBytes)), nil |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// GetFMEFeatureFlagDefinitionTool creates a tool for getting a specific FME feature flag definition |
| 93 | +func GetFMEFeatureFlagDefinitionTool(config *config.Config, fmeService *client.FMEService) (mcp.Tool, server.ToolHandlerFunc) { |
| 94 | + return mcp.NewTool("get_fme_feature_flag_definition", |
| 95 | + mcp.WithDescription("Get the definition of a specific Feature Management & Experimentation (FME) feature flag in an environment."), |
| 96 | + mcp.WithString("ws_id", |
| 97 | + mcp.Required(), |
| 98 | + mcp.Description("The workspace ID"), |
| 99 | + ), |
| 100 | + mcp.WithString("feature_flag_name", |
| 101 | + mcp.Required(), |
| 102 | + mcp.Description("The name of the feature flag"), |
| 103 | + ), |
| 104 | + mcp.WithString("environment_id_or_name", |
| 105 | + mcp.Required(), |
| 106 | + mcp.Description("The environment ID or name"), |
| 107 | + ), |
| 108 | + ), |
| 109 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 110 | + wsID, err := RequiredParam[string](request, "ws_id") |
| 111 | + if err != nil { |
| 112 | + return mcp.NewToolResultError(err.Error()), nil |
| 113 | + } |
| 114 | + |
| 115 | + flagName, err := RequiredParam[string](request, "feature_flag_name") |
| 116 | + if err != nil { |
| 117 | + return mcp.NewToolResultError(err.Error()), nil |
| 118 | + } |
| 119 | + |
| 120 | + envIDOrName, err := RequiredParam[string](request, "environment_id_or_name") |
| 121 | + if err != nil { |
| 122 | + return mcp.NewToolResultError(err.Error()), nil |
| 123 | + } |
| 124 | + |
| 125 | + definition, err := fmeService.GetFeatureFlagDefinition(ctx, wsID, flagName, envIDOrName) |
| 126 | + if err != nil { |
| 127 | + return mcp.NewToolResultError(fmt.Sprintf("failed to get FME feature flag definition: %v", err)), nil |
| 128 | + } |
| 129 | + |
| 130 | + responseBytes, err := json.Marshal(definition) |
| 131 | + if err != nil { |
| 132 | + return nil, fmt.Errorf("failed to marshal feature flag definition: %w", err) |
| 133 | + } |
| 134 | + |
| 135 | + return mcp.NewToolResultText(string(responseBytes)), nil |
| 136 | + } |
| 137 | +} |
0 commit comments