|
| 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/v72/github" |
| 13 | +"github.com/mark3labs/mcp-go/mcp" |
| 14 | +"github.com/mark3labs/mcp-go/server" |
| 15 | +) |
| 16 | + |
| 17 | +funcGetDependabotAlert(getClientGetClientFn,t translations.TranslationHelperFunc) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 18 | +returnmcp.NewTool( |
| 19 | +"get_dependabot_alert", |
| 20 | +mcp.WithDescription(t("TOOL_GET_DEPENDABOT_ALERT_DESCRIPTION","Get details of a specific dependabot alert in a GitHub repository.")), |
| 21 | +mcp.WithToolAnnotation(mcp.ToolAnnotation{ |
| 22 | +Title:t("TOOL_GET_DEPENDABOT_ALERT_USER_TITLE","Get dependabot alert"), |
| 23 | +ReadOnlyHint:ToBoolPtr(true), |
| 24 | +}), |
| 25 | +mcp.WithString("owner", |
| 26 | +mcp.Required(), |
| 27 | +mcp.Description("The owner of the repository."), |
| 28 | +), |
| 29 | +mcp.WithString("repo", |
| 30 | +mcp.Required(), |
| 31 | +mcp.Description("The name of the repository."), |
| 32 | +), |
| 33 | +mcp.WithNumber("alertNumber", |
| 34 | +mcp.Required(), |
| 35 | +mcp.Description("The number of the alert."), |
| 36 | +), |
| 37 | +), |
| 38 | +func(ctx context.Context,request mcp.CallToolRequest) (*mcp.CallToolResult,error) { |
| 39 | +owner,err:=RequiredParam[string](request,"owner") |
| 40 | +iferr!=nil { |
| 41 | +returnmcp.NewToolResultError(err.Error()),nil |
| 42 | +} |
| 43 | +repo,err:=RequiredParam[string](request,"repo") |
| 44 | +iferr!=nil { |
| 45 | +returnmcp.NewToolResultError(err.Error()),nil |
| 46 | +} |
| 47 | +alertNumber,err:=RequiredInt(request,"alertNumber") |
| 48 | +iferr!=nil { |
| 49 | +returnmcp.NewToolResultError(err.Error()),nil |
| 50 | +} |
| 51 | + |
| 52 | +client,err:=getClient(ctx) |
| 53 | +iferr!=nil { |
| 54 | +returnnil,fmt.Errorf("failed to get GitHub client: %w",err) |
| 55 | +} |
| 56 | + |
| 57 | +alert,resp,err:=client.Dependabot.GetRepoAlert(ctx,owner,repo,alertNumber) |
| 58 | +iferr!=nil { |
| 59 | +returnghErrors.NewGitHubAPIErrorResponse(ctx, |
| 60 | +fmt.Sprintf("failed to get alert with number '%d'",alertNumber), |
| 61 | +resp, |
| 62 | +err, |
| 63 | +),nil |
| 64 | +} |
| 65 | +deferfunc() {_=resp.Body.Close() }() |
| 66 | + |
| 67 | +ifresp.StatusCode!=http.StatusOK { |
| 68 | +body,err:=io.ReadAll(resp.Body) |
| 69 | +iferr!=nil { |
| 70 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 71 | +} |
| 72 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to get alert: %s",string(body))),nil |
| 73 | +} |
| 74 | + |
| 75 | +r,err:=json.Marshal(alert) |
| 76 | +iferr!=nil { |
| 77 | +returnnil,fmt.Errorf("failed to marshal alert: %w",err) |
| 78 | +} |
| 79 | + |
| 80 | +returnmcp.NewToolResultText(string(r)),nil |
| 81 | +} |
| 82 | +} |
| 83 | + |
| 84 | +funcListDependabotAlerts(getClientGetClientFn,t translations.TranslationHelperFunc) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 85 | +returnmcp.NewTool( |
| 86 | +"list_dependabot_alerts", |
| 87 | +mcp.WithDescription(t("TOOL_LIST_DEPENDABOT_ALERTS_DESCRIPTION","List dependabot alerts in a GitHub repository.")), |
| 88 | +mcp.WithToolAnnotation(mcp.ToolAnnotation{ |
| 89 | +Title:t("TOOL_LIST_DEPENDABOT_ALERTS_USER_TITLE","List dependabot alerts"), |
| 90 | +ReadOnlyHint:ToBoolPtr(true), |
| 91 | +}), |
| 92 | +mcp.WithString("owner", |
| 93 | +mcp.Required(), |
| 94 | +mcp.Description("The owner of the repository."), |
| 95 | +), |
| 96 | +mcp.WithString("repo", |
| 97 | +mcp.Required(), |
| 98 | +mcp.Description("The name of the repository."), |
| 99 | +), |
| 100 | +mcp.WithString("state", |
| 101 | +mcp.Description("Filter dependabot alerts by state. Defaults to open"), |
| 102 | +mcp.DefaultString("open"), |
| 103 | +mcp.Enum("open","fixed","dismissed","auto_dismissed"), |
| 104 | +), |
| 105 | +mcp.WithString("severity", |
| 106 | +mcp.Description("Filter dependabot alerts by severity"), |
| 107 | +mcp.Enum("low","medium","high","critical"), |
| 108 | +), |
| 109 | +), |
| 110 | +func(ctx context.Context,request mcp.CallToolRequest) (*mcp.CallToolResult,error) { |
| 111 | +owner,err:=RequiredParam[string](request,"owner") |
| 112 | +iferr!=nil { |
| 113 | +returnmcp.NewToolResultError(err.Error()),nil |
| 114 | +} |
| 115 | +repo,err:=RequiredParam[string](request,"repo") |
| 116 | +iferr!=nil { |
| 117 | +returnmcp.NewToolResultError(err.Error()),nil |
| 118 | +} |
| 119 | +state,err:=OptionalParam[string](request,"state") |
| 120 | +iferr!=nil { |
| 121 | +returnmcp.NewToolResultError(err.Error()),nil |
| 122 | +} |
| 123 | +severity,err:=OptionalParam[string](request,"severity") |
| 124 | +iferr!=nil { |
| 125 | +returnmcp.NewToolResultError(err.Error()),nil |
| 126 | +} |
| 127 | + |
| 128 | +client,err:=getClient(ctx) |
| 129 | +iferr!=nil { |
| 130 | +returnnil,fmt.Errorf("failed to get GitHub client: %w",err) |
| 131 | +} |
| 132 | + |
| 133 | +alerts,resp,err:=client.Dependabot.ListRepoAlerts(ctx,owner,repo,&github.ListAlertsOptions{ |
| 134 | +State:ToStringPtr(state), |
| 135 | +Severity:ToStringPtr(severity), |
| 136 | +}) |
| 137 | +iferr!=nil { |
| 138 | +returnghErrors.NewGitHubAPIErrorResponse(ctx, |
| 139 | +fmt.Sprintf("failed to list alerts for repository '%s/%s'",owner,repo), |
| 140 | +resp, |
| 141 | +err, |
| 142 | +),nil |
| 143 | +} |
| 144 | +deferfunc() {_=resp.Body.Close() }() |
| 145 | + |
| 146 | +ifresp.StatusCode!=http.StatusOK { |
| 147 | +body,err:=io.ReadAll(resp.Body) |
| 148 | +iferr!=nil { |
| 149 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 150 | +} |
| 151 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to list alerts: %s",string(body))),nil |
| 152 | +} |
| 153 | + |
| 154 | +r,err:=json.Marshal(alerts) |
| 155 | +iferr!=nil { |
| 156 | +returnnil,fmt.Errorf("failed to marshal alerts: %w",err) |
| 157 | +} |
| 158 | + |
| 159 | +returnmcp.NewToolResultText(string(r)),nil |
| 160 | +} |
| 161 | +} |