Repository navigation
fix: escape proto comments correctly in generated descriptions and schemas (#100) - #101
Merged
Merged
Conversation
added 2 commits
October 8, 2026 19:04
Two bugs in how proto comments become Go string literals and JSON Schema descriptions: - processCommentToString escaped only ", so a comment containing a backslash (e.g. "matches \d+") produced an invalid Go string literal. Tool descriptions are now emitted with strconv.Quote instead of hand-wrapping the raw text in quotes, so any character valid in the source comment round-trips correctly. - processCommentToString also backslash-escaped " before the result was fed to schema.go's JSONSchema (later encoding/json-marshaled), so json.Marshal escaped it a second time and clients saw \\"quote\\" in schema descriptions. processCommentToString no longer escapes anything: callers are responsible for escaping the raw comment text for whatever they embed it in (strconv.Quote for a Go literal, encoding/json for a schema description). Added comments with a double quote, a backslash, a backtick and a newline to examples/protos/example.proto's SetVibeRequest.vibe field and the VibeService's SetVibe RPC, regenerated the example and golden file, and added a unit test (TestProcessCommentToString) covering each character directly.
3 tasks
Contributor
Author
|
🔍 Review (Claude, on behalf of @erikist): verified in the factory image.
Nits for later:
Merging ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #100
Summary
Two bugs in how proto comments become Go string literals and JSON Schema descriptions, both in
processCommentToString(cmd/protoc-gen-go-mcp/mcp.go):processCommentToStringonly escaped", so a comment like// matches \d+produced Go source that didn't parse (strconv.Quoterequires\\, not a bare\). Fixed by emitting the toolDescriptionfield withstrconv.Quote(methodDescription)instead of hand-wrapping the raw string in"s.schema.goruns comments throughprocessCommentToString, which escaped"to\", and thenjson.Marshalescaped the resulting backslash a second time, so clients saw\"quote\"in tool and field descriptions. Fixed by makingprocessCommentToStringreturn the raw comment text, untouched beyond whitespace normalization: each caller now does its own escaping exactly once (strconv.Quotefor the Go literal inmcp.go,encoding/jsonfor the schema description inschema.go).processCommentToString's whitespace-normalization logic (joining lines, collapsing runs of spaces) also got a small rewrite so it strips both line-comment (// foo, with per-line//prefixes) and block-comment (/* foo */) markers uniformly, without relying on the two comment styles leaving inconsistent artifacts.Decisions made
",\, a backtick, and a newline) to the existing example proto (examples/protos/example.proto) rather than a separate test proto, extending theSetVibeRequest.vibefield comment and theVibeService's block comment onSetVibe, since the golden-file and e2e tests already exercise that proto end to end.examples/gen/example/v1/*.pb.gowithmake generate(protoc v29.3, protoc-gen-go v1.36.6, protoc-gen-go-grpc v1.5.1 — matching what's already pinned ingolden_test.goand used elsewhere in this repo) and updated the golden file.Test evidence
New unit test
TestProcessCommentToString(cmd/protoc-gen-go-mcp/mcp_test.go) asserts the function passes through a double quote, a backslash, a backtick, multi-line joining, and a block comment unescaped. The existing end-to-end testTestToolsList(examples/gen/example/v1/e2e_test.go) was updated to assert theSetVibetool's description and itsvibefield's schema description match the new comment text exactly, as read off the real MCP JSON-RPC wire response.Confirmed the backslash bug existed before this fix: before the fix, adding the backslash to the proto source made
go build ./...fail (invalid Go string literal) andTestToolsListshowed the double-escaped quote diff.Notes for reviewers
Self-reviewed with the
reviewersubagent against the issue's acceptance criteria; it found no blocking issues. One clarity nit aboutprocessCommentToString's internal doc comment was addressed in a follow-up commit.No changes outside
cmd/protoc-gen-go-mcp/mcp.go, its test, the example proto, and the regenerated example files.Factory-authored