8000 feat: add basic code scanning methods · github/github-mcp-server@81cc812 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81cc812

Browse files
feat: add basic code scanning methods
1 parent 62b20f7 commit 81cc812

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ and set it as the GITHUB_PERSONAL_ACCESS_TOKEN environment variable.
166166
- `page`: Page number (number, optional)
167167
- `per_page`: Results per page (number, optional)
168168

169+
### Code Scanning
170+
171+
- **get_code_scanning_alert** - Get a code scanning alert
172+
173+
- `owner`: Repository owner (string, required)
174+
- `repo`: Repository name (string, required)
175+
- `alert_number`: Alert number (number, required)
176+
177+
- **list_code_scanning_alerts** - List code scanning alerts for a repository
178+
- `owner`: Repository owner (string, required)
179+
- `repo`: Repository name (string, required)
180+
- `ref`: Git reference (string, optional)
181+
- `state`: Alert state (string, optional)
182+
- `severity`: Alert severity (string, optional)
183+
169184
## Standard input/output server
170185

171186
```sh

pkg/github/code_scanning.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

pkg/github/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func NewServer(client *github.Client) *server.MCPServer {
5151
// Add GitHub tools - Users
5252
s.AddTool(getMe(client))
5353

54+
// Add GitHub tools - Code Scanning
55+
s.AddTool(getCodeScanningAlert(client))
56+
s.AddTool(listCodeScanningAlerts(client))
57+
5458
return s
5559
}
5660

0 commit comments

Comments
 (0)
0