Skip to content

Commit 3588e63

Browse files
Copilotomgitsads
andcommitted
Migrate code-scanning toolset to modelcontextprotocol/go-sdk
Co-authored-by: omgitsads <4619+omgitsads@users.noreply.github.com>
1 parent 87b79b1 commit 3588e63

File tree

4 files changed

+162
-138
lines changed

4 files changed

+162
-138
lines changed
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
{
22
"annotations": {
3-
"title": "Get code scanning alert",
4-
"readOnlyHint": true
3+
"readOnlyHint": true,
4+
"title": "Get code scanning alert"
55
},
66
"description": "Get details of a specific code scanning alert in a GitHub repository.",
77
"inputSchema": {
8+
"type": "object",
9+
"required": [
10+
"owner",
11+
"repo",
12+
"alertNumber"
13+
],
814
"properties": {
915
"alertNumber": {
10-
"description": "The number of the alert.",
11-
"type": "number"
16+
"type": "number",
17+
"description": "The number of the alert."
1218
},
1319
"owner": {
14-
"description": "The owner of the repository.",
15-
"type": "string"
20+
"type": "string",
21+
"description": "The owner of the repository."
1622
},
1723
"repo": {
18-
"description": "The name of the repository.",
19-
"type": "string"
24+
"type": "string",
25+
"description": "The name of the repository."
2026
}
21-
},
22-
"required": [
23-
"owner",
24-
"repo",
25-
"alertNumber"
26-
],
27-
"type": "object"
27+
}
2828
},
2929
"name": "get_code_scanning_alert"
3030
}
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
{
22
"annotations": {
3-
"title": "List code scanning alerts",
4-
"readOnlyHint": true
3+
"readOnlyHint": true,
4+
"title": "List code scanning alerts"
55
},
66
"description": "List code scanning alerts in a GitHub repository.",
77
"inputSchema": {
8+
"type": "object",
9+
"required": [
10+
"owner",
11+
"repo"
12+
],
813
"properties": {
914
"owner": {
10-
"description": "The owner of the repository.",
11-
"type": "string"
15+
"type": "string",
16+
"description": "The owner of the repository."
1217
},
1318
"ref": {
14-
"description": "The Git reference for the results you want to list.",
15-
"type": "string"
19+
"type": "string",
20+
"description": "The Git reference for the results you want to list."
1621
},
1722
"repo": {
18-
"description": "The name of the repository.",
19-
"type": "string"
23+
"type": "string",
24+
"description": "The name of the repository."
2025
},
2126
"severity": {
27+
"type": "string",
2228
"description": "Filter code scanning alerts by severity",
2329
"enum": [
2430
"critical",
@@ -28,30 +34,24 @@
2834
"warning",
2935
"note",
3036
"error"
31-
],
32-
"type": "string"
37+
]
3338
},
3439
"state": {
35-
"default": "open",
40+
"type": "string",
3641
"description": "Filter code scanning alerts by state. Defaults to open",
42+
"default": "open",
3743
"enum": [
3844
"open",
3945
"closed",
4046
"dismissed",
4147
"fixed"
42-
],
43-
"type": "string"
48+
]
4449
},
4550
"tool_name": {
46-
"description": "The name of the tool used for code scanning.",
47-
"type": "string"
51+
"type": "string",
52+
"description": "The name of the tool used for code scanning."
4853
}
49-
},
50-
"required": [
51-
"owner",
52-
"repo"
53-
],
54-
"type": "object"
54+
}
5555
},
5656
"name": "list_code_scanning_alerts"
5757
}

pkg/github/code_scanning.go

Lines changed: 102 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build ignore
2-
31
package github
42

53
import (
@@ -11,48 +9,56 @@ import (
119

1210
ghErrors "github.com/github/github-mcp-server/pkg/errors"
1311
"github.com/github/github-mcp-server/pkg/translations"
12+
"github.com/github/github-mcp-server/pkg/utils"
1413
"github.com/google/go-github/v79/github"
15-
"github.com/mark3labs/mcp-go/mcp"
16-
"github.com/mark3labs/mcp-go/server"
14+
"github.com/google/jsonschema-go/jsonschema"
15+
"github.com/modelcontextprotocol/go-sdk/mcp"
1716
)
1817

19-
func GetCodeScanningAlert(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
20-
return mcp.NewTool("get_code_scanning_alert",
21-
mcp.WithDescription(t("TOOL_GET_CODE_SCANNING_ALERT_DESCRIPTION", "Get details of a specific code scanning alert in a GitHub repository.")),
22-
mcp.WithToolAnnotation(mcp.ToolAnnotation{
18+
func GetCodeScanningAlert(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.Tool, mcp.ToolHandlerFor[map[string]any, any]) {
19+
return mcp.Tool{
20+
Name: "get_code_scanning_alert",
21+
Description: t("TOOL_GET_CODE_SCANNING_ALERT_DESCRIPTION", "Get details of a specific code scanning alert in a GitHub repository."),
22+
Annotations: &mcp.ToolAnnotations{
2323
Title: t("TOOL_GET_CODE_SCANNING_ALERT_USER_TITLE", "Get code scanning alert"),
24-
ReadOnlyHint: ToBoolPtr(true),
25-
}),
26-
mcp.WithString("owner",
27-
mcp.Required(),
28-
mcp.Description("The owner of the repository."),
29-
),
30-
mcp.WithString("repo",
31-
mcp.Required(),
32-
mcp.Description("The name of the repository."),
33-
),
34-
mcp.WithNumber("alertNumber",
35-
mcp.Required(),
36-
mcp.Description("The number of the alert."),
37-
),
38-
),
39-
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
40-
owner, err := RequiredParam[string](request, "owner")
24+
ReadOnlyHint: true,
25+
},
26+
InputSchema: &jsonschema.Schema{
27+
Type: "object",
28+
Properties: map[string]*jsonschema.Schema{
29+
"owner": {
30+
Type: "string",
31+
Description: "The owner of the repository.",
32+
},
33+
"repo": {
34+
Type: "string",
35+
Description: "The name of the repository.",
36+
},
37+
"alertNumber": {
38+
Type: "number",
39+
Description: "The number of the alert.",
40+
},
41+
},
42+
Required: []string{"owner", "repo", "alertNumber"},
43+
},
44+
},
45+
func(ctx context.Context, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
46+
owner, err := RequiredParam[string](args, "owner")
4147
if err != nil {
42-
return mcp.NewToolResultError(err.Error()), nil
48+
return utils.NewToolResultError(err.Error()), nil, nil
4349
}
44-
repo, err := RequiredParam[string](request, "repo")
50+
repo, err := RequiredParam[string](args, "repo")
4551
if err != nil {
46-
return mcp.NewToolResultError(err.Error()), nil
52+
return utils.NewToolResultError(err.Error()), nil, nil
4753
}
48-
alertNumber, err := RequiredInt(request, "alertNumber")
54+
alertNumber, err := RequiredInt(args, "alertNumber")
4955
if err != nil {
50-
return mcp.NewToolResultError(err.Error()), nil
56+
return utils.NewToolResultError(err.Error()), nil, nil
5157
}
5258

5359
client, err := getClient(ctx)
5460
if err != nil {
55-
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
61+
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
5662
}
5763

5864
alert, resp, err := client.CodeScanning.GetAlert(ctx, owner, repo, int64(alertNumber))
@@ -61,111 +67,122 @@ func GetCodeScanningAlert(getClient GetClientFn, t translations.TranslationHelpe
6167
"failed to get alert",
6268
resp,
6369
err,
64-
), nil
70+
), nil, nil
6571
}
6672
defer func() { _ = resp.Body.Close() }()
6773

6874
if resp.StatusCode != http.StatusOK {
6975
body, err := io.ReadAll(resp.Body)
7076
if err != nil {
71-
return nil, fmt.Errorf("failed to read response body: %w", err)
77+
return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil
7278
}
73-
return mcp.NewToolResultError(fmt.Sprintf("failed to get alert: %s", string(body))), nil
79+
return utils.NewToolResultError(fmt.Sprintf("failed to get alert: %s", string(body))), nil, nil
7480
}
7581

7682
r, err := json.Marshal(alert)
7783
if err != nil {
78-
return nil, fmt.Errorf("failed to marshal alert: %w", err)
84+
return utils.NewToolResultErrorFromErr("failed to marshal alert", err), nil, nil
7985
}
8086

81-
return mcp.NewToolResultText(string(r)), nil
87+
return utils.NewToolResultText(string(r)), nil, nil
8288
}
8389
}
8490

85-
func ListCodeScanningAlerts(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
86-
return mcp.NewTool("list_code_scanning_alerts",
87-
mcp.WithDescription(t("TOOL_LIST_CODE_SCANNING_ALERTS_DESCRIPTION", "List code scanning alerts in a GitHub repository.")),
88-
mcp.WithToolAnnotation(mcp.ToolAnnotation{
91+
func ListCodeScanningAlerts(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.Tool, mcp.ToolHandlerFor[map[string]any, any]) {
92+
return mcp.Tool{
93+
Name: "list_code_scanning_alerts",
94+
Description: t("TOOL_LIST_CODE_SCANNING_ALERTS_DESCRIPTION", "List code scanning alerts in a GitHub repository."),
95+
Annotations: &mcp.ToolAnnotations{
8996
Title: t("TOOL_LIST_CODE_SCANNING_ALERTS_USER_TITLE", "List code scanning 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 code scanning alerts by state. Defaults to open"),
102-
mcp.DefaultString("open"),
103-
mcp.Enum("open", "closed", "dismissed", "fixed"),
104-
),
105-
mcp.WithString("ref",
106-
mcp.Description("The Git reference for the results you want to list."),
107-
),
108-
mcp.WithString("severity",
109-
mcp.Description("Filter code scanning alerts by severity"),
110-
mcp.Enum("critical", "high", "medium", "low", "warning", "note", "error"),
111-
),
112-
mcp.WithString("tool_name",
113-
mcp.Description("The name of the tool used for code scanning."),
114-
),
115-
),
116-
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
117-
owner, err := RequiredParam[string](request, "owner")
97+
ReadOnlyHint: true,
98+
},
99+
InputSchema: &jsonschema.Schema{
100+
Type: "object",
101+
Properties: map[string]*jsonschema.Schema{
102+
"owner": {
103+
Type: "string",
104+
Description: "The owner of the repository.",
105+
},
106+
"repo": {
107+
Type: "string",
108+
Description: "The name of the repository.",
109+
},
110+
"state": {
111+
Type: "string",
112+
Description: "Filter code scanning alerts by state. Defaults to open",
113+
Enum: []any{"open", "closed", "dismissed", "fixed"},
114+
Default: json.RawMessage(`"open"`),
115+
},
116+
"ref": {
117+
Type: "string",
118+
Description: "The Git reference for the results you want to list.",
119+
},
120+
"severity": {
121+
Type: "string",
122+
Description: "Filter code scanning alerts by severity",
123+
Enum: []any{"critical", "high", "medium", "low", "warning", "note", "error"},
124+
},
125+
"tool_name": {
126+
Type: "string",
127+
Description: "The name of the tool used for code scanning.",
128+
},
129+
},
130+
Required: []string{"owner", "repo"},
131+
},
132+
},
133+
func(ctx context.Context, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
134+
owner, err := RequiredParam[string](args, "owner")
118135
if err != nil {
119-
return mcp.NewToolResultError(err.Error()), nil
136+
return utils.NewToolResultError(err.Error()), nil, nil
120137
}
121-
repo, err := RequiredParam[string](request, "repo")
138+
repo, err := RequiredParam[string](args, "repo")
122139
if err != nil {
123-
return mcp.NewToolResultError(err.Error()), nil
140+
return utils.NewToolResultError(err.Error()), nil, nil
124141
}
125-
ref, err := OptionalParam[string](request, "ref")
142+
ref, err := OptionalParam[string](args, "ref")
126143
if err != nil {
127-
return mcp.NewToolResultError(err.Error()), nil
144+
return utils.NewToolResultError(err.Error()), nil, nil
128145
}
129-
state, err := OptionalParam[string](request, "state")
146+
state, err := OptionalParam[string](args, "state")
130147
if err != nil {
131-
return mcp.NewToolResultError(err.Error()), nil
148+
return utils.NewToolResultError(err.Error()), nil, nil
132149
}
133-
severity, err := OptionalParam[string](request, "severity")
150+
severity, err := OptionalParam[string](args, "severity")
134151
if err != nil {
135-
return mcp.NewToolResultError(err.Error()), nil
152+
return utils.NewToolResultError(err.Error()), nil, nil
136153
}
137-
toolName, err := OptionalParam[string](request, "tool_name")
154+
toolName, err := OptionalParam[string](args, "tool_name")
138155
if err != nil {
139-
return mcp.NewToolResultError(err.Error()), nil
156+
return utils.NewToolResultError(err.Error()), nil, nil
140157
}
141158

142159
client, err := getClient(ctx)
143160
if err != nil {
144-
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
161+
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
145162
}
146163
alerts, resp, err := client.CodeScanning.ListAlertsForRepo(ctx, owner, repo, &github.AlertListOptions{Ref: ref, State: state, Severity: severity, ToolName: toolName})
147164
if err != nil {
148165
return ghErrors.NewGitHubAPIErrorResponse(ctx,
149166
"failed to list alerts",
150167
resp,
151168
err,
152-
), nil
169+
), nil, nil
153170
}
154171
defer func() { _ = resp.Body.Close() }()
155172

156173
if resp.StatusCode != http.StatusOK {
157174
body, err := io.ReadAll(resp.Body)
158175
if err != nil {
159-
return nil, fmt.Errorf("failed to read response body: %w", err)
176+
return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil
160177
}
161-
return mcp.NewToolResultError(fmt.Sprintf("failed to list alerts: %s", string(body))), nil
178+
return utils.NewToolResultError(fmt.Sprintf("failed to list alerts: %s", string(body))), nil, nil
162179
}
163180

164181
r, err := json.Marshal(alerts)
165182
if err != nil {
166-
return nil, fmt.Errorf("failed to marshal alerts: %w", err)
183+
return utils.NewToolResultErrorFromErr("failed to marshal alerts", err), nil, nil
167184
}
168185

169-
return mcp.NewToolResultText(string(r)), nil
186+
return utils.NewToolResultText(string(r)), nil, nil
170187
}
171188
}

0 commit comments

Comments
 (0)