Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cmd/mcpproxy/catalog_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func printCatalogTable(formatter clioutput.OutputFormatter, results []registries
if r.FromCache {
source += " (cached)"
}
rows = append(rows, []string{source, r.ID, truncateStr(r.Title, 40), r.Transport, added})
rows = append(rows, []string{source, r.ID, truncateCell(r.Title, 40), r.Transport, added})
}
out, err := formatter.FormatTable(headers, rows)
if err == nil {
Expand Down
10 changes: 0 additions & 10 deletions cmd/mcpproxy/credential_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,16 +333,6 @@ func renderCredentialDetail(c cliclient.CredentialStatus) string {
return strings.TrimRight(b.String(), "\n")
}

func truncateCell(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
if maxLen <= 3 {
return s[:maxLen]
}
return s[:maxLen-3] + "..."
}

// printCredentialBanner emits the FR-034 honesty line. Human-readable output
// begins with it on stdout (contracts/rest-endpoints.md §12); structured
// json/yaml output must stay machine-parseable, so there it goes to stderr.
Expand Down
9 changes: 1 addition & 8 deletions cmd/mcpproxy/profile_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func runProfileList(_ *cobra.Command, _ []string) error {
rows := make([][]string, 0, len(list.Profiles))
for _, p := range list.Profiles {
rows = append(rows, []string{
p.Name, dash(p.Title), shorten(dash(strings.Join(p.EffectiveServers, ",")), 30), dash(p.MaxTier),
p.Name, dash(p.Title), truncateCell(dash(strings.Join(p.EffectiveServers, ",")), 30), dash(p.MaxTier),
strconv.Itoa(p.ToolCounts.Read), strconv.Itoa(p.ToolCounts.Write), strconv.Itoa(p.ToolCounts.Destructive),
strconv.Itoa(p.ToolCounts.UnannotatedHidden), usedBySummary(p.UsedBy),
strconv.Itoa(p.Calls24h), strconv.Itoa(p.Blocked24h),
Expand Down Expand Up @@ -125,13 +125,6 @@ func plural(n int) string {
return "s"
}

func shorten(s string, max int) string {
if len(s) <= max {
return s
}
return s[:max-3] + "..."
}

type profileShowOpts struct {
effective bool
client string
Expand Down
11 changes: 2 additions & 9 deletions cmd/mcpproxy/registry_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ func renderRegistries(formatter clioutput.OutputFormatter, regs []map[string]int
headers := []string{"ID", "NAME", "DESCRIPTION"}
rows := make([][]string, 0, len(regs))
for _, r := range regs {
rows = append(rows, []string{mapString(r, "id"), mapString(r, "name"), truncateStr(mapString(r, "description"), 60)})
rows = append(rows, []string{mapString(r, "id"), mapString(r, "name"), truncateCell(mapString(r, "description"), 60)})
}
out, err := formatter.FormatTable(headers, rows)
if err != nil {
Expand All @@ -586,7 +586,7 @@ func renderRegistryServers(formatter clioutput.OutputFormatter, servers []map[st
if installCmd == "" {
installCmd = "-"
}
rows = append(rows, []string{mapString(s, "id"), mapString(s, "name"), truncateStr(mapString(s, "description"), 45), installCmd})
rows = append(rows, []string{mapString(s, "id"), mapString(s, "name"), truncateCell(mapString(s, "description"), 45), installCmd})
}
out, err := formatter.FormatTable(headers, rows)
if err != nil {
Expand All @@ -611,13 +611,6 @@ func mapString(m map[string]interface{}, key string) string {
return ""
}

func truncateStr(s string, max int) string {
if len(s) > max {
return s[:max-3] + "..."
}
return s
}

// loadRegistryConfig loads config for the registry commands, honoring the
// command's --config flag and the global --data-dir, falling back to defaults
// so 'list'/'search' still work without a config file.
Expand Down
30 changes: 30 additions & 0 deletions cmd/mcpproxy/table_cell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"github.com/smart-mcp-proxy/mcpproxy-go/internal/stringutil"
)

// truncateCell bounds one CLI table cell to maxLen bytes, marking a shortened
// value with an ellipsis. It is the single cut used by every table in this
// package: `shorten` (profile), `truncateStr` (registry, catalog) and two
// inline `s[:n] + "..."` sites (upstream tool list, token list) each carried
// an independent copy of the same arithmetic, and all of them cut at a raw
// byte offset.
//
// The cut goes through stringutil.UTF8Prefix, finishing the sweep of #1647 /
// #1664 / #1665 in the part of cmd/mcpproxy those PRs set aside. Table cells
// carry upstream- and operator-authored text — registry and tool
// descriptions, catalog titles, server names — so a non-ASCII value longer
// than its column can put a multi-byte rune astride the cut, and a raw byte
// slice then writes a partial rune to the terminal.
//
// The result never exceeds maxLen; ASCII behaviour is unchanged.
func truncateCell(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
if maxLen <= 3 {
return stringutil.UTF8Prefix(s, maxLen)
}
return stringutil.UTF8Prefix(s, maxLen-3) + "..."
}
82 changes: 82 additions & 0 deletions cmd/mcpproxy/table_cell_utf8_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"fmt"
"strings"
"testing"
"unicode/utf8"
)

// Finishing the sweep of #1647 / #1664 / #1665 in cmd/mcpproxy. Every CLI
// table cut was a raw byte slice, so a non-ASCII registry/tool description,
// catalog title or server name longer than its column ended in a partial
// rune on the terminal.
//
// The 1-byte padding shifts the cut through all three offsets inside a 3-byte
// rune; only two of the three are mid-rune, so a single fixed width would
// pass by luck.
func TestTruncateCellKeepsRuneBoundary(t *testing.T) {
for _, maxLen := range []int{10, 24, 30, 40, 45, 60} {
for pad := 0; pad < 3; pad++ {
cell := strings.Repeat("a", pad) + strings.Repeat("語", 100)

got := truncateCell(cell, maxLen)
if len(got) > maxLen {
t.Errorf("maxLen=%d pad=%d: %d bytes, over the column width", maxLen, pad, len(got))
}
if !utf8.ValidString(got) {
t.Errorf("maxLen=%d pad=%d: cell ends in a partial rune: % x", maxLen, pad, []byte(got))
}
}
}
}

// Short cells and exact fits are returned verbatim, and ASCII truncation is
// byte-for-byte what the four removed helpers produced.
func TestTruncateCellASCIIUnchanged(t *testing.T) {
cases := []struct {
s string
maxLen int
want string
}{
{"short", 10, "short"},
{"exactlyten", 10, "exactlyten"},
{strings.Repeat("x", 30), 10, strings.Repeat("x", 7) + "..."},
{strings.Repeat("x", 100), 60, strings.Repeat("x", 57) + "..."},
{strings.Repeat("x", 10), 3, "xxx"},
{strings.Repeat("x", 10), 2, "xx"},
}
for _, c := range cases {
if got := truncateCell(c.s, c.maxLen); got != c.want {
t.Errorf("truncateCell(%d chars, %d) = %q, want %q", len(c.s), c.maxLen, got, c.want)
}
}
}

// joinInterfaceSlice keeps maxLen == 0 meaning "do not truncate", and takes
// the rune-safe cut for a positive width. allowed_servers holds server names,
// which the config only forbids from being empty or containing ':'.
func TestJoinInterfaceSliceTruncationIsRuneSafe(t *testing.T) {
name := strings.Repeat("語", 20)
m := map[string]interface{}{"allowed_servers": []interface{}{name, name}}

if got := joinInterfaceSlice(m, "allowed_servers", 0); got != name+","+name {
t.Errorf("maxLen 0 must not truncate: got %d bytes, want %d", len(got), len(name)*2+1)
}

got := joinInterfaceSlice(m, "allowed_servers", 23)
if len(got) > 23 {
t.Errorf("got %d bytes, over the 23-byte column", len(got))
}
if !utf8.ValidString(got) {
t.Errorf("joined server names end in a partial rune: % x", []byte(got))
}
if !strings.HasSuffix(got, "...") {
t.Errorf("a shortened value must stay marked: %q", got)
}
fmt.Fprint(nopWriter{}, got)
}

type nopWriter struct{}

func (nopWriter) Write(p []byte) (int, error) { return len(p), nil }
4 changes: 2 additions & 2 deletions cmd/mcpproxy/token_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,8 @@ func joinInterfaceSlice(m map[string]interface{}, key string, maxLen int) string
strs[i] = fmt.Sprintf("%v", s)
}
result := strings.Join(strs, ",")
if maxLen > 0 && len(result) > maxLen {
result = result[:maxLen-3] + "..."
if maxLen > 0 {
result = truncateCell(result, maxLen)
}
return result
}
5 changes: 1 addition & 4 deletions cmd/mcpproxy/upstream_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2482,10 +2482,7 @@ func runUpstreamInspect(_ *cobra.Command, args []string) error {
approvedCount++
}

desc := r.Description
if len(desc) > 60 {
desc = desc[:57] + "..."
}
desc := truncateCell(r.Description, 60)

hash := r.Hash
if len(hash) > 12 {
Expand Down
Loading