Skip to content

Commit 59535ff

Browse files
committed
feat(shell): add --quiet/-q flag to suppress startup messages
1 parent e7bd724 commit 59535ff

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

cmd/mcptools/commands/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const (
1919
FlagTransport = "--transport"
2020
FlagAuthUser = "--auth-user"
2121
FlagAuthHeader = "--auth-header"
22+
FlagQuiet = "--quiet"
23+
FlagQuietShort = "-q"
2224
)
2325

2426
// entity types.
@@ -51,6 +53,8 @@ var (
5153
AuthUser string
5254
// AuthHeader is a custom Authorization header.
5355
AuthHeader string
56+
// QuietMode suppresses startup messages.
57+
QuietMode bool
5458
)
5559

5660
// RootCmd creates the root command.

cmd/mcptools/commands/shell.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func ShellCmd() *cobra.Command { //nolint:gocyclo
4646
case cmdArgs[i] == FlagAuthHeader && i+1 < len(cmdArgs):
4747
AuthHeader = cmdArgs[i+1]
4848
i += 2
49+
case cmdArgs[i] == FlagQuiet || cmdArgs[i] == FlagQuietShort:
50+
QuietMode = true
51+
i++
4952
default:
5053
parsedArgs = append(parsedArgs, cmdArgs[i])
5154
i++
@@ -64,9 +67,11 @@ func ShellCmd() *cobra.Command { //nolint:gocyclo
6467
os.Exit(1)
6568
}
6669

67-
fmt.Fprintf(thisCmd.OutOrStdout(), "mcp > MCP Tools Shell (%s)\n", Version)
68-
fmt.Fprintf(thisCmd.OutOrStdout(), "mcp > Connected to Server: %s\n", strings.Join(parsedArgs, " "))
69-
fmt.Fprintf(thisCmd.OutOrStdout(), "\nmcp > Type '/h' for help or '/q' to quit\n")
70+
if !QuietMode {
71+
fmt.Fprintf(thisCmd.OutOrStdout(), "mcp > MCP Tools Shell (%s)\n", Version)
72+
fmt.Fprintf(thisCmd.OutOrStdout(), "mcp > Connected to Server: %s\n", strings.Join(parsedArgs, " "))
73+
fmt.Fprintf(thisCmd.OutOrStdout(), "\nmcp > Type '/h' for help or '/q' to quit\n")
74+
}
7075

7176
line := liner.NewLiner()
7277
line.SetCtrlCAborts(true)

cmd/mcptools/commands/shell_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,64 @@ func TestShellExit(t *testing.T) {
365365
})
366366
}
367367
}
368+
369+
func TestShellQuietFlag(t *testing.T) {
370+
tests := []struct { //nolint:govet
371+
name string
372+
args []string
373+
unexpectedOutput []string
374+
expectStartup bool
375+
}{
376+
{
377+
name: "without quiet flag shows startup messages",
378+
args: []string{"echo", "test"},
379+
expectStartup: true,
380+
},
381+
{
382+
name: "with --quiet flag suppresses startup messages",
383+
args: []string{"--quiet", "echo", "test"},
384+
expectStartup: false,
385+
unexpectedOutput: []string{"MCP Tools Shell", "Connected to Server", "Type '/h' for help"},
386+
},
387+
{
388+
name: "with -q flag suppresses startup messages",
389+
args: []string{"-q", "echo", "test"},
390+
expectStartup: false,
391+
unexpectedOutput: []string{"MCP Tools Shell", "Connected to Server", "Type '/h' for help"},
392+
},
393+
}
394+
395+
for _, tt := range tests {
396+
t.Run(tt.name, func(t *testing.T) {
397+
// Reset QuietMode before each test
398+
QuietMode = false
399+
400+
cmd, buf, cleanupSetup := setupTestCommand(t, "/q\n")
401+
defer cleanupSetup()
402+
403+
cleanupClient := setupMockClient(func(_ string, _ any) (map[string]any, error) {
404+
return map[string]any{}, nil
405+
})
406+
defer cleanupClient()
407+
408+
cmd.SetArgs(tt.args)
409+
err := cmd.Execute()
410+
if err != nil {
411+
t.Errorf("cmd.Execute() error = %v", err)
412+
}
413+
414+
output := buf.String()
415+
if tt.expectStartup {
416+
if !strings.Contains(output, "MCP Tools Shell") {
417+
t.Errorf("Expected startup messages, got: %s", output)
418+
}
419+
} else {
420+
for _, unexpected := range tt.unexpectedOutput {
421+
if strings.Contains(output, unexpected) {
422+
t.Errorf("Expected output to NOT contain %q, got: %s", unexpected, output)
423+
}
424+
}
425+
}
426+
})
427+
}
428+
}

0 commit comments

Comments
 (0)