Skip to content

fix(connect): keep string contents when stripping trailing commas - #1768

Merged
Dumbris merged 1 commit into
smart-mcp-proxy:mainfrom
rudycelekli:fix/connect-trailing-comma-strings-20261023-01
Oct 26, 2026
Merged

Dumbris merged 1 commit into
smart-mcp-proxy:mainfrom
rudycelekli:fix/connect-trailing-comma-strings-20261023-01

Conversation

@rudycelekli

Copy link
Copy Markdown
Contributor

Bug

unmarshalLenientJSON (the JSONC fallback added for #922) removes trailing commas with the regex ,\s*([}\]]). The regex has no notion of JSON strings, so it also rewrites , } and , ] inside string values. The fallback runs whenever strict parsing fails, for example on a hand-edited opencode.json with one trailing comma. Connect, disconnect, undo and preview then work on the altered document, and connect writes it back to the client's config file. Another server's arguments are silently changed on disk.

stripJSONComments, the other half of the same fallback, is already string-aware for exactly this reason.

Trigger

~/.config/opencode/opencode.json:

{
  "mcp": {
    "other": {"type": "local", "command": ["sh", "-c", "echo {a, } [b, ]"]},
  },
}

After mcpproxy connect opencode, the other server's command on disk is ["sh", "-c", "echo {a} [b]"].

Fix

Replace the regex with stripTrailingCommas, a small scanner that tracks string and escape state the same way stripJSONComments does. It drops a , only when it is outside a string and the next non-whitespace byte is } or ]. The now-unused regexp import is removed.

Test

TestConnect_TrailingCommaCleanupPreservesStringContents runs a real Connect("opencode", ...) on the file above and checks the other server's command is byte-for-byte unchanged after the write. The existing trailing-comma and JSONC tests still pass.

Commands run

  • CGO_ENABLED=0 go vet ./internal/connect: clean
  • CGO_ENABLED=0 go test -count=1 ./internal/connect: ok
  • With the fix reverted (git stash of connect.go), the new test fails with other server's command rewritten: got ["sh" "-c" "echo {a} [b]"].
  • gofmt -l on the edited files: no output

@codecov-commenter

codecov-commenter commented Oct 24, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 85.18519% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/connect/connect.go 85.18% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@Dumbris Dumbris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified: the PR test fails on main for the reason described and passes on this branch merged with main; race tests for the touched packages and CI lint (bare and server tags) are clean. Also built and tested together with the rest of this batch, independently reviewed, and cross-reviewed with a second model.

@Dumbris
Dumbris merged commit 3af8e25 into smart-mcp-proxy:main Oct 26, 2026
46 checks passed
@Dumbris

Dumbris commented Oct 26, 2026

Copy link
Copy Markdown
Member

Merged. Thank you, @rudycelekli! connect no longer rewrites , } / , ] inside string values of a client's JSONC config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants