Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbbba3bb

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

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 JSONor JSONLresponses)")
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:=rangeresp.Result.Content {
428427
ifcontent.Type=="text" {
429-
// Unmarshal the text content
430-
vartextContentmap[string]interface{}
431-
iferr:=json.Unmarshal([]byte(content.Text),&textContent);err!=nil {
432-
returnfmt.Errorf("failed to parse text content: %w",err)
428+
vartextContentObjmap[string]interface{}
429+
err:=json.Unmarshal([]byte(content.Text),&textContentObj)
430+
431+
iferr==nil {
432+
prettyText,err:=json.MarshalIndent(textContentObj,""," ")
433+
iferr!=nil {
434+
returnfmt.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+
vartextContentList []map[string]interface{}
442+
iferr:=json.Unmarshal([]byte(content.Text),&textContentList);err!=nil {
443+
returnfmt.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
iferr!=nil {
437-
returnfmt.Errorf("failed to pretty printtext content: %w",err)
447+
returnfmt.Errorf("failed to pretty printarray content: %w",err)
438448
}
439449
fmt.Println(string(prettyText))
440450
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp