|
| 1 | +package harness |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/harness/harness-mcp/client" |
| 9 | + "github.com/harness/harness-mcp/client/dto" |
| 10 | + "github.com/harness/harness-mcp/cmd/harness-mcp-server/config" |
| 11 | + "github.com/mark3labs/mcp-go/mcp" |
| 12 | + "github.com/mark3labs/mcp-go/server" |
| 13 | +) |
| 14 | + |
| 15 | +// FindSimilarTemplates creates a tool that allows finding similar templates based on provided description. |
| 16 | +func FindSimilarTemplates(config *config.Config, client *client.IntelligenceService) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 17 | + return mcp.NewTool("intelligent_template_search", |
| 18 | + mcp.WithDescription("Finds the most relevant templates based on a natural language description. " + |
| 19 | + "Searches across template identifiers, names, types, capabilities, and use cases to find the best matches. " + |
| 20 | + "Returns templates ranked by similarity score with metadata including template IDs and organizational context. " + |
| 21 | + "Ideal for discovering templates that fulfill specific requirements without knowing exact identifiers."), |
| 22 | + mcp.WithString("description", |
| 23 | + mcp.Required(), |
| 24 | + mcp.Description("Description of the template to find similar templates"), |
| 25 | + ), |
| 26 | + mcp.WithString("template_type", |
| 27 | + mcp.Description("Type of templates to find similar templates (e.g., Step, Stage, Pipeline)"), |
| 28 | + ), |
| 29 | + mcp.WithNumber("count", |
| 30 | + mcp.Description("Maximum number of similar templates to return"), |
| 31 | + ), |
| 32 | + WithScope(config, false), |
| 33 | + ), |
| 34 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 35 | + // Get required description parameter |
| 36 | + description, err := requiredParam[string](request, "description") |
| 37 | + if err != nil { |
| 38 | + return mcp.NewToolResultError(err.Error()), nil |
| 39 | + } |
| 40 | + |
| 41 | + // Fetch optional parameters |
| 42 | + templateType, err := OptionalParam[string](request, "template_type") |
| 43 | + if err != nil { |
| 44 | + return mcp.NewToolResultError(err.Error()), nil |
| 45 | + } |
| 46 | + |
| 47 | + // Handle count parameter as float64 (JSON default) and convert to int |
| 48 | + countFloat, err := OptionalParam[float64](request, "count") |
| 49 | + if err != nil { |
| 50 | + return mcp.NewToolResultError(err.Error()), nil |
| 51 | + } |
| 52 | + count := int(countFloat) |
| 53 | + |
| 54 | + // Try to fetch scope parameters (account_id, org_id, project_id) if provided |
| 55 | + scope, err := fetchScope(config, request, false) |
| 56 | + if err != nil { |
| 57 | + return mcp.NewToolResultError(err.Error()), nil |
| 58 | + } |
| 59 | + |
| 60 | + // Create similarity search request |
| 61 | + similarityRequest := &dto.SimilaritySearchRequest{ |
| 62 | + AccountID: scope.AccountID, |
| 63 | + OrgID: scope.OrgID, |
| 64 | + ProjectID: scope.ProjectID, |
| 65 | + Description: description, |
| 66 | + Count: count, |
| 67 | + TemplateType: templateType, |
| 68 | + } |
| 69 | + |
| 70 | + // Call the similarity search API |
| 71 | + result, err := client.SimilaritySearch(ctx, similarityRequest) |
| 72 | + if err != nil { |
| 73 | + return nil, fmt.Errorf("failed to perform similarity search: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + // Marshal and return the result |
| 77 | + r, err := json.Marshal(result) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("failed to marshal similarity search response: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + return mcp.NewToolResultText(string(r)), nil |
| 83 | + } |
| 84 | +} |
0 commit comments