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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ Uses stdin/stdout to communicate with an MCP server via JSON-RPC 2.0. This is us
mcp tools npx -y @modelcontextprotocol/server-filesystem ~
```

For `tools`, `resources`, `prompts`, and `guard`, put `--` before the server
command when its arguments overlap with MCP Tools options. All arguments after
the first standalone `--` are passed unchanged to the server, including any
additional `--`. Put MCP Tools options before the separator:

```bash
mcp tools -f json -- python3 server.py -f settings.yaml --transport stdio
mcp resources -- python3 server.py --format yaml
mcp prompts -- python3 server.py --server-logs
mcp guard --allow tools:read_* -- python3 server.py --allow local
```

Without a separator, MCP Tools options can still appear after the server command
as before.

#### HTTP SSE Transport

Uses HTTP and Server-Sent Events (SSE) to communicate with an MCP server via JSON-RPC 2.0. This is useful for connecting to remote servers that implement the legacy MCP protocol. Transport is automatically detected when the URL ends with `/sse`.
Expand Down
90 changes: 90 additions & 0 deletions cmd/mcptools/commands/flag_separator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package commands

import (
"io"
"testing"

"github.com/mark3labs/mcp-go/client"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func resetFlagOptions(t *testing.T) {
t.Helper()
format, transport, logs, authUser, authHeader := FormatOption, TransportOption, ShowServerLogs, AuthUser, AuthHeader
t.Cleanup(func() {
FormatOption, TransportOption, ShowServerLogs, AuthUser, AuthHeader = format, transport, logs, authUser, authHeader
})
FormatOption, TransportOption, ShowServerLogs, AuthUser, AuthHeader = formatTable, TransportHTTP, false, "", ""
}

func TestProcessFlagsSeparator(t *testing.T) {
tests := []struct {
name string
args []string
want []string
}{
{"short format", []string{"--", "server", "-f", "settings.yaml"}, []string{"server", "-f", "settings.yaml"}},
{"long format", []string{"--", "server", "--format", "yaml"}, []string{"server", "--format", "yaml"}},
{"transport", []string{"--", "server", "--transport", "stdio"}, []string{"server", "--transport", "stdio"}},
{"server logs", []string{"--", "server", "--server-logs"}, []string{"server", "--server-logs"}},
{"auth user", []string{"--", "server", "--auth-user", "server-user-option"}, []string{"server", "--auth-user", "server-user-option"}},
{"auth header", []string{"--", "server", "--auth-header", "server-header-option"}, []string{"server", "--auth-header", "server-header-option"}},
{"server separator", []string{"--", "server", "--", "-f", ""}, []string{"server", "--", "-f", ""}},
{"separator after command", []string{"server", "--", "--format", "yaml"}, []string{"server", "--format", "yaml"}},
{"trailing separator", []string{"server", "--"}, []string{"server"}},
{"separator only", []string{"--"}, []string{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resetFlagOptions(t)
assert.Equal(t, tt.want, ProcessFlags(tt.args))
assert.Equal(t, formatTable, FormatOption)
assert.Equal(t, TransportHTTP, TransportOption)
assert.False(t, ShowServerLogs)
assert.Empty(t, AuthUser)
assert.Empty(t, AuthHeader)
})
}
}

func TestListCommandsSeparator(t *testing.T) {
for _, newCommand := range []func() *cobra.Command{ToolsCmd, ResourcesCmd, PromptsCmd} {
command := newCommand()
t.Run(command.Name(), func(t *testing.T) {
resetFlagOptions(t)
t.Cleanup(setupMockClient(func(_ string, _ any) (map[string]any, error) {
return map[string]any{}, nil
}))
mockFactory := CreateClientFunc
var received []string
CreateClientFunc = func(args []string, options ...client.ClientOption) (*client.Client, error) {
received = append([]string{}, args...)
return mockFactory(args, options...)
}

root := RootCmd()
root.AddCommand(command)
root.SetOut(io.Discard)
serverArgs := []string{"python3", "server.py", "-f", "settings.yaml", "--transport", "stdio", "--server-logs", "--"}
root.SetArgs(append([]string{command.Name(), "-f", "json", "--"}, serverArgs...))
require.NoError(t, root.Execute())
assert.Equal(t, serverArgs, received)
assert.Equal(t, "json", FormatOption)
assert.Equal(t, TransportHTTP, TransportOption)
assert.False(t, ShowServerLogs)
})
}
}

func TestGuardSeparator(t *testing.T) {
resetFlagOptions(t)
serverArgs := []string{"server", "--allow", "server-allow", "--deny", "server-deny", "-a", "short-allow", "-d", "short-deny", "-f", "settings.yaml", "--"}
args := append([]string{"--allow", "tools:read_*", "--deny", "tools:write_*", "-f", "json", "--"}, serverArgs...)
allow, deny, cmdArgs := extractPatterns(args)
assert.Equal(t, []string{"read_*"}, allow[EntityTypeTool])
assert.Equal(t, []string{"write_*"}, deny[EntityTypeTool])
assert.Equal(t, serverArgs, ProcessFlags(cmdArgs))
assert.Equal(t, "json", FormatOption)
}
3 changes: 3 additions & 0 deletions cmd/mcptools/commands/guard.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func extractPatterns(args []string) (map[string][]string, map[string][]string, [
i := 0
for i < len(args) {
switch {
case args[i] == "--":
// Preserve the separator for the subsequent ProcessFlags pass.
return allowPatterns, denyPatterns, append(cmdArgs, args[i:]...)
case (args[i] == FlagAllow || args[i] == FlagAllowShort) && i+1 < len(args):
// Process --allow flag
patternsStr := args[i+1]
Expand Down
3 changes: 3 additions & 0 deletions cmd/mcptools/commands/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ var CreateClientFunc = func(args []string, _ ...client.ClientOption) (*client.Cl
// ProcessFlags processes command line flags, sets the format option, and returns the remaining
// arguments. Supported format options: json, pretty, and table.
// Supported transport options: http and sse.
// A standalone -- ends flag processing; subsequent arguments are returned unchanged.
//
// For example, if the input arguments are ["tools", "--format", "pretty", "npx", "-y",
// "@modelcontextprotocol/server-filesystem", "~"], it would return ["npx", "-y",
Expand All @@ -196,6 +197,8 @@ func ProcessFlags(args []string) []string {
i := 0
for i < len(args) {
switch {
case args[i] == "--":
return append(parsedArgs, args[i+1:]...)
case (args[i] == FlagFormat || args[i] == FlagFormatShort) && i+1 < len(args):
FormatOption = args[i+1]
i += 2
Expand Down