-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathtools.go
More file actions
446 lines (402 loc) · 12.8 KB
/
tools.go
File metadata and controls
446 lines (402 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package github
import (
"context"
"slices"
"strings"
"github.com/github/github-mcp-server/pkg/inventory"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/google/go-github/v82/github"
"github.com/shurcooL/githubv4"
)
type GetClientFn func(context.Context) (*github.Client, error)
type GetGQLClientFn func(context.Context) (*githubv4.Client, error)
// Toolset metadata constants - these define all available toolsets and their descriptions.
// Tools use these constants to declare which toolset they belong to.
// Icons are Octicon names from https://primer.style/foundations/icons
var (
ToolsetMetadataAll = inventory.ToolsetMetadata{
ID: "all",
Description: "Special toolset that enables all available toolsets",
Icon: "apps",
}
ToolsetMetadataDefault = inventory.ToolsetMetadata{
ID: "default",
Description: "Special toolset that enables the default toolset configuration. When no toolsets are specified, this is the set that is enabled",
Icon: "check-circle",
}
ToolsetMetadataContext = inventory.ToolsetMetadata{
ID: "context",
Description: "Tools that provide context about the current user and GitHub context you are operating in",
Default: true,
Icon: "person",
InstructionsFunc: generateContextToolsetInstructions,
}
ToolsetMetadataRepos = inventory.ToolsetMetadata{
ID: "repos",
Description: "GitHub Repository related tools",
Default: true,
Icon: "repo",
}
ToolsetMetadataGit = inventory.ToolsetMetadata{
ID: "git",
Description: "GitHub Git API related tools for low-level Git operations",
Icon: "git-branch",
}
ToolsetMetadataIssues = inventory.ToolsetMetadata{
ID: "issues",
Description: "GitHub Issues related tools",
Default: true,
Icon: "issue-opened",
InstructionsFunc: generateIssuesToolsetInstructions,
}
ToolsetMetadataPullRequests = inventory.ToolsetMetadata{
ID: "pull_requests",
Description: "GitHub Pull Request related tools",
Default: true,
Icon: "git-pull-request",
InstructionsFunc: generatePullRequestsToolsetInstructions,
}
ToolsetMetadataUsers = inventory.ToolsetMetadata{
ID: "users",
Description: "GitHub User related tools",
Default: true,
Icon: "people",
}
ToolsetMetadataOrgs = inventory.ToolsetMetadata{
ID: "orgs",
Description: "GitHub Organization related tools",
Icon: "organization",
}
ToolsetMetadataActions = inventory.ToolsetMetadata{
ID: "actions",
Description: "GitHub Actions workflows and CI/CD operations",
Icon: "workflow",
}
ToolsetMetadataCodeSecurity = inventory.ToolsetMetadata{
ID: "code_security",
Description: "Code security related tools, such as GitHub Code Scanning",
Icon: "codescan",
}
ToolsetMetadataSecretProtection = inventory.ToolsetMetadata{
ID: "secret_protection",
Description: "Secret protection related tools, such as GitHub Secret Scanning",
Icon: "shield-lock",
}
ToolsetMetadataDependabot = inventory.ToolsetMetadata{
ID: "dependabot",
Description: "Dependabot tools",
Icon: "dependabot",
}
ToolsetMetadataNotifications = inventory.ToolsetMetadata{
ID: "notifications",
Description: "GitHub Notifications related tools",
Icon: "bell",
}
ToolsetMetadataDiscussions = inventory.ToolsetMetadata{
ID: "discussions",
Description: "GitHub Discussions related tools",
Icon: "comment-discussion",
InstructionsFunc: generateDiscussionsToolsetInstructions,
}
ToolsetMetadataGists = inventory.ToolsetMetadata{
ID: "gists",
Description: "GitHub Gist related tools",
Icon: "logo-gist",
}
ToolsetMetadataSecurityAdvisories = inventory.ToolsetMetadata{
ID: "security_advisories",
Description: "Security advisories related tools",
Icon: "shield",
}
ToolsetMetadataProjects = inventory.ToolsetMetadata{
ID: "projects",
Description: "GitHub Projects related tools",
Icon: "project",
InstructionsFunc: generateProjectsToolsetInstructions,
}
ToolsetMetadataStargazers = inventory.ToolsetMetadata{
ID: "stargazers",
Description: "GitHub Stargazers related tools",
Icon: "star",
}
ToolsetMetadataDynamic = inventory.ToolsetMetadata{
ID: "dynamic",
Description: "Discover GitHub MCP tools that can help achieve tasks by enabling additional sets of tools, you can control the enablement of any toolset to access its tools when this toolset is enabled.",
Icon: "tools",
}
ToolsetLabels = inventory.ToolsetMetadata{
ID: "labels",
Description: "GitHub Labels related tools",
Icon: "tag",
}
ToolsetMetadataCopilot = inventory.ToolsetMetadata{
ID: "copilot",
Description: "Copilot related tools",
Default: true,
Icon: "copilot",
}
// Remote-only toolsets - these are only available in the remote MCP server
// but are documented here for consistency and to enable automated documentation.
ToolsetMetadataCopilotSpaces = inventory.ToolsetMetadata{
ID: "copilot_spaces",
Description: "Copilot Spaces tools",
Icon: "copilot",
}
ToolsetMetadataSupportSearch = inventory.ToolsetMetadata{
ID: "github_support_docs_search",
Description: "Retrieve documentation to answer GitHub product and support questions. Topics include: GitHub Actions Workflows, Authentication, ...",
Icon: "book",
}
)
// AllTools returns all tools with their embedded toolset metadata.
// Tool functions return ServerTool directly with toolset info.
func AllTools(t translations.TranslationHelperFunc) []inventory.ServerTool {
return []inventory.ServerTool{
// Context tools
GetMe(t),
GetTeams(t),
GetTeamMembers(t),
// Repository tools
SearchRepositories(t),
GetFileContents(t),
ListCommits(t),
SearchCode(t),
GetCommit(t),
ListBranches(t),
ListTags(t),
GetTag(t),
ListReleases(t),
GetLatestRelease(t),
GetReleaseByTag(t),
CreateOrUpdateFile(t),
CreateRepository(t),
ForkRepository(t),
CreateBranch(t),
PushFiles(t),
DeleteFile(t),
ListStarredRepositories(t),
StarRepository(t),
UnstarRepository(t),
// Git tools
GetRepositoryTree(t),
// Issue tools
IssueRead(t),
SearchIssues(t),
ListIssues(t),
ListIssueTypes(t),
IssueWrite(t),
AddIssueComment(t),
SubIssueWrite(t),
// User tools
SearchUsers(t),
// Organization tools
SearchOrgs(t),
// Pull request tools
PullRequestRead(t),
ListPullRequests(t),
SearchPullRequests(t),
MergePullRequest(t),
UpdatePullRequestBranch(t),
CreatePullRequest(t),
UpdatePullRequest(t),
PullRequestReviewWrite(t),
AddCommentToPendingReview(t),
AddReplyToPullRequestComment(t),
// Copilot tools
AssignCopilotToIssue(t),
RequestCopilotReview(t),
// Code security tools
GetCodeScanningAlert(t),
ListCodeScanningAlerts(t),
// Secret protection tools
GetSecretScanningAlert(t),
ListSecretScanningAlerts(t),
// Dependabot tools
GetDependabotAlert(t),
ListDependabotAlerts(t),
// Notification tools
ListNotifications(t),
GetNotificationDetails(t),
DismissNotification(t),
MarkAllNotificationsRead(t),
ManageNotificationSubscription(t),
ManageRepositoryNotificationSubscription(t),
// Discussion tools
ListDiscussions(t),
GetDiscussion(t),
GetDiscussionComments(t),
ListDiscussionCategories(t),
// Actions tools
ActionsList(t),
ActionsGet(t),
ActionsRunTrigger(t),
ActionsGetJobLogs(t),
// Security advisories tools
ListGlobalSecurityAdvisories(t),
GetGlobalSecurityAdvisory(t),
ListRepositorySecurityAdvisories(t),
ListOrgRepositorySecurityAdvisories(t),
// Gist tools
ListGists(t),
GetGist(t),
CreateGist(t),
UpdateGist(t),
// Project tools
ProjectsList(t),
ProjectsGet(t),
ProjectsWrite(t),
// Label tools
GetLabel(t),
GetLabelForLabelsToolset(t),
ListLabels(t),
LabelWrite(t),
}
}
// ToBoolPtr converts a bool to a *bool pointer.
func ToBoolPtr(b bool) *bool {
return &b
}
// ToStringPtr converts a string to a *string pointer.
// Returns nil if the string is empty.
func ToStringPtr(s string) *string {
if s == "" {
return nil
}
return &s
}
// GenerateToolsetsHelp generates the help text for the toolsets flag
func GenerateToolsetsHelp() string {
// Get toolset group to derive defaults and available toolsets
// Build() can only fail if WithTools specifies invalid tools - not used here
r, _ := NewInventory(stubTranslator).Build()
// Format default tools from metadata using strings.Builder
var defaultBuf strings.Builder
defaultIDs := r.DefaultToolsetIDs()
for i, id := range defaultIDs {
if i > 0 {
defaultBuf.WriteString(", ")
}
defaultBuf.WriteString(string(id))
}
// Get all available toolsets (excludes context and dynamic for display)
allToolsets := r.AvailableToolsets("context", "dynamic")
var availableBuf strings.Builder
const maxLineLength = 70
currentLine := ""
for i, toolset := range allToolsets {
id := string(toolset.ID)
switch {
case i == 0:
currentLine = id
case len(currentLine)+len(id)+2 <= maxLineLength:
currentLine += ", " + id
default:
if availableBuf.Len() > 0 {
availableBuf.WriteString(",\n\t ")
}
availableBuf.WriteString(currentLine)
currentLine = id
}
}
if currentLine != "" {
if availableBuf.Len() > 0 {
availableBuf.WriteString(",\n\t ")
}
availableBuf.WriteString(currentLine)
}
// Build the complete help text using strings.Builder
var buf strings.Builder
buf.WriteString("Comma-separated list of tool groups to enable (no spaces).\n")
buf.WriteString("Available: ")
buf.WriteString(availableBuf.String())
buf.WriteString("\n")
buf.WriteString("Special toolset keywords:\n")
buf.WriteString(" - all: Enables all available toolsets\n")
buf.WriteString(" - default: Enables the default toolset configuration of:\n\t ")
buf.WriteString(defaultBuf.String())
buf.WriteString("\n")
buf.WriteString("Examples:\n")
buf.WriteString(" - --toolsets=actions,gists,notifications\n")
buf.WriteString(" - Default + additional: --toolsets=default,actions,gists\n")
buf.WriteString(" - All tools: --toolsets=all")
return buf.String()
}
// stubTranslator is a passthrough translator for cases where we need an Inventory
// but don't need actual translations (e.g., getting toolset IDs for CLI help).
func stubTranslator(_, fallback string) string { return fallback }
// AddDefaultToolset removes the default toolset and expands it to the actual default toolset IDs
func AddDefaultToolset(result []string) []string {
hasDefault := false
seen := make(map[string]bool)
for _, toolset := range result {
seen[toolset] = true
if toolset == string(ToolsetMetadataDefault.ID) {
hasDefault = true
}
}
// Only expand if "default" keyword was found
if !hasDefault {
return result
}
result = RemoveToolset(result, string(ToolsetMetadataDefault.ID))
// Get default toolset IDs from the Inventory
// Build() can only fail if WithTools specifies invalid tools - not used here
r, _ := NewInventory(stubTranslator).Build()
for _, id := range r.DefaultToolsetIDs() {
if !seen[string(id)] {
result = append(result, string(id))
}
}
return result
}
func RemoveToolset(tools []string, toRemove string) []string {
result := make([]string, 0, len(tools))
for _, tool := range tools {
if tool != toRemove {
result = append(result, tool)
}
}
return result
}
func ContainsToolset(tools []string, toCheck string) bool {
return slices.Contains(tools, toCheck)
}
// CleanTools cleans tool names by removing duplicates and trimming whitespace.
// Validation of tool existence is done during registration.
func CleanTools(toolNames []string) []string {
seen := make(map[string]bool)
result := make([]string, 0, len(toolNames))
// Remove duplicates and trim whitespace
for _, tool := range toolNames {
trimmed := strings.TrimSpace(tool)
if trimmed == "" {
continue
}
if !seen[trimmed] {
seen[trimmed] = true
result = append(result, trimmed)
}
}
return result
}
// GetDefaultToolsetIDs returns the IDs of toolsets marked as Default.
// This is a convenience function that builds an inventory to determine defaults.
func GetDefaultToolsetIDs() []string {
// Build() can only fail if WithTools specifies invalid tools - not used here
r, _ := NewInventory(stubTranslator).Build()
ids := r.DefaultToolsetIDs()
result := make([]string, len(ids))
for i, id := range ids {
result[i] = string(id)
}
return result
}
// RemoteOnlyToolsets returns toolset metadata for toolsets that are only
// available in the remote MCP server. These are documented but not registered
// in the local server.
func RemoteOnlyToolsets() []inventory.ToolsetMetadata {
return []inventory.ToolsetMetadata{
ToolsetMetadataCopilotSpaces,
ToolsetMetadataSupportSearch,
}
}