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

feat: pretty-print JSONL text responses inmcpcurl#239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
juruen merged 2 commits intogithub:mainfromsimondanielsson:feat/pretty-print-jsonl
Apr 12, 2025
Merged
Changes fromall 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
30 changes: 20 additions & 10 deletionscmd/mcpcurl/main.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@ package main

import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"io"
Expand All@@ -11,8 +12,6 @@ import (
"slices"
"strings"

"crypto/rand"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand DownExpand Up@@ -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 JSONor JSONLresponses)")

// Add the tools command to the root command
rootCmd.AddCommand(toolsCmd)
Expand DownExpand Up@@ -426,15 +425,26 @@ 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)
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))
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)
}
// Pretty print the text content
prettyText, err := json.MarshalIndent(textContent, "", " ")
prettyText, err := json.MarshalIndent(textContentList, "", " ")
if err != nil {
return fmt.Errorf("failed to pretty printtext content: %w", err)
return fmt.Errorf("failed to pretty printarray content: %w", err)
}
fmt.Println(string(prettyText))
}
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp