-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add mcpd config volumes list command #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package volumes | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "maps" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/mozilla-ai/mcpd/internal/cmd" | ||
| cmdopts "github.com/mozilla-ai/mcpd/internal/cmd/options" | ||
| "github.com/mozilla-ai/mcpd/internal/context" | ||
| "github.com/mozilla-ai/mcpd/internal/flags" | ||
| ) | ||
|
|
||
| // listCmd handles listing volume mappings for an MCP server. | ||
| type listCmd struct { | ||
| *cmd.BaseCmd | ||
| ctxLoader context.Loader | ||
| } | ||
|
|
||
| // NewListCmd creates a new list command for volume configuration. | ||
| func NewListCmd(baseCmd *cmd.BaseCmd, opt ...cmdopts.CmdOption) (*cobra.Command, error) { | ||
| opts, err := cmdopts.NewOptions(opt...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| c := &listCmd{ | ||
| BaseCmd: baseCmd, | ||
| ctxLoader: opts.ContextLoader, | ||
| } | ||
|
|
||
| cobraCmd := &cobra.Command{ | ||
| Use: "list <server-name>", | ||
| Short: "List configured volume mappings for a server", | ||
| Long: "List configured volume mappings for an MCP server from the " + | ||
| "runtime context configuration file (e.g. " + flags.RuntimeFile + ").", | ||
| RunE: c.run, | ||
| Args: cobra.ExactArgs(1), | ||
| } | ||
|
|
||
| return cobraCmd, nil | ||
| } | ||
|
|
||
| // run executes the list command, displaying volume mappings for the given server. | ||
| func (c *listCmd) run(cobraCmd *cobra.Command, args []string) error { | ||
| serverName := strings.TrimSpace(args[0]) | ||
| if serverName == "" { | ||
| return fmt.Errorf("server-name is required") | ||
| } | ||
|
|
||
| cfg, err := c.ctxLoader.Load(flags.RuntimeFile) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load execution context config: %w", err) | ||
| } | ||
|
|
||
| server, ok := cfg.Get(serverName) | ||
| if !ok { | ||
| return fmt.Errorf("server '%s' not found in configuration", serverName) | ||
| } | ||
|
|
||
| out := cobraCmd.OutOrStdout() | ||
|
|
||
| if _, err := fmt.Fprintf(out, "Volumes for '%s':\n", serverName); err != nil { | ||
| return fmt.Errorf("failed to write output: %w", err) | ||
| } | ||
|
|
||
| if len(server.Volumes) == 0 { | ||
| if _, err := fmt.Fprintln(out, " (No volumes set)"); err != nil { | ||
| return fmt.Errorf("failed to write output: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Sort keys for deterministic output. | ||
| keys := slices.Collect(maps.Keys(server.Volumes)) | ||
| slices.Sort(keys) | ||
|
|
||
| for _, k := range keys { | ||
| if _, err := fmt.Fprintf(out, " %s = %s\n", k, server.Volumes[k]); err != nil { | ||
| return fmt.Errorf("failed to write output: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| package volumes | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/mozilla-ai/mcpd/internal/cmd" | ||
| cmdopts "github.com/mozilla-ai/mcpd/internal/cmd/options" | ||
| "github.com/mozilla-ai/mcpd/internal/context" | ||
| ) | ||
|
|
||
| func TestNewListCmd(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| base := &cmd.BaseCmd{} | ||
| c, err := NewListCmd(base) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, c) | ||
|
|
||
| require.Equal(t, "list <server-name>", c.Use) | ||
| require.Contains(t, c.Short, "List configured volume mappings") | ||
| require.NotNil(t, c.RunE) | ||
| } | ||
|
|
||
| func TestListCmd_run(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| serverName string | ||
| existingServers map[string]context.ServerExecutionContext | ||
| expectedOutput string | ||
| expectedError string | ||
| }{ | ||
| { | ||
| name: "list volumes for server with volumes", | ||
| serverName: "filesystem", | ||
| existingServers: map[string]context.ServerExecutionContext{ | ||
| "filesystem": { | ||
| Name: "filesystem", | ||
| Volumes: context.VolumeExecutionContext{ | ||
| "workspace": "/Users/foo/repos/mcpd", | ||
| }, | ||
| }, | ||
| }, | ||
| expectedOutput: "Volumes for 'filesystem':\n workspace = /Users/foo/repos/mcpd", | ||
| }, | ||
| { | ||
| name: "list volumes for server with multiple volumes sorted", | ||
| serverName: "filesystem", | ||
| existingServers: map[string]context.ServerExecutionContext{ | ||
| "filesystem": { | ||
| Name: "filesystem", | ||
| Volumes: context.VolumeExecutionContext{ | ||
| "workspace": "/Users/foo/repos", | ||
| "data": "my-named-volume", | ||
| "gdrive": "/mcp/gdrive", | ||
| }, | ||
| }, | ||
| }, | ||
| expectedOutput: "Volumes for 'filesystem':\n data = my-named-volume\n gdrive = /mcp/gdrive\n workspace = /Users/foo/repos", | ||
| }, | ||
| { | ||
| name: "list volumes for server with no volumes", | ||
| serverName: "filesystem", | ||
| existingServers: map[string]context.ServerExecutionContext{ | ||
| "filesystem": { | ||
| Name: "filesystem", | ||
| Volumes: context.VolumeExecutionContext{}, | ||
| }, | ||
| }, | ||
| expectedOutput: "Volumes for 'filesystem':\n (No volumes set)", | ||
| }, | ||
| { | ||
| name: "list volumes for server with nil volumes", | ||
| serverName: "filesystem", | ||
| existingServers: map[string]context.ServerExecutionContext{ | ||
| "filesystem": { | ||
| Name: "filesystem", | ||
| }, | ||
| }, | ||
| expectedOutput: "Volumes for 'filesystem':\n (No volumes set)", | ||
| }, | ||
| { | ||
| name: "server not found", | ||
| serverName: "nonexistent", | ||
| existingServers: map[string]context.ServerExecutionContext{}, | ||
| expectedError: "server 'nonexistent' not found in configuration", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| modifier := &mockModifier{ | ||
| servers: tc.existingServers, | ||
| } | ||
| loader := &mockLoader{modifier: modifier} | ||
|
|
||
| base := &cmd.BaseCmd{} | ||
| listCmd, err := NewListCmd(base, cmdopts.WithContextLoader(loader)) | ||
| require.NoError(t, err) | ||
|
|
||
| // Capture output. | ||
| var output bytes.Buffer | ||
| listCmd.SetOut(&output) | ||
| listCmd.SetErr(&output) | ||
|
|
||
| listCmd.SetArgs([]string{tc.serverName}) | ||
|
|
||
| err = listCmd.Execute() | ||
|
|
||
| if tc.expectedError != "" { | ||
| require.EqualError(t, err, tc.expectedError) | ||
| return | ||
| } | ||
|
|
||
| require.NoError(t, err) | ||
|
|
||
| actualOutput := strings.TrimSpace(output.String()) | ||
| require.Equal(t, tc.expectedOutput, actualOutput) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestListCmd_Validation(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| args []string | ||
| expectedError string | ||
| }{ | ||
| { | ||
| name: "missing server name", | ||
| args: []string{}, | ||
| expectedError: "accepts 1 arg(s), received 0", | ||
| }, | ||
| { | ||
| name: "empty server name", | ||
| args: []string{""}, | ||
| expectedError: "server-name is required", | ||
| }, | ||
| { | ||
| name: "whitespace only server name", | ||
| args: []string{" "}, | ||
| expectedError: "server-name is required", | ||
| }, | ||
| { | ||
| name: "too many args", | ||
| args: []string{"server1", "server2"}, | ||
| expectedError: "accepts 1 arg(s), received 2", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| modifier := &mockModifier{ | ||
| servers: map[string]context.ServerExecutionContext{}, | ||
| } | ||
| loader := &mockLoader{modifier: modifier} | ||
|
|
||
| base := &cmd.BaseCmd{} | ||
| listCmd, err := NewListCmd(base, cmdopts.WithContextLoader(loader)) | ||
| require.NoError(t, err) | ||
|
|
||
| listCmd.SetArgs(tc.args) | ||
| err = listCmd.Execute() | ||
| require.EqualError(t, err, tc.expectedError) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestListCmd_LoaderError(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| loader := &mockLoader{ | ||
| loadError: fmt.Errorf("failed to load"), | ||
| } | ||
|
|
||
| base := &cmd.BaseCmd{} | ||
| listCmd, err := NewListCmd(base, cmdopts.WithContextLoader(loader)) | ||
| require.NoError(t, err) | ||
|
|
||
| listCmd.SetArgs([]string{"server"}) | ||
| err = listCmd.Execute() | ||
| require.EqualError(t, err, "failed to load execution context config: failed to load") | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to use string consts here and reuse them in the tests to avoid false positives, unless we want to verify the string contents themselves.