Skip to content

Commit cc19b64

Browse files
Pretty-print jsonl text responses
1 parent 9dacf70 commit cc19b64

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

cmd/mcpcurl/main.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"crypto/rand"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -11,8 +12,6 @@ import (
1112
"slices"
1213
"strings"
1314

14-
"crypto/rand"
15-
1615
"github.com/spf13/cobra"
1716
"github.com/spf13/viper"
1817
)
@@ -161,7 +160,7 @@ func main() {
161160
_ = rootCmd.MarkPersistentFlagRequired("stdio-server-cmd")
162161

163162
// Add global flag for pretty printing
164-
rootCmd.PersistentFlags().Bool("pretty", true, "Pretty print MCP response (only for JSON responses)")
163+
rootCmd.PersistentFlags().Bool("pretty", true, "Pretty print MCP response (only for JSON or JSONL responses)")
165164

166165
// Add the tools command to the root command
167166
rootCmd.AddCommand(toolsCmd)
@@ -426,17 +425,27 @@ func printResponse(response string, prettyPrint bool) error {
426425
// Extract text from content items of type "text"
427426
for _, content := range resp.Result.Content {
428427
if content.Type == "text" {
429-
// Unmarshal the text content
430-
var textContent map[string]interface{}
431-
if err := json.Unmarshal([]byte(content.Text), &textContent); err != nil {
432-
return fmt.Errorf("failed to parse text content: %w", err)
433-
}
434-
// Pretty print the text content
435-
prettyText, err := json.MarshalIndent(textContent, "", " ")
436-
if err != nil {
437-
return fmt.Errorf("failed to pretty print text content: %w", err)
428+
var textContentObj map[string]interface{}
429+
err := json.Unmarshal([]byte(content.Text), &textContentObj)
430+
431+
if err == nil {
432+
prettyText, err := json.MarshalIndent(textContentObj, "", " ")
433+
if err != nil {
434+
return fmt.Errorf("failed to pretty print text content: %w", err)
435+
}
436+
fmt.Println(string(prettyText))
437+
} else {
438+
var textContentList []interface{}
439+
if err := json.Unmarshal([]byte(content.Text), &textContentList); err != nil {
440+
return fmt.Errorf("failed to parse text content as a list: %w", err)
441+
}
442+
// Pretty print the array content
443+
prettyText, err := json.MarshalIndent(textContentList, "", " ")
444+
if err != nil {
445+
return fmt.Errorf("failed to pretty print array content: %w", err)
446+
}
447+
fmt.Println(string(prettyText))
438448
}
439-
fmt.Println(string(prettyText))
440449
}
441450
}
442451

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy