Skip to content

Commit 1f0529a

Browse files
committed
feat(shell): add --quiet/-q flag to suppress startup messages
1 parent 543732d commit 1f0529a

3 files changed

Lines changed: 94 additions & 3 deletions

File tree

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.
@@ -44,6 +46,8 @@ var (
4446
AuthUser string
4547
// AuthHeader is a custom Authorization header.
4648
AuthHeader string
49+
// QuietMode suppresses startup messages in shell mode.
50+
QuietMode bool
4751
)
4852

4953
// RootCmd creates the root command.

cmd/mcptools/commands/shell.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ func ShellCmd() *cobra.Command { //nolint:gocyclo
4040
case cmdArgs[i] == FlagServerLogs:
4141
ShowServerLogs = true
4242
i++
43+
case cmdArgs[i] == FlagQuiet || cmdArgs[i] == FlagQuietShort:
44+
QuietMode = true
45+
i++
4346
case cmdArgs[i] == FlagAuthUser && i+1 < len(cmdArgs):
4447
AuthUser = cmdArgs[i+1]
4548
i += 2
@@ -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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,85 @@ func TestShellExit(t *testing.T) {
365365
})
366366
}
367367
}
368+
369+
370+
func TestShellQuietFlag(t *testing.T) {
371+
tests := []struct {
372+
name string
373+
args []string
374+
expectStartup bool
375+
}{
376+
{
377+
name: "without quiet flag shows startup messages",
378+
args: []string{"server"},
379+
expectStartup: true,
380+
},
381+
{
382+
name: "with --quiet flag suppresses startup messages",
383+
args: []string{"--quiet", "server"},
384+
expectStartup: false,
385+
},
386+
{
387+
name: "with -q flag suppresses startup messages",
388+
args: []string{"-q", "server"},
389+
expectStartup: false,
390+
},
391+
{
392+
name: "quiet flag after server arg",
393+
args: []string{"server", "--quiet"},
394+
expectStartup: false,
395+
},
396+
}
397+
398+
for _, tt := range tests {
399+
t.Run(tt.name, func(t *testing.T) {
400+
// Reset QuietMode before each test
401+
QuietMode = false
402+
403+
cmd := ShellCmd()
404+
buf := new(bytes.Buffer)
405+
cmd.SetOut(buf)
406+
cmd.SetArgs(tt.args)
407+
408+
// Create a pipe to simulate stdin
409+
r, w, err := os.Pipe()
410+
if err != nil {
411+
t.Fatalf("Failed to create pipe: %v", err)
412+
}
413+
414+
oldStdin := os.Stdin
415+
os.Stdin = r
416+
oldStdout := os.Stdout
417+
os.Stdout = nil
418+
419+
go func() {
420+
defer w.Close()
421+
w.Write([]byte("/q\n"))
422+
}()
423+
424+
cleanupClient := setupMockClient(func(method string, _ any) (map[string]any, error) {
425+
return map[string]any{}, nil
426+
})
427+
defer cleanupClient()
428+
429+
err = cmd.Execute()
430+
431+
os.Stdin = oldStdin
432+
os.Stdout = oldStdout
433+
434+
if err != nil {
435+
t.Errorf("cmd.Execute() error = %v", err)
436+
}
437+
438+
output := buf.String()
439+
hasStartup := strings.Contains(output, "MCP Tools Shell")
440+
441+
if tt.expectStartup && !hasStartup {
442+
t.Errorf("Expected startup messages, but got: %s", output)
443+
}
444+
if !tt.expectStartup && hasStartup {
445+
t.Errorf("Expected no startup messages with quiet flag, but got: %s", output)
446+
}
447+
})
448+
}
449+
}

0 commit comments

Comments
 (0)