Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/github-mcp-server/list_scopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ func runListScopes() error {
}
}

readOnly := viper.GetBool("read-only")
readOnly, err := parseBoolFlag("read-only")
if err != nil {
return err
}
outputFormat := viper.GetString("list-scopes-output")

// Create translation helper
Expand Down
50 changes: 46 additions & 4 deletions cmd/github-mcp-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ var (
}
}

readOnly, err := parseBoolFlag("read-only")
if err != nil {
return err
}
lockdownMode, err := parseBoolFlag("lockdown-mode")
if err != nil {
return err
}
ttl := viper.GetDuration("repo-access-cache-ttl")
stdioServerConfig := ghmcp.StdioServerConfig{
Version: serverVersion,
Expand All @@ -123,12 +131,12 @@ var (
EnabledToolsets: enabledToolsets,
EnabledTools: enabledTools,
EnabledFeatures: enabledFeatures,
ReadOnly: viper.GetBool("read-only"),
ReadOnly: readOnly,
ExportTranslations: viper.GetBool("export-translations"),
EnableCommandLogging: viper.GetBool("enable-command-logging"),
LogFilePath: viper.GetString("log-file"),
ContentWindowSize: viper.GetInt("content-window-size"),
LockdownMode: viper.GetBool("lockdown-mode"),
LockdownMode: lockdownMode,
InsidersMode: viper.GetBool("insiders"),
ExcludeTools: excludeTools,
RepoAccessCacheTTL: &ttl,
Expand Down Expand Up @@ -202,6 +210,14 @@ var (
}
}

readOnly, err := parseBoolFlag("read-only")
if err != nil {
return err
}
lockdownMode, err := parseBoolFlag("lockdown-mode")
if err != nil {
return err
}
ttl := viper.GetDuration("repo-access-cache-ttl")
httpConfig := ghhttp.ServerConfig{
Version: version,
Expand All @@ -215,10 +231,10 @@ var (
EnableCommandLogging: viper.GetBool("enable-command-logging"),
LogFilePath: viper.GetString("log-file"),
ContentWindowSize: viper.GetInt("content-window-size"),
LockdownMode: viper.GetBool("lockdown-mode"),
LockdownMode: lockdownMode,
RepoAccessCacheTTL: &ttl,
ScopeChallenge: viper.GetBool("scope-challenge"),
ReadOnly: viper.GetBool("read-only"),
ReadOnly: readOnly,
EnabledToolsets: enabledToolsets,
EnabledTools: enabledTools,
ExcludeTools: excludeTools,
Expand Down Expand Up @@ -373,3 +389,29 @@ func wordSepNormalizeFunc(_ *pflag.FlagSet, name string) pflag.NormalizedName {
}
return pflag.NormalizedName(name)
}

// parseBoolFlag reads a boolean config value that may come from a flag or an
// environment variable. The pflag Bool flag already fails closed on an invalid
// value, but viper.GetBool silently coerces unrecognized strings (e.g. "yes",
// "on", or the typo "tru") to false. The read-only and lockdown switches keep
// write tools away from an agent, so they must fail closed rather than silently
// turn off.
func parseBoolFlag(key string) (bool, error) {
raw := viper.GetString(key)
if raw == "" {
return false, nil
}
// A whitespace-only value is not a valid setting, so it falls through to
// the error below instead of trimming to the unset default.
switch strings.ToLower(strings.TrimSpace(raw)) {
case "0", "f", "false", "no", "n", "off", "disabled":
return false, nil
case "1", "t", "true", "yes", "y", "on", "enabled":
return true, nil
default:
return false, fmt.Errorf(
"invalid boolean value %q for --%s (or GITHUB_%s)",
raw, key, strings.ToUpper(strings.ReplaceAll(key, "-", "_")),
)
}
}
46 changes: 46 additions & 0 deletions cmd/github-mcp-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,49 @@ func TestSchemaTypeString(t *testing.T) {
})
}
}

func TestParseBoolFlag(t *testing.T) {
tests := []struct {
name string
value string
want bool
wantErr bool
}{
{name: "empty", value: "", want: false},
{name: "true", value: "true", want: true},
{name: "TRUE", value: "TRUE", want: true},
{name: "one", value: "1", want: true},
{name: "t", value: "t", want: true},
{name: "yes", value: "yes", want: true},
{name: "y", value: "y", want: true},
{name: "on", value: "on", want: true},
{name: "enabled", value: "enabled", want: true},
{name: "false", value: "false", want: false},
{name: "zero", value: "0", want: false},
{name: "no", value: "no", want: false},
{name: "off", value: "off", want: false},
{name: "disabled", value: "disabled", want: false},
{name: "typo", value: "tru", wantErr: true},
{name: "garbage", value: "maybe", wantErr: true},
{name: "whitespace only", value: " ", wantErr: true},
{name: "padded true", value: " true ", want: true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
viper.Set("read-only", tc.value)
got, err := parseBoolFlag("read-only")
if tc.wantErr {
if err == nil {
t.Fatalf("parseBoolFlag(%q) = %v, want error", tc.value, got)
}
return
}
if err != nil {
t.Fatalf("parseBoolFlag(%q) error: %v", tc.value, err)
}
if got != tc.want {
t.Errorf("parseBoolFlag(%q) = %v, want %v", tc.value, got, tc.want)
}
})
}
}