-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathnotifications.go
More file actions
603 lines (547 loc) · 21.7 KB
/
notifications.go
File metadata and controls
603 lines (547 loc) · 21.7 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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
package github
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
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/v82/github"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
const (
FilterDefault = "default"
FilterIncludeRead = "include_read_notifications"
FilterOnlyParticipating = "only_participating"
)
// ListNotifications creates a tool to list notifications for the current user.
func ListNotifications(t translations.TranslationHelperFunc) inventory.ServerTool {
return NewTool(
ToolsetMetadataNotifications,
mcp.Tool{
Name: "list_notifications",
Description: t("TOOL_LIST_NOTIFICATIONS_DESCRIPTION", "Lists all GitHub notifications for the authenticated user, including unread notifications, mentions, review requests, assignments, and updates on issues or pull requests. Use this tool whenever the user asks what to work on next, requests a summary of their GitHub activity, wants to see pending reviews, or needs to check for new updates or tasks. This tool is the primary way to discover actionable items, reminders, and outstanding work on GitHub. Always call this tool when asked what to work on next, what is pending, or what needs attention in GitHub."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_LIST_NOTIFICATIONS_USER_TITLE", "List notifications"),
ReadOnlyHint: true,
},
InputSchema: WithPagination(&jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"filter": {
Type: "string",
Description: "Filter notifications to, use default unless specified. Read notifications are ones that have already been acknowledged by the user. Participating notifications are those that the user is directly involved in, such as issues or pull requests they have commented on or created.",
Enum: []any{FilterDefault, FilterIncludeRead, FilterOnlyParticipating},
},
"since": {
Type: "string",
Description: "Only show notifications updated after the given time (ISO 8601 format)",
},
"before": {
Type: "string",
Description: "Only show notifications updated before the given time (ISO 8601 format)",
},
"owner": {
Type: "string",
Description: "Optional repository owner. If provided with repo, only notifications for this repository are listed.",
},
"repo": {
Type: "string",
Description: "Optional repository name. If provided with owner, only notifications for this repository are listed.",
},
},
}),
},
[]scopes.Scope{scopes.Notifications},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
filter, err := OptionalParam[string](args, "filter")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
since, err := OptionalParam[string](args, "since")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
before, err := OptionalParam[string](args, "before")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
owner, err := OptionalParam[string](args, "owner")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
repo, err := OptionalParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
paginationParams, err := OptionalPaginationParams(args)
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
// Build options
opts := &github.NotificationListOptions{
All: filter == FilterIncludeRead,
Participating: filter == FilterOnlyParticipating,
ListOptions: github.ListOptions{
Page: paginationParams.Page,
PerPage: paginationParams.PerPage,
},
}
// Parse time parameters if provided
if since != "" {
sinceTime, err := time.Parse(time.RFC3339, since)
if err != nil {
return utils.NewToolResultError(fmt.Sprintf("invalid since time format, should be RFC3339/ISO8601: %v", err)), nil, nil
}
opts.Since = sinceTime
}
if before != "" {
beforeTime, err := time.Parse(time.RFC3339, before)
if err != nil {
return utils.NewToolResultError(fmt.Sprintf("invalid before time format, should be RFC3339/ISO8601: %v", err)), nil, nil
}
opts.Before = beforeTime
}
var notifications []*github.Notification
var resp *github.Response
if owner != "" && repo != "" {
notifications, resp, err = client.Activity.ListRepositoryNotifications(ctx, owner, repo, opts)
} else {
notifications, resp, err = client.Activity.ListNotifications(ctx, opts)
}
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
"failed to list notifications",
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 get notifications", resp, body), nil, nil
}
// Marshal response to JSON
r, err := json.Marshal(notifications)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal response", err), nil, nil
}
return utils.NewToolResultText(string(r)), nil, nil
},
)
}
// DismissNotification creates a tool to mark a notification as read/done.
func DismissNotification(t translations.TranslationHelperFunc) inventory.ServerTool {
return NewTool(
ToolsetMetadataNotifications,
mcp.Tool{
Name: "dismiss_notification",
Description: t("TOOL_DISMISS_NOTIFICATION_DESCRIPTION", "Dismiss a notification by marking it as read or done"),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_DISMISS_NOTIFICATION_USER_TITLE", "Dismiss notification"),
ReadOnlyHint: false,
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"threadID": {
Type: "string",
Description: "The ID of the notification thread",
},
"state": {
Type: "string",
Description: "The new state of the notification (read/done)",
Enum: []any{"read", "done"},
},
},
Required: []string{"threadID", "state"},
},
},
[]scopes.Scope{scopes.Notifications},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
threadID, err := RequiredParam[string](args, "threadID")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
state, err := RequiredParam[string](args, "state")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
var resp *github.Response
switch state {
case "done":
// for some inexplicable reason, the API seems to have threadID as int64 and string depending on the endpoint
var threadIDInt int64
threadIDInt, err = strconv.ParseInt(threadID, 10, 64)
if err != nil {
return utils.NewToolResultError(fmt.Sprintf("invalid threadID format: %v", err)), nil, nil
}
resp, err = client.Activity.MarkThreadDone(ctx, threadIDInt)
case "read":
resp, err = client.Activity.MarkThreadRead(ctx, threadID)
default:
return utils.NewToolResultError("Invalid state. Must be one of: read, done."), nil, nil
}
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
fmt.Sprintf("failed to mark notification as %s", state),
resp,
err,
), nil, nil
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusResetContent && resp.StatusCode != http.StatusNoContent && 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, fmt.Sprintf("failed to mark notification as %s", state), resp, body), nil, nil
}
return utils.NewToolResultText(fmt.Sprintf("Notification marked as %s", state)), nil, nil
},
)
}
// MarkAllNotificationsRead creates a tool to mark all notifications as read.
func MarkAllNotificationsRead(t translations.TranslationHelperFunc) inventory.ServerTool {
return NewTool(
ToolsetMetadataNotifications,
mcp.Tool{
Name: "mark_all_notifications_read",
Description: t("TOOL_MARK_ALL_NOTIFICATIONS_READ_DESCRIPTION", "Mark all notifications as read"),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_MARK_ALL_NOTIFICATIONS_READ_USER_TITLE", "Mark all notifications as read"),
ReadOnlyHint: false,
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"lastReadAt": {
Type: "string",
Description: "Describes the last point that notifications were checked (optional). Default: Now",
},
"owner": {
Type: "string",
Description: "Optional repository owner. If provided with repo, only notifications for this repository are marked as read.",
},
"repo": {
Type: "string",
Description: "Optional repository name. If provided with owner, only notifications for this repository are marked as read.",
},
},
},
},
[]scopes.Scope{scopes.Notifications},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
lastReadAt, err := OptionalParam[string](args, "lastReadAt")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
owner, err := OptionalParam[string](args, "owner")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
repo, err := OptionalParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
var lastReadTime time.Time
if lastReadAt != "" {
lastReadTime, err = time.Parse(time.RFC3339, lastReadAt)
if err != nil {
return utils.NewToolResultError(fmt.Sprintf("invalid lastReadAt time format, should be RFC3339/ISO8601: %v", err)), nil, nil
}
} else {
lastReadTime = time.Now()
}
markReadOptions := github.Timestamp{
Time: lastReadTime,
}
var resp *github.Response
if owner != "" && repo != "" {
resp, err = client.Activity.MarkRepositoryNotificationsRead(ctx, owner, repo, markReadOptions)
} else {
resp, err = client.Activity.MarkNotificationsRead(ctx, markReadOptions)
}
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
"failed to mark all notifications as read",
resp,
err,
), nil, nil
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusResetContent && 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 mark all notifications as read", resp, body), nil, nil
}
return utils.NewToolResultText("All notifications marked as read"), nil, nil
},
)
}
// GetNotificationDetails creates a tool to get details for a specific notification.
func GetNotificationDetails(t translations.TranslationHelperFunc) inventory.ServerTool {
return NewTool(
ToolsetMetadataNotifications,
mcp.Tool{
Name: "get_notification_details",
Description: t("TOOL_GET_NOTIFICATION_DETAILS_DESCRIPTION", "Get detailed information for a specific GitHub notification, always call this tool when the user asks for details about a specific notification, if you don't know the ID list notifications first."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_GET_NOTIFICATION_DETAILS_USER_TITLE", "Get notification details"),
ReadOnlyHint: true,
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"notificationID": {
Type: "string",
Description: "The ID of the notification",
},
},
Required: []string{"notificationID"},
},
},
[]scopes.Scope{scopes.Notifications},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
notificationID, err := RequiredParam[string](args, "notificationID")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
thread, resp, err := client.Activity.GetThread(ctx, notificationID)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
fmt.Sprintf("failed to get notification details for ID '%s'", notificationID),
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 get notification details", resp, body), nil, nil
}
r, err := json.Marshal(thread)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal response", err), nil, nil
}
return utils.NewToolResultText(string(r)), nil, nil
},
)
}
// Enum values for ManageNotificationSubscription action
const (
NotificationActionIgnore = "ignore"
NotificationActionWatch = "watch"
NotificationActionDelete = "delete"
)
// ManageNotificationSubscription creates a tool to manage a notification subscription (ignore, watch, delete)
func ManageNotificationSubscription(t translations.TranslationHelperFunc) inventory.ServerTool {
return NewTool(
ToolsetMetadataNotifications,
mcp.Tool{
Name: "manage_notification_subscription",
Description: t("TOOL_MANAGE_NOTIFICATION_SUBSCRIPTION_DESCRIPTION", "Manage a notification subscription: ignore, watch, or delete a notification thread subscription."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_MANAGE_NOTIFICATION_SUBSCRIPTION_USER_TITLE", "Manage notification subscription"),
ReadOnlyHint: false,
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"notificationID": {
Type: "string",
Description: "The ID of the notification thread.",
},
"action": {
Type: "string",
Description: "Action to perform: ignore, watch, or delete the notification subscription.",
Enum: []any{NotificationActionIgnore, NotificationActionWatch, NotificationActionDelete},
},
},
Required: []string{"notificationID", "action"},
},
},
[]scopes.Scope{scopes.Notifications},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
notificationID, err := RequiredParam[string](args, "notificationID")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
action, err := RequiredParam[string](args, "action")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
var (
resp *github.Response
result any
apiErr error
)
switch action {
case NotificationActionIgnore:
sub := &github.Subscription{Ignored: ToBoolPtr(true)}
result, resp, apiErr = client.Activity.SetThreadSubscription(ctx, notificationID, sub)
case NotificationActionWatch:
sub := &github.Subscription{Ignored: ToBoolPtr(false), Subscribed: ToBoolPtr(true)}
result, resp, apiErr = client.Activity.SetThreadSubscription(ctx, notificationID, sub)
case NotificationActionDelete:
resp, apiErr = client.Activity.DeleteThreadSubscription(ctx, notificationID)
default:
return utils.NewToolResultError("Invalid action. Must be one of: ignore, watch, delete."), nil, nil
}
if apiErr != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
fmt.Sprintf("failed to %s notification subscription", action),
resp,
apiErr,
), nil, nil
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, fmt.Sprintf("failed to %s notification subscription", action), resp, body), nil, nil
}
if action == NotificationActionDelete {
// Special case for delete as there is no response body
return utils.NewToolResultText("Notification subscription deleted"), nil, nil
}
r, err := json.Marshal(result)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal response", err), nil, nil
}
return utils.NewToolResultText(string(r)), nil, nil
},
)
}
const (
RepositorySubscriptionActionWatch = "watch"
RepositorySubscriptionActionIgnore = "ignore"
RepositorySubscriptionActionDelete = "delete"
)
// ManageRepositoryNotificationSubscription creates a tool to manage a repository notification subscription (ignore, watch, delete)
func ManageRepositoryNotificationSubscription(t translations.TranslationHelperFunc) inventory.ServerTool {
return NewTool(
ToolsetMetadataNotifications,
mcp.Tool{
Name: "manage_repository_notification_subscription",
Description: t("TOOL_MANAGE_REPOSITORY_NOTIFICATION_SUBSCRIPTION_DESCRIPTION", "Manage a repository notification subscription: ignore, watch, or delete repository notifications subscription for the provided repository."),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_MANAGE_REPOSITORY_NOTIFICATION_SUBSCRIPTION_USER_TITLE", "Manage repository notification subscription"),
ReadOnlyHint: false,
},
InputSchema: &jsonschema.Schema{
Type: "object",
Properties: map[string]*jsonschema.Schema{
"owner": {
Type: "string",
Description: "The account owner of the repository.",
},
"repo": {
Type: "string",
Description: "The name of the repository.",
},
"action": {
Type: "string",
Description: "Action to perform: ignore, watch, or delete the repository notification subscription.",
Enum: []any{RepositorySubscriptionActionIgnore, RepositorySubscriptionActionWatch, RepositorySubscriptionActionDelete},
},
},
Required: []string{"owner", "repo", "action"},
},
},
[]scopes.Scope{scopes.Notifications},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) {
client, err := deps.GetClient(ctx)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil
}
owner, err := RequiredParam[string](args, "owner")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
repo, err := RequiredParam[string](args, "repo")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
action, err := RequiredParam[string](args, "action")
if err != nil {
return utils.NewToolResultError(err.Error()), nil, nil
}
var (
resp *github.Response
result any
apiErr error
)
switch action {
case RepositorySubscriptionActionIgnore:
sub := &github.Subscription{Ignored: ToBoolPtr(true)}
result, resp, apiErr = client.Activity.SetRepositorySubscription(ctx, owner, repo, sub)
case RepositorySubscriptionActionWatch:
sub := &github.Subscription{Ignored: ToBoolPtr(false), Subscribed: ToBoolPtr(true)}
result, resp, apiErr = client.Activity.SetRepositorySubscription(ctx, owner, repo, sub)
case RepositorySubscriptionActionDelete:
resp, apiErr = client.Activity.DeleteRepositorySubscription(ctx, owner, repo)
default:
return utils.NewToolResultError("Invalid action. Must be one of: ignore, watch, delete."), nil, nil
}
if apiErr != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
fmt.Sprintf("failed to %s repository subscription", action),
resp,
apiErr,
), nil, nil
}
if resp != nil {
defer func() { _ = resp.Body.Close() }()
}
// Handle non-2xx status codes
if resp != nil && (resp.StatusCode < 200 || resp.StatusCode >= 300) {
body, _ := io.ReadAll(resp.Body)
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, fmt.Sprintf("failed to %s repository subscription", action), resp, body), nil, nil
}
if action == RepositorySubscriptionActionDelete {
// Special case for delete as there is no response body
return utils.NewToolResultText("Repository subscription deleted"), nil, nil
}
r, err := json.Marshal(result)
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal response", err), nil, nil
}
return utils.NewToolResultText(string(r)), nil, nil
},
)
}