forked from github/github-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_tools.go
More file actions
555 lines (494 loc) · 16.1 KB
/
Copy pathui_tools.go
File metadata and controls
555 lines (494 loc) · 16.1 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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package github
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"sort"
"strings"
ghErrors "github.com/github/github-mcp-server/pkg/errors"
"github.com/github/github-mcp-server/pkg/inventory"
"github.com/github/github-mcp-server/pkg/scopes"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/github/github-mcp-server/pkg/utils"
"github.com/google/go-github/v89/github"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/shurcooL/githubv4"
)
// uiGetMaxPages bounds how many pages each ui_get pagination loop will fetch.
// ui_get backs a synchronous UI picker (dropdowns for labels/assignees/etc. in
// the MCP App issue/PR write surfaces), so responsiveness matters more than
// completeness. At PerPage 100 this caps a call at ~1000 items and a bounded
// number of API round-trips, keeping latency predictable on very large
// repos/orgs. Results past the cap are truncated and surfaced via a "has_more"
// flag, which is acceptable because the picker pairs truncation with typeahead.
const uiGetMaxPages = 10
// UIGet creates a tool to fetch UI data for MCP Apps.
func UIGet(t translations.TranslationHelperFunc) inventory.ServerTool {
st := NewTool(
ToolsetMetadataContext, // Use context toolset so it's always available
mcp.Tool{
Name: "ui_get",
Description: t("TOOL_UI_GET_DESCRIPTION", "Fetch UI data for MCP Apps (labels, assignees, milestones, issue types, branches, issue fields, reviewers)."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_UI_GET_USER_TITLE", "Get UI data"),
ReadOnlyHint: true,
},
// ui_get only backs MCP App views; declaring app-only visibility keeps
// it out of the agent's tool list while remaining callable by the views
// via tools/call (per the MCP Apps 2026-01-26 spec).
Meta: mcp.Meta{
"ui": map[string]any{
"visibility": []string{"app"},
},
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"method": {
Type: "string",
Enum: []any{"labels", "assignees", "milestones", "issue_types", "branches", "issue_fields", "reviewers"},
Description: "The type of data to fetch",
},
"owner": {
Type: "string",
Description: "Repository owner (required for all methods)",
},
"repo": {
Type: "string",
Description: "Repository name (required for labels, assignees, milestones, branches, issue fields, reviewers)",
},
},
Required: []string{"method", "owner"},
},
},
[]scopes.Scope{scopes.Repo, scopes.ReadOrg},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
method, err := RequiredParam[string](args, "method")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
owner, err := RequiredParam[string](args, "owner")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
switch method {
case "labels":
return uiGetLabels(ctx, deps, args, owner)
case "assignees":
return uiGetAssignees(ctx, deps, args, owner)
case "milestones":
return uiGetMilestones(ctx, deps, args, owner)
case "issue_types":
return uiGetIssueTypes(ctx, deps, owner)
case "branches":
return uiGetBranches(ctx, deps, args, owner)
case "issue_fields":
return uiGetIssueFields(ctx, deps, args, owner)
case "reviewers":
return uiGetReviewers(ctx, deps, args, owner)
default:
return utils.NewToolResultError(fmt.Sprintf("unknown method: %s", method)), nil, nil
}
})
st.FeatureFlagEnable = MCPAppsFeatureFlag
return st
}
func uiGetLabels(ctx context.Context, deps ToolDependencies, args map[string]any, owner string) (*mcp.CallToolResult, any, error) {
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
client, err := deps.GetGQLClient(ctx)
if err != nil {
return nil, nil, fmt.Errorf("failed to get GitHub client: %w", err)
}
var query struct {
Repository struct {
Labels struct {
Nodes []struct {
ID githubv4.ID
Name githubv4.String
Color githubv4.String
Description githubv4.String
}
TotalCount githubv4.Int
PageInfo struct {
HasNextPage githubv4.Boolean
EndCursor githubv4.String
}
} `graphql:"labels(first: 100, after: $cursor)"`
} `graphql:"repository(owner: $owner, name: $repo)"`
}
vars := map[string]any{
"owner": githubv4.String(owner),
"repo": githubv4.String(repo),
"cursor": (*githubv4.String)(nil),
}
labels := make([]map[string]any, 0)
var totalCount int
hasMore := false
for page := 1; ; page++ {
if err := client.Query(ctx, &query, vars); err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "Failed to list labels", err), nil, nil
}
for _, labelNode := range query.Repository.Labels.Nodes {
labels = append(labels, map[string]any{
"id": fmt.Sprintf("%v", labelNode.ID),
"name": string(labelNode.Name),
"color": string(labelNode.Color),
"description": string(labelNode.Description),
})
}
totalCount = int(query.Repository.Labels.TotalCount)
if !query.Repository.Labels.PageInfo.HasNextPage {
break
}
if page >= uiGetMaxPages {
hasMore = true
break
}
vars["cursor"] = githubv4.NewString(query.Repository.Labels.PageInfo.EndCursor)
}
response := map[string]any{
"labels": labels,
"totalCount": totalCount,
"has_more": hasMore,
}
out, err := json.Marshal(response)
if err != nil {
return nil, nil, fmt.Errorf("failed to marshal labels: %w", err)
}
return utils.NewToolResultText(string(out)), nil, nil
}
func uiGetAssignees(ctx context.Context, deps ToolDependencies, args map[string]any, owner string) (*mcp.CallToolResult, any, error) {
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
opts := &github.ListOptions{PerPage: 100}
var allAssignees []*github.User
hasMore := false
for page := 1; ; page++ {
assignees, resp, err := client.Issues.ListAssignees(ctx, owner, repo, opts)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to list assignees", resp, err), nil, nil
}
allAssignees = append(allAssignees, assignees...)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
if resp.NextPage == 0 {
break
}
if page >= uiGetMaxPages {
hasMore = true
break
}
opts.Page = resp.NextPage
}
result := make([]map[string]string, len(allAssignees))
for i, u := range allAssignees {
result[i] = map[string]string{
"login": u.GetLogin(),
"avatar_url": u.GetAvatarURL(),
}
}
out, err := json.Marshal(map[string]any{
"assignees": result,
"totalCount": len(result),
"has_more": hasMore,
})
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal assignees", err), nil, nil
}
return utils.NewToolResultText(string(out)), nil, nil
}
func uiGetMilestones(ctx context.Context, deps ToolDependencies, args map[string]any, owner string) (*mcp.CallToolResult, any, error) {
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
opts := &github.MilestoneListOptions{
State: "open",
ListOptions: github.ListOptions{PerPage: 100},
}
var allMilestones []*github.Milestone
hasMore := false
for page := 1; ; page++ {
milestones, resp, err := client.Issues.ListMilestones(ctx, owner, repo, opts)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to list milestones", resp, err), nil, nil
}
allMilestones = append(allMilestones, milestones...)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
if resp.NextPage == 0 {
break
}
if page >= uiGetMaxPages {
hasMore = true
break
}
opts.Page = resp.NextPage
}
result := make([]map[string]any, len(allMilestones))
for i, m := range allMilestones {
dueOn := ""
if m.DueOn != nil {
dueOn = m.GetDueOn().Format("2006-01-02")
}
result[i] = map[string]any{
"number": m.GetNumber(),
"title": m.GetTitle(),
"description": m.GetDescription(),
"state": m.GetState(),
"open_issues": m.GetOpenIssues(),
"due_on": dueOn,
}
}
out, err := json.Marshal(map[string]any{
"milestones": result,
"totalCount": len(result),
"has_more": hasMore,
})
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal milestones", err), nil, nil
}
return utils.NewToolResultText(string(out)), nil, nil
}
func uiGetIssueTypes(ctx context.Context, deps ToolDependencies, owner string) (*mcp.CallToolResult, any, error) {
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
issueTypes, resp, err := client.Organizations.ListIssueTypes(ctx, owner)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to list issue types", resp, err), nil, nil
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil
}
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to list issue types", resp, body), nil, nil
}
r, err := json.Marshal(issueTypes)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal issue types", err), nil, nil
}
return utils.NewToolResultText(string(r)), nil, nil
}
func uiGetBranches(ctx context.Context, deps ToolDependencies, args map[string]any, owner string) (*mcp.CallToolResult, any, error) {
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
opts := &github.BranchListOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
var allBranches []*github.Branch
hasMore := false
for page := 1; ; page++ {
branches, resp, err := client.Repositories.ListBranches(ctx, owner, repo, opts)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to list branches", resp, err), nil, nil
}
allBranches = append(allBranches, branches...)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
if resp.NextPage == 0 {
break
}
if page >= uiGetMaxPages {
hasMore = true
break
}
opts.Page = resp.NextPage
}
minimalBranches := make([]MinimalBranch, 0, len(allBranches))
for _, branch := range allBranches {
minimalBranches = append(minimalBranches, convertToMinimalBranch(branch))
}
r, err := json.Marshal(map[string]any{
"branches": minimalBranches,
"totalCount": len(minimalBranches),
"has_more": hasMore,
})
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal response", err), nil, nil
}
return utils.NewToolResultText(string(r)), nil, nil
}
func uiGetIssueFields(ctx context.Context, deps ToolDependencies, args map[string]any, owner string) (*mcp.CallToolResult, any, error) {
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
gqlClient, err := deps.GetGQLClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub GraphQL client", err), nil, nil
}
fields, err := fetchIssueFields(ctx, gqlClient, owner, repo)
if err != nil {
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "failed to list issue fields", err), nil, nil
}
return marshalUIGetIssueFields(fields)
}
func marshalUIGetIssueFields(fields []IssueField) (*mcp.CallToolResult, any, error) {
resultFields := make([]map[string]any, 0, len(fields))
for _, field := range fields {
if !uiSupportedIssueFieldDataType(field.DataType) {
continue
}
fieldResult := map[string]any{
"id": field.ID,
"name": field.Name,
"data_type": field.DataType,
"description": field.Description,
}
if field.DataType == "single_select" {
fieldOptions := append([]IssueSingleSelectFieldOption(nil), field.Options...)
sort.SliceStable(fieldOptions, func(i, j int) bool {
left, leftOK := issueFieldOptionPriority(fieldOptions[i])
right, rightOK := issueFieldOptionPriority(fieldOptions[j])
if leftOK != rightOK {
return leftOK
}
return left < right
})
options := make([]map[string]string, 0, len(fieldOptions))
for _, option := range fieldOptions {
options = append(options, map[string]string{
"name": option.Name,
"description": option.Description,
"color": option.Color,
})
}
fieldResult["options"] = options
}
resultFields = append(resultFields, fieldResult)
}
r, err := json.Marshal(map[string]any{
"fields": resultFields,
"totalCount": len(resultFields),
})
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal issue fields", err), nil, nil
}
return utils.NewToolResultText(string(r)), nil, nil
}
func uiSupportedIssueFieldDataType(dataType string) bool {
switch dataType {
case "text", "number", "date", "single_select":
return true
default:
return false
}
}
func issueFieldOptionPriority(option IssueSingleSelectFieldOption) (int, bool) {
if option.Priority == nil {
return 0, false
}
return *option.Priority, true
}
func uiGetReviewers(ctx context.Context, deps ToolDependencies, args map[string]any, owner string) (*mcp.CallToolResult, any, error) {
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
collaboratorOpts := &github.ListCollaboratorsOptions{
Affiliation: "all",
ListOptions: github.ListOptions{PerPage: 100},
}
var allCollaborators []*github.User
hasMore := false
for page := 1; ; page++ {
collaborators, resp, err := client.Repositories.ListCollaborators(ctx, owner, repo, collaboratorOpts)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to list reviewers", resp, err), nil, nil
}
allCollaborators = append(allCollaborators, collaborators...)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
if resp.NextPage == 0 {
break
}
if page >= uiGetMaxPages {
hasMore = true
break
}
collaboratorOpts.Page = resp.NextPage
}
teamOpts := &github.ListOptions{PerPage: 100}
var allTeams []*github.Team
for page := 1; ; page++ {
teams, resp, err := client.Repositories.ListTeams(ctx, owner, repo, teamOpts)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to list reviewer teams", resp, err), nil, nil
}
allTeams = append(allTeams, teams...)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
if resp.NextPage == 0 {
break
}
if page >= uiGetMaxPages {
hasMore = true
break
}
teamOpts.Page = resp.NextPage
}
users := make([]map[string]string, 0, len(allCollaborators))
for _, user := range allCollaborators {
login := user.GetLogin()
if user.GetType() == "Bot" || strings.HasSuffix(login, "[bot]") {
continue
}
users = append(users, map[string]string{
"login": login,
"avatar_url": user.GetAvatarURL(),
})
}
teams := make([]map[string]string, len(allTeams))
for i, team := range allTeams {
teams[i] = map[string]string{
"slug": team.GetSlug(),
"name": team.GetName(),
"org": owner,
}
}
r, err := json.Marshal(map[string]any{
"users": users,
"teams": teams,
"totalCount": len(users) + len(teams),
"has_more": hasMore,
})
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal reviewers", err), nil, nil
}
return utils.NewToolResultText(string(r)), nil, nil
}