Files
Sam Morrow ce2e4f9472 refactor: Introduce Inventory pattern with builder, filtering, and per-request optimization (#1589)
* refactor: separate ServerTool into own file with HandlerFunc pattern

- Extract ServerTool struct into pkg/toolsets/server_tool.go
- Add ToolDependencies struct for passing common dependencies to handlers
- HandlerFunc allows lazy handler generation from Tool definitions
- NewServerTool for new dependency-based tools
- NewServerToolLegacy for backward compatibility with existing handlers
- Update toolsets.go to store and pass dependencies
- Update all call sites to use NewServerToolLegacy

Co-authored-by: Adam Holt <4619+omgitsads@users.noreply.github.com>

* Wire ToolDependencies through toolsets

- Move ToolDependencies to pkg/github/dependencies.go with proper types
- Use 'any' in toolsets package to avoid circular dependencies
- Add NewTool/NewToolFromHandler helpers that isolate type assertion
- Tool implementations will be fully typed with no assertions scattered
- Infrastructure ready for incremental tool migration

* refactor(search): migrate search tools to new ServerTool pattern

Migrate search.go tools (SearchRepositories, SearchCode, SearchUsers,
SearchOrgs) to use the new NewTool helper and ToolDependencies pattern.

- Functions now take only TranslationHelperFunc and return ServerTool
- Handler generation uses ToolDependencies for typed access to clients
- Update tools.go call sites to remove getClient parameter
- Update tests to use new Handler(deps) pattern

This demonstrates the migration pattern for additional tool files.

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* Migrate context_tools to new ServerTool pattern (#1590)

* refactor(search): migrate search tools to new ServerTool pattern

Migrate search.go tools (SearchRepositories, SearchCode, SearchUsers,
SearchOrgs) to use the new NewTool helper and ToolDependencies pattern.

- Functions now take only TranslationHelperFunc and return ServerTool
- Handler generation uses ToolDependencies for typed access to clients
- Update tools.go call sites to remove getClient parameter
- Update tests to use new Handler(deps) pattern

This demonstrates the migration pattern for additional tool files.

Co-authored-by: Adam Holt <oholt@github.com>

* Migrate context_tools to new ServerTool pattern

Convert GetMe, GetTeams, and GetTeamMembers to use the new typed
dependency injection pattern:
- Functions now take only translations helper, return toolsets.ServerTool
- Handler is generated lazily via deps.GetClient/deps.GetGQLClient
- Tests updated to use serverTool.Handler(deps) pattern
- Fixed error return pattern to return nil for Go error (via result.IsError)

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* refactor(gists): migrate gists.go to NewTool pattern (#1591)

* Migrate context_tools to new ServerTool pattern

Convert GetMe, GetTeams, and GetTeamMembers to use the new typed
dependency injection pattern:
- Functions now take only translations helper, return toolsets.ServerTool
- Handler is generated lazily via deps.GetClient/deps.GetGQLClient
- Tests updated to use serverTool.Handler(deps) pattern
- Fixed error return pattern to return nil for Go error (via result.IsError)

Co-authored-by: Adam Holt <oholt@github.com>

* refactor(gists): migrate gists.go to NewTool pattern

Convert all gist tools (ListGists, GetGist, CreateGist, UpdateGist)
to use the new NewTool helper with ToolDependencies injection.

- Remove getClient parameter from function signatures
- Use deps.GetClient(ctx) inside handlers
- Standardize error handling with utils.NewToolResultErrorFromErr()
- Update all tests to use serverTool.Handler(deps) pattern

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

---------

Co-authored-by: Adam Holt <oholt@github.com>
Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

---------

Co-authored-by: Adam Holt <oholt@github.com>
Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* refactor(notifications): migrate notifications.go to NewTool pattern (#1592)

* refactor(notifications): migrate notifications.go to NewTool pattern

Convert all notification tools to use the new NewTool helper with
ToolDependencies injection.

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* Refactor repositories.go tools to use NewTool pattern with ToolDependencies

Convert all 18 tool functions in repositories.go to use the new NewTool helper
pattern with typed ToolDependencies, isolating type assertions to a single
location and improving code maintainability.

Functions converted:
- GetCommit, ListCommits, ListBranches
- CreateOrUpdateFile, CreateRepository, GetFileContents
- ForkRepository, DeleteFile, CreateBranch, PushFiles
- ListTags, GetTag, ListReleases, GetLatestRelease, GetReleaseByTag
- ListStarredRepositories, StarRepository, UnstarRepository

This is part of a stacked PR series to systematically migrate all tool
files to the new pattern.

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* refactor(issues): migrate issues.go to NewTool pattern

Convert all 8 tool functions in issues.go to use the new NewTool
helper pattern which standardizes dependency injection:

- IssueRead: GetClient, GetGQLClient, RepoAccessCache, Flags
- ListIssueTypes: GetClient
- AddIssueComment: GetClient
- SubIssueWrite: GetClient
- SearchIssues: GetClient
- IssueWrite: GetClient, GetGQLClient
- ListIssues: GetGQLClient
- AssignCopilotToIssue: GetGQLClient

Updated tools.go to use direct function calls instead of
NewServerToolLegacy wrappers. Updated all tests in issues_test.go
to use the new ToolDependencies pattern and Handler() method.

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* refactor(pullrequests): convert PR tools to NewTool pattern

Convert all 10 pull request tool functions to use the NewTool
pattern with ToolDependencies injection:
- PullRequestRead
- CreatePullRequest
- UpdatePullRequest
- ListPullRequests
- MergePullRequest
- SearchPullRequests
- UpdatePullRequestBranch
- PullRequestReviewWrite
- AddCommentToPendingReview
- RequestCopilotReview

Update tools.go to use direct function calls (removing
NewServerToolLegacy wrappers) for PR functions.

Update all tests in pullrequests_test.go to use the new
handler pattern with deps and 2-value return.

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* Refactor actions.go to use NewTool pattern

Convert all 14 tool functions in actions.go to use the NewTool pattern with
ToolDependencies for dependency injection. This is part of a broader effort
to standardize the tool implementation pattern across the codebase.

Changes:
- ListWorkflows, ListWorkflowRuns, RunWorkflow, GetWorkflowRun
- GetWorkflowRunLogs, ListWorkflowJobs, GetJobLogs
- RerunWorkflowRun, RerunFailedJobs, CancelWorkflowRun
- ListWorkflowRunArtifacts, DownloadWorkflowRunArtifact
- DeleteWorkflowRunLogs, GetWorkflowRunUsage

The new pattern:
- Takes only translations.TranslationHelperFunc as parameter
- Returns toolsets.ServerTool with Tool and Handler
- Handler receives ToolDependencies for client access
- Enables better testability and consistent interface

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* refactor(git): migrate GetRepositoryTree to NewTool pattern

* refactor(security): migrate code_scanning, secret_scanning, dependabot to NewTool pattern

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* refactor(discussions): migrate to NewTool pattern

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* Refactor security_advisories tools to use NewTool pattern

Convert 4 functions from NewServerToolLegacy wrapper to NewTool:
- ListGlobalSecurityAdvisories
- GetGlobalSecurityAdvisory
- ListRepositorySecurityAdvisories
- ListOrgRepositorySecurityAdvisories

Update tools.go toolset registration and tests.

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* refactor: convert projects, labels, and dynamic_tools to NewTool pattern

This PR converts projects.go, labels.go, and dynamic_tools.go from the
legacy NewServerToolLegacy wrapper pattern to the new NewTool pattern with
proper ToolDependencies.

Changes:
- projects.go: Convert all 9 project functions to use NewTool with
  ToolHandlerFor[map[string]any, any] and 3-return-value handlers
- projects_test.go: Update tests to use new serverTool.Handler(deps) pattern
- labels.go: Convert GetLabel, ListLabels, and LabelWrite to NewTool pattern
- labels_test.go: Update tests to use new pattern
- dynamic_tools.go: Refactor functions to return ServerTool directly
  (using NewServerToolLegacy internally since they have special dependencies)
- tools.go: Remove NewServerToolLegacy wrappers for dynamic tools registration

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>

* Add --features CLI flag for feature flag support

Add CLI flag and config support for feature flags in the local server:

- Add --features flag to main.go (StringSlice, comma-separated)
- Add EnabledFeatures field to StdioServerConfig and MCPServerConfig
- Create createFeatureChecker() that builds a set from enabled features
- Wire WithFeatureChecker() into the toolset group filter chain

This enables tools/resources/prompts that have FeatureFlagEnable set to
a flag name that is passed via --features. The checker uses a simple
set membership test for O(1) lookup.

Usage:
  github-mcp-server stdio --features=my_feature,another_feature
  GITHUB_FEATURES=my_feature github-mcp-server stdio

* Add validation tests for tools, resources, and prompts metadata

This commit adds comprehensive validation tests to ensure all MCP items
have required metadata:

- TestAllToolsHaveRequiredMetadata: Validates Toolset.ID and Annotations
- TestAllToolsHaveValidToolsetID: Ensures toolsets are in AvailableToolsets()
- TestAllResourcesHaveRequiredMetadata: Validates resource metadata
- TestAllPromptsHaveRequiredMetadata: Validates prompt metadata
- TestToolReadOnlyHintConsistency: Validates IsReadOnly() matches annotation
- TestNoDuplicate*Names: Ensures unique names across tools/resources/prompts
- TestAllToolsHaveHandlerFunc: Ensures all tools have handlers
- TestDefaultToolsetsAreValid: Validates default toolset IDs
- TestToolsetMetadataConsistency: Ensures consistent descriptions per toolset

Also fixes a bug discovered by these tests: ToolsetMetadataGit was defined
but not added to AvailableToolsets(), causing get_repository_tree to have
an invalid toolset ID.

* Fix default toolsets behavior when not in dynamic mode

When no toolsets are specified and dynamic mode is disabled, the server
should use the default toolsets. The bug was introduced when adding
dynamic toolsets support:

1. CleanToolsets(nil) was converting nil to empty slice
2. Empty slice passed to WithToolsets means 'no toolsets'
3. This resulted in zero tools being registered

Fix: Preserve nil for non-dynamic mode (nil = use defaults in WithToolsets)
and only set empty slice when dynamic mode is enabled without explicit
toolsets.

* refactor: address PR review feedback for toolsets

- Rename AddDeprecatedToolAliases to WithDeprecatedToolAliases for
  immutable filter chain consistency (returns new ToolsetGroup)
- Remove unused mockGetRawClient from generate_docs.go (use nil instead)
- Remove legacy ServerTool functions (NewServerToolLegacy and
  NewServerToolFromHandlerLegacy) - no usages
- Add panic in Handler()/RegisterFunc() when HandlerFunc is nil
- Add HasHandler() method for checking if tool has a handler
- Add tests for HasHandler and nil handler panic behavior
- Update all tests to use new WithDeprecatedToolAliases pattern

* refactor: Apply HandlerFunc pattern to resources for stateless NewToolsetGroup

This change applies the same HandlerFunc pattern used by tools to resources,
allowing NewToolsetGroup to be fully stateless (only requiring translations).

Key changes:
- Add ResourceHandlerFunc type to toolsets package
- Update ServerResourceTemplate to use HandlerFunc instead of direct Handler
- Add HasHandler() and Handler(deps) methods to ServerResourceTemplate
- Update RegisterResourceTemplates to take deps parameter
- Refactor repository resource definitions to use HandlerFunc pattern
- Make AllResources(t) stateless (only takes translations)
- Make NewToolsetGroup(t) stateless (only takes translations)
- Update generate_docs.go - no longer needs mock clients
- Update tests to use new patterns

This resolves the concern about mixed concerns in doc generation - the
toolset metadata and resource templates can now be created without any
runtime dependencies, while handlers are generated on-demand when deps
are provided during registration.

* refactor: simplify ForMCPRequest switch cases

* refactor(generate_docs): use strings.Builder and AllTools() iteration

- Replace slice joining with strings.Builder for all doc generation
- Iterate AllTools() directly instead of ToolsetIDs()/ToolsForToolset()
- Removes need for special 'dynamic' toolset handling (no tools = no output)
- Context toolset still explicitly handled for custom description
- Consistent pattern across generateToolsetsDoc, generateToolsDoc,
  generateRemoteToolsetsDoc, and generateDeprecatedAliasesTable

* feat(toolsets): add AvailableToolsets() with exclude filter

- Add AvailableToolsets() method that returns toolsets with actual tools
- Support variadic exclude parameter for filtering out specific toolsets
- Simplifies doc generation by removing manual skip logic
- Naturally excludes empty toolsets (like 'dynamic') without special cases

* refactor(generate_docs): hoist success logging to generateAllDocs

* refactor: consolidate toolset validation into ToolsetGroup

- Add Default field to ToolsetMetadata and derive defaults from metadata
- Move toolset validation into WithToolsets (trims whitespace, dedupes, tracks unrecognized)
- Add UnrecognizedToolsets() method for warning about typos
- Add DefaultToolsetIDs() method to derive defaults from metadata
- Remove redundant functions: CleanToolsets, GetValidToolsetIDs, AvailableToolsets, GetDefaultToolsetIDs
- Update DynamicTools to take ToolsetGroup for schema enum generation
- Add stubTranslator for cases needing ToolsetGroup without translations

This eliminates hardcoded toolset lists - everything is now derived from
the actual registered tools and their metadata.

* refactor: rename toolsets package to registry with builder pattern

- Rename pkg/toolsets to pkg/registry (better reflects its purpose)
- Split monolithic toolsets.go into focused files:
  - registry.go: Core Registry struct and MCP methods
  - builder.go: Builder pattern for creating Registry instances
  - filters.go: All filtering logic (toolsets, read-only, feature flags)
  - resources.go: ServerResourceTemplate type
  - prompts.go: ServerPrompt type
  - errors.go: Error types
  - server_tool.go: ServerTool and ToolsetMetadata (existing)
- Fix lint: Rename RegistryBuilder to Builder (avoid stuttering)
- Update all imports across ~45 files

This refactoring improves code organization and makes the registry's
purpose clearer. The builder pattern provides a clean API:

  reg := registry.NewBuilder().
      SetTools(tools).
      WithReadOnly(true).
      WithToolsets([]string{"repos"}).
      Build()

* fix: remove unnecessary type arguments in helper_test.go

* fix: restore correct behavior for --tools and --toolsets flags

Two behavioral regressions were fixed in resolveEnabledToolsets():

1. When --tools=X is used without --toolsets, the server should only
   register the specified tools, not the default toolsets. Now returns
   an empty slice instead of nil when EnabledTools is set.

2. When --toolsets=all --dynamic-toolsets is used, the 'all' and 'default'
   pseudo-toolsets should be removed so only the dynamic management tools
   are registered. This matches the original pre-refactor behavior.

* Move labels tools to issues toolset

Labels are closely related to issues - you add labels to issues,
search issues by label, etc. Keeping them in a separate toolset
required users to explicitly enable 'labels' to get this functionality.

Moving to issues toolset makes labels available by default since
issues is a default toolset.

* Restore labels toolset with get_label in both issues and labels

This restores conformance with the original behavior where:
- get_label is in issues toolset (read-only label access for issue workflows)
- get_label, list_label, label_write are in labels toolset (full management)

The duplicate get_label registration is intentional - it was in both toolsets
in the original implementation. Added test exception to allow this case.

* Fix instruction generation and capability advertisement

- Expand nil toolsets to default IDs before GenerateInstructions
  (nil means 'use defaults' in registry but instructions need actual names)
- Remove unconditional HasTools/HasResources/HasPrompts=true in NewServer
  (let SDK determine capabilities based on registered items, matching main)

* Add tests for dynamic toolset management tools

Tests cover:
- list_available_toolsets: verifies toolsets are listed with enabled status
- get_toolset_tools: verifies tools can be retrieved for a toolset
- enable_toolset: verifies toolset can be enabled and marked as enabled
- enable_toolset invalid: verifies proper error for non-existent toolset
- toolsets enum: verifies tools have proper enum values in schema

* Advertise all capabilities in dynamic toolsets mode

In dynamic mode, explicitly set HasTools/HasResources/HasPrompts=true
since toolsets with those capabilities can be enabled at runtime.
This ensures clients know the server supports these features even
when no tools/resources/prompts are initially registered.

* Improve conformance test with dynamic tool calls and JSON normalization

- Add dynamic tool call testing (list_available_toolsets, get_toolset_tools, enable_toolset)
- Parse and sort embedded JSON in text fields for proper comparison
- Separate progress output (stderr) from summary (stdout) for CI
- Add test type field to distinguish standard vs dynamic tests

* Add conformance-report to .gitignore

* Add conformance test CI workflow

- Runs on pull requests to main
- Compares PR branch against merge-base with origin/main
- Outputs full conformance report to GitHub Actions Job Summary
- Uploads detailed report as artifact for deeper investigation
- Does not fail the build on differences (may be intentional)

* Add map indexes for O(1) lookups in Registry

Address review feedback to use maps for collections. Added lookup maps
(toolsByName, resourcesByURI, promptsByName) while keeping slices for
ordered iteration. This provides O(1) lookup for:

- FindToolByName
- filterToolsByName (used by ForMCPRequest)
- filterResourcesByURI
- filterPromptsByName

Maps are built once during Build() and shared in ForMCPRequest copies.

* perf(registry): O(1) HasToolset lookup via pre-computed set

Add toolsetIDSet (map[ToolsetID]bool) to Registry for O(1) HasToolset lookups.
Previously HasToolset iterated through all tools, resourceTemplates, and prompts
to check if any belonged to the given toolset. Now it's a simple map lookup.

The set is populated during the single-pass processToolsets() call, which already
collected all valid toolset IDs. This adds zero new iteration - just returns the
existing validIDs map.

processToolsets now returns 6 values:
- enabledToolsets, unrecognized, toolsetIDs, toolsetIDSet, defaultToolsetIDs, descriptions

* simplify: remove lazy toolsByName map - not needed for actual use cases

FindToolByName() is only called once per request at most (to find toolset ID
for dynamic enablement). The SDK handles tool dispatch after registration.

A simple linear scan over ~90 tools is trivially fast and avoids:
- sync.Once complexity
- Map allocation
- Premature optimization for non-existent 'repeated lookups'

The pre-computed maps we keep (toolsetIDSet, etc.) are justified because
they're used for filtering logic that runs on every request.

* Add generic tool filtering mechanisms to registry package

- Add Enabled field to ServerTool for self-filtering based on context
- Add ToolFilter type and WithFilter method to Builder for cross-cutting filters
- Update isToolEnabled to check Enabled function and builder filters in order:
  1. Tool's Enabled function
  2. Feature flags (FeatureFlagEnable/FeatureFlagDisable)
  3. Read-only filter
  4. Builder filters
  5. Toolset/additional tools check
- Add FilteredTools method to Registry as alias for AvailableTools
- Add comprehensive tests for all new functionality
- All tests pass and linter is clean

Closes #1618

Co-authored-by: SamMorrowDrums <4811358+SamMorrowDrums@users.noreply.github.com>

* docs: improve filter evaluation order and FilteredTools documentation

- Add numbered filter evaluation order to isToolEnabled function doc
- Number inline comments for each filter step (1-5)
- Clarify FilteredTools error return is for future extensibility
- Document that library consumers may need to surface recoverable errors

Addresses review feedback on PR #1620

* Refactor GenerateToolsetsHelp() to use strings.Builder pattern

Co-authored-by: SamMorrowDrums <4811358+SamMorrowDrums@users.noreply.github.com>

---------

Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: SamMorrowDrums <4811358+SamMorrowDrums@users.noreply.github.com>

* Port functional changes from main to registry pattern

Port three functional improvements from main branch:
- GraphQL review comments grouped as threads (#1554)
- get_file_contents description improvement (#1582)
- create_or_update_file SHA validation fix (#1621)

Adapted implementations to use the new registry pattern with:
- BaseDeps for providing clients via ToolDependencies interface
- deps.GetClient(ctx) and deps.GetGQLClient(ctx) patterns
- Updated tests to use GraphQL mocks for review comments
- Added SHA validation test cases for create_or_update_file

* fix(e2e): Fix e2e test compilation and add rate limit handling

- Fix DefaultToolsetIDs() type mismatch by using github.GetDefaultToolsetIDs()
- Add waitForRateLimit() to check and wait for rate limits before each test
- Add skip conditions for Copilot tests when Copilot isn't available
- Use multi-line file content in TestPullRequestReviewCommentSubmit for
  multi-line review comments to work correctly
- Improve error messages to include response details

* fix(gists): Use proper GitHub API error handling for observability

The gists.go file was using NewToolResultErrorFromErr for GitHub API
errors, which breaks the error middleware tracking that the remote
server uses for observability and incident detection.

Changed API errors (client.Gists.List, Get, Create, Edit) to use
ghErrors.NewGitHubAPIErrorResponse which properly:
- Records errors in the context for middleware access
- Preserves the response object for rate limit and status tracking
- Maintains consistency with other tools that use this pattern

This ensures production observability is maintained for Gist operations.

* chore: Update server.json schema to 2025-12-11

- Update schema URL to latest version (2025-12-11)
- Remove 'status' field (now managed by registry per 2025-09-29 changelog)

* fix(get_file_contents): Restore correct implementation from #1582

The refactor incorrectly restructured the GetFileContents logic:
- Move 'if rawOpts.SHA != "" { ref = rawOpts.SHA }' before GetContents call
- Always call GetContents first (not conditionally based on path suffix)
- Restore matchFiles helper function for proper fallback handling
- Use matchFiles when Contents API fails or raw API fails

This aligns with the improvements from PR #1582 that was merged into main.

* Rename registry to inventory in comments

Update remaining references to 'registry' in code comments to use
'inventory' consistently after the package rename.

---------

Co-authored-by: Adam Holt <4619+omgitsads@users.noreply.github.com>
Co-authored-by: Adam Holt <omgitsads@users.noreply.github.com>
Co-authored-by: Adam Holt <oholt@github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: SamMorrowDrums <4811358+SamMorrowDrums@users.noreply.github.com>
2025-12-17 09:56:24 +01:00

1712 lines
62 KiB
Go

//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/v79/github"
"github.com/modelcontextprotocol/go-sdk/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
// Rate limit management
rateLimitMu sync.Mutex
)
// minRateLimitRemaining is the minimum number of API requests we want to have
// remaining before we start waiting for the rate limit to reset.
const minRateLimitRemaining = 50
// 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
}
// waitForRateLimit checks the current rate limit and waits if necessary.
// It ensures we have at least minRateLimitRemaining requests available before proceeding.
func waitForRateLimit(t *testing.T) {
rateLimitMu.Lock()
defer rateLimitMu.Unlock()
ghClient := getRESTClient(t)
ctx := context.Background()
rateLimits, _, err := ghClient.RateLimit.Get(ctx)
if err != nil {
t.Logf("Warning: failed to check rate limit: %v", err)
return
}
core := rateLimits.Core
if core.Remaining < minRateLimitRemaining {
waitDuration := time.Until(core.Reset.Time) + time.Second // Add 1 second buffer
if waitDuration > 0 {
t.Logf("Rate limit low (%d/%d remaining). Waiting %v until reset...",
core.Remaining, core.Limit, waitDuration.Round(time.Second))
time.Sleep(waitDuration)
t.Log("Rate limit reset, continuing...")
}
} else {
t.Logf("Rate limit OK: %d/%d remaining (reset in %v)",
core.Remaining, core.Limit, time.Until(core.Reset.Time).Round(time.Second))
}
}
// 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) *mcp.ClientSession {
// Check rate limit before setting up the client
waitForRateLimit(t)
// Get token and ensure Docker image is built
token := getE2EToken(t)
// Create and configure options with default to all toolsets
opts := &clientOpts{
enabledToolsets: []string{"all"},
}
// Apply all options to configure the opts struct
for _, option := range options {
option(opts)
}
ctx := context.Background()
// 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 session *mcp.ClientSession
if os.Getenv("GITHUB_MCP_SERVER_E2E_DEBUG") == "" {
ensureDockerImageBuilt(t)
// Prepare Docker arguments
args := []string{
"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
// We need to include os.Environ() so docker can find its socket and config
dockerEnvVars := append(os.Environ(),
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 using CommandTransport
t.Log("Starting Stdio MCP client...")
transport := &mcp.CommandTransport{Command: exec.Command("docker", args...)}
transport.Command.Env = dockerEnvVars
client := mcp.NewClient(&mcp.Implementation{
Name: "e2e-test-client",
Version: "0.0.1",
}, nil)
var err error
session, err = client.Connect(ctx, transport, nil)
require.NoError(t, err, "expected to connect 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.GetDefaultToolsetIDs()
}
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...")
serverTransport, clientTransport := mcp.NewInMemoryTransports()
go func() {
_ = ghServer.Run(ctx, serverTransport)
}()
client := mcp.NewClient(&mcp.Implementation{
Name: "e2e-test-client",
Version: "0.0.1",
}, nil)
session, err = client.Connect(ctx, clientTransport, nil)
require.NoError(t, err, "expected to create in-process client successfully")
}
t.Cleanup(func() {
require.NoError(t, session.Close(), "expected to close client successfully")
})
return session
}
func TestGetMe(t *testing.T) {
t.Parallel()
mcpClient := setupMCPClient(t)
ctx := context.Background()
// When we call the "get_me" tool
response, err := mcpClient.CallTool(ctx, &mcp.CallToolParams{Name: "get_me"})
require.NoError(t, err, "expected to call 'get_me' tool successfully")
require.False(t, response.IsError, fmt.Sprintf("expected result not to be an error: %+v", response))
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()
response, err := mcpClient.ListTools(ctx, &mcp.ListToolsParams{})
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("issue_read"), "expected to find 'issue_read' tool")
require.True(t, toolsContains("list_branches"), "expected to find 'list_branches' tool")
require.False(t, toolsContains("pull_request_read"), "expected not to find 'pull_request_read' tool")
}
func TestTags(t *testing.T) {
t.Parallel()
mcpClient := setupMCPClient(t)
ctx := context.Background()
// First, who am I
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, &mcp.CallToolParams{Name: "get_me"})
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())
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_repository",
Arguments: map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
},
})
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.CreateTag{
Tag: "v0.0.1",
Message: "v0.0.1",
Object: *ref.Object.SHA,
Type: "commit",
})
require.NoError(t, err, "expected to create tag object successfully")
_, _, err = ghClient.Git.CreateRef(context.Background(), currentOwner, repoName, gogithub.CreateRef{
Ref: "refs/tags/v0.0.1",
SHA: *tagObj.SHA,
})
require.NoError(t, err, "expected to create tag ref successfully")
// List the tags
t.Logf("Listing tags for %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "list_tags",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
},
})
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
t.Logf("Getting tag %s/%s:%s...", currentOwner, repoName, "v0.0.1")
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "get_tag",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"tag": "v0.0.1",
},
})
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
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, &mcp.CallToolParams{Name: "get_me"})
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())
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_repository",
Arguments: map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
},
})
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
t.Logf("Creating branch in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_branch",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"branch": "test-branch",
"from_branch": "main",
},
})
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
t.Logf("Creating commit with new file in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_or_update_file",
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",
},
})
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
t.Logf("Getting file contents in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "get_file_contents",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-file.txt",
"ref": "refs/heads/test-branch",
},
})
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")
// Access Resource directly - ResourceContents is a pointer, not an interface
textResource := embeddedResource.Resource
require.NotNil(t, textResource, "expected embedded resource to have Resource")
require.Equal(t, fmt.Sprintf("Created by e2e test %s", t.Name()), textResource.Text, "expected file content to match")
// Delete the file
t.Logf("Deleting file in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "delete_file",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-file.txt",
"message": "Delete test file",
"branch": "test-branch",
},
})
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
t.Logf("Listing commits in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "list_commits",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"sha": "test-branch", // can be SHA or branch, which is an unfortunate API design
},
})
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
t.Logf("Getting commit %s/%s:%s...", currentOwner, repoName, deletionCommit.SHA)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "get_commit",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"sha": deletionCommit.SHA,
},
})
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
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, &mcp.CallToolParams{Name: "get_me"})
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())
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_repository",
Arguments: map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
},
})
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
t.Logf("Creating branch in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_branch",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"branch": "test-branch",
"from_branch": "main",
},
})
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
t.Logf("Creating commit with new file in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_or_update_file",
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",
},
})
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))
_, ok = resp.Content[0].(*mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
// Check the file exists
t.Logf("Getting file contents in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "get_file_contents",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-dir/test-file.txt",
"ref": "refs/heads/test-branch",
},
})
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")
// Access Resource directly - ResourceContents is a pointer, not an interface
textResource := embeddedResource.Resource
require.NotNil(t, textResource, "expected embedded resource to have Resource")
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
t.Logf("Deleting directory in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "delete_file",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-dir/test-file.txt",
"message": "Delete test directory",
"branch": "test-branch",
},
})
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
t.Logf("Listing commits in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "list_commits",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"sha": "test-branch", // can be SHA or branch, which is an unfortunate API design
},
})
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")
// Find the deletion commit (list_commits returns in reverse chronological order,
// but timing can sometimes cause unexpected ordering)
// TODO: The delete_file tool only deletes individual files, not directories.
// This test creates a file in test-dir/ and deletes it, but doesn't actually
// test recursive directory deletion. We should either:
// 1. Rename TestDirectoryDeletion to TestFileDeletionInSubdirectory
// 2. Implement actual directory deletion in the MCP server (delete all files in dir)
// 3. Create multiple files and verify all are deleted
var deletionCommit *struct {
SHA string `json:"sha"`
Commit struct {
Message string `json:"message"`
}
Files []struct {
Filename string `json:"filename"`
Deletions int `json:"deletions"`
} `json:"files"`
}
for i := range trimmedListCommitsText {
if trimmedListCommitsText[i].Commit.Message == "Delete test directory" {
deletionCommit = &trimmedListCommitsText[i]
break
}
}
require.NotNil(t, deletionCommit, "expected to find a commit with message 'Delete test directory'")
// Now get the commit so we can look at the file changes because list_commits doesn't include them
t.Logf("Getting commit %s/%s:%s...", currentOwner, repoName, deletionCommit.SHA)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "get_commit",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"sha": deletionCommit.SHA,
},
})
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
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, &mcp.CallToolParams{Name: "get_me"})
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())
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_repository",
Arguments: map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
},
})
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
t.Logf("Creating branch in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_branch",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"branch": "test-branch",
"from_branch": "main",
},
})
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
t.Logf("Creating commit with new file in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_or_update_file",
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",
},
})
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
t.Logf("Creating pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_pull_request",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"title": "Test PR",
"body": "This is a test PR",
"head": "test-branch",
"base": "main",
"commitID": commitID,
},
})
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
t.Logf("Requesting Copilot review for pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "request_copilot_review",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
},
})
require.NoError(t, err, "expected to call 'request_copilot_review' tool successfully")
// Check if Copilot is available - skip if not
if resp.IsError {
if tc, ok := resp.Content[0].(*mcp.TextContent); ok {
if strings.Contains(tc.Text, "copilot") || strings.Contains(tc.Text, "Copilot") {
t.Skip("skipping because copilot isn't available as a reviewer on this repository")
}
}
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 if Copilot was added as a reviewer - skip if not available
if len(reviewRequests.Users) == 0 {
t.Skip("skipping because copilot wasn't added as a reviewer (likely not enabled for this account)")
}
// 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
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, &mcp.CallToolParams{Name: "get_me"})
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())
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_repository",
Arguments: map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
},
})
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
t.Logf("Creating issue in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "issue_write",
Arguments: map[string]any{
"method": "create",
"owner": currentOwner,
"repo": repoName,
"title": "Test issue to assign copilot to",
},
})
require.NoError(t, err, "expected to call 'issue_write' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Assign copilot to the issue
t.Logf("Assigning copilot to issue in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "assign_copilot_to_issue",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"issueNumber": 1,
},
})
require.NoError(t, err, "expected to call 'assign_copilot_to_issue' tool successfully")
textContent, ok = resp.Content[0].(*mcp.TextContent)
require.True(t, ok, "expected content to be of type TextContent")
possibleExpectedFailure := "copilot isn't available as an assignee for this issue. Please inform the user to visit https://docs.github.com/en/copilot/using-github-copilot/using-copilot-coding-agent-to-work-on-tasks/about-assigning-tasks-to-copilot for more information."
if resp.IsError && textContent.Text == possibleExpectedFailure {
t.Skip("skipping because copilot wasn't available as an assignee on this issue, it's likely that the owner doesn't have copilot enabled in their settings")
}
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
require.Equal(t, "successfully assigned copilot to issue", textContent.Text)
// Check that copilot is assigned to the issue
// MCP Server doesn't support getting assignees yet
ghClient := getRESTClient(t)
assignees, response, err := ghClient.Issues.Get(context.Background(), currentOwner, repoName, 1)
require.NoError(t, err, "expected to get issue successfully")
require.Equal(t, http.StatusOK, response.StatusCode, "expected to get issue successfully")
require.Len(t, assignees.Assignees, 1, "expected to find one assignee")
require.Equal(t, "Copilot", *assignees.Assignees[0].Login, "expected copilot to be assigned to the issue")
}
func TestPullRequestAtomicCreateAndSubmit(t *testing.T) {
t.Parallel()
mcpClient := setupMCPClient(t)
ctx := context.Background()
// First, who am I
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, &mcp.CallToolParams{Name: "get_me"})
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())
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_repository",
Arguments: map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
},
})
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
t.Logf("Creating branch in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_branch",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"branch": "test-branch",
"from_branch": "main",
},
})
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
t.Logf("Creating commit with new file in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_or_update_file",
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",
},
})
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 {
Commit struct {
SHA string `json:"sha"`
} `json:"commit"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedCommitText)
require.NoError(t, err, "expected to unmarshal text content successfully")
commitID := trimmedCommitText.Commit.SHA
// Create a pull request
t.Logf("Creating pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_pull_request",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"title": "Test PR",
"body": "This is a test PR",
"head": "test-branch",
"base": "main",
"commitID": commitID,
},
})
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))
// Create and submit a review
t.Logf("Creating and submitting review for pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "pull_request_review_write",
Arguments: map[string]any{
"method": "create",
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
"event": "COMMENT", // the only event we can use as the creator of the PR
"body": "Looks good if you like bad code I guess!",
"commitID": commitID,
},
})
require.NoError(t, err, "expected to call 'pull_request_review_write' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Finally, get the list of reviews and see that our review has been submitted
t.Logf("Getting reviews for pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "pull_request_read",
Arguments: map[string]any{
"method": "get_reviews",
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
},
})
require.NoError(t, err, "expected to call 'pull_request_read' 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 reviews []struct {
State string `json:"state"`
}
err = json.Unmarshal([]byte(textContent.Text), &reviews)
require.NoError(t, err, "expected to unmarshal text content successfully")
// Check that there is one review
require.Len(t, reviews, 1, "expected to find one review")
require.Equal(t, "COMMENTED", reviews[0].State, "expected review state to be COMMENTED")
}
func TestPullRequestReviewCommentSubmit(t *testing.T) {
t.Parallel()
mcpClient := setupMCPClient(t)
ctx := context.Background()
// First, who am I
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, &mcp.CallToolParams{Name: "get_me"})
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())
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_repository",
Arguments: map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
},
})
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 a branch on which to create a new commit
t.Logf("Creating branch in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_branch",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"branch": "test-branch",
"from_branch": "main",
},
})
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 (multi-line content to support multi-line review comments)
t.Logf("Creating commit with new file in %s/%s...", currentOwner, repoName)
multiLineContent := fmt.Sprintf("Line 1: Created by e2e test %s\nLine 2: Additional content for multi-line comments\nLine 3: More content", t.Name())
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_or_update_file",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"path": "test-file.txt",
"content": multiLineContent,
"message": "Add test file",
"branch": "test-branch",
},
})
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 {
Commit struct {
SHA string `json:"sha"`
} `json:"commit"`
}
err = json.Unmarshal([]byte(textContent.Text), &trimmedCommitText)
require.NoError(t, err, "expected to unmarshal text content successfully")
commitID := trimmedCommitText.Commit.SHA
// Create a pull request
t.Logf("Creating pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_pull_request",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"title": "Test PR",
"body": "This is a test PR",
"head": "test-branch",
"base": "main",
"commitID": commitID,
},
})
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))
// Create a review for the pull request, but we can't approve it
// because the current owner also owns the PR.
t.Logf("Creating pending review for pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "pull_request_review_write",
Arguments: map[string]any{
"method": "create",
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
},
})
require.NoError(t, err, "expected to call 'pull_request_review_write' 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, "pending pull request created", textContent.Text)
// Add a file review comment
// TODO: FILE-level comments are silently dropped by GitHub API when:
// - The comment targets the wrong side of a diff
// - The comment targets a deleted part of a diff
// - The comment targets a line outside the actual diff range
// This test currently doesn't verify FILE-level comments are created because
// ListReviewComments API doesn't return them. We should investigate proper
// FILE-level comment parameters or use a different API to verify.
t.Logf("Adding file review comment to pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "add_comment_to_pending_review",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
"path": "test-file.txt",
"subjectType": "FILE",
"body": "File review comment",
"side": "RIGHT",
},
})
require.NoError(t, err, "expected to call 'add_comment_to_pending_review' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Add a single line review comment
t.Logf("Adding single line review comment to pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "add_comment_to_pending_review",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
"path": "test-file.txt",
"subjectType": "LINE",
"body": "Single line review comment",
"line": 1,
"side": "RIGHT",
"commitID": commitID,
},
})
require.NoError(t, err, "expected to call 'add_comment_to_pending_review' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Add a multiline review comment
t.Logf("Adding multi line review comment to pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "add_comment_to_pending_review",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
"path": "test-file.txt",
"subjectType": "LINE",
"body": "Multiline review comment",
"startLine": 1,
"line": 2,
"startSide": "RIGHT",
"side": "RIGHT",
"commitID": commitID,
},
})
require.NoError(t, err, "expected to call 'add_comment_to_pending_review' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Submit the review
t.Logf("Submitting review for pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "pull_request_review_write",
Arguments: map[string]any{
"method": "submit_pending",
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
"event": "COMMENT", // the only event we can use as the creator of the PR
"body": "Looks good if you like bad code I guess!",
},
})
require.NoError(t, err, "expected to call 'pull_request_review_write' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// Finally, get the review and see that it has been created
t.Logf("Getting reviews for pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "pull_request_read",
Arguments: map[string]any{
"method": "get_reviews",
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
},
})
require.NoError(t, err, "expected to call 'pull_request_read' 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 reviews []struct {
ID int `json:"id"`
State string `json:"state"`
}
err = json.Unmarshal([]byte(textContent.Text), &reviews)
require.NoError(t, err, "expected to unmarshal text content successfully")
// Check that there is one review
require.Len(t, reviews, 1, "expected to find one review")
require.Equal(t, "COMMENTED", reviews[0].State, "expected review state to be COMMENTED")
// Check that there are review comments
// MCP Server doesn't support this, but we can use the GitHub Client
// Note: FILE-level comments may not be returned by ListReviewComments API,
// so we expect at least the LINE-level comments (single-line and multi-line)
ghClient := getRESTClient(t)
comments, _, err := ghClient.PullRequests.ListReviewComments(context.Background(), currentOwner, repoName, 1, int64(reviews[0].ID), nil)
require.NoError(t, err, "expected to list review comments successfully")
require.GreaterOrEqual(t, len(comments), 2, "expected to find at least two review comments (LINE-level)")
}
func TestPullRequestReviewDeletion(t *testing.T) {
t.Parallel()
mcpClient := setupMCPClient(t)
ctx := context.Background()
// First, who am I
t.Log("Getting current user...")
resp, err := mcpClient.CallTool(ctx, &mcp.CallToolParams{Name: "get_me"})
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())
t.Logf("Creating repository %s/%s...", currentOwner, repoName)
_, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_repository",
Arguments: map[string]any{
"name": repoName,
"private": true,
"autoInit": true,
},
})
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
t.Logf("Creating branch in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_branch",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"branch": "test-branch",
"from_branch": "main",
},
})
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
t.Logf("Creating commit with new file in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_or_update_file",
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",
},
})
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))
// Create a pull request
t.Logf("Creating pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "create_pull_request",
Arguments: map[string]any{
"owner": currentOwner,
"repo": repoName,
"title": "Test PR",
"body": "This is a test PR",
"head": "test-branch",
"base": "main",
},
})
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))
// Create a review for the pull request, but we can't approve it
// because the current owner also owns the PR.
t.Logf("Creating pending review for pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "pull_request_review_write",
Arguments: map[string]any{
"method": "create",
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
},
})
require.NoError(t, err, "expected to call 'pull_request_review_write' 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, "pending pull request created", textContent.Text)
// See that there is a pending review
t.Logf("Getting reviews for pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "pull_request_read",
Arguments: map[string]any{
"method": "get_reviews",
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
},
})
require.NoError(t, err, "expected to call 'pull_request_read' 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 reviews []struct {
State string `json:"state"`
}
err = json.Unmarshal([]byte(textContent.Text), &reviews)
require.NoError(t, err, "expected to unmarshal text content successfully")
// Check that there is one review
require.Len(t, reviews, 1, "expected to find one review")
require.Equal(t, "PENDING", reviews[0].State, "expected review state to be PENDING")
// Delete the review
t.Logf("Deleting review for pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "pull_request_review_write",
Arguments: map[string]any{
"method": "delete_pending",
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
},
})
require.NoError(t, err, "expected to call 'pull_request_review_write' tool successfully")
require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp))
// See that there are no reviews
t.Logf("Getting reviews for pull request in %s/%s...", currentOwner, repoName)
resp, err = mcpClient.CallTool(ctx, &mcp.CallToolParams{
Name: "pull_request_read",
Arguments: map[string]any{
"method": "get_reviews",
"owner": currentOwner,
"repo": repoName,
"pullNumber": 1,
},
})
require.NoError(t, err, "expected to call 'pull_request_read' 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 noReviews []struct{}
err = json.Unmarshal([]byte(textContent.Text), &noReviews)
require.NoError(t, err, "expected to unmarshal text content successfully")
require.Len(t, noReviews, 0, "expected to find no reviews")
}