Skip to content

fix: escape proto comments correctly in generated descriptions and schemas #100

Closed

Description

@erikist

Found while reviewing #99. Both bugs are in how proto comments become Go string literals and JSON Schema descriptions:

  1. A backslash in an RPC or message comment breaks generation. For example, // matches \d+ produces Go that doesn't parse, because processCommentToString (cmd/protoc-gen-go-mcp/mcp.go) escapes only ". This already happens on main before the migration.
    • Fix: emit string literals with strconv.Quote (or %q) instead of hand-escaping.
  2. Quotes are double-escaped in schema descriptions. schema.go (feat: generate JSON Schema for tool inputs from proto descriptors (protojson-compatible) #88) runs comments through processCommentToString and then json.Marshal escapes them again, so clients see \"quote\" in tool and field descriptions. feat!: generate MCP servers on the official Go MCP SDK (#89) #99 is the first PR to expose this to clients.
    • Fix: the schema builder uses the raw comment text, and only the final Go literal gets escaped.

Acceptance criteria

  • Add comments with ", \, a backtick and a newline to the example proto (or a test proto), and regenerate.
  • The generated code compiles, and the tool description and schema descriptions match the comment text exactly (an e2e or unit assertion).
  • Update the golden file.

Activity

  1. erikist commented on Oct 8, 2026

    @erikist
    ContributorAuthor

    🤖 Factory: picked up by dispatcher; working on branch factory/100-fix-escape-proto-comments-correctly-in.

  2. erikist commented on Oct 8, 2026

    @erikist
    ContributorAuthor

    🤖 Factory: opened PR #101 to fix this: #101

    Summary: processCommentToString no longer escapes "/\ itself (that was causing the double-escaping and the invalid-Go-literal bug); the tool Description field is now emitted with strconv.Quote, and the schema builder's raw comment text is escaped exactly once by encoding/json. Added ", \, a backtick and a newline to examples/protos/example.proto's SetVibeRequest.vibe field and the SetVibe RPC comment, regenerated the example and golden file, and added TestProcessCommentToString plus updated e2e assertions.

    Checks: gofmt -l ., go vet ./..., go build ./..., go test ./..., and the golden-file test all pass — see the PR body for full output.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions