-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathe2e_test.go
More file actions
1626 lines (1348 loc) · 62.7 KB
/
e2e_test.go
File metadata and controls
1626 lines (1348 loc) · 62.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
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build e2e
package e2e_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/github/github-mcp-server/internal/ghmcp"
"github.com/github/github-mcp-server/pkg/github"
"github.com/github/github-mcp-server/pkg/translations"
gogithub "github.com/google/go-github/v73/github"
mcpClient "github.com/mark3labs/mcp-go/client"
"github.com/mark3labs/mcp-go/mcp"
"github.com/stretchr/testify/require"
)
var (
// Shared variables and sync.Once instances to ensure one-time execution
getTokenOnce sync.Once
token string
getHostOnce sync.Once
host string
buildOnce sync.Once
buildError error
)
// getE2EToken ensures the environment variable is checked only once and returns the token
func getE2EToken(t *testing.T) string {
getTokenOnce.Do(func() {
token = os.Getenv("GITHUB_MCP_SERVER_E2E_TOKEN")
if token == "" {
t.Fatalf("GITHUB_MCP_SERVER_E2E_TOKEN environment variable is not set")
}
})
return token
}
// getE2EHost ensures the environment variable is checked only once and returns the host
func getE2EHost() string {
getHostOnce.Do(func() {
host = os.Getenv("GITHUB_MCP_SERVER_E2E_HOST")
})
return host
}
func getRESTClient(t *testing.T) *gogithub.Client {
// Get token and ensure Docker image is built
token := getE2EToken(t)
// Create a new GitHub client with the token
ghClient := gogithub.NewClient(nil).WithAuthToken(token)
if host := getE2EHost(); host != "" && host != "https://github.com" {
var err error
// Currently this works for GHEC because the API is exposed at the api subdomain and the path prefix
// but it would be preferable to extract the host parsing from the main server logic, and use it here.
ghClient, err = ghClient.WithEnterpriseURLs(host, host)
require.NoError(t, err, "expected to create GitHub client with host")
}
return ghClient
}
// ensureDockerImageBuilt makes sure the Docker image is built only once across all tests
func ensureDockerImageBuilt(t *testing.T) {
buildOnce.Do(func() {
t.Log("Building Docker image for e2e tests...")
cmd := exec.Command("docker", "build", "-t", "github/e2e-github-mcp-server", ".")
cmd.Dir = ".." // Run this in the context of the root, where the Dockerfile is located.
output, err := cmd.CombinedOutput()
buildError = err
if err != nil {
t.Logf("Docker build output: %s", string(output))
}
})
// Check if the build was successful
require.NoError(t, buildError, "expected to build Docker image successfully")
}
// clientOpts holds configuration options for the MCP client setup
type clientOpts struct {
// Toolsets to enable in the MCP server
enabledToolsets []string
}
// clientOption defines a function type for configuring ClientOpts
type clientOption func(*clientOpts)
// withToolsets returns an option that either sets the GITHUB_TOOLSETS envvar when executing in docker,
// or sets the toolsets in the MCP server when running in-process.
func withToolsets(toolsets []string) clientOption {
return func(opts *clientOpts) {
opts.enabledToolsets = toolsets
}
}
func setupMCPClient(t *testing.T, options ...clientOption) *mcpClient.Client {
// Get token and ensure Docker image is built
token := getE2EToken(t)
// Create and configure options
opts := &clientOpts{}
// Apply all options to configure the opts struct
for _, option := range options {
option(opts)
}
// By default, we run the tests including the Docker image, but with DEBUG
// enabled, we run the server in-process, allowing for easier debugging.
var client *mcpClient.Client
if os.Getenv("GITHUB_MCP_SERVER_E2E_DEBUG") == "" {
ensureDockerImageBuilt(t)
// Prepare Docker arguments
args := []string{
"docker",
"run",
"-i",
"--rm",
"-e",
"GITHUB_PERSONAL_ACCESS_TOKEN", // Personal access token is all required
}
host := getE2EHost()
if host != "" {
args = append(args, "-e", "GITHUB_HOST")
}
// Add toolsets environment variable to the Docker arguments
if len(opts.enabledToolsets) > 0 {
args = append(args, "-e", "GITHUB_TOOLSETS")
}
// Add the image name
args = append(args, "github/e2e-github-mcp-server")
// Construct the env vars for the MCP Client to execute docker with
dockerEnvVars := []string{
fmt.Sprintf("GITHUB_PERSONAL_ACCESS_TOKEN=%s", token),
fmt.Sprintf("GITHUB_TOOLSETS=%s", strings.Join(opts.enabledToolsets, ",")),
}
if host != "" {
dockerEnvVars = append(dockerEnvVars, fmt.Sprintf("GITHUB_HOST=%s", host))
}
// Create the client
t.Log("Starting Stdio MCP client...")
var err error
client, err = mcpClient.NewStdioMCPClient(args[0], dockerEnvVars, args[1:]...)
require.NoError(t, err, "expected to create client successfully")
} else {
// We need this because the fully compiled server has a default for the viper config, which is
// not in scope for using the MCP server directly. This probably indicates that we should refactor
// so that there is a shared setup mechanism, but let's wait till we feel more friction.
enabledToolsets := opts.enabledToolsets
if enabledToolsets == nil {
enabledToolsets = github.DefaultTools
}
ghServer, err := ghmcp.NewMCPServer(ghmcp.MCPServerConfig{
Token: token,
EnabledToolsets: enabledToolsets,
Host: getE2EHost(),
Translator: translations.NullTranslationHelper,
})
require.NoError(t, err, "expected to construct MCP server successfully")
t.Log("Starting In Process MCP client...")
client, err = mcpClient.NewInProcessClient(ghServer)
require.NoError(t, err, "expected to create in-process client successfully")
}
t.Cleanup(func() {
require.NoError(t, client.Close(), "expected to close client successfully")
})
// Initialize the client
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
request := mcp.InitializeRequest{}
request.Params.ProtocolVersion = "2025-03-26"
request.Params.ClientInfo = mcp.Implementation{
Name: "e2e-test-client",
Version: "0.0.1",
}
result, err := client.Initialize(ctx, request)
require.NoError(t, err, "failed to initialize client")
require.Equal(t, "github-mcp-server", result.ServerInfo.Name, "unexpected server name")
return client
}
func TestGetMe(t *testing.T) {
t.Parallel()
mcpClient := setupMCPClient(t)
ctx := context.Background()
// When we call the "get_me" tool
request := mcp.CallToolRequest{}
request.Params.Name = "get_me"
response, err := mcpClient.CallTool(ctx, request)
require.NoError(t, err, "expected to call 'get_me' tool successfully")
require.False(t, response.IsError, "expected result not to be an error")
require.Len(t, response.Content, 1, "expected content to have one item")
textContent, ok := response.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedContent struct {
Login string `json:"login"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedContent)
require.NoError(t, err, "expected to unmarshal text content successfully")
// Then the login in the response should match the login obtained via the same
// token using the GitHub API.
ghClient := getRESTClient(t)
user, _, err := ghClient.Users.Get(context.Background(), "")
require.NoError(t, err, "expected to get user successfully")
require.Equal(t, trimmedContent.Login, *user.Login, "expected login to match")
}
func TestToolsets(t *testing.T) {
t.Parallel()
mcpClient := setupMCPClient(
t,
withToolsets([]string{"repos", "issues"}),
)
ctx := context.Background()
request := mcp.ListToolsRequest{}
response, err := mcpClient.ListTools(ctx, request)
require.NoError(t, err, "expected to list tools successfully")
// We could enumerate the tools here, but we'll need to expose that information
// declaratively in the MCP server, so for the moment let's just check the existence
// of an issue and repo tool, and the non-existence of a pull_request tool.
var toolsContains = func(expectedName string) bool {
return slices.ContainsFunc(response.Tools, func(tool mcp.Tool) bool {
return tool.Name == expectedName
})
}
require.True(t, toolsContains("get_issue"), "expected to find 'get_issue' tool")
require.True(t, toolsContains("list_branches"), "expected to find 'list_branches' tool")
require.False(t, toolsContains("get_pull_request"), "expected not to find 'get_pull_request' tool")
}
func TestTags(t *testing.T) {
t.Parallel()
mcpClient := setupMCPClient(t)
ctx := context.Background()
// First, who am I
getMeRequest := mcp.CallToolRequest{}
getMeRequest.Params.Name = "get_me"
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, getMeRequest)
require.NoError(t, err, "expected to call 'get_me' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
require.False(t, resp.IsError, "expected result not to be an error")
require.Len(t, resp.Content, 1, "expected content to have one item")
textContent, ok := resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedGetMeText struct {
Login string `json:"login"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedGetMeText)
require.NoError(t, err, "expected to unmarshal text content successfully")
currentOwner := trimmedGetMeText.Login
// Then create a repository with a README (via autoInit)
repoName := fmt.Sprintf("github-mcp-server-e2e-%s-%d", t.Name(), time.Now().UnixMilli())
createRepoRequest := mcp.CallToolRequest{}
createRepoRequest.Params.Name = "create_repository"
createRepoRequest.Params.Arguments = map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
}
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, createRepoRequest)
require.NoError(t, err, "expected to call 'get_me' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Cleanup the repository after the test
t.Cleanup(func() {
// MCP Server doesn't support deletions, but we can use the GitHub Client
ghClient := getRESTClient(t)
t.Logf("Deleting repository %s/%s...", currentOwner, repoName)
_, err := ghClient.Repositories.Delete(context.Background(), currentOwner, repoName)
require.NoError(t, err, "expected to delete repository successfully")
})
// Then create a tag
// MCP Server doesn't support tag creation, but we can use the GitHub Client
ghClient := getRESTClient(t)
t.Logf("Creating tag %s/%s:%s...", currentOwner, repoName, "v0.0.1")
ref, _, err := ghClient.Git.GetRef(context.Background(), currentOwner, repoName, "refs/heads/main")
require.NoError(t, err, "expected to get ref successfully")
tagObj, _, err := ghClient.Git.CreateTag(context.Background(), currentOwner, repoName, &gogithub.Tag{
Tag: gogithub.Ptr("v0.0.1"),
Message: gogithub.Ptr("v0.0.1"),
Object: &gogithub.GitObject{
SHA: ref.Object.SHA,
Type: gogithub.Ptr("commit"),
},
})
require.NoError(t, err, "expected to create tag object successfully")
_, _, err = ghClient.Git.CreateRef(context.Background(), currentOwner, repoName, &gogithub.Reference{
Ref: gogithub.Ptr("refs/tags/v0.0.1"),
Object: &gogithub.GitObject{
SHA: tagObj.SHA,
},
})
require.NoError(t, err, "expected to create tag ref successfully")
// List the tags
listTagsRequest := mcp.CallToolRequest{}
listTagsRequest.Params.Name = "list_tags"
listTagsRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
}
t.Logf("Listing tags for %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, listTagsRequest)
require.NoError(t, err, "expected to call 'list_tags' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
require.False(t, resp.IsError, "expected result not to be an error")
require.Len(t, resp.Content, 1, "expected content to have one item")
textContent, ok = resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedTags []struct {
Name string `json:"name"`
Commit struct {
SHA string `json:"sha"`
} `json:"commit"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedTags)
require.NoError(t, err, "expected to unmarshal text content successfully")
require.Len(t, trimmedTags, 1, "expected to find one tag")
require.Equal(t, "v0.0.1", trimmedTags[0].Name, "expected tag name to match")
require.Equal(t, *ref.Object.SHA, trimmedTags[0].Commit.SHA, "expected tag SHA to match")
// And fetch an individual tag
getTagRequest := mcp.CallToolRequest{}
getTagRequest.Params.Name = "get_tag"
getTagRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"tag": "v0.0.1",
}
t.Logf("Getting tag %s/%s:%s...", currentOwner, repoName, "v0.0.1")
resp, err = mcpClient.CallTool(ctx, getTagRequest)
require.NoError(t, err, "expected to call 'get_tag' tool successfully")
require.False(t, resp.IsError, "expected result not to be an error")
var trimmedTag []struct { // don't understand why this is an array
Name string `json:"name"`
Commit struct {
SHA string `json:"sha"`
} `json:"commit"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedTag)
require.NoError(t, err, "expected to unmarshal text content successfully")
require.Len(t, trimmedTag, 1, "expected to find one tag")
require.Equal(t, "v0.0.1", trimmedTag[0].Name, "expected tag name to match")
require.Equal(t, *ref.Object.SHA, trimmedTag[0].Commit.SHA, "expected tag SHA to match")
}
func TestFileDeletion(t *testing.T) {
t.Parallel()
mcpClient := setupMCPClient(t)
ctx := context.Background()
// First, who am I
getMeRequest := mcp.CallToolRequest{}
getMeRequest.Params.Name = "get_me"
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, getMeRequest)
require.NoError(t, err, "expected to call 'get_me' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
require.False(t, resp.IsError, "expected result not to be an error")
require.Len(t, resp.Content, 1, "expected content to have one item")
textContent, ok := resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedGetMeText struct {
Login string `json:"login"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedGetMeText)
require.NoError(t, err, "expected to unmarshal text content successfully")
currentOwner := trimmedGetMeText.Login
// Then create a repository with a README (via autoInit)
repoName := fmt.Sprintf("github-mcp-server-e2e-%s-%d", t.Name(), time.Now().UnixMilli())
createRepoRequest := mcp.CallToolRequest{}
createRepoRequest.Params.Name = "create_repository"
createRepoRequest.Params.Arguments = map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
}
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, createRepoRequest)
require.NoError(t, err, "expected to call 'get_me' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Cleanup the repository after the test
t.Cleanup(func() {
// MCP Server doesn't support deletions, but we can use the GitHub Client
ghClient := getRESTClient(t)
t.Logf("Deleting repository %s/%s...", currentOwner, repoName)
_, err := ghClient.Repositories.Delete(context.Background(), currentOwner, repoName)
require.NoError(t, err, "expected to delete repository successfully")
})
// Create a branch on which to create a new commit
createBranchRequest := mcp.CallToolRequest{}
createBranchRequest.Params.Name = "create_branch"
createBranchRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"branch": "test-branch",
"from_branch": "main",
}
t.Logf("Creating branch in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, createBranchRequest)
require.NoError(t, err, "expected to call 'create_branch' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Create a commit with a new file
commitRequest := mcp.CallToolRequest{}
commitRequest.Params.Name = "create_or_update_file"
commitRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-file.txt",
"content": fmt.Sprintf("Created by e2e test %s", t.Name()),
"message": "Add test file",
"branch": "test-branch",
}
t.Logf("Creating commit with new file in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, commitRequest)
require.NoError(t, err, "expected to call 'create_or_update_file' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Check the file exists
getFileContentsRequest := mcp.CallToolRequest{}
getFileContentsRequest.Params.Name = "get_file_contents"
getFileContentsRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-file.txt",
"branch": "test-branch",
}
t.Logf("Getting file contents in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, getFileContentsRequest)
require.NoError(t, err, "expected to call 'get_file_contents' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
embeddedResource, ok := resp.Content[1].(mcp.EmbeddedResource)
require.True(t, ok, "expected content to be of type EmbeddedResource")
// raw api
textResource, ok := embeddedResource.Resource.(mcp.TextResourceContents)
require.True(t, ok, "expected embedded resource to be of type TextResourceContents")
require.Equal(t, fmt.Sprintf("Created by e2e test %s", t.Name()), textResource.Text, "expected file content to match")
// Delete the file
deleteFileRequest := mcp.CallToolRequest{}
deleteFileRequest.Params.Name = "delete_file"
deleteFileRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-file.txt",
"message": "Delete test file",
"branch": "test-branch",
}
t.Logf("Deleting file in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, deleteFileRequest)
require.NoError(t, err, "expected to call 'delete_file' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// See that there is a commit that removes the file
listCommitsRequest := mcp.CallToolRequest{}
listCommitsRequest.Params.Name = "list_commits"
listCommitsRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"sha": "test-branch", // can be SHA or branch, which is an unfortunate API design
}
t.Logf("Listing commits in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, listCommitsRequest)
require.NoError(t, err, "expected to call 'list_commits' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
textContent, ok = resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedListCommitsText []struct {
SHA string `json:"sha"`
Commit struct {
Message string `json:"message"`
}
Files []struct {
Filename string `json:"filename"`
Deletions int `json:"deletions"`
}
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedListCommitsText)
require.NoError(t, err, "expected to unmarshal text content successfully")
require.GreaterOrEqual(t, len(trimmedListCommitsText), 1, "expected to find at least one commit")
deletionCommit := trimmedListCommitsText[0]
require.Equal(t, "Delete test file", deletionCommit.Commit.Message, "expected commit message to match")
// Now get the commit so we can look at the file changes because list_commits doesn't include them
getCommitRequest := mcp.CallToolRequest{}
getCommitRequest.Params.Name = "get_commit"
getCommitRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"sha": deletionCommit.SHA,
}
t.Logf("Getting commit %s/%s:%s...", currentOwner, repoName, deletionCommit.SHA)
resp, err = mcpClient.CallTool(ctx, getCommitRequest)
require.NoError(t, err, "expected to call 'get_commit' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
textContent, ok = resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedGetCommitText struct {
Files []struct {
Filename string `json:"filename"`
Deletions int `json:"deletions"`
}
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedGetCommitText)
require.NoError(t, err, "expected to unmarshal text content successfully")
require.Len(t, trimmedGetCommitText.Files, 1, "expected to find one file change")
require.Equal(t, "test-file.txt", trimmedGetCommitText.Files[0].Filename, "expected filename to match")
require.Equal(t, 1, trimmedGetCommitText.Files[0].Deletions, "expected one deletion")
}
func TestDirectoryDeletion(t *testing.T) {
t.Parallel()
mcpClient := setupMCPClient(t)
ctx := context.Background()
// First, who am I
getMeRequest := mcp.CallToolRequest{}
getMeRequest.Params.Name = "get_me"
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, getMeRequest)
require.NoError(t, err, "expected to call 'get_me' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
require.False(t, resp.IsError, "expected result not to be an error")
require.Len(t, resp.Content, 1, "expected content to have one item")
textContent, ok := resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedGetMeText struct {
Login string `json:"login"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedGetMeText)
require.NoError(t, err, "expected to unmarshal text content successfully")
currentOwner := trimmedGetMeText.Login
// Then create a repository with a README (via autoInit)
repoName := fmt.Sprintf("github-mcp-server-e2e-%s-%d", t.Name(), time.Now().UnixMilli())
createRepoRequest := mcp.CallToolRequest{}
createRepoRequest.Params.Name = "create_repository"
createRepoRequest.Params.Arguments = map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
}
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, createRepoRequest)
require.NoError(t, err, "expected to call 'get_me' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Cleanup the repository after the test
t.Cleanup(func() {
// MCP Server doesn't support deletions, but we can use the GitHub Client
ghClient := getRESTClient(t)
t.Logf("Deleting repository %s/%s...", currentOwner, repoName)
_, err := ghClient.Repositories.Delete(context.Background(), currentOwner, repoName)
require.NoError(t, err, "expected to delete repository successfully")
})
// Create a branch on which to create a new commit
createBranchRequest := mcp.CallToolRequest{}
createBranchRequest.Params.Name = "create_branch"
createBranchRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"branch": "test-branch",
"from_branch": "main",
}
t.Logf("Creating branch in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, createBranchRequest)
require.NoError(t, err, "expected to call 'create_branch' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Create a commit with a new file
commitRequest := mcp.CallToolRequest{}
commitRequest.Params.Name = "create_or_update_file"
commitRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-dir/test-file.txt",
"content": fmt.Sprintf("Created by e2e test %s", t.Name()),
"message": "Add test file",
"branch": "test-branch",
}
t.Logf("Creating commit with new file in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, commitRequest)
require.NoError(t, err, "expected to call 'create_or_update_file' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
textContent, ok = resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
// Check the file exists
getFileContentsRequest := mcp.CallToolRequest{}
getFileContentsRequest.Params.Name = "get_file_contents"
getFileContentsRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-dir/test-file.txt",
"branch": "test-branch",
}
t.Logf("Getting file contents in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, getFileContentsRequest)
require.NoError(t, err, "expected to call 'get_file_contents' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
embeddedResource, ok := resp.Content[1].(mcp.EmbeddedResource)
require.True(t, ok, "expected content to be of type EmbeddedResource")
// raw api
textResource, ok := embeddedResource.Resource.(mcp.TextResourceContents)
require.True(t, ok, "expected embedded resource to be of type TextResourceContents")
require.Equal(t, fmt.Sprintf("Created by e2e test %s", t.Name()), textResource.Text, "expected file content to match")
// Delete the directory containing the file
deleteFileRequest := mcp.CallToolRequest{}
deleteFileRequest.Params.Name = "delete_file"
deleteFileRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-dir",
"message": "Delete test directory",
"branch": "test-branch",
}
t.Logf("Deleting directory in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, deleteFileRequest)
require.NoError(t, err, "expected to call 'delete_file' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// See that there is a commit that removes the directory
listCommitsRequest := mcp.CallToolRequest{}
listCommitsRequest.Params.Name = "list_commits"
listCommitsRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"sha": "test-branch", // can be SHA or branch, which is an unfortunate API design
}
t.Logf("Listing commits in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, listCommitsRequest)
require.NoError(t, err, "expected to call 'list_commits' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
textContent, ok = resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedListCommitsText []struct {
SHA string `json:"sha"`
Commit struct {
Message string `json:"message"`
}
Files []struct {
Filename string `json:"filename"`
Deletions int `json:"deletions"`
} `json:"files"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedListCommitsText)
require.NoError(t, err, "expected to unmarshal text content successfully")
require.GreaterOrEqual(t, len(trimmedListCommitsText), 1, "expected to find at least one commit")
deletionCommit := trimmedListCommitsText[0]
require.Equal(t, "Delete test directory", deletionCommit.Commit.Message, "expected commit message to match")
// Now get the commit so we can look at the file changes because list_commits doesn't include them
getCommitRequest := mcp.CallToolRequest{}
getCommitRequest.Params.Name = "get_commit"
getCommitRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"sha": deletionCommit.SHA,
}
t.Logf("Getting commit %s/%s:%s...", currentOwner, repoName, deletionCommit.SHA)
resp, err = mcpClient.CallTool(ctx, getCommitRequest)
require.NoError(t, err, "expected to call 'get_commit' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
textContent, ok = resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedGetCommitText struct {
Files []struct {
Filename string `json:"filename"`
Deletions int `json:"deletions"`
}
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedGetCommitText)
require.NoError(t, err, "expected to unmarshal text content successfully")
require.Len(t, trimmedGetCommitText.Files, 1, "expected to find one file change")
require.Equal(t, "test-dir/test-file.txt", trimmedGetCommitText.Files[0].Filename, "expected filename to match")
require.Equal(t, 1, trimmedGetCommitText.Files[0].Deletions, "expected one deletion")
}
func TestRequestCopilotReview(t *testing.T) {
t.Parallel()
if getE2EHost() != "" && getE2EHost() != "https://github.com" {
t.Skip("Skipping test because the host does not support copilot reviews")
}
mcpClient := setupMCPClient(t)
ctx := context.Background()
// First, who am I
getMeRequest := mcp.CallToolRequest{}
getMeRequest.Params.Name = "get_me"
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, getMeRequest)
require.NoError(t, err, "expected to call 'get_me' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
require.False(t, resp.IsError, "expected result not to be an error")
require.Len(t, resp.Content, 1, "expected content to have one item")
textContent, ok := resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedGetMeText struct {
Login string `json:"login"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedGetMeText)
require.NoError(t, err, "expected to unmarshal text content successfully")
currentOwner := trimmedGetMeText.Login
// Then create a repository with a README (via autoInit)
repoName := fmt.Sprintf("github-mcp-server-e2e-%s-%d", t.Name(), time.Now().UnixMilli())
createRepoRequest := mcp.CallToolRequest{}
createRepoRequest.Params.Name = "create_repository"
createRepoRequest.Params.Arguments = map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
}
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, createRepoRequest)
require.NoError(t, err, "expected to call 'create_repository' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Cleanup the repository after the test
t.Cleanup(func() {
// MCP Server doesn't support deletions, but we can use the GitHub Client
ghClient := gogithub.NewClient(nil).WithAuthToken(getE2EToken(t))
t.Logf("Deleting repository %s/%s...", currentOwner, repoName)
_, err := ghClient.Repositories.Delete(context.Background(), currentOwner, repoName)
require.NoError(t, err, "expected to delete repository successfully")
})
// Create a branch on which to create a new commit
createBranchRequest := mcp.CallToolRequest{}
createBranchRequest.Params.Name = "create_branch"
createBranchRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"branch": "test-branch",
"from_branch": "main",
}
t.Logf("Creating branch in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, createBranchRequest)
require.NoError(t, err, "expected to call 'create_branch' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Create a commit with a new file
commitRequest := mcp.CallToolRequest{}
commitRequest.Params.Name = "create_or_update_file"
commitRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-file.txt",
"content": fmt.Sprintf("Created by e2e test %s", t.Name()),
"message": "Add test file",
"branch": "test-branch",
}
t.Logf("Creating commit with new file in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, commitRequest)
require.NoError(t, err, "expected to call 'create_or_update_file' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
textContent, ok = resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedCommitText struct {
SHA string `json:"sha"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedCommitText)
require.NoError(t, err, "expected to unmarshal text content successfully")
commitId := trimmedCommitText.SHA
// Create a pull request
prRequest := mcp.CallToolRequest{}
prRequest.Params.Name = "create_pull_request"
prRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"title": "Test PR",
"body": "This is a test PR",
"head": "test-branch",
"base": "main",
"commitId": commitId,
}
t.Logf("Creating pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, prRequest)
require.NoError(t, err, "expected to call 'create_pull_request' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Request a copilot review
requestCopilotReviewRequest := mcp.CallToolRequest{}
requestCopilotReviewRequest.Params.Name = "request_copilot_review"
requestCopilotReviewRequest.Params.Arguments = map[string]any{
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
}
t.Logf("Requesting Copilot review for pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, requestCopilotReviewRequest)
require.NoError(t, err, "expected to call 'request_copilot_review' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
textContent, ok = resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
require.Equal(t, "", textContent.Text, "expected content to be empty")
// Finally, get requested reviews and see copilot is in there
// MCP Server doesn't support requesting reviews yet, but we can use the GitHub Client
ghClient := gogithub.NewClient(nil).WithAuthToken(getE2EToken(t))
t.Logf("Getting reviews for pull request in %s/%s...", currentOwner, repoName)
reviewRequests, _, err := ghClient.PullRequests.ListReviewers(context.Background(), currentOwner, repoName, 1, nil)
require.NoError(t, err, "expected to get review requests successfully")
// Check that there is one review request from copilot
require.Len(t, reviewRequests.Users, 1, "expected to find one review request")
require.Equal(t, "Copilot", *reviewRequests.Users[0].Login, "expected review request to be for Copilot")
require.Equal(t, "Bot", *reviewRequests.Users[0].Type, "expected review request to be for Bot")
}
func TestAssignCopilotToIssue(t *testing.T) {
t.Parallel()
if getE2EHost() != "" && getE2EHost() != "https://github.com" {
t.Skip("Skipping test because the host does not support copilot being assigned to issues")
}
mcpClient := setupMCPClient(t)
ctx := context.Background()
// First, who am I
getMeRequest := mcp.CallToolRequest{}
getMeRequest.Params.Name = "get_me"
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, getMeRequest)
require.NoError(t, err, "expected to call 'get_me' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
require.False(t, resp.IsError, "expected result not to be an error")
require.Len(t, resp.Content, 1, "expected content to have one item")
textContent, ok := resp.Content[0].(mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
var trimmedGetMeText struct {
Login string `json:"login"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedGetMeText)
require.NoError(t, err, "expected to unmarshal text content successfully")
currentOwner := trimmedGetMeText.Login
// Then create a repository with a README (via autoInit)
repoName := fmt.Sprintf("github-mcp-server-e2e-%s-%d", t.Name(), time.Now().UnixMilli())
createRepoRequest := mcp.CallToolRequest{}
createRepoRequest.Params.Name = "create_repository"
createRepoRequest.Params.Arguments = map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
}
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, createRepoRequest)
require.NoError(t, err, "expected to call 'create_repository' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Cleanup the repository after the test
t.Cleanup(func() {
// MCP Server doesn't support deletions, but we can use the GitHub Client
ghClient := getRESTClient(t)
t.Logf("Deleting repository %s/%s...", currentOwner, repoName)
_, err := ghClient.Repositories.Delete(context.Background(), currentOwner, repoName)
require.NoError(t, err, "expected to delete repository successfully")
})
// Create an issue
createIssueRequest := mcp.CallToolRequest{}
createIssueRequest.Params.Name = "create_issue"
createIssueRequest.Params.Arguments = map[string]any{
"owner": currentOwner,