|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + ghErrors "github.com/github/github-mcp-server/pkg/errors" |
| 12 | + "github.com/github/github-mcp-server/pkg/inventory" |
| 13 | + "github.com/github/github-mcp-server/pkg/scopes" |
| 14 | + "github.com/github/github-mcp-server/pkg/translations" |
| 15 | + "github.com/github/github-mcp-server/pkg/utils" |
| 16 | + "github.com/google/jsonschema-go/jsonschema" |
| 17 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 18 | +) |
| 19 | + |
| 20 | +// duplicateIssueResult mirrors a single "Ranked Similar Issue" element returned |
| 21 | +// by the semantic-similarity endpoint. Issue is kept as raw JSON so the full |
| 22 | +// issue representation is preserved verbatim, and Score is nullable because the |
| 23 | +// API may omit a similarity score. |
| 24 | +type duplicateIssueResult struct { |
| 25 | + Issue json.RawMessage `json:"issue"` |
| 26 | + Score *float64 `json:"score"` |
| 27 | + Confidence string `json:"confidence"` |
| 28 | + LikelyDuplicate bool `json:"likely_duplicate"` |
| 29 | +} |
| 30 | + |
| 31 | +// FindDuplicate creates a read-only tool that returns ranked duplicate |
| 32 | +// candidates for an existing issue. It is a separate, feature-flagged tool so |
| 33 | +// duplicate detection is only advertised when explicitly opted in, keeping the |
| 34 | +// default tool surface small. The semantic ranking itself is owned by the API; |
| 35 | +// this tool only forwards the request and projects the ranked results. |
| 36 | +func FindDuplicate(t translations.TranslationHelperFunc) inventory.ServerTool { |
| 37 | + schema := &jsonschema.Schema{ |
| 38 | + Type: "object", |
| 39 | + Properties: map[string]*jsonschema.Schema{ |
| 40 | + "owner": { |
| 41 | + Type: "string", |
| 42 | + Description: "The owner of the repository", |
| 43 | + }, |
| 44 | + "repo": { |
| 45 | + Type: "string", |
| 46 | + Description: "The name of the repository", |
| 47 | + }, |
| 48 | + "issue_number": { |
| 49 | + Type: "number", |
| 50 | + Description: "The number of the existing issue to find duplicates for", |
| 51 | + }, |
| 52 | + "confidence_threshold": { |
| 53 | + Type: "number", |
| 54 | + Description: "Minimum similarity threshold for a candidate to be returned. When omitted, the API's high-precision default is used.", |
| 55 | + Minimum: jsonschema.Ptr(0.0), |
| 56 | + Maximum: jsonschema.Ptr(1.0), |
| 57 | + }, |
| 58 | + }, |
| 59 | + Required: []string{"owner", "repo", "issue_number"}, |
| 60 | + } |
| 61 | + WithPagination(schema) |
| 62 | + |
| 63 | + st := NewTool( |
| 64 | + ToolsetMetadataIssues, |
| 65 | + mcp.Tool{ |
| 66 | + Name: "find_duplicate", |
| 67 | + Description: t("TOOL_FIND_DUPLICATE_DESCRIPTION", "Find likely duplicate issues for an existing issue in a GitHub repository. This is a read-only search scoped to the source issue's repository: it returns ranked candidate issues with a similarity score and confidence, and does not close, link, comment on, or otherwise modify any issue."), |
| 68 | + Annotations: &mcp.ToolAnnotations{ |
| 69 | + Title: t("TOOL_FIND_DUPLICATE_USER_TITLE", "Find duplicate issues"), |
| 70 | + ReadOnlyHint: true, |
| 71 | + }, |
| 72 | + InputSchema: schema, |
| 73 | + }, |
| 74 | + []scopes.Scope{scopes.Repo}, |
| 75 | + func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) { |
| 76 | + owner, err := RequiredParam[string](args, "owner") |
| 77 | + if err != nil { |
| 78 | + return utils.NewToolResultError(err.Error()), nil, nil |
| 79 | + } |
| 80 | + repo, err := RequiredParam[string](args, "repo") |
| 81 | + if err != nil { |
| 82 | + return utils.NewToolResultError(err.Error()), nil, nil |
| 83 | + } |
| 84 | + issueNumber, err := RequiredInt(args, "issue_number") |
| 85 | + if err != nil { |
| 86 | + return utils.NewToolResultError(err.Error()), nil, nil |
| 87 | + } |
| 88 | + |
| 89 | + // Build the query preserving whether each optional value was supplied |
| 90 | + // so unset parameters fall back to the API's own defaults. |
| 91 | + query := url.Values{} |
| 92 | + if threshold, ok, err := OptionalParamOK[float64](args, "confidence_threshold"); err != nil { |
| 93 | + return utils.NewToolResultError(err.Error()), nil, nil |
| 94 | + } else if ok { |
| 95 | + query.Set("threshold", strconv.FormatFloat(threshold, 'g', -1, 64)) |
| 96 | + } |
| 97 | + if _, ok := args["perPage"]; ok { |
| 98 | + perPage, err := OptionalIntParam(args, "perPage") |
| 99 | + if err != nil { |
| 100 | + return utils.NewToolResultError(err.Error()), nil, nil |
| 101 | + } |
| 102 | + query.Set("per_page", strconv.Itoa(perPage)) |
| 103 | + } |
| 104 | + if _, ok := args["page"]; ok { |
| 105 | + page, err := OptionalIntParam(args, "page") |
| 106 | + if err != nil { |
| 107 | + return utils.NewToolResultError(err.Error()), nil, nil |
| 108 | + } |
| 109 | + query.Set("page", strconv.Itoa(page)) |
| 110 | + } |
| 111 | + |
| 112 | + client, err := deps.GetClient(ctx) |
| 113 | + if err != nil { |
| 114 | + return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil |
| 115 | + } |
| 116 | + |
| 117 | + apiURL := fmt.Sprintf("repos/%s/%s/issues/%d/semantically_similar", owner, repo, issueNumber) |
| 118 | + if encoded := query.Encode(); encoded != "" { |
| 119 | + apiURL += "?" + encoded |
| 120 | + } |
| 121 | + |
| 122 | + req, err := client.NewRequest(ctx, http.MethodGet, apiURL, nil) |
| 123 | + if err != nil { |
| 124 | + return utils.NewToolResultErrorFromErr("failed to create request", err), nil, nil |
| 125 | + } |
| 126 | + |
| 127 | + var results []duplicateIssueResult |
| 128 | + resp, err := client.Do(req, &results) |
| 129 | + if err != nil { |
| 130 | + return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to find duplicate issues", resp, err), nil, nil |
| 131 | + } |
| 132 | + defer func() { _ = resp.Body.Close() }() |
| 133 | + |
| 134 | + // When ranked duplicate detection is not enabled for the caller, the |
| 135 | + // endpoint returns bare issue resources instead of ranked results. |
| 136 | + // Surface that as an explicit error rather than incomplete candidates. |
| 137 | + for i := range results { |
| 138 | + if results[i].Confidence == "" || len(results[i].Issue) == 0 { |
| 139 | + return utils.NewToolResultError("ranked duplicate detection is unavailable: the semantic-similarity endpoint returned issues without ranking metadata (the server-side duplicate-ranking feature is not enabled for this caller or repository)"), nil, nil |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + r, err := json.Marshal(results) |
| 144 | + if err != nil { |
| 145 | + return utils.NewToolResultErrorFromErr("failed to marshal duplicate candidates", err), nil, nil |
| 146 | + } |
| 147 | + |
| 148 | + return utils.NewToolResultText(string(r)), nil, nil |
| 149 | + }) |
| 150 | + st.FeatureFlagEnable = FeatureFlagDuplicateDetection |
| 151 | + return st |
| 152 | +} |
0 commit comments