Skip to content

Agent Mesh

mcp-client: pretty-print JSON output when stdout is a terminal

Federico Bongiovannicf93686efe53
diff --git a/cmd/mcp-client/main.go b/cmd/mcp-client/main.go
index 1e3b5386..268382c8 100644
--- a/cmd/mcp-client/main.go
+++ b/cmd/mcp-client/main.go
@@ -16,6 +16,7 @@ package main
import (
"bufio"
+ "bytes"
"context"
"encoding/json"
"flag"
@@ -29,6 +30,7 @@ import (
"syscall"
"time"
+ "github.com/mattn/go-isatty"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/google/agentmesh/api"
@@ -41,6 +43,7 @@ func main() {
toolArgs := flag.String("args", "{}", "JSON arguments for the tool or discovery parameters")
timeoutArgs := flag.Int("timeout", 10, "Timeout in seconds")
listTools := flag.Bool("list", false, "List available tools and exit")
+ prettyOpt := flag.Bool("pretty", stdoutIsTerminal(), "Pretty-print JSON output (default: when stdout is a terminal)")
streamOpt := flag.Bool("stream", false, "Enable streaming mode for service discovery HTTP API")
tokenOpt := flag.String("token", "", "Authorization Bearer token for protected sidecar endpoints (deprecated: prefer -token-path or AGENTMESH_API_TOKEN)")
tokenPathOpt := flag.String("token-path", "", "Path to file containing the Authorization Bearer token (or env AGENTMESH_API_TOKEN)")
@@ -150,7 +153,7 @@ func main() {
if strings.HasPrefix(line, "data:") {
dataContent := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
- fmt.Println(dataContent)
+ fmt.Println(formatOutput(dataContent, *prettyOpt))
} else if strings.HasPrefix(line, "event:") {
fmt.Printf("[%s]\n", strings.TrimSpace(strings.TrimPrefix(line, "event:")))
}
@@ -229,11 +232,43 @@ func main() {
for _, content := range result.Content {
if textContent, ok := content.(*mcp.TextContent); ok {
- fmt.Println(textContent.Text)
+ fmt.Println(formatOutput(textContent.Text, *prettyOpt))
}
}
}
+// stdoutIsTerminal reports whether stdout is an interactive terminal, so human
+// invocations get indented JSON by default while pipes, jq and test harnesses
+// keep single-line JSON.
+func stdoutIsTerminal() bool {
+ return isTerminal(os.Stdout)
+}
+
+func isTerminal(f *os.File) bool {
+ if f == nil {
+ return false
+ }
+ fd := f.Fd()
+ return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
+}
+
+// formatOutput indents JSON objects and arrays when pretty is true, leaving
+// plain-text tool outputs, usage hints and malformed fragments untouched.
+func formatOutput(text string, pretty bool) string {
+ if !pretty {
+ return text
+ }
+ trimmed := strings.TrimSpace(text)
+ if trimmed == "" || (trimmed[0] != '{' && trimmed[0] != '[') {
+ return text
+ }
+ var buf bytes.Buffer
+ if err := json.Indent(&buf, []byte(trimmed), "", " "); err != nil {
+ return text
+ }
+ return buf.String()
+}
+
type authTransport struct {
token string
underlying http.RoundTripper
diff --git a/cmd/mcp-client/main_test.go b/cmd/mcp-client/main_test.go
new file mode 100644
index 00000000..c1b06f9d
--- /dev/null
+++ b/cmd/mcp-client/main_test.go
@@ -0,0 +1,197 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+ "context"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/modelcontextprotocol/go-sdk/mcp"
+)
+
+func TestFormatOutput(t *testing.T) {
+ compactMeshInfo := `{"peer_id":"12D3KooWTest","connected_peers":["12D3KooWA","12D3KooWB"],"attached_routers":["12D3KooWR"],"router_peer_id":"12D3KooWR","local_api_socket":"/tmp/agentmesh.sock"}`
+ prettyMeshInfo := "{\n" +
+ " \"peer_id\": \"12D3KooWTest\",\n" +
+ " \"connected_peers\": [\n" +
+ " \"12D3KooWA\",\n" +
+ " \"12D3KooWB\"\n" +
+ " ],\n" +
+ " \"attached_routers\": [\n" +
+ " \"12D3KooWR\"\n" +
+ " ],\n" +
+ " \"router_peer_id\": \"12D3KooWR\",\n" +
+ " \"local_api_socket\": \"/tmp/agentmesh.sock\"\n" +
+ "}"
+
+ cases := []struct {
+ name string
+ input string
+ pretty bool
+ want string
+ }{
+ {
+ name: "json_object_pretty_enabled",
+ input: compactMeshInfo,
+ pretty: true,
+ want: prettyMeshInfo,
+ },
+ {
+ name: "json_object_pretty_disabled",
+ input: compactMeshInfo,
+ pretty: false,
+ want: compactMeshInfo,
+ },
+ {
+ name: "json_array_pretty_enabled",
+ input: `[{"peer_id":"12D3KooWA","srv_name":"erp-db"}]`,
+ pretty: true,
+ want: "[\n {\n \"peer_id\": \"12D3KooWA\",\n \"srv_name\": \"erp-db\"\n }\n]",
+ },
+ {
+ name: "empty_json_collections",
+ input: `[]`,
+ pretty: true,
+ want: `[]`,
+ },
+ {
+ name: "surrounding_whitespace_trimmed_when_valid_json",
+ input: " {\"peer_id\":\"12D3KooWTest\"} \n",
+ pretty: true,
+ want: "{\n \"peer_id\": \"12D3KooWTest\"\n}",
+ },
+ {
+ name: "plain_text_untouched",
+ input: "To call an inference service, send a normal HTTP request.",
+ pretty: true,
+ want: "To call an inference service, send a normal HTTP request.",
+ },
+ {
+ name: "bracket_prefixed_non_json_untouched",
+ input: "[A2A Auditor] APPROVED",
+ pretty: true,
+ want: "[A2A Auditor] APPROVED",
+ },
+ {
+ name: "empty_string_untouched",
+ input: "",
+ pretty: true,
+ want: "",
+ },
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got := formatOutput(tc.input, tc.pretty)
+ if got != tc.want {
+ t.Errorf("formatOutput(%q, %v) =\n%s\nwant:\n%s", tc.input, tc.pretty, got, tc.want)
+ }
+ })
+ }
+}
+
+func TestIsTerminalNonTTY(t *testing.T) {
+ t.Run("nil_file", func(t *testing.T) {
+ if isTerminal(nil) {
+ t.Fatalf("isTerminal(nil) = true, want false")
+ }
+ })
+
+ t.Run("pipe_is_not_terminal", func(t *testing.T) {
+ r, w, err := os.Pipe()
+ if err != nil {
+ t.Fatalf("os.Pipe() failed: %v", err)
+ }
+ t.Cleanup(func() {
+ _ = r.Close()
+ _ = w.Close()
+ })
+ if isTerminal(r) || isTerminal(w) {
+ t.Fatalf("isTerminal on os.Pipe() = true, want false")
+ }
+ })
+
+ t.Run("regular_file_is_not_terminal", func(t *testing.T) {
+ f, err := os.Create(filepath.Join(t.TempDir(), "out.json"))
+ if err != nil {
+ t.Fatalf("os.Create failed: %v", err)
+ }
+ t.Cleanup(func() { _ = f.Close() })
+ if isTerminal(f) {
+ t.Fatalf("isTerminal on regular file = true, want false")
+ }
+ })
+}
+
+func TestMCPClientCLIGetMeshInfoPrettyAndPiped(t *testing.T) {
+ compact := `{"peer_id":"12D3KooWNode1","connected_peers":["12D3KooWRouter"],"attached_routers":["12D3KooWRouter"],"router_peer_id":"12D3KooWRouter","local_api_socket":"/tmp/node1.sock"}`
+
+ srv := mcp.NewServer(&mcp.Implementation{Name: "test-node", Version: "0.0.1"}, nil)
+ type emptyArgs struct{}
+ mcp.AddTool(srv, &mcp.Tool{
+ Name: "get_mesh_info",
+ Description: "Get information about the mesh network",
+ }, func(_ context.Context, _ *mcp.CallToolRequest, _ emptyArgs) (*mcp.CallToolResult, any, error) {
+ return &mcp.CallToolResult{
+ Content: []mcp.Content{
+ &mcp.TextContent{Text: compact},
+ &mcp.TextContent{Text: "Hint: plain text stays as-is"},
+ },
+ }, nil, nil
+ })
+
+ httpSrv := httptest.NewServer(mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server {
+ return srv
+ }, nil))
+ t.Cleanup(httpSrv.Close)
+
+ binPath := filepath.Join(t.TempDir(), "mcp-client")
+ buildCmd := exec.Command("go", "build", "-o", binPath, ".")
+ if out, err := buildCmd.CombinedOutput(); err != nil {
+ t.Fatalf("go build failed: %v\n%s", err, string(out))
+ }
+
+ t.Run("piped_stdout_defaults_to_compact_single_line", func(t *testing.T) {
+ out, err := exec.Command(binPath, "-url", httpSrv.URL, "-tool", "get_mesh_info").Output()
+ if err != nil {
+ t.Fatalf("mcp-client failed: %v", err)
+ }
+ got := string(out)
+ want := compact + "\nHint: plain text stays as-is\n"
+ if got != want {
+ t.Errorf("piped stdout =\n%q\nwant:\n%q", got, want)
+ }
+ })
+
+ t.Run("pretty_flag_indents_json_and_preserves_plain_text", func(t *testing.T) {
+ out, err := exec.Command(binPath, "-url", httpSrv.URL, "-tool", "get_mesh_info", "-pretty").Output()
+ if err != nil {
+ t.Fatalf("mcp-client -pretty failed: %v", err)
+ }
+ got := string(out)
+ if !strings.Contains(got, " \"peer_id\": \"12D3KooWNode1\",\n") {
+ t.Errorf("expected 2-space indented peer_id in output, got:\n%s", got)
+ }
+ if !strings.HasSuffix(got, "\nHint: plain text stays as-is\n") {
+ t.Errorf("expected trailing plain-text content untouched, got:\n%s", got)
+ }
+ })
+}