Repository navigation
Add list_code_quality_findings tool to code_quality toolset #3564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7272ef3
4e8641d
95f972a
314335a
a5bc898
c12b117
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| { | ||
| "annotations": { | ||
| "idempotentHint": false, | ||
| "readOnlyHint": true, | ||
| "title": "List code quality findings" | ||
| }, | ||
| "description": "List code quality findings in a GitHub repository.", | ||
| "inputSchema": { | ||
| "properties": { | ||
| "after": { | ||
| "description": "Cursor for pagination. Use the cursor from the previous response.", | ||
| "type": "string" | ||
| }, | ||
| "direction": { | ||
| "description": "Sort by creation date: `desc` (newest first, default) or `asc` (oldest first).", | ||
| "enum": [ | ||
| "asc", | ||
| "desc" | ||
| ], | ||
| "type": "string" | ||
| }, | ||
| "owner": { | ||
| "description": "The owner of the repository.", | ||
| "type": "string" | ||
| }, | ||
| "perPage": { | ||
| "description": "Results per page for pagination (min 1, max 100)", | ||
| "maximum": 100, | ||
| "minimum": 1, | ||
| "type": "number" | ||
| }, | ||
| "repo": { | ||
| "description": "The name of the repository.", | ||
| "type": "string" | ||
| }, | ||
| "state": { | ||
| "default": "open", | ||
| "description": "Filter code quality findings by state. Defaults to open", | ||
| "enum": [ | ||
| "open", | ||
| "dismissed" | ||
| ], | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "owner", | ||
| "repo" | ||
| ], | ||
| "type": "object" | ||
| }, | ||
| "name": "list_code_quality_findings", | ||
| "outputSchema": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "findings": { | ||
| "items": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "created_at": { | ||
| "description": "Finding creation time (RFC 3339).", | ||
| "type": [ | ||
| "null", | ||
| "string" | ||
| ] | ||
| }, | ||
| "location": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "end_column": { | ||
| "type": [ | ||
| "null", | ||
| "integer" | ||
| ] | ||
| }, | ||
| "end_line": { | ||
| "type": [ | ||
| "null", | ||
| "integer" | ||
| ] | ||
| }, | ||
| "path": { | ||
| "type": [ | ||
| "null", | ||
| "string" | ||
| ] | ||
| }, | ||
| "start_column": { | ||
| "type": [ | ||
| "null", | ||
| "integer" | ||
| ] | ||
| }, | ||
| "start_line": { | ||
| "type": [ | ||
| "null", | ||
| "integer" | ||
| ] | ||
| } | ||
| }, | ||
| "type": [ | ||
| "null", | ||
| "object" | ||
| ] | ||
| }, | ||
| "message": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "markdown": { | ||
| "type": "string" | ||
| }, | ||
| "text": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "text", | ||
| "markdown" | ||
| ], | ||
| "type": [ | ||
| "null", | ||
| "object" | ||
| ] | ||
| }, | ||
| "number": { | ||
| "type": [ | ||
| "null", | ||
| "integer" | ||
| ] | ||
| }, | ||
| "rule": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "category": { | ||
| "type": [ | ||
| "null", | ||
| "string" | ||
| ] | ||
| }, | ||
| "description": { | ||
| "type": [ | ||
| "null", | ||
| "string" | ||
| ] | ||
| }, | ||
| "help": { | ||
| "type": [ | ||
| "null", | ||
| "string" | ||
| ] | ||
| }, | ||
| "id": { | ||
| "type": [ | ||
| "null", | ||
| "string" | ||
| ] | ||
| }, | ||
| "severity": { | ||
| "type": [ | ||
| "null", | ||
| "string" | ||
| ] | ||
| }, | ||
| "title": { | ||
| "type": [ | ||
| "null", | ||
| "string" | ||
| ] | ||
| } | ||
| }, | ||
| "type": [ | ||
| "null", | ||
| "object" | ||
| ] | ||
| }, | ||
| "state": { | ||
| "type": [ | ||
| "null", | ||
| "string" | ||
| ] | ||
| } | ||
| }, | ||
| "type": [ | ||
| "null", | ||
| "object" | ||
| ] | ||
| }, | ||
| "type": [ | ||
| "null", | ||
| "array" | ||
| ] | ||
| }, | ||
| "pageInfo": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "hasNextPage": { | ||
| "type": "boolean" | ||
| }, | ||
| "hasPreviousPage": { | ||
| "type": "boolean" | ||
| }, | ||
| "nextCursor": { | ||
| "type": "string" | ||
| }, | ||
| "prevCursor": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "hasNextPage", | ||
| "hasPreviousPage" | ||
| ], | ||
| "type": "object" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "findings", | ||
| "pageInfo" | ||
| ], | ||
| "type": "object" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,11 +6,14 @@ import ( | |
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
|
|
||
| "github.com/google/jsonschema-go/jsonschema" | ||
| "github.com/modelcontextprotocol/go-sdk/mcp" | ||
|
|
||
| ghErrors "github.com/github/github-mcp-server/v2/pkg/errors" | ||
| "github.com/github/github-mcp-server/v2/pkg/ifc" | ||
| "github.com/github/github-mcp-server/v2/pkg/inventory" | ||
| "github.com/github/github-mcp-server/v2/pkg/scopes" | ||
| "github.com/github/github-mcp-server/v2/pkg/translations" | ||
|
|
@@ -99,6 +102,138 @@ func GetCodeQualityFinding(t translations.TranslationHelperFunc) inventory.Serve | |
| ) | ||
| } | ||
|
|
||
| func ListCodeQualityFindings(t translations.TranslationHelperFunc) inventory.ServerTool { | ||
| schema := &jsonschema.Schema{ | ||
| Type: "object", | ||
| Properties: map[string]*jsonschema.Schema{ | ||
| "owner": { | ||
| Type: "string", | ||
| Description: "The owner of the repository.", | ||
| }, | ||
| "repo": { | ||
| Type: "string", | ||
| Description: "The name of the repository.", | ||
| }, | ||
| "state": { | ||
| Type: "string", | ||
| Description: "Filter code quality findings by state. Defaults to open", | ||
| Enum: []any{"open", "dismissed"}, | ||
| Default: json.RawMessage(`"open"`), | ||
| }, | ||
| "direction": { | ||
| Type: "string", | ||
| Description: "Sort by creation date: `desc` (newest first, default) or `asc` (oldest first).", | ||
| Enum: []any{"asc", "desc"}, | ||
| }, | ||
| }, | ||
| Required: []string{"owner", "repo"}, | ||
| } | ||
| WithCursorPagination(schema) | ||
|
|
||
| return NewToolWithSchemaOptions[ListCodeQualityFindingsInput, CodeQualityFindingsOutput]( | ||
| ToolsetMetadataCodeQuality, | ||
| mcp.Tool{ | ||
| Name: "list_code_quality_findings", | ||
| Description: t("TOOL_LIST_CODE_QUALITY_FINDINGS_DESCRIPTION", "List code quality findings in a GitHub repository."), | ||
| Annotations: &mcp.ToolAnnotations{ | ||
| Title: t("TOOL_LIST_CODE_QUALITY_FINDINGS_USER_TITLE", "List code quality findings"), | ||
| ReadOnlyHint: true, | ||
| }, | ||
| InputSchema: schema, | ||
| }, | ||
| scopes.PublicRead(scopes.Repo), | ||
| inventory.TypedSchemaOptions{ | ||
| ValidationInputSchema: inventory.CloneSchemaWithoutDefaults(schema), | ||
| }, | ||
| func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args ListCodeQualityFindingsInput) (*mcp.CallToolResult, CodeQualityFindingsOutput, error) { | ||
| if args.Owner == "" { | ||
| return utils.NewToolResultError("missing required parameter: owner"), CodeQualityFindingsOutput{}, nil | ||
| } | ||
| if args.Repo == "" { | ||
| return utils.NewToolResultError("missing required parameter: repo"), CodeQualityFindingsOutput{}, nil | ||
| } | ||
|
|
||
| pagination := securityCursorPagination(args.PerPage, args.After) | ||
| client, err := deps.GetClient(ctx) | ||
| if err != nil { | ||
| return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), CodeQualityFindingsOutput{}, nil | ||
| } | ||
|
|
||
| query := url.Values{} | ||
| if args.State != "" { | ||
| query.Set("state", args.State) | ||
| } | ||
| if args.Direction != "" { | ||
| query.Set("direction", args.Direction) | ||
| } | ||
| query.Set("per_page", strconv.Itoa(pagination.PerPage)) | ||
| if pagination.After != "" { | ||
| query.Set("after", pagination.After) | ||
| } | ||
| apiURL := fmt.Sprintf("repos/%s/%s/code-quality/findings?%s", url.PathEscape(args.Owner), url.PathEscape(args.Repo), query.Encode()) | ||
| req, err := client.NewRequest(ctx, http.MethodGet, apiURL, nil) | ||
| if err != nil { | ||
| return utils.NewToolResultErrorFromErr("failed to create request", err), CodeQualityFindingsOutput{}, nil | ||
| } | ||
|
|
||
| var rawFindings json.RawMessage | ||
| resp, err := client.Do(req, &rawFindings) | ||
| if resp != nil { | ||
| defer func() { _ = resp.Body.Close() }() | ||
| } | ||
| if err != nil { | ||
| return ghErrors.NewGitHubAPIErrorResponse(ctx, | ||
| fmt.Sprintf("failed to list findings for repository '%s/%s'", args.Owner, args.Repo), | ||
| resp, | ||
| err, | ||
| ), CodeQualityFindingsOutput{}, nil | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return utils.NewToolResultErrorFromErr("failed to read response body", err), CodeQualityFindingsOutput{}, nil | ||
| } | ||
| return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to list findings", resp, body), CodeQualityFindingsOutput{}, nil | ||
| } | ||
|
|
||
| if len(rawFindings) == 0 { | ||
| rawFindings = json.RawMessage("null") | ||
| } | ||
| var findings []*CodeQualityFindingOutput | ||
| if err := json.Unmarshal(rawFindings, &findings); err != nil { | ||
| return utils.NewToolResultErrorFromErr("failed to decode findings", err), CodeQualityFindingsOutput{}, nil | ||
| } | ||
|
|
||
| apiPageInfo := buildPageInfo(resp) | ||
| output := CodeQualityFindingsOutput{ | ||
| Findings: findings, | ||
| PageInfo: SecurityPageInfo(apiPageInfo), | ||
| } | ||
|
|
||
| response := struct { | ||
| Findings json.RawMessage `json:"findings"` | ||
| PageInfo pageInfo `json:"pageInfo"` | ||
| }{ | ||
| Findings: rawFindings, | ||
| PageInfo: apiPageInfo, | ||
| } | ||
| r, err := json.Marshal(response) | ||
| if err != nil { | ||
| return utils.NewToolResultErrorFromErr("failed to marshal findings", err), CodeQualityFindingsOutput{}, nil | ||
| } | ||
|
|
||
| result := utils.NewToolResultText(string(r)) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a known potential issue, but from what I can see in other tools we have there's no perfect fix for it. The suggested fix drops the findings from the text output, which breaks clients that read text instead of structured content; the same change was declined in #3719. Notably this tool will only return findings from the default branch of the repository at this time, so any malicious instructions would have to be in code that someone with write access has merged into the default branch.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, Copilot is being a bit extra here. Don't worry about that. |
||
| // Like code scanning alerts, findings embed text derived from | ||
| // repository code (file paths and messages), so they share the | ||
| // private-untrusted security alert label. | ||
| result = attachStaticIFCLabel(ctx, deps, result, ifc.LabelSecurityAlert()) | ||
| return result, output, nil | ||
| }, | ||
| normalizeTypedReadArguments(nil, false), | ||
| ) | ||
| } | ||
|
|
||
| func marshalLegacyCodeQualityFinding(rawFinding json.RawMessage) ([]byte, error) { | ||
| // Match main's map-based formatting, including key ordering and number encoding. | ||
| var finding map[string]any | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a known potential issue, but from what I can see in other tools we have there's no perfect fix for it. It looks like the metadata wrapping this autofix is suggesting was rejected in other PRs. Notably this tool will only return findings from the default branch of the repository at this time, so any malicious instructions would have to be placed by folks with permission to write code to the repo.