|
| 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 | +// GetRepositoryTool creates a tool for getting a specific repository |
| 16 | +func GetRepositoryTool(config *config.Config, client *client.Client) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 17 | + return mcp.NewTool("get_repository", |
| 18 | + mcp.WithDescription("Get details of a specific repository in Harness."), |
| 19 | + mcp.WithString("repo_identifier", |
| 20 | + mcp.Required(), |
| 21 | + mcp.Description("The identifier of the repository"), |
| 22 | + ), |
| 23 | + WithScope(config, false), |
| 24 | + ), |
| 25 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 26 | + repoIdentifier, err := requiredParam[string](request, "repo_identifier") |
| 27 | + if err != nil { |
| 28 | + return mcp.NewToolResultError(err.Error()), nil |
| 29 | + } |
| 30 | + |
| 31 | + scope, err := fetchScope(config, request, false) |
| 32 | + if err != nil { |
| 33 | + return mcp.NewToolResultError(err.Error()), nil |
| 34 | + } |
| 35 | + |
| 36 | + data, err := client.Repositories.Get(ctx, scope, repoIdentifier) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("failed to get repository: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + r, err := json.Marshal(data) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("failed to marshal repository: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + return mcp.NewToolResultText(string(r)), nil |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// ListRepositoriesTool creates a tool for listing repositories |
| 51 | +func ListRepositoriesTool(config *config.Config, client *client.Client) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 52 | + return mcp.NewTool("list_repositories", |
| 53 | + mcp.WithDescription("List repositories in Harness."), |
| 54 | + mcp.WithString("query", |
| 55 | + mcp.Description("Optional search term to filter repositories"), |
| 56 | + ), |
| 57 | + mcp.WithString("sort", |
| 58 | + mcp.Description("Optional field to sort by (e.g., identifier)"), |
| 59 | + ), |
| 60 | + mcp.WithString("order", |
| 61 | + mcp.Description("Optional sort order (asc or desc)"), |
| 62 | + ), |
| 63 | + mcp.WithNumber("page", |
| 64 | + mcp.DefaultNumber(1), |
| 65 | + mcp.Description("Page number for pagination"), |
| 66 | + ), |
| 67 | + mcp.WithNumber("limit", |
| 68 | + mcp.DefaultNumber(5), |
| 69 | + mcp.Description("Number of items per page"), |
| 70 | + ), |
| 71 | + WithScope(config, false), |
| 72 | + ), |
| 73 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 74 | + scope, err := fetchScope(config, request, false) |
| 75 | + if err != nil { |
| 76 | + return mcp.NewToolResultError(err.Error()), nil |
| 77 | + } |
| 78 | + |
| 79 | + opts := &dto.RepositoryOptions{} |
| 80 | + |
| 81 | + // Handle pagination |
| 82 | + page, err := OptionalParam[float64](request, "page") |
| 83 | + if err != nil { |
| 84 | + return mcp.NewToolResultError(err.Error()), nil |
| 85 | + } |
| 86 | + if page > 0 { |
| 87 | + opts.Page = int(page) |
| 88 | + } |
| 89 | + |
| 90 | + limit, err := OptionalParam[float64](request, "limit") |
| 91 | + if err != nil { |
| 92 | + return mcp.NewToolResultError(err.Error()), nil |
| 93 | + } |
| 94 | + if limit > 0 { |
| 95 | + opts.Limit = int(limit) |
| 96 | + } |
| 97 | + |
| 98 | + // Handle other optional parameters |
| 99 | + query, err := OptionalParam[string](request, "query") |
| 100 | + if err != nil { |
| 101 | + return mcp.NewToolResultError(err.Error()), nil |
| 102 | + } |
| 103 | + if query != "" { |
| 104 | + opts.Query = query |
| 105 | + } |
| 106 | + |
| 107 | + sort, err := OptionalParam[string](request, "sort") |
| 108 | + if err != nil { |
| 109 | + return mcp.NewToolResultError(err.Error()), nil |
| 110 | + } |
| 111 | + if sort != "" { |
| 112 | + opts.Sort = sort |
| 113 | + } |
| 114 | + |
| 115 | + order, err := OptionalParam[string](request, "order") |
| 116 | + if err != nil { |
| 117 | + return mcp.NewToolResultError(err.Error()), nil |
| 118 | + } |
| 119 | + if order != "" { |
| 120 | + opts.Order = order |
| 121 | + } |
| 122 | + |
| 123 | + data, err := client.Repositories.List(ctx, scope, opts) |
| 124 | + if err != nil { |
| 125 | + return nil, fmt.Errorf("failed to list repositories: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + r, err := json.Marshal(data) |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("failed to marshal repository list: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + return mcp.NewToolResultText(string(r)), nil |
| 134 | + } |
| 135 | +} |
0 commit comments