diff --git a/cmd/mcptools/commands/read_resource.go b/cmd/mcptools/commands/read_resource.go index d4cd2ac..735c681 100644 --- a/cmd/mcptools/commands/read_resource.go +++ b/cmd/mcptools/commands/read_resource.go @@ -1,10 +1,8 @@ package commands import ( - "encoding/json" "fmt" "os" - "strings" "github.com/spf13/cobra" ) @@ -65,14 +63,6 @@ func ReadResourceCmd() *cobra.Command { os.Exit(1) } - var params map[string]any - if len(parsedArgs) > 0 { - if jsonErr := json.Unmarshal([]byte(strings.Join(parsedArgs, " ")), ¶ms); jsonErr != nil { - fmt.Fprintf(os.Stderr, "Error: invalid JSON for params: %v\n", jsonErr) - os.Exit(1) - } - } - mcpClient, clientErr := CreateClientFunc(parsedArgs) if clientErr != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", clientErr) diff --git a/cmd/mcptools/commands/read_resource_test.go b/cmd/mcptools/commands/read_resource_test.go new file mode 100644 index 0000000..4b5ac5e --- /dev/null +++ b/cmd/mcptools/commands/read_resource_test.go @@ -0,0 +1,71 @@ +package commands + +import ( + "bytes" + "testing" +) + +func TestResourceReadCmd_RunHelp(t *testing.T) { + // Given: the read resource command is run + cmd := ReadResourceCmd() + buf := new(bytes.Buffer) + cmd.SetOut(buf) + + // When: the command is run with the help flag + cmd.SetArgs([]string{"--help"}) + + // Then: no error is returned. + err := cmd.Execute() + if err != nil { + t.Errorf("cmd.Execute() error = %v", err) + } + + // Then: the help output is not empty. + if buf.String() == "" { + t.Error("Expected help output to not be empty.") + } +} + +func TestReadResourceCmdRun_Success(t *testing.T) { + t.Setenv("COLS", "120") + // Given: the read resource command is run + cmd := ReadResourceCmd() + buf := new(bytes.Buffer) + cmd.SetOut(buf) + // NOTE: we currently truncate output in tabular output. + cmd.SetArgs([]string{"server", "-f", "json", "arg"}) + + // Given: a mock client that returns a successful read resource response + mockResponse := map[string]any{ + "result": map[string]any{ + "contents": []any{ + map[string]any{ + "uri": "test://foo", + "mimeType": "text/plain", + "text": "bar", + }, + }, + }, + } + + cleanup := setupMockClient(func(method string, _ any) (map[string]any, error) { + if method != "resources/read" { + t.Errorf("Expected method 'resources/read', got %q", method) + } + return mockResponse, nil + }) + defer cleanup() + + // When: the command is executed + err := cmd.Execute() + // Then: no error is returned + if err != nil { + t.Errorf("cmd.Execute() error = %v", err) + } + + // Then: the expected content is returned + output := buf.String() + assertContains(t, output, "test://foo") + assertContains(t, output, "text/plain") + assertContains(t, output, "bar") +} diff --git a/pkg/jsonutils/jsonutils.go b/pkg/jsonutils/jsonutils.go index dc9d80c..b23c3fd 100644 --- a/pkg/jsonutils/jsonutils.go +++ b/pkg/jsonutils/jsonutils.go @@ -766,15 +766,17 @@ func formatGenericMap(data map[string]any) (string, error) { useColors := isTerminal() if useColors { - fmt.Fprintf(w, "%s%sKEY%s\t%sVALUE%s\n", - ColorBold, ColorCyan, ColorReset, + fmt.Fprintf(w, "%sKEY%s\t%sVALUE%s\n", + ColorCyan, ColorReset, + ColorCyan, ColorReset) + fmt.Fprintf(w, "%s---%s\t%s-----%s\n", + ColorCyan, ColorReset, ColorCyan, ColorReset) } else { fmt.Fprintln(w, "KEY\tVALUE") + fmt.Fprintln(w, "---\t-----") } - fmt.Fprintln(w, "---\t-----") - keys := make([]string, 0, len(data)) for k := range data { keys = append(keys, k)