|
| 1 | +package harness |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/harness/harness-mcp/client" |
| 11 | + "github.com/harness/harness-mcp/client/dto" |
| 12 | + "github.com/harness/harness-mcp/cmd/harness-mcp-server/config" |
| 13 | + "github.com/mark3labs/mcp-go/mcp" |
| 14 | + "github.com/mark3labs/mcp-go/server" |
| 15 | +) |
| 16 | + |
| 17 | +// GetPullRequestTool creates a tool for getting a specific pull request |
| 18 | +func GetPullRequestTool(config *config.Config, client *client.Client) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 19 | + return mcp.NewTool("get_pull_request", |
| 20 | + mcp.WithDescription("Get details of a specific pull request in a Harness repository."), |
| 21 | + mcp.WithString("repo_id", |
| 22 | + mcp.Required(), |
| 23 | + mcp.Description("The ID of the repository"), |
| 24 | + ), |
| 25 | + mcp.WithNumber("pr_number", |
| 26 | + mcp.Required(), |
| 27 | + mcp.Description("The number of the pull request"), |
| 28 | + ), |
| 29 | + WithScope(config, true), |
| 30 | + ), |
| 31 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 32 | + repoID, err := requiredParam[string](request, "repo_id") |
| 33 | + if err != nil { |
| 34 | + return mcp.NewToolResultError(err.Error()), nil |
| 35 | + } |
| 36 | + |
| 37 | + prNumberFloat, err := requiredParam[float64](request, "pr_number") |
| 38 | + if err != nil { |
| 39 | + return mcp.NewToolResultError(err.Error()), nil |
| 40 | + } |
| 41 | + prNumber := int(prNumberFloat) |
| 42 | + |
| 43 | + scope, err := fetchScope(config, request, true) |
| 44 | + if err != nil { |
| 45 | + return mcp.NewToolResultError(err.Error()), nil |
| 46 | + } |
| 47 | + |
| 48 | + data, err := client.PullRequests.Get(ctx, scope, repoID, prNumber) |
| 49 | + if err != nil { |
| 50 | + return nil, fmt.Errorf("failed to get pull request: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + r, err := json.Marshal(data) |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("failed to marshal pull request: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + return mcp.NewToolResultText(string(r)), nil |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// ListPullRequestsTool creates a tool for listing pull requests |
| 63 | +// TODO: more options can be added (sort, order, timestamps, etc) |
| 64 | +func ListPullRequestsTool(config *config.Config, client *client.Client) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 65 | + return mcp.NewTool("list_pull_requests", |
| 66 | + mcp.WithDescription("List pull requests in a Harness repository."), |
| 67 | + mcp.WithString("repo_id", |
| 68 | + mcp.Required(), |
| 69 | + mcp.Description("The ID of the repository"), |
| 70 | + ), |
| 71 | + mcp.WithString("state", |
| 72 | + mcp.Description("Optional comma-separated states to filter pull requests (possible values: open,closed,merged)"), |
| 73 | + ), |
| 74 | + mcp.WithString("source_branch", |
| 75 | + mcp.Description("Optional source branch to filter pull requests"), |
| 76 | + ), |
| 77 | + mcp.WithString("target_branch", |
| 78 | + mcp.Description("Optional target branch to filter pull requests"), |
| 79 | + ), |
| 80 | + mcp.WithString("query", |
| 81 | + mcp.Description("Optional search query to filter pull requests"), |
| 82 | + ), |
| 83 | + mcp.WithBoolean("include_checks", |
| 84 | + mcp.Description("Optional flag to include CI check information for builds ran in the PR"), |
| 85 | + ), |
| 86 | + mcp.WithNumber("page", |
| 87 | + mcp.Description("Page number for pagination"), |
| 88 | + ), |
| 89 | + mcp.WithNumber("limit", |
| 90 | + mcp.Description("Number of items per page"), |
| 91 | + ), |
| 92 | + WithScope(config, true), |
| 93 | + ), |
| 94 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 95 | + repoID, err := requiredParam[string](request, "repo_id") |
| 96 | + if err != nil { |
| 97 | + return mcp.NewToolResultError(err.Error()), nil |
| 98 | + } |
| 99 | + |
| 100 | + scope, err := fetchScope(config, request, true) |
| 101 | + if err != nil { |
| 102 | + return mcp.NewToolResultError(err.Error()), nil |
| 103 | + } |
| 104 | + |
| 105 | + opts := &dto.PullRequestOptions{} |
| 106 | + |
| 107 | + // Handle pagination |
| 108 | + page, err := OptionalParam[float64](request, "page") |
| 109 | + if err != nil { |
| 110 | + return mcp.NewToolResultError(err.Error()), nil |
| 111 | + } |
| 112 | + if page > 0 { |
| 113 | + opts.Page = int(page) |
| 114 | + } |
| 115 | + |
| 116 | + limit, err := OptionalParam[float64](request, "limit") |
| 117 | + if err != nil { |
| 118 | + return mcp.NewToolResultError(err.Error()), nil |
| 119 | + } |
| 120 | + if limit > 0 { |
| 121 | + opts.Limit = int(limit) |
| 122 | + } |
| 123 | + |
| 124 | + // Handle other optional parameters |
| 125 | + stateStr, err := OptionalParam[string](request, "state") |
| 126 | + if err != nil { |
| 127 | + return mcp.NewToolResultError(err.Error()), nil |
| 128 | + } |
| 129 | + if stateStr != "" { |
| 130 | + opts.State = parseCommaSeparatedList(stateStr) |
| 131 | + } |
| 132 | + |
| 133 | + sourceBranch, err := OptionalParam[string](request, "source_branch") |
| 134 | + if err != nil { |
| 135 | + return mcp.NewToolResultError(err.Error()), nil |
| 136 | + } |
| 137 | + if sourceBranch != "" { |
| 138 | + opts.SourceBranch = sourceBranch |
| 139 | + } |
| 140 | + |
| 141 | + targetBranch, err := OptionalParam[string](request, "target_branch") |
| 142 | + if err != nil { |
| 143 | + return mcp.NewToolResultError(err.Error()), nil |
| 144 | + } |
| 145 | + if targetBranch != "" { |
| 146 | + opts.TargetBranch = targetBranch |
| 147 | + } |
| 148 | + |
| 149 | + query, err := OptionalParam[string](request, "query") |
| 150 | + if err != nil { |
| 151 | + return mcp.NewToolResultError(err.Error()), nil |
| 152 | + } |
| 153 | + if query != "" { |
| 154 | + opts.Query = query |
| 155 | + } |
| 156 | + |
| 157 | + authorIDStr, err := OptionalParam[string](request, "author_id") |
| 158 | + if err != nil { |
| 159 | + return mcp.NewToolResultError(err.Error()), nil |
| 160 | + } |
| 161 | + if authorIDStr != "" { |
| 162 | + authorID, err := strconv.Atoi(authorIDStr) |
| 163 | + if err != nil { |
| 164 | + return mcp.NewToolResultError(fmt.Sprintf("invalid author_id: %s", authorIDStr)), nil |
| 165 | + } |
| 166 | + opts.AuthorID = authorID |
| 167 | + } |
| 168 | + |
| 169 | + includeChecks, err := OptionalParam[bool](request, "include_checks") |
| 170 | + if err != nil { |
| 171 | + return mcp.NewToolResultError(err.Error()), nil |
| 172 | + } |
| 173 | + opts.IncludeChecks = includeChecks |
| 174 | + |
| 175 | + data, err := client.PullRequests.List(ctx, scope, repoID, opts) |
| 176 | + if err != nil { |
| 177 | + return nil, fmt.Errorf("failed to list pull requests: %w", err) |
| 178 | + } |
| 179 | + |
| 180 | + r, err := json.Marshal(data) |
| 181 | + if err != nil { |
| 182 | + return nil, fmt.Errorf("failed to marshal pull request list: %w", err) |
| 183 | + } |
| 184 | + |
| 185 | + return mcp.NewToolResultText(string(r)), nil |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +// Helper function to parse comma-separated list |
| 190 | +func parseCommaSeparatedList(input string) []string { |
| 191 | + if input == "" { |
| 192 | + return nil |
| 193 | + } |
| 194 | + return splitAndTrim(input, ",") |
| 195 | +} |
| 196 | + |
| 197 | +// splitAndTrim splits a string by the given separator and trims spaces from each element |
| 198 | +func splitAndTrim(s, sep string) []string { |
| 199 | + if s == "" { |
| 200 | + return nil |
| 201 | + } |
| 202 | + |
| 203 | + parts := strings.Split(s, sep) |
| 204 | + result := make([]string, 0, len(parts)) |
| 205 | + |
| 206 | + for _, part := range parts { |
| 207 | + trimmed := strings.TrimSpace(part) |
| 208 | + if trimmed != "" { |
| 209 | + result = append(result, trimmed) |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + return result |
| 214 | +} |
0 commit comments