|
| 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 | +func getCodeScanningAlert(client *github.Client) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 15 | + return mcp.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 | + if err != nil { |
| 37 | + return nil, fmt.Errorf("failed to get alert: %w", err) |
| 38 | + } |
| 39 | + defer func() { _ = resp.Body.Close() }() |
| 40 | + |
| 41 | + if resp.StatusCode != 200 { |
| 42 | + body, err := io.ReadAll(resp.Body) |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 45 | + } |
| 46 | + return mcp.NewToolResultError(fmt.Sprintf("failed to get alert: %s", string(body))), nil |
| 47 | + } |
| 48 | + |
| 49 | + r, err := json.Marshal(alert) |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("failed to marshal alert: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + return mcp.NewToolResultText(string(r)), nil |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func listCodeScanningAlerts(client *github.Client) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 59 | + return mcp.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 | + if err != nil { |
| 89 | + return nil, fmt.Errorf("failed to list alerts: %w", err) |
| 90 | + } |
| 91 | + defer func() { _ = resp.Body.Close() }() |
| 92 | + |
| 93 | + if resp.StatusCode != 200 { |
| 94 | + body, err := io.ReadAll(resp.Body) |
| 95 | + if err != nil { |
| 96 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 97 | + } |
| 98 | + return mcp.NewToolResultError(fmt.Sprintf("failed to list alerts: %s", string(body))), nil |
| 99 | + } |
| 100 | + |
| 101 | + r, err := json.Marshal(alerts) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("failed to marshal alerts: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + return mcp.NewToolResultText(string(r)), nil |
| 107 | + } |
| 108 | +} |
0 commit comments