|
| 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 | +funcGetCodeScanningAlert(getClientGetClientFn,t translations.TranslationHelperFunc) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 18 | +returnmcp.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 | +iferr!=nil { |
| 40 | +returnmcp.NewToolResultError(err.Error()),nil |
| 41 | +} |
| 42 | +repo,err:=RequiredParam[string](request,"repo") |
| 43 | +iferr!=nil { |
| 44 | +returnmcp.NewToolResultError(err.Error()),nil |
| 45 | +} |
| 46 | +alertNumber,err:=RequiredInt(request,"alertNumber") |
| 47 | +iferr!=nil { |
| 48 | +returnmcp.NewToolResultError(err.Error()),nil |
| 49 | +} |
| 50 | + |
| 51 | +client,err:=getClient(ctx) |
| 52 | +iferr!=nil { |
| 53 | +returnnil,fmt.Errorf("failed to get GitHub client: %w",err) |
| 54 | +} |
| 55 | + |
| 56 | +alert,resp,err:=client.CodeScanning.GetAlert(ctx,owner,repo,int64(alertNumber)) |
| 57 | +iferr!=nil { |
| 58 | +returnghErrors.NewGitHubAPIErrorResponse(ctx, |
| 59 | +"failed to get alert", |
| 60 | +resp, |
| 61 | +err, |
| 62 | +),nil |
| 63 | +} |
| 64 | +deferfunc() {_=resp.Body.Close() }() |
| 65 | + |
| 66 | +ifresp.StatusCode!=http.StatusOK { |
| 67 | +body,err:=io.ReadAll(resp.Body) |
| 68 | +iferr!=nil { |
| 69 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 70 | +} |
| 71 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to get alert: %s",string(body))),nil |
| 72 | +} |
| 73 | + |
| 74 | +r,err:=json.Marshal(alert) |
| 75 | +iferr!=nil { |
| 76 | +returnnil,fmt.Errorf("failed to marshal alert: %w",err) |
| 77 | +} |
| 78 | + |
| 79 | +returnmcp.NewToolResultText(string(r)),nil |
| 80 | +} |
| 81 | +} |
| 82 | + |
| 83 | +funcListCodeScanningAlerts(getClientGetClientFn,t translations.TranslationHelperFunc) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 84 | +returnmcp.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 | +iferr!=nil { |
| 117 | +returnmcp.NewToolResultError(err.Error()),nil |
| 118 | +} |
| 119 | +repo,err:=RequiredParam[string](request,"repo") |
| 120 | +iferr!=nil { |
| 121 | +returnmcp.NewToolResultError(err.Error()),nil |
| 122 | +} |
| 123 | +ref,err:=OptionalParam[string](request,"ref") |
| 124 | +iferr!=nil { |
| 125 | +returnmcp.NewToolResultError(err.Error()),nil |
| 126 | +} |
| 127 | +state,err:=OptionalParam[string](request,"state") |
| 128 | +iferr!=nil { |
| 129 | +returnmcp.NewToolResultError(err.Error()),nil |
| 130 | +} |
| 131 | +severity,err:=OptionalParam[string](request,"severity") |
| 132 | +iferr!=nil { |
| 133 | +returnmcp.NewToolResultError(err.Error()),nil |
| 134 | +} |
| 135 | +toolName,err:=OptionalParam[string](request,"tool_name") |
| 136 | +iferr!=nil { |
| 137 | +returnmcp.NewToolResultError(err.Error()),nil |
| 138 | +} |
| 139 | + |
| 140 | +client,err:=getClient(ctx) |
| 141 | +iferr!=nil { |
| 142 | +returnnil,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 | +iferr!=nil { |
| 146 | +returnghErrors.NewGitHubAPIErrorResponse(ctx, |
| 147 | +"failed to list alerts", |
| 148 | +resp, |
| 149 | +err, |
| 150 | +),nil |
| 151 | +} |
| 152 | +deferfunc() {_=resp.Body.Close() }() |
| 153 | + |
| 154 | +ifresp.StatusCode!=http.StatusOK { |
| 155 | +body,err:=io.ReadAll(resp.Body) |
| 156 | +iferr!=nil { |
| 157 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 158 | +} |
| 159 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to list alerts: %s",string(body))),nil |
| 160 | +} |
| 161 | + |
| 162 | +r,err:=json.Marshal(alerts) |
| 163 | +iferr!=nil { |
| 164 | +returnnil,fmt.Errorf("failed to marshal alerts: %w",err) |
| 165 | +} |
| 166 | + |
| 167 | +returnmcp.NewToolResultText(string(r)),nil |
| 168 | +} |
| 169 | +} |