From cc19b643066ce248d28115d3c2da10329390991d Mon Sep 17 00:00:00 2001 From: danielssonsimon Date: Sat, 12 Apr 2025 13:07:20 +0200 Subject: [PATCH 1/2] Pretty-print jsonl text responses --- cmd/mcpcurl/main.go | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/cmd/mcpcurl/main.go b/cmd/mcpcurl/main.go index b6676bfec..c34e4afad 100644 --- a/cmd/mcpcurl/main.go +++ b/cmd/mcpcurl/main.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "crypto/rand" "encoding/json" "fmt" "io" @@ -11,8 +12,6 @@ import ( "slices" "strings" - "crypto/rand" - "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -161,7 +160,7 @@ func main() { _ = rootCmd.MarkPersistentFlagRequired("stdio-server-cmd") // Add global flag for pretty printing - rootCmd.PersistentFlags().Bool("pretty", true, "Pretty print MCP response (only for JSON responses)") + rootCmd.PersistentFlags().Bool("pretty", true, "Pretty print MCP response (only for JSON or JSONL responses)") // Add the tools command to the root command rootCmd.AddCommand(toolsCmd) @@ -426,17 +425,27 @@ func printResponse(response string, prettyPrint bool) error { // Extract text from content items of type "text" for _, content := range resp.Result.Content { if content.Type == "text" { - // Unmarshal the text content - var textContent map[string]interface{} - if err := json.Unmarshal([]byte(content.Text), &textContent); err != nil { - return fmt.Errorf("failed to parse text content: %w", err) - } - // Pretty print the text content - prettyText, err := json.MarshalIndent(textContent, "", " ") - if err != nil { - return fmt.Errorf("failed to pretty print text content: %w", err) + var textContentObj map[string]interface{} + err := json.Unmarshal([]byte(content.Text), &textContentObj) + + if err == nil { + prettyText, err := json.MarshalIndent(textContentObj, "", " ") + if err != nil { + return fmt.Errorf("failed to pretty print text content: %w", err) + } + fmt.Println(string(prettyText)) + } else { + var textContentList []interface{} + if err := json.Unmarshal([]byte(content.Text), &textContentList); err != nil { + return fmt.Errorf("failed to parse text content as a list: %w", err) + } + // Pretty print the array content + prettyText, err := json.MarshalIndent(textContentList, "", " ") + if err != nil { + return fmt.Errorf("failed to pretty print array content: %w", err) + } + fmt.Println(string(prettyText)) } - fmt.Println(string(prettyText)) } } From c84c44cf2667c53e70a767e36a4b52e30e712354 Mon Sep 17 00:00:00 2001 From: danielssonsimon Date: Sat, 12 Apr 2025 14:21:55 +0200 Subject: [PATCH 2/2] Remove else in favour of continue --- cmd/mcpcurl/main.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/cmd/mcpcurl/main.go b/cmd/mcpcurl/main.go index c34e4afad..dfc639b99 100644 --- a/cmd/mcpcurl/main.go +++ b/cmd/mcpcurl/main.go @@ -434,18 +434,19 @@ func printResponse(response string, prettyPrint bool) error { return fmt.Errorf("failed to pretty print text content: %w", err) } fmt.Println(string(prettyText)) - } else { - var textContentList []interface{} - if err := json.Unmarshal([]byte(content.Text), &textContentList); err != nil { - return fmt.Errorf("failed to parse text content as a list: %w", err) - } - // Pretty print the array content - prettyText, err := json.MarshalIndent(textContentList, "", " ") - if err != nil { - return fmt.Errorf("failed to pretty print array content: %w", err) - } - fmt.Println(string(prettyText)) + continue + } + + // Fallback parsing as JSONL + var textContentList []map[string]interface{} + if err := json.Unmarshal([]byte(content.Text), &textContentList); err != nil { + return fmt.Errorf("failed to parse text content as a list: %w", err) + } + prettyText, err := json.MarshalIndent(textContentList, "", " ") + if err != nil { + return fmt.Errorf("failed to pretty print array content: %w", err) } + fmt.Println(string(prettyText)) } }