|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package e2e_test |
| 4 | + |
| 5 | +import ( |
| 6 | +"context" |
| 7 | +"encoding/json" |
| 8 | +"os" |
| 9 | +"os/exec" |
| 10 | +"testing" |
| 11 | +"time" |
| 12 | + |
| 13 | +"github.com/google/go-github/v69/github" |
| 14 | +mcpClient"github.com/mark3labs/mcp-go/client" |
| 15 | +"github.com/mark3labs/mcp-go/mcp" |
| 16 | +"github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +funcTestE2E(t*testing.T) { |
| 20 | +e2eServerToken:=os.Getenv("GITHUB_MCP_SERVER_E2E_TOKEN") |
| 21 | +ife2eServerToken=="" { |
| 22 | +t.Fatalf("GITHUB_MCP_SERVER_E2E_TOKEN environment variable is not set") |
| 23 | +} |
| 24 | + |
| 25 | +// Build the Docker image for the MCP server. |
| 26 | +buildDockerImage(t) |
| 27 | + |
| 28 | +t.Setenv("GITHUB_PERSONAL_ACCESS_TOKEN",e2eServerToken)// The MCP Client merges the existing environment. |
| 29 | +args:= []string{ |
| 30 | +"docker", |
| 31 | +"run", |
| 32 | +"-i", |
| 33 | +"--rm", |
| 34 | +"-e", |
| 35 | +"GITHUB_PERSONAL_ACCESS_TOKEN", |
| 36 | +"github/e2e-github-mcp-server", |
| 37 | +} |
| 38 | +t.Log("Starting Stdio MCP client...") |
| 39 | +client,err:=mcpClient.NewStdioMCPClient(args[0], []string{},args[1:]...) |
| 40 | +require.NoError(t,err,"expected to create client successfully") |
| 41 | + |
| 42 | +t.Run("Initialize",func(t*testing.T) { |
| 43 | +ctx,cancel:=context.WithTimeout(context.Background(),5*time.Second) |
| 44 | +defercancel() |
| 45 | + |
| 46 | +request:= mcp.InitializeRequest{} |
| 47 | +request.Params.ProtocolVersion="2025-03-26" |
| 48 | +request.Params.ClientInfo= mcp.Implementation{ |
| 49 | +Name:"e2e-test-client", |
| 50 | +Version:"0.0.1", |
| 51 | +} |
| 52 | + |
| 53 | +result,err:=client.Initialize(ctx,request) |
| 54 | +require.NoError(t,err,"expected to initialize successfully") |
| 55 | + |
| 56 | +require.Equal(t,"github-mcp-server",result.ServerInfo.Name) |
| 57 | +}) |
| 58 | + |
| 59 | +t.Run("CallTool get_me",func(t*testing.T) { |
| 60 | +ctx,cancel:=context.WithTimeout(context.Background(),5*time.Second) |
| 61 | +defercancel() |
| 62 | + |
| 63 | +// When we call the "get_me" tool |
| 64 | +request:= mcp.CallToolRequest{} |
| 65 | +request.Params.Name="get_me" |
| 66 | + |
| 67 | +response,err:=client.CallTool(ctx,request) |
| 68 | +require.NoError(t,err,"expected to call 'get_me' tool successfully") |
| 69 | + |
| 70 | +require.False(t,response.IsError,"expected result not to be an error") |
| 71 | +require.Len(t,response.Content,1,"expected content to have one item") |
| 72 | + |
| 73 | +textContent,ok:=response.Content[0].(mcp.TextContent) |
| 74 | +require.True(t,ok,"expected content to be of type TextContent") |
| 75 | + |
| 76 | +vartrimmedContentstruct { |
| 77 | +Loginstring`json:"login"` |
| 78 | +} |
| 79 | +err=json.Unmarshal([]byte(textContent.Text),&trimmedContent) |
| 80 | +require.NoError(t,err,"expected to unmarshal text content successfully") |
| 81 | + |
| 82 | +// Then the login in the response should match the login obtained via the same |
| 83 | +// token using the GitHub API. |
| 84 | +client:=github.NewClient(nil).WithAuthToken(e2eServerToken) |
| 85 | +user,_,err:=client.Users.Get(context.Background(),"") |
| 86 | +require.NoError(t,err,"expected to get user successfully") |
| 87 | +require.Equal(t,trimmedContent.Login,*user.Login,"expected login to match") |
| 88 | +}) |
| 89 | + |
| 90 | +require.NoError(t,client.Close(),"expected to close client successfully") |
| 91 | +} |
| 92 | + |
| 93 | +funcbuildDockerImage(t*testing.T) { |
| 94 | +t.Log("Building Docker image for e2e tests...") |
| 95 | + |
| 96 | +cmd:=exec.Command("docker","build","-t","github/e2e-github-mcp-server",".") |
| 97 | +cmd.Dir=".."// Run this in the context of the root, where the Dockerfile is located. |
| 98 | +output,err:=cmd.CombinedOutput() |
| 99 | +require.NoError(t,err,"expected to build Docker image successfully, output: %s",string(output)) |
| 100 | +} |