f929c58c6b
* 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>
218 lines
6.6 KiB
Go
218 lines
6.6 KiB
Go
package github
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/github/github-mcp-server/pkg/inventory"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// stubTranslation is a simple translation function for testing
|
|
func stubTranslation(_, fallback string) string {
|
|
return fallback
|
|
}
|
|
|
|
// TestAllToolsHaveRequiredMetadata validates that all tools have mandatory metadata:
|
|
// - Toolset must be set (non-empty ID)
|
|
// - ReadOnlyHint annotation must be explicitly set (not nil)
|
|
func TestAllToolsHaveRequiredMetadata(t *testing.T) {
|
|
tools := AllTools(stubTranslation)
|
|
|
|
require.NotEmpty(t, tools, "AllTools should return at least one tool")
|
|
|
|
for _, tool := range tools {
|
|
t.Run(tool.Tool.Name, func(t *testing.T) {
|
|
// Toolset ID must be set
|
|
assert.NotEmpty(t, tool.Toolset.ID,
|
|
"Tool %q must have a Toolset.ID", tool.Tool.Name)
|
|
|
|
// Toolset description should be set for documentation
|
|
assert.NotEmpty(t, tool.Toolset.Description,
|
|
"Tool %q should have a Toolset.Description", tool.Tool.Name)
|
|
|
|
// Annotations must exist and have ReadOnlyHint explicitly set
|
|
require.NotNil(t, tool.Tool.Annotations,
|
|
"Tool %q must have Annotations set (for ReadOnlyHint)", tool.Tool.Name)
|
|
|
|
// We can't distinguish between "not set" and "set to false" for a bool,
|
|
// but having Annotations non-nil confirms the developer thought about it.
|
|
// The ReadOnlyHint value itself is validated by ensuring Annotations exist.
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestAllResourcesHaveRequiredMetadata validates that all resources have mandatory metadata
|
|
func TestAllResourcesHaveRequiredMetadata(t *testing.T) {
|
|
// Resources are now stateless - no client functions needed
|
|
resources := AllResources(stubTranslation)
|
|
|
|
require.NotEmpty(t, resources, "AllResources should return at least one resource")
|
|
|
|
for _, res := range resources {
|
|
t.Run(res.Template.Name, func(t *testing.T) {
|
|
// Toolset ID must be set
|
|
assert.NotEmpty(t, res.Toolset.ID,
|
|
"Resource %q must have a Toolset.ID", res.Template.Name)
|
|
|
|
// HandlerFunc must be set
|
|
assert.True(t, res.HasHandler(),
|
|
"Resource %q must have a HandlerFunc", res.Template.Name)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestAllPromptsHaveRequiredMetadata validates that all prompts have mandatory metadata
|
|
func TestAllPromptsHaveRequiredMetadata(t *testing.T) {
|
|
prompts := AllPrompts(stubTranslation)
|
|
|
|
require.NotEmpty(t, prompts, "AllPrompts should return at least one prompt")
|
|
|
|
for _, prompt := range prompts {
|
|
t.Run(prompt.Prompt.Name, func(t *testing.T) {
|
|
// Toolset ID must be set
|
|
assert.NotEmpty(t, prompt.Toolset.ID,
|
|
"Prompt %q must have a Toolset.ID", prompt.Prompt.Name)
|
|
|
|
// Handler must be set
|
|
assert.NotNil(t, prompt.Handler,
|
|
"Prompt %q must have a Handler", prompt.Prompt.Name)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestToolReadOnlyHintConsistency validates that read-only tools are correctly annotated
|
|
func TestToolReadOnlyHintConsistency(t *testing.T) {
|
|
tools := AllTools(stubTranslation)
|
|
|
|
for _, tool := range tools {
|
|
t.Run(tool.Tool.Name, func(t *testing.T) {
|
|
require.NotNil(t, tool.Tool.Annotations,
|
|
"Tool %q must have Annotations", tool.Tool.Name)
|
|
|
|
// Verify IsReadOnly() method matches the annotation
|
|
assert.Equal(t, tool.Tool.Annotations.ReadOnlyHint, tool.IsReadOnly(),
|
|
"Tool %q: IsReadOnly() should match Annotations.ReadOnlyHint", tool.Tool.Name)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestNoDuplicateToolNames ensures all tools have unique names
|
|
func TestNoDuplicateToolNames(t *testing.T) {
|
|
tools := AllTools(stubTranslation)
|
|
seen := make(map[string]bool)
|
|
featureFlagged := make(map[string]bool)
|
|
|
|
// get_label is intentionally in both issues and labels toolsets for conformance
|
|
// with original behavior where it was registered in both
|
|
allowedDuplicates := map[string]bool{
|
|
"get_label": true,
|
|
}
|
|
|
|
// First pass: identify tools that have feature flags (mutually exclusive at runtime)
|
|
for _, tool := range tools {
|
|
if tool.FeatureFlagEnable != "" || tool.FeatureFlagDisable != "" {
|
|
featureFlagged[tool.Tool.Name] = true
|
|
}
|
|
}
|
|
|
|
for _, tool := range tools {
|
|
name := tool.Tool.Name
|
|
// Allow duplicates for explicitly allowed tools and feature-flagged tools
|
|
if !allowedDuplicates[name] && !featureFlagged[name] {
|
|
assert.False(t, seen[name],
|
|
"Duplicate tool name found: %q", name)
|
|
}
|
|
seen[name] = true
|
|
}
|
|
}
|
|
|
|
// TestNoDuplicateResourceNames ensures all resources have unique names
|
|
func TestNoDuplicateResourceNames(t *testing.T) {
|
|
resources := AllResources(stubTranslation)
|
|
seen := make(map[string]bool)
|
|
|
|
for _, res := range resources {
|
|
name := res.Template.Name
|
|
assert.False(t, seen[name],
|
|
"Duplicate resource name found: %q", name)
|
|
seen[name] = true
|
|
}
|
|
}
|
|
|
|
// TestNoDuplicatePromptNames ensures all prompts have unique names
|
|
func TestNoDuplicatePromptNames(t *testing.T) {
|
|
prompts := AllPrompts(stubTranslation)
|
|
seen := make(map[string]bool)
|
|
|
|
for _, prompt := range prompts {
|
|
name := prompt.Prompt.Name
|
|
assert.False(t, seen[name],
|
|
"Duplicate prompt name found: %q", name)
|
|
seen[name] = true
|
|
}
|
|
}
|
|
|
|
// TestAllToolsHaveHandlerFunc ensures all tools have a handler function
|
|
func TestAllToolsHaveHandlerFunc(t *testing.T) {
|
|
tools := AllTools(stubTranslation)
|
|
|
|
for _, tool := range tools {
|
|
t.Run(tool.Tool.Name, func(t *testing.T) {
|
|
assert.NotNil(t, tool.HandlerFunc,
|
|
"Tool %q must have a HandlerFunc", tool.Tool.Name)
|
|
assert.True(t, tool.HasHandler(),
|
|
"Tool %q HasHandler() should return true", tool.Tool.Name)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestToolsetMetadataConsistency ensures tools in the same toolset have consistent descriptions
|
|
func TestToolsetMetadataConsistency(t *testing.T) {
|
|
tools := AllTools(stubTranslation)
|
|
toolsetDescriptions := make(map[inventory.ToolsetID]string)
|
|
|
|
for _, tool := range tools {
|
|
id := tool.Toolset.ID
|
|
desc := tool.Toolset.Description
|
|
|
|
if existing, ok := toolsetDescriptions[id]; ok {
|
|
assert.Equal(t, existing, desc,
|
|
"Toolset %q has inconsistent descriptions across tools", id)
|
|
} else {
|
|
toolsetDescriptions[id] = desc
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGitHubPackageDoesNotReadInsidersMode(t *testing.T) {
|
|
files, err := filepath.Glob("*.go")
|
|
require.NoError(t, err)
|
|
|
|
for _, file := range files {
|
|
if strings.HasSuffix(file, "_test.go") {
|
|
continue
|
|
}
|
|
|
|
fset := token.NewFileSet()
|
|
node, err := parser.ParseFile(fset, file, nil, 0)
|
|
require.NoError(t, err, "failed to parse %s", file)
|
|
|
|
ast.Inspect(node, func(n ast.Node) bool {
|
|
selector, ok := n.(*ast.SelectorExpr)
|
|
if !ok || selector.Sel.Name != "InsidersMode" {
|
|
return true
|
|
}
|
|
|
|
position := fset.Position(selector.Sel.Pos())
|
|
t.Errorf("%s reads InsidersMode directly; gate behavior on concrete feature flags instead", position)
|
|
return true
|
|
})
|
|
}
|
|
}
|