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
43 changes: 39 additions & 4 deletions internal/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -1701,8 +1700,6 @@ func entryPointsToBridge(entry map[string]interface{}, baseURL string) bool {
return hasBridgePkg && pointsToUs
}

var trailingCommaPattern = regexp.MustCompile(`,\s*([}\]])`)

func unmarshalLenientJSON(raw []byte, out interface{}) error {
if err := json.Unmarshal(raw, out); err == nil {
normalizeNilConfigMap(out)
Expand All @@ -1715,7 +1712,7 @@ func unmarshalLenientJSON(raw []byte, out interface{}) error {
if cerr != nil {
return cerr
}
cleaned = trailingCommaPattern.ReplaceAll(cleaned, []byte(`$1`))
cleaned = stripTrailingCommas(cleaned)
if err := json.Unmarshal(cleaned, out); err != nil {
return err
}
Expand Down Expand Up @@ -1791,6 +1788,44 @@ func stripJSONComments(raw []byte) ([]byte, error) {
return out, nil
}

// stripTrailingCommas drops a ',' that is followed (after whitespace) by '}'
// or ']'. It is string-aware like stripJSONComments: a ", }" inside a JSON
// string value (a shell snippet in args, say) is content and must survive,
// because connect writes the parsed document back to the client's file.
func stripTrailingCommas(raw []byte) []byte {
out := make([]byte, 0, len(raw))
inString := false
escaped := false
for i := 0; i < len(raw); i++ {
c := raw[i]
if inString {
switch {
case escaped:
escaped = false
case c == '\\':
escaped = true
case c == '"':
inString = false
}
out = append(out, c)
continue
}
if c == '"' {
inString = true
} else if c == ',' {
j := i + 1
for j < len(raw) && (raw[j] == ' ' || raw[j] == '\t' || raw[j] == '\n' || raw[j] == '\r') {
j++
}
if j < len(raw) && (raw[j] == '}' || raw[j] == ']') {
continue
}
}
out = append(out, c)
}
return out
}

// jsonHasComments reports whether stripping comments would change the content —
// i.e. the file actually uses JSONC comments (not just slashes inside strings).
// An unterminated block comment counts as commented (the write guard refuses).
Expand Down
48 changes: 48 additions & 0 deletions internal/connect/trailing_comma_strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package connect

import (
"encoding/json"
"os"
"path/filepath"
"testing"
)

// The trailing-comma cleanup must not touch ", }" / ", ]" inside string
// values: connect writes the parsed document back, so a rewritten shell
// snippet in another server's args would silently change on disk.
func TestConnect_TrailingCommaCleanupPreservesStringContents(t *testing.T) {
const snippet = `echo {a, } [b, ]`
svc, home := testService(t)
cfgPath := ConfigPath("opencode", home)
if err := os.MkdirAll(filepath.Dir(cfgPath), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(cfgPath, []byte(`{
"mcp": {
"other": {"type": "local", "command": ["sh", "-c", "echo {a, } [b, ]"]},
},
}`), 0o644); err != nil {
t.Fatal(err)
}

if res, err := svc.Connect("opencode", "mcpproxy", false); err != nil || !res.Success {
t.Fatalf("connect: %+v, %v", res, err)
}

raw, err := os.ReadFile(cfgPath)
if err != nil {
t.Fatal(err)
}
var data struct {
MCP map[string]struct {
Command []string `json:"command"`
} `json:"mcp"`
}
if err := json.Unmarshal(raw, &data); err != nil {
t.Fatal(err)
}
cmd := data.MCP["other"].Command
if len(cmd) != 3 || cmd[2] != snippet {
t.Fatalf("other server's command rewritten: got %q, want [sh -c %q]", cmd, snippet)
}
}
Loading