|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"encoding/json" |
| 6 | +"fmt" |
| 7 | +"io" |
| 8 | + |
| 9 | +"github.com/google/go-github/v69/github" |
| 10 | +"github.com/mark3labs/mcp-go/mcp" |
| 11 | +"github.com/mark3labs/mcp-go/server" |
| 12 | +) |
| 13 | + |
| 14 | +funcgetCodeScanningAlert(client*github.Client) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 15 | +returnmcp.NewTool("get_code_scanning_alert", |
| 16 | +mcp.WithDescription("Get details of a specific code scanning alert in a GitHub repository."), |
| 17 | +mcp.WithString("owner", |
| 18 | +mcp.Required(), |
| 19 | +mcp.Description("The owner of the repository."), |
| 20 | +), |
| 21 | +mcp.WithString("repo", |
| 22 | +mcp.Required(), |
| 23 | +mcp.Description("The name of the repository."), |
| 24 | +), |
| 25 | +mcp.WithNumber("alert_number", |
| 26 | +mcp.Required(), |
| 27 | +mcp.Description("The number of the alert."), |
| 28 | +), |
| 29 | +), |
| 30 | +func(ctx context.Context,request mcp.CallToolRequest) (*mcp.CallToolResult,error) { |
| 31 | +owner,_:=request.Params.Arguments["owner"].(string) |
| 32 | +repo,_:=request.Params.Arguments["repo"].(string) |
| 33 | +alertNumber,_:=request.Params.Arguments["alert_number"].(float64) |
| 34 | + |
| 35 | +alert,resp,err:=client.CodeScanning.GetAlert(ctx,owner,repo,int64(alertNumber)) |
| 36 | +iferr!=nil { |
| 37 | +returnnil,fmt.Errorf("failed to get alert: %w",err) |
| 38 | +} |
| 39 | +deferfunc() {_=resp.Body.Close() }() |
| 40 | + |
| 41 | +ifresp.StatusCode!=200 { |
| 42 | +body,err:=io.ReadAll(resp.Body) |
| 43 | +iferr!=nil { |
| 44 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 45 | +} |
| 46 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to get alert: %s",string(body))),nil |
| 47 | +} |
| 48 | + |
| 49 | +r,err:=json.Marshal(alert) |
| 50 | +iferr!=nil { |
| 51 | +returnnil,fmt.Errorf("failed to marshal alert: %w",err) |
| 52 | +} |
| 53 | + |
| 54 | +returnmcp.NewToolResultText(string(r)),nil |
| 55 | +} |
| 56 | +} |
| 57 | + |
| 58 | +funclistCodeScanningAlerts(client*github.Client) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 59 | +returnmcp.NewTool("list_code_scanning_alerts", |
| 60 | +mcp.WithDescription("List code scanning alerts in a GitHub repository."), |
| 61 | +mcp.WithString("owner", |
| 62 | +mcp.Required(), |
| 63 | +mcp.Description("The owner of the repository."), |
| 64 | +), |
| 65 | +mcp.WithString("repo", |
| 66 | +mcp.Required(), |
| 67 | +mcp.Description("The name of the repository."), |
| 68 | +), |
| 69 | +mcp.WithString("ref", |
| 70 | +mcp.Description("The Git reference for the results you want to list."), |
| 71 | +), |
| 72 | +mcp.WithString("state", |
| 73 | +mcp.Description("State of the code scanning alerts to list. Set to closed to list only closed code scanning alerts. Default: open"), |
| 74 | +mcp.DefaultString("open"), |
| 75 | +), |
| 76 | +mcp.WithString("severity", |
| 77 | +mcp.Description("Only code scanning alerts with this severity will be returned. Possible values are: critical, high, medium, low, warning, note, error."), |
| 78 | +), |
| 79 | +), |
| 80 | +func(ctx context.Context,request mcp.CallToolRequest) (*mcp.CallToolResult,error) { |
| 81 | +owner,_:=request.Params.Arguments["owner"].(string) |
| 82 | +repo,_:=request.Params.Arguments["repo"].(string) |
| 83 | +ref,_:=request.Params.Arguments["ref"].(string) |
| 84 | +state,_:=request.Params.Arguments["state"].(string) |
| 85 | +severity,_:=request.Params.Arguments["severity"].(string) |
| 86 | + |
| 87 | +alerts,resp,err:=client.CodeScanning.ListAlertsForRepo(ctx,owner,repo,&github.AlertListOptions{Ref:ref,State:state,Severity:severity}) |
| 88 | +iferr!=nil { |
| 89 | +returnnil,fmt.Errorf("failed to list alerts: %w",err) |
| 90 | +} |
| 91 | +deferfunc() {_=resp.Body.Close() }() |
| 92 | + |
| 93 | +ifresp.StatusCode!=200 { |
| 94 | +body,err:=io.ReadAll(resp.Body) |
| 95 | +iferr!=nil { |
| 96 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 97 | +} |
| 98 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to list alerts: %s",string(body))),nil |
| 99 | +} |
| 100 | + |
| 101 | +r,err:=json.Marshal(alerts) |
| 102 | +iferr!=nil { |
| 103 | +returnnil,fmt.Errorf("failed to marshal alerts: %w",err) |
| 104 | +} |
| 105 | + |
| 106 | +returnmcp.NewToolResultText(string(r)),nil |
| 107 | +} |
| 108 | +} |