Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
10 changes: 0 additions & 10 deletions cmd/mcptools/commands/read_resource.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package commands

import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -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, " ")), &params); 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)
Expand Down
71 changes: 71 additions & 0 deletions cmd/mcptools/commands/read_resource_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
10 changes: 6 additions & 4 deletions pkg/jsonutils/jsonutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down