Files
github--github-mcp-server/pkg/github/server_test.go
Ross Tarrant f929c58c6b feat: Add CSV output format for default list tools under insiders mode (#2450)
* Add CSV output for list tools under insiders mode

* fix: resolve rebase feature flag conflicts

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Simplify feature-flag handling: collapse CSV dual-variant + skip filtering when no checker (#2516)

* refactor: generic toolset+name sort, clarify feature flag intent

Address review feedback on #2450:

- Collapse the three near-identical sort helpers in pkg/inventory/filters.go
  into a generic sortByToolsetThenName so adding new inventory item types
  doesn't require copying the comparator.
- Expand the doc comments on the three *WithoutFeatureFiltering helpers to
  spell out why they exist: HTTP mode builds a static (process-wide)
  inventory as an upper bound, but per-request feature flags from headers
  (X-MCP-Features, X-MCP-Insiders) are evaluated later, so feature-flagged
  variants must be preserved here.
- Strengthen the doc comment on ResolveFeatureFlags to make the contract
  explicit: user-supplied flags are validated against AllowedFeatureFlags,
  but insiders expansion deliberately is not — InsidersFeatureFlags may
  include server-controlled flags that are not user-toggleable.

CORS comments are intentionally left for the PR author.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* docs(feature-flags): clarify allowed and insiders sets are independent

Also add tests covering:
- a user-toggleable flag (FeatureFlagIssuesGranular) that insiders does
  not turn on automatically
- insiders mode not turning on user-only allowed flags

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* refactor(inventory): collapse three *WithoutFeatureFiltering helpers into StaticUpperBound

The three parallel methods (AvailableToolsWithoutFeatureFiltering,
AvailableResourceTemplatesWithoutFeatureFiltering,
AvailablePromptsWithoutFeatureFiltering) were always called as a triple
in exactly two places: HTTP buildStaticInventory and its test mirror.
They exist because the dual-variant pattern (sibling tools with mirrored
FeatureFlagEnable / FeatureFlagDisable on the same name, e.g. CSV output)
makes feature filtering at static-build time impossible — both variants
must be kept and resolved per-request.

Replace the three with one method, Inventory.StaticUpperBound(ctx), that
returns (tools, resources, prompts) and carries the rationale in its
doc comment. Reduces API surface, eliminates the triplication, and makes
the single "skip feature filtering" concept obvious to readers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* refactor: simplify feature-flag handling

Two related simplifications, both about treating insiders as a meta flag
that expands once at startup and then stops mattering:

- Collapse CSV's dual-variant pattern into a single tool whose handler
  performs a runtime feature-flag check via deps.IsFeatureEnabled. CSV
  is a pure response-format toggle, not a schema change, so it does not
  need the dual-name pattern that genuine schema variants (granular
  issues/PRs) still use.

- When no feature checker is installed, skip feature-flag filtering and
  return the full upper bound. The static HTTP inventory now uses plain
  AvailableTools/Resources/Prompts; the per-request inventory always
  installs a checker, so MCP registration (which serves a tool name once)
  always sees a deduplicated set. The bespoke StaticUpperBound helper and
  the isToolEnabledWithFeatureFlags split go away.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* ci(mcp-diff): add insiders + per-feature configs

The mcp-diff matrix now includes:
  - --insiders (and --insiders --read-only)
  - one config per github.AllowedFeatureFlags entry, generated by
    script/print-mcp-diff-configs so new user-controllable flags get
    diffed automatically without editing the workflow

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* docs(insiders): explain feature-flag resolution for contributors

Adds a 'How feature flags are resolved' section covering:
  - Insiders is a meta flag, like 'all'/'default' for toolsets
  - User input -> allowlist filter -> insiders expansion ->
    server-side fallback (remote only)
  - AllowedFeatureFlags vs InsidersFeatureFlags are independent
  - How to add a new feature flag, including the
    TestGitHubPackageDoesNotReadInsidersMode guard

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* refactor(inventory): make feature-flag gating a regular ToolFilter

Move tool feature-flag evaluation out of isToolEnabled and into a
ToolFilter installed at the head of the pipeline by Build() when
WithFeatureChecker received a non-nil checker. The 'no checker = no
filtering' contract is now expressed structurally (the filter isn't
installed) instead of by a runtime nil check inside the helper.

Resources and prompts have no filter pipeline, so they call the now-pure
featureFlagAllowed helper behind an explicit r.featureChecker != nil
guard at the iteration site.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* perf(inventory): cache extracted toolset IDs in sort comparator

Avoid evaluating the extractor closures up to three times per comparison.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: correct MCP features header in cors

* docs: regenerate README for CSV output toolset

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix: remove duplicate MCPFeaturesHeader from CORS headers

* ci(mcp-diff): add streamable-http job with header-based configs

Adds a sibling mcp-diff-http job that exercises the streamable-http
transport against a shared HTTP server, with per-config settings supplied
via X-MCP-* request headers — mirroring how the remote server is invoked
in production (server-side defaults + per-user header overrides).

The config generator gains a -transport flag:
- stdio (default, unchanged behaviour)
- http-headers (emits headers-only configs targeting a shared server)

Two new combined entries layer multiple headers together as a smoke test
for header-merging regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* docs: regenerate after merging main

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Sam Morrow <info@sam-morrow.com>
Co-authored-by: sammorrowdrums <sammorrowdrums@github.com>
2026-05-21 16:50:55 +02:00

353 lines
10 KiB
Go

package github
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"strings"
"testing"
"time"
"github.com/github/github-mcp-server/pkg/lockdown"
"github.com/github/github-mcp-server/pkg/observability"
"github.com/github/github-mcp-server/pkg/observability/metrics"
"github.com/github/github-mcp-server/pkg/raw"
"github.com/github/github-mcp-server/pkg/translations"
gogithub "github.com/google/go-github/v87/github"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/shurcooL/githubv4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// stubDeps is a test helper that implements ToolDependencies with configurable behavior.
// Use this when you need to test error paths or when you need closure-based client creation.
type stubDeps struct {
clientFn func(context.Context) (*gogithub.Client, error)
gqlClientFn func(context.Context) (*githubv4.Client, error)
rawClientFn func(context.Context) (*raw.Client, error)
repoAccessCache *lockdown.RepoAccessCache
t translations.TranslationHelperFunc
flags FeatureFlags
contentWindowSize int
obsv observability.Exporters
}
func (s stubDeps) GetClient(ctx context.Context) (*gogithub.Client, error) {
if s.clientFn != nil {
return s.clientFn(ctx)
}
return nil, nil
}
func (s stubDeps) GetGQLClient(ctx context.Context) (*githubv4.Client, error) {
if s.gqlClientFn != nil {
return s.gqlClientFn(ctx)
}
return nil, nil
}
func (s stubDeps) GetRawClient(ctx context.Context) (*raw.Client, error) {
if s.rawClientFn != nil {
return s.rawClientFn(ctx)
}
return nil, nil
}
func (s stubDeps) GetRepoAccessCache(_ context.Context) (*lockdown.RepoAccessCache, error) {
return s.repoAccessCache, nil
}
func (s stubDeps) GetT() translations.TranslationHelperFunc { return s.t }
func (s stubDeps) GetFlags(_ context.Context) FeatureFlags { return s.flags }
func (s stubDeps) GetContentWindowSize() int { return s.contentWindowSize }
func (s stubDeps) IsFeatureEnabled(_ context.Context, _ string) bool { return false }
func (s stubDeps) Logger(_ context.Context) *slog.Logger {
return s.obsv.Logger()
}
func (s stubDeps) Metrics(ctx context.Context) metrics.Metrics {
return s.obsv.Metrics(ctx)
}
// Helper functions to create stub client functions for error testing
// stubExporters returns a discard-logger + noop-metrics Exporters for tests.
func stubExporters() observability.Exporters {
obs, _ := observability.NewExporters(slog.New(slog.DiscardHandler), metrics.NewNoopMetrics())
return obs
}
func stubClientFnFromHTTP(t *testing.T, httpClient *http.Client) func(context.Context) (*gogithub.Client, error) {
t.Helper()
return func(_ context.Context) (*gogithub.Client, error) {
return mustNewGHClient(t, httpClient), nil
}
}
func stubClientFnErr(errMsg string) func(context.Context) (*gogithub.Client, error) {
return func(_ context.Context) (*gogithub.Client, error) {
return nil, errors.New(errMsg)
}
}
func stubGQLClientFnErr(errMsg string) func(context.Context) (*githubv4.Client, error) {
return func(_ context.Context) (*githubv4.Client, error) {
return nil, errors.New(errMsg)
}
}
func stubRepoAccessCache(restClient *gogithub.Client, ttl time.Duration) *lockdown.RepoAccessCache {
cacheName := fmt.Sprintf("repo-access-cache-test-%d", time.Now().UnixNano())
return lockdown.NewRepoAccessCache(
githubv4.NewClient(newRepoAccessHTTPClient()),
restClient,
lockdown.WithTTL(ttl),
lockdown.WithCacheName(cacheName),
)
}
func mockRESTPermissionServer(t *testing.T, defaultPerm string, overrides map[string]string) *gogithub.Client {
t.Helper()
return mustNewGHClient(t, MockHTTPClientWithHandler(func(w http.ResponseWriter, r *http.Request) {
perm := defaultPerm
for user, p := range overrides {
if strings.Contains(r.URL.Path, "/collaborators/"+user+"/") {
perm = p
break
}
}
resp := gogithub.RepositoryPermissionLevel{
Permission: gogithub.Ptr(perm),
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
}))
}
func stubFeatureFlags(enabledFlags map[string]bool) FeatureFlags {
return FeatureFlags{
LockdownMode: enabledFlags["lockdown-mode"],
}
}
func badRequestHandler(msg string) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
structuredErrorResponse := gogithub.ErrorResponse{
Message: msg,
}
b, err := json.Marshal(structuredErrorResponse)
if err != nil {
http.Error(w, "failed to marshal error response", http.StatusInternalServerError)
}
http.Error(w, string(b), http.StatusBadRequest)
}
}
// TestNewMCPServer_CreatesSuccessfully verifies that the server can be created
// with the deps injection middleware properly configured.
func TestNewMCPServer_CreatesSuccessfully(t *testing.T) {
t.Parallel()
// Create a minimal server configuration
cfg := MCPServerConfig{
Version: "test",
Host: "", // defaults to github.com
Token: "test-token",
EnabledToolsets: []string{"context"},
ReadOnly: false,
Translator: translations.NullTranslationHelper,
ContentWindowSize: 5000,
LockdownMode: false,
}
deps := stubDeps{obsv: stubExporters()}
// Build inventory
inv, err := NewInventory(cfg.Translator).
WithDeprecatedAliases(DeprecatedToolAliases).
WithToolsets(cfg.EnabledToolsets).
Build()
require.NoError(t, err, "expected inventory build to succeed")
// Create the server
server, err := NewMCPServer(context.Background(), &cfg, deps, inv)
require.NoError(t, err, "expected server creation to succeed")
require.NotNil(t, server, "expected server to be non-nil")
// The fact that the server was created successfully indicates that:
// 1. The deps injection middleware is properly added
// 2. Tools can be registered without panicking
//
// If the middleware wasn't properly added, tool calls would panic with
// "ToolDependencies not found in context" when executed.
//
// The actual middleware functionality and tool execution with ContextWithDeps
// is already tested in pkg/github/*_test.go.
}
// TestNewServer_NameAndTitleViaTranslation verifies that server name and title
// can be overridden via the translation helper (GITHUB_MCP_SERVER_NAME /
// GITHUB_MCP_SERVER_TITLE env vars or github-mcp-server-config.json) and
// fall back to sensible defaults when not overridden.
func TestNewServer_NameAndTitleViaTranslation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
translator translations.TranslationHelperFunc
expectedName string
expectedTitle string
}{
{
name: "defaults when using NullTranslationHelper",
translator: translations.NullTranslationHelper,
expectedName: "github-mcp-server",
expectedTitle: "GitHub MCP Server",
},
{
name: "custom name and title via translator",
translator: func(key, defaultValue string) string {
switch key {
case "SERVER_NAME":
return "my-github-server"
case "SERVER_TITLE":
return "My GitHub MCP Server"
default:
return defaultValue
}
},
expectedName: "my-github-server",
expectedTitle: "My GitHub MCP Server",
},
{
name: "custom name only via translator",
translator: func(key, defaultValue string) string {
if key == "SERVER_NAME" {
return "ghes-server"
}
return defaultValue
},
expectedName: "ghes-server",
expectedTitle: "GitHub MCP Server",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
srv := NewServer("v1.0.0", tt.translator("SERVER_NAME", "github-mcp-server"), tt.translator("SERVER_TITLE", "GitHub MCP Server"), nil)
require.NotNil(t, srv)
// Connect a client to retrieve the initialize result and verify ServerInfo.
st, ct := mcp.NewInMemoryTransports()
client := mcp.NewClient(&mcp.Implementation{Name: "test-client"}, nil)
type clientResult struct {
result *mcp.InitializeResult
err error
}
clientResultCh := make(chan clientResult, 1)
go func() {
cs, err := client.Connect(context.Background(), ct, nil)
if err != nil {
clientResultCh <- clientResult{err: err}
return
}
t.Cleanup(func() { _ = cs.Close() })
clientResultCh <- clientResult{result: cs.InitializeResult()}
}()
ss, err := srv.Connect(context.Background(), st, nil)
require.NoError(t, err)
t.Cleanup(func() { _ = ss.Close() })
got := <-clientResultCh
require.NoError(t, got.err)
require.NotNil(t, got.result)
require.NotNil(t, got.result.ServerInfo)
assert.Equal(t, tt.expectedName, got.result.ServerInfo.Name)
assert.Equal(t, tt.expectedTitle, got.result.ServerInfo.Title)
})
}
}
// TestResolveEnabledToolsets verifies the toolset resolution logic.
func TestResolveEnabledToolsets(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cfg MCPServerConfig
expectedResult []string
}{
{
name: "nil toolsets and no tools - use defaults",
cfg: MCPServerConfig{
EnabledToolsets: nil,
EnabledTools: nil,
},
expectedResult: nil, // nil means "use defaults"
},
{
name: "explicit toolsets",
cfg: MCPServerConfig{
EnabledToolsets: []string{"repos", "issues"},
},
expectedResult: []string{"repos", "issues"},
},
{
name: "empty toolsets - disable all",
cfg: MCPServerConfig{
EnabledToolsets: []string{},
},
expectedResult: []string{},
},
{
name: "specific tools without toolsets - no default toolsets",
cfg: MCPServerConfig{
EnabledToolsets: nil,
EnabledTools: []string{"get_me"},
},
expectedResult: []string{}, // empty slice when tools specified but no toolsets
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := ResolvedEnabledToolsets(tc.cfg.EnabledToolsets, tc.cfg.EnabledTools)
assert.Equal(t, tc.expectedResult, result)
})
}
}
func TestCompletionsHandler_RejectsMissingRef(t *testing.T) {
getClient := func(_ context.Context) (*gogithub.Client, error) {
return &gogithub.Client{}, nil
}
handler := CompletionsHandler(getClient)
tests := []struct {
name string
req *mcp.CompleteRequest
}{
{name: "nil request", req: nil},
{name: "nil params", req: &mcp.CompleteRequest{}},
{name: "nil ref", req: &mcp.CompleteRequest{Params: &mcp.CompleteParams{}}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result, err := handler(context.Background(), tc.req)
require.Error(t, err)
assert.Nil(t, result)
assert.Contains(t, err.Error(), "missing required parameter: ref")
})
}
}