Skip to content

Commit bbba3bb

Browse files
feat: pretty-print JSONL text responses in mcpcurl (#239)
* Pretty-print jsonl text responses * Remove else in favour of continue --------- Co-authored-by: danielssonsimon <danielsson.simon@bcg.com>
1 parent 9dacf70 commit bbba3bb

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

cmd/mcpcurl/main.go

Lines changed: 20 additions & 10 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,15 +425,26 @@ 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)
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+
continue
438+
}
439+
440+
// Fallback parsing as JSONL
441+
var textContentList []map[string]interface{}
442+
if err := json.Unmarshal([]byte(content.Text), &textContentList); err != nil {
443+
return fmt.Errorf("failed to parse text content as a list: %w", err)
433444
}
434-
// Pretty print the text content
435-
prettyText, err := json.MarshalIndent(textContent, "", " ")
445+
prettyText, err := json.MarshalIndent(textContentList, "", " ")
436446
if err != nil {
437-
return fmt.Errorf("failed to pretty print text content: %w", err)
447+
return fmt.Errorf("failed to pretty print array content: %w", err)
438448
}
439449
fmt.Println(string(prettyText))
440450
}

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