|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + ghErrors "github.com/github/github-mcp-server/pkg/errors" |
| 11 | + "github.com/github/github-mcp-server/pkg/translations" |
| 12 | + "github.com/google/go-github/v77/github" |
| 13 | + "github.com/mark3labs/mcp-go/mcp" |
| 14 | + "github.com/mark3labs/mcp-go/server" |
| 15 | +) |
| 16 | + |
| 17 | +func GetCodeScanningAlert(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 18 | + return mcp.NewTool("get_code_scanning_alert", |
| 19 | + mcp.WithDescription(t("TOOL_GET_CODE_SCANNING_ALERT_DESCRIPTION", "Get details of a specific code scanning alert in a GitHub repository.")), |
| 20 | + mcp.WithToolAnnotation(mcp.ToolAnnotation{ |
| 21 | + Title: t("TOOL_GET_CODE_SCANNING_ALERT_USER_TITLE", "Get code scanning alert"), |
| 22 | + ReadOnlyHint: ToBoolPtr(true), |
| 23 | + }), |
| 24 | + mcp.WithString("owner", |
| 25 | + mcp.Required(), |
| 26 | + mcp.Description("The owner of the repository."), |
| 27 | + ), |
| 28 | + mcp.WithString("repo", |
| 29 | + mcp.Required(), |
| 30 | + mcp.Description("The name of the repository."), |
| 31 | + ), |
| 32 | + mcp.WithNumber("alertNumber", |
| 33 | + mcp.Required(), |
| 34 | + mcp.Description("The number of the alert."), |
| 35 | + ), |
| 36 | + ), |
| 37 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 38 | + owner, err := RequiredParam[string](request, "owner") |
| 39 | + if err != nil { |
| 40 | + return mcp.NewToolResultError(err.Error()), nil |
| 41 | + } |
| 42 | + repo, err := RequiredParam[string](request, "repo") |
| 43 | + if err != nil { |
| 44 | + return mcp.NewToolResultError(err.Error()), nil |
| 45 | + } |
| 46 | + alertNumber, err := RequiredInt(request, "alertNumber") |
| 47 | + if err != nil { |
| 48 | + return mcp.NewToolResultError(err.Error()), nil |
| 49 | + } |
| 50 | + |
| 51 | + client, err := getClient(ctx) |
| 52 | + if err != nil { |
| 53 | + return nil, fmt.Errorf("failed to get GitHub client: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + alert, resp, err := client.CodeScanning.GetAlert(ctx, owner, repo, int64(alertNumber)) |
| 57 | + if err != nil { |
| 58 | + return ghErrors.NewGitHubAPIErrorResponse(ctx, |
| 59 | + "failed to get alert", |
| 60 | + resp, |
| 61 | + err, |
| 62 | + ), nil |
| 63 | + } |
| 64 | + defer func() { _ = resp.Body.Close() }() |
| 65 | + |
| 66 | + if resp.StatusCode != http.StatusOK { |
| 67 | + body, err := io.ReadAll(resp.Body) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 70 | + } |
| 71 | + return mcp.NewToolResultError(fmt.Sprintf("failed to get alert: %s", string(body))), nil |
| 72 | + } |
| 73 | + |
| 74 | + r, err := json.Marshal(alert) |
| 75 | + if err != nil { |
| 76 | + return nil, fmt.Errorf("failed to marshal alert: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + return mcp.NewToolResultText(string(r)), nil |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func ListCodeScanningAlerts(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 84 | + return mcp.NewTool("list_code_scanning_alerts", |
| 85 | + mcp.WithDescription(t("TOOL_LIST_CODE_SCANNING_ALERTS_DESCRIPTION", "List code scanning alerts in a GitHub repository.")), |
| 86 | + mcp.WithToolAnnotation(mcp.ToolAnnotation{ |
| 87 | + Title: t("TOOL_LIST_CODE_SCANNING_ALERTS_USER_TITLE", "List code scanning alerts"), |
| 88 | + ReadOnlyHint: ToBoolPtr(true), |
| 89 | + }), |
| 90 | + mcp.WithString("owner", |
| 91 | + mcp.Required(), |
| 92 | + mcp.Description("The owner of the repository."), |
| 93 | + ), |
| 94 | + mcp.WithString("repo", |
| 95 | + mcp.Required(), |
| 96 | + mcp.Description("The name of the repository."), |
| 97 | + ), |
| 98 | + mcp.WithString("state", |
| 99 | + mcp.Description("Filter code scanning alerts by state. Defaults to open"), |
| 100 | + mcp.DefaultString("open"), |
| 101 | + mcp.Enum("open", "closed", "dismissed", "fixed"), |
| 102 | + ), |
| 103 | + mcp.WithString("ref", |
| 104 | + mcp.Description("The Git reference for the results you want to list."), |
| 105 | + ), |
| 106 | + mcp.WithString("severity", |
| 107 | + mcp.Description("Filter code scanning alerts by severity"), |
| 108 | + mcp.Enum("critical", "high", "medium", "low", "warning", "note", "error"), |
| 109 | + ), |
| 110 | + mcp.WithString("tool_name", |
| 111 | + mcp.Description("The name of the tool used for code scanning."), |
| 112 | + ), |
| 113 | + ), |
| 114 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 115 | + owner, err := RequiredParam[string](request, "owner") |
| 116 | + if err != nil { |
| 117 | + return mcp.NewToolResultError(err.Error()), nil |
| 118 | + } |
| 119 | + repo, err := RequiredParam[string](request, "repo") |
| 120 | + if err != nil { |
| 121 | + return mcp.NewToolResultError(err.Error()), nil |
| 122 | + } |
| 123 | + ref, err := OptionalParam[string](request, "ref") |
| 124 | + if err != nil { |
| 125 | + return mcp.NewToolResultError(err.Error()), nil |
| 126 | + } |
| 127 | + state, err := OptionalParam[string](request, "state") |
| 128 | + if err != nil { |
| 129 | + return mcp.NewToolResultError(err.Error()), nil |
| 130 | + } |
| 131 | + severity, err := OptionalParam[string](request, "severity") |
| 132 | + if err != nil { |
| 133 | + return mcp.NewToolResultError(err.Error()), nil |
| 134 | + } |
| 135 | + toolName, err := OptionalParam[string](request, "tool_name") |
| 136 | + if err != nil { |
| 137 | + return mcp.NewToolResultError(err.Error()), nil |
| 138 | + } |
| 139 | + |
| 140 | + client, err := getClient(ctx) |
| 141 | + if err != nil { |
| 142 | + return nil, fmt.Errorf("failed to get GitHub client: %w", err) |
| 143 | + } |
| 144 | + alerts, resp, err := client.CodeScanning.ListAlertsForRepo(ctx, owner, repo, &github.AlertListOptions{Ref: ref, State: state, Severity: severity, ToolName: toolName}) |
| 145 | + if err != nil { |
| 146 | + return ghErrors.NewGitHubAPIErrorResponse(ctx, |
| 147 | + "failed to list alerts", |
| 148 | + resp, |
| 149 | + err, |
| 150 | + ), nil |
| 151 | + } |
| 152 | + defer func() { _ = resp.Body.Close() }() |
| 153 | + |
| 154 | + if resp.StatusCode != http.StatusOK { |
| 155 | + body, err := io.ReadAll(resp.Body) |
| 156 | + if err != nil { |
| 157 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 158 | + } |
| 159 | + return mcp.NewToolResultError(fmt.Sprintf("failed to list alerts: %s", string(body))), nil |
| 160 | + } |
| 161 | + |
| 162 | + r, err := json.Marshal(alerts) |
| 163 | + if err != nil { |
| 164 | + return nil, fmt.Errorf("failed to marshal alerts: %w", err) |
| 165 | + } |
| 166 | + |
| 167 | + return mcp.NewToolResultText(string(r)), nil |
| 168 | + } |
| 169 | +} |
0 commit comments