-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
798 lines (735 loc) · 30.1 KB
/
index.ts
File metadata and controls
798 lines (735 loc) · 30.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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import fetch, { Request, Response } from 'node-fetch';
import * as repository from './operations/repository.js';
import * as files from './operations/files.js';
import * as issues from './operations/issues.js';
import * as pulls from './operations/pulls.js';
import * as branches from './operations/branches.js';
import * as search from './operations/search.js';
import * as commits from './operations/commits.js';
import * as milestones from './operations/milestones.js';
import {
GitHubError,
GitHubValidationError,
GitHubResourceNotFoundError,
GitHubAuthenticationError,
GitHubPermissionError,
GitHubRateLimitError,
GitHubConflictError,
isGitHubError,
} from './common/errors.js';
import { VERSION } from "./common/version.js";
// If fetch doesn't exist in global scope, add it
if (!globalThis.fetch) {
globalThis.fetch = fetch as unknown as typeof global.fetch;
}
const server = new Server(
{
name: "github-mcp-server",
version: VERSION,
},
{
capabilities: {
tools: {},
},
}
);
function formatGitHubError(error: GitHubError): string {
let message = `GitHub API Error: ${error.message}`;
if (error instanceof GitHubValidationError) {
message = `Validation Error: ${error.message}`;
if (error.response) {
message += `\nDetails: ${JSON.stringify(error.response)}`;
}
} else if (error instanceof GitHubResourceNotFoundError) {
message = `Not Found: ${error.message}`;
} else if (error instanceof GitHubAuthenticationError) {
message = `Authentication Failed: ${error.message}`;
} else if (error instanceof GitHubPermissionError) {
message = `Permission Denied: ${error.message}`;
} else if (error instanceof GitHubRateLimitError) {
message = `Rate Limit Exceeded: ${error.message}\nResets at: ${error.resetAt.toISOString()}`;
} else if (error instanceof GitHubConflictError) {
message = `Conflict: ${error.message}`;
}
return message;
}
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "create_or_update_file",
description: "Create or update a single file in a GitHub repository",
inputSchema: zodToJsonSchema(files.CreateOrUpdateFileSchema),
},
{
name: "search_repositories",
description: "Search for GitHub repositories",
inputSchema: zodToJsonSchema(repository.SearchRepositoriesSchema),
},
{
name: "create_repository",
description: "Create a new GitHub repository in your account",
inputSchema: zodToJsonSchema(repository.CreateRepositoryOptionsSchema),
},
{
name: "get_file_contents",
description: "Get the contents of a file or directory from a GitHub repository",
inputSchema: zodToJsonSchema(files.GetFileContentsSchema),
},
{
name: "push_files",
description: "Push multiple files to a GitHub repository in a single commit",
inputSchema: zodToJsonSchema(files.PushFilesSchema),
},
{
name: "create_issue",
description: "Create a new issue in a GitHub repository",
inputSchema: zodToJsonSchema(issues.CreateIssueSchema),
},
{
name: "create_pull_request",
description: "Create a new pull request in a GitHub repository",
inputSchema: zodToJsonSchema(pulls.CreatePullRequestSchema),
},
{
name: "fork_repository",
description: "Fork a GitHub repository to your account or specified organization",
inputSchema: zodToJsonSchema(repository.ForkRepositorySchema),
},
{
name: "create_branch",
description: "Create a new branch in a GitHub repository",
inputSchema: zodToJsonSchema(branches.CreateBranchSchema),
},
{
name: "list_commits",
description: "Get list of commits of a branch in a GitHub repository",
inputSchema: zodToJsonSchema(commits.ListCommitsSchema)
},
{
name: "list_issues",
description: "List issues in a GitHub repository with filtering options: milestone string (If an integer is passed, it should refer to a milestone by its number field. If the string * is passed, issues with any milestone are accepted. If the string none is passed, issues without milestones are returned.) ,state string(Indicates the state of the issues to return.Default: open - Can be one of: open, closed, all) ,assignee string (Can be the name of a user. Pass in none for issues with no assigned user, and * for issues assigned to any user.) ,type string (Can be the name of an issue type. If the string * is passed, issues with any type are accepted. If the string none is passed, issues without type are returned.) ,creator string (The user that created the issue.) ,mentioned string (A user that's mentioned in the issue.) ,labels (An array of label names.)",
inputSchema: zodToJsonSchema(issues.ListIssuesOptionsSchema)
},
{
name: "update_issue",
description: "Update an existing issue in a GitHub repository",
inputSchema: zodToJsonSchema(issues.UpdateIssueOptionsSchema)
},
{
name: "add_issue_comment",
description: "Add a comment to an existing issue",
inputSchema: zodToJsonSchema(issues.IssueCommentSchema)
},
{
name: "search_code",
description: "Search for code across GitHub repositories",
inputSchema: zodToJsonSchema(search.SearchCodeSchema),
},
{
name: "search_issues",
description: "Search for issues and pull requests across GitHub repositories",
inputSchema: zodToJsonSchema(search.SearchIssuesSchema),
},
{
name: "search_users",
description: "Search for users on GitHub",
inputSchema: zodToJsonSchema(search.SearchUsersSchema),
},
{
name: "get_issue",
description: "Get details of a specific issue in a GitHub repository.",
inputSchema: zodToJsonSchema(issues.GetIssueSchema)
},
{
name: "get_pull_request",
description: "Get details of a specific pull request",
inputSchema: zodToJsonSchema(pulls.GetPullRequestSchema)
},
{
name: "list_pull_requests",
description: "List and filter repository pull requests",
inputSchema: zodToJsonSchema(pulls.ListPullRequestsSchema)
},
{
name: "create_pull_request_review",
description: "Create a review on a pull request",
inputSchema: zodToJsonSchema(pulls.CreatePullRequestReviewSchema)
},
{
name: "merge_pull_request",
description: "Merge a pull request",
inputSchema: zodToJsonSchema(pulls.MergePullRequestSchema)
},
{
name: "get_pull_request_files",
description: "Get the list of files changed in a pull request",
inputSchema: zodToJsonSchema(pulls.GetPullRequestFilesSchema)
},
{
name: "get_pull_request_status",
description: "Get the combined status of all status checks for a pull request",
inputSchema: zodToJsonSchema(pulls.GetPullRequestStatusSchema)
},
{
name: "update_pull_request_branch",
description: "Update a pull request branch with the latest changes from the base branch",
inputSchema: zodToJsonSchema(pulls.UpdatePullRequestBranchSchema)
},
{
name: "get_pull_request_comments",
description: "Get the review comments on a pull request",
inputSchema: zodToJsonSchema(pulls.GetPullRequestCommentsSchema)
},
{
name: "get_pull_request_reviews",
description: "Get the reviews on a pull request",
inputSchema: zodToJsonSchema(pulls.GetPullRequestReviewsSchema)
},
{
name: "list_sub_issues",
description: "List sub issues of an issue",
inputSchema: zodToJsonSchema(issues.ListSubIssuesOptionsSchema)
},
{
name: "reprioritize_sub_issue",
description: "Reprioritize a sub issue of an issue, make sure to specify either after_id or before_id, not both",
inputSchema: zodToJsonSchema(issues.ReprioritizeSubIssueOptionsSchema)
},
{
name: "remove_sub_issue",
description: "Remove a sub issue of an issue, sub_issue_id is the id of the issue to be removed not the issue number",
inputSchema: zodToJsonSchema(issues.RemoveSubIssueOptionsSchema)
},
{
name: "add_sub_issue",
description: "Add a sub issue to an issue, sub_issue_id is the id of the issue to be added as sub-issue not the issue number",
inputSchema: zodToJsonSchema(issues.AddSubIssueOptionsSchema)
},
{
name: "list_labels_for_issue",
description: "List all labels for an issue",
inputSchema: zodToJsonSchema(issues.ListLabelsForIssueSchema),
},
{
name: "add_labels_to_issue",
description: "Adds labels to an issue",
inputSchema: zodToJsonSchema(issues.AddLabelsToIssueSchema),
},
{
name: "set_labels_for_issue",
description: "Sets the labels for an issue, replacing any existing labels",
inputSchema: zodToJsonSchema(issues.SetLabelsForIssueSchema),
},
{
name: "remove_label_from_issue",
description: "Remove a label from an issue",
inputSchema: zodToJsonSchema(issues.RemoveLabelFromIssueSchema),
},
{
name: "remove_all_labels_from_issue",
description: "Remove all labels from an issue",
inputSchema: zodToJsonSchema(issues.RemoveAllLabelsFromIssueSchema),
},
{
name: "list_labels_for_repo",
description: "List all labels for a repository",
inputSchema: zodToJsonSchema(issues.ListLabelsForRepoSchema),
},
{
name: "create_label",
description: "Create a label for a repository",
inputSchema: zodToJsonSchema(issues.CreateLabelSchema),
},
{
name: "get_label",
description: "Get a single label for a repository",
inputSchema: zodToJsonSchema(issues.GetLabelSchema),
},
{
name: "update_label",
description: "Update an existing label for a repository",
inputSchema: zodToJsonSchema(issues.UpdateLabelSchema),
},
{
name: "delete_label",
description: "Delete a label from a repository",
inputSchema: zodToJsonSchema(issues.DeleteLabelSchema),
},
{
name: "list_labels_for_milestone",
description: "List labels for issues in a milestone",
inputSchema: zodToJsonSchema(issues.ListLabelsForMilestoneSchema),
},
{
name: "list_milestones",
description: "List milestones for a repository",
inputSchema: zodToJsonSchema(milestones.ListMilestonesSchema),
},
{
name: "create_milestone",
description: "Create a milestone for a repository",
inputSchema: zodToJsonSchema(milestones.CreateMilestoneSchema),
},
{
name: "get_milestone",
description: "Get a single milestone for a repository",
inputSchema: zodToJsonSchema(milestones.GetMilestoneSchema),
},
{
name: "update_milestone",
description: "Update an existing milestone for a repository",
inputSchema: zodToJsonSchema(milestones.UpdateMilestoneSchema),
},
{
name: "delete_milestone",
description: "Delete a milestone from a repository",
inputSchema: zodToJsonSchema(milestones.DeleteMilestoneSchema),
}
],
};
});
server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
if (!request.params.arguments) {
throw new Error("Arguments are required");
}
switch (request.params.name) {
case "fork_repository": {
const args = repository.ForkRepositorySchema.parse(request.params.arguments);
const fork = await repository.forkRepository(args.owner, args.repo, args.organization);
return {
content: [{ type: "text", text: JSON.stringify(fork, null, 2) }],
};
}
case "create_branch": {
const args = branches.CreateBranchSchema.parse(request.params.arguments);
const branch = await branches.createBranchFromRef(
args.owner,
args.repo,
args.branch,
args.from_branch
);
return {
content: [{ type: "text", text: JSON.stringify(branch, null, 2) }],
};
}
case "search_repositories": {
const args = repository.SearchRepositoriesSchema.parse(request.params.arguments);
const results = await repository.searchRepositories(
args.query,
args.page,
args.perPage
);
return {
content: [{ type: "text", text: JSON.stringify(results, null, 2) }],
};
}
case "create_repository": {
const args = repository.CreateRepositoryOptionsSchema.parse(request.params.arguments);
const result = await repository.createRepository(args);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "get_file_contents": {
const args = files.GetFileContentsSchema.parse(request.params.arguments);
const contents = await files.getFileContents(
args.owner,
args.repo,
args.path,
args.branch
);
return {
content: [{ type: "text", text: JSON.stringify(contents, null, 2) }],
};
}
case "create_or_update_file": {
const args = files.CreateOrUpdateFileSchema.parse(request.params.arguments);
const result = await files.createOrUpdateFile(
args.owner,
args.repo,
args.path,
args.content,
args.message,
args.branch,
args.sha
);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "push_files": {
const args = files.PushFilesSchema.parse(request.params.arguments);
const result = await files.pushFiles(
args.owner,
args.repo,
args.branch,
args.files,
args.message
);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "create_issue": {
const args = issues.CreateIssueSchema.parse(request.params.arguments);
const { owner, repo, ...options } = args;
try {
console.error(`[DEBUG] Attempting to create issue in ${owner}/${repo}`);
console.error(`[DEBUG] Issue options:`, JSON.stringify(options, null, 2));
const issue = await issues.createIssue(owner, repo, options);
console.error(`[DEBUG] Issue created successfully`);
return {
content: [{ type: "text", text: JSON.stringify(issue, null, 2) }],
};
} catch (err) {
// Type guard for Error objects
const error = err instanceof Error ? err : new Error(String(err));
console.error(`[ERROR] Failed to create issue:`, error);
if (error instanceof GitHubResourceNotFoundError) {
throw new Error(
`Repository '${owner}/${repo}' not found. Please verify:\n` +
`1. The repository exists\n` +
`2. You have correct access permissions\n` +
`3. The owner and repository names are spelled correctly`
);
}
// Safely access error properties
throw new Error(
`Failed to create issue: ${error.message}${
error.stack ? `\nStack: ${error.stack}` : ''
}`
);
}
}
case "create_pull_request": {
const args = pulls.CreatePullRequestSchema.parse(request.params.arguments);
const pullRequest = await pulls.createPullRequest(args);
return {
content: [{ type: "text", text: JSON.stringify(pullRequest, null, 2) }],
};
}
case "search_code": {
const args = search.SearchCodeSchema.parse(request.params.arguments);
const results = await search.searchCode(args);
return {
content: [{ type: "text", text: JSON.stringify(results, null, 2) }],
};
}
case "search_issues": {
const args = search.SearchIssuesSchema.parse(request.params.arguments);
const results = await search.searchIssues(args);
return {
content: [{ type: "text", text: JSON.stringify(results, null, 2) }],
};
}
case "search_users": {
const args = search.SearchUsersSchema.parse(request.params.arguments);
const results = await search.searchUsers(args);
return {
content: [{ type: "text", text: JSON.stringify(results, null, 2) }],
};
}
case "list_issues": {
const args = issues.ListIssuesOptionsSchema.parse(request.params.arguments);
const { owner, repo, ...options } = args;
const result = await issues.listIssues(owner, repo, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "update_issue": {
const args = issues.UpdateIssueOptionsSchema.parse(request.params.arguments);
const { owner, repo, issue_number, ...options } = args;
const result = await issues.updateIssue(owner, repo, issue_number, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "add_issue_comment": {
const args = issues.IssueCommentSchema.parse(request.params.arguments);
const { owner, repo, issue_number, body } = args;
const result = await issues.addIssueComment(owner, repo, issue_number, body);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "list_commits": {
const args = commits.ListCommitsSchema.parse(request.params.arguments);
const results = await commits.listCommits(
args.owner,
args.repo,
args.page,
args.perPage,
args.sha
);
return {
content: [{ type: "text", text: JSON.stringify(results, null, 2) }],
};
}
case "get_issue": {
const args = issues.GetIssueSchema.parse(request.params.arguments);
const issue = await issues.getIssue(args.owner, args.repo, args.issue_number);
return {
content: [{ type: "text", text: JSON.stringify(issue, null, 2) }],
};
}
case "get_pull_request": {
const args = pulls.GetPullRequestSchema.parse(request.params.arguments);
const pullRequest = await pulls.getPullRequest(args.owner, args.repo, args.pull_number);
return {
content: [{ type: "text", text: JSON.stringify(pullRequest, null, 2) }],
};
}
case "list_pull_requests": {
const args = pulls.ListPullRequestsSchema.parse(request.params.arguments);
const { owner, repo, ...options } = args;
const pullRequests = await pulls.listPullRequests(owner, repo, options);
return {
content: [{ type: "text", text: JSON.stringify(pullRequests, null, 2) }],
};
}
case "create_pull_request_review": {
const args = pulls.CreatePullRequestReviewSchema.parse(request.params.arguments);
const { owner, repo, pull_number, ...options } = args;
const review = await pulls.createPullRequestReview(owner, repo, pull_number, options);
return {
content: [{ type: "text", text: JSON.stringify(review, null, 2) }],
};
}
case "merge_pull_request": {
const args = pulls.MergePullRequestSchema.parse(request.params.arguments);
const { owner, repo, pull_number, ...options } = args;
const result = await pulls.mergePullRequest(owner, repo, pull_number, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "get_pull_request_files": {
const args = pulls.GetPullRequestFilesSchema.parse(request.params.arguments);
const files = await pulls.getPullRequestFiles(args.owner, args.repo, args.pull_number);
return {
content: [{ type: "text", text: JSON.stringify(files, null, 2) }],
};
}
case "get_pull_request_status": {
const args = pulls.GetPullRequestStatusSchema.parse(request.params.arguments);
const status = await pulls.getPullRequestStatus(args.owner, args.repo, args.pull_number);
return {
content: [{ type: "text", text: JSON.stringify(status, null, 2) }],
};
}
case "update_pull_request_branch": {
const args = pulls.UpdatePullRequestBranchSchema.parse(request.params.arguments);
const { owner, repo, pull_number, expected_head_sha } = args;
await pulls.updatePullRequestBranch(owner, repo, pull_number, expected_head_sha);
return {
content: [{ type: "text", text: JSON.stringify({ success: true }, null, 2) }],
};
}
case "get_pull_request_comments": {
const args = pulls.GetPullRequestCommentsSchema.parse(request.params.arguments);
const comments = await pulls.getPullRequestComments(args.owner, args.repo, args.pull_number);
return {
content: [{ type: "text", text: JSON.stringify(comments, null, 2) }],
};
}
case "get_pull_request_reviews": {
const args = pulls.GetPullRequestReviewsSchema.parse(request.params.arguments);
const reviews = await pulls.getPullRequestReviews(args.owner, args.repo, args.pull_number);
return {
content: [{ type: "text", text: JSON.stringify(reviews, null, 2) }],
};
}
case "list_sub_issues": {
const args = issues.ListSubIssuesOptionsSchema.parse(request.params.arguments);
const { owner, repo, issue_number, ...options } = args;
const result = await issues.listSubIssues(owner, repo, issue_number, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "reprioritize_sub_issue": {
const args = issues.ReprioritizeSubIssueOptionsSchema.parse(request.params.arguments);
const { owner, repo, issue_number, ...options } = args;
const result = await issues.reprioritizeSubIssue(owner, repo, issue_number, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "remove_sub_issue": {
const args = issues.RemoveSubIssueOptionsSchema.parse(request.params.arguments);
const { owner, repo, issue_number, ...options } = args;
const result = await issues.removeSubIssue(owner, repo, issue_number, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "add_sub_issue": {
const args = issues.AddSubIssueOptionsSchema.parse(request.params.arguments);
const { owner, repo, issue_number, ...options } = args;
const result = await issues.addSubIssue(owner, repo, issue_number, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "list_labels_for_issue": {
const args = issues.ListLabelsForIssueSchema.parse(request.params.arguments);
const { owner, repo, issue_number, ...options } = args;
const result = await issues.listLabelsForIssue(owner, repo, issue_number, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "add_labels_to_issue": {
const args = issues.AddLabelsToIssueSchema.parse(request.params.arguments);
const { owner, repo, issue_number, labels } = args;
const result = await issues.addLabelsToIssue(owner, repo, issue_number, labels);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "set_labels_for_issue": {
const args = issues.SetLabelsForIssueSchema.parse(request.params.arguments);
const { owner, repo, issue_number, labels } = args;
const result = await issues.setLabelsForIssue(owner, repo, issue_number, labels);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "remove_label_from_issue": {
const args = issues.RemoveLabelFromIssueSchema.parse(request.params.arguments);
const { owner, repo, issue_number, name } = args;
const result = await issues.removeLabelFromIssue(owner, repo, issue_number, name);
return {
content: [{ type: "text", text: JSON.stringify(result ?? { success: true }, null, 2) }],
};
}
case "remove_all_labels_from_issue": {
const args = issues.RemoveAllLabelsFromIssueSchema.parse(request.params.arguments);
const { owner, repo, issue_number } = args;
const result = await issues.removeAllLabelsFromIssue(owner, repo, issue_number);
return {
content: [{ type: "text", text: JSON.stringify(result ?? { success: true }, null, 2) }],
};
}
case "list_labels_for_repo": {
const args = issues.ListLabelsForRepoSchema.parse(request.params.arguments);
const { owner, repo, ...options } = args;
const result = await issues.listLabelsForRepo(owner, repo, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "create_label": {
const args = issues.CreateLabelSchema.parse(request.params.arguments);
const { owner, repo, ...options } = args;
const result = await issues.createLabel(owner, repo, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "get_label": {
const args = issues.GetLabelSchema.parse(request.params.arguments);
const { owner, repo, name } = args;
const result = await issues.getLabel(owner, repo, name);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "update_label": {
const args = issues.UpdateLabelSchema.parse(request.params.arguments);
const { owner, repo, name, ...options } = args;
const result = await issues.updateLabel(owner, repo, name, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "delete_label": {
const args = issues.DeleteLabelSchema.parse(request.params.arguments);
const { owner, repo, name } = args;
const result = await issues.deleteLabel(owner, repo, name);
return {
content: [{ type: "text", text: JSON.stringify(result ?? { success: true }, null, 2) }],
};
}
case "list_labels_for_milestone": {
const args = issues.ListLabelsForMilestoneSchema.parse(request.params.arguments);
const { owner, repo, milestone_number, ...options } = args;
const result = await issues.listLabelsForMilestone(owner, repo, milestone_number, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "list_milestones": {
const args = milestones.ListMilestonesSchema.parse(request.params.arguments);
const { owner, repo, ...options } = args;
const result = await milestones.listMilestones(owner, repo, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "create_milestone": {
const args = milestones.CreateMilestoneSchema.parse(request.params.arguments);
const { owner, repo, ...options } = args;
const result = await milestones.createMilestone(owner, repo, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "get_milestone": {
const args = milestones.GetMilestoneSchema.parse(request.params.arguments);
const { owner, repo, milestone_number } = args;
const result = await milestones.getMilestone(owner, repo, milestone_number);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "update_milestone": {
const args = milestones.UpdateMilestoneSchema.parse(request.params.arguments);
const { owner, repo, milestone_number, ...options } = args;
const result = await milestones.updateMilestone(owner, repo, milestone_number, options);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "delete_milestone": {
const args = milestones.DeleteMilestoneSchema.parse(request.params.arguments);
const { owner, repo, milestone_number } = args;
const result = await milestones.deleteMilestone(owner, repo, milestone_number);
return {
content: [{ type: "text", text: JSON.stringify(result ?? { success: true }, null, 2) }],
};
}
default:
throw new Error(`Unknown tool: ${request.params.name}`);
}
} catch (error) {
if (error instanceof z.ZodError) {
throw new Error(`Invalid input: ${JSON.stringify(error.errors)}`);
}
if (isGitHubError(error)) {
throw new Error(formatGitHubError(error));
}
throw error;
}
});
async function runServer() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("GitHub MCP Server running on stdio");
}
runServer().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});