MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / EncodeIndent

Function EncodeIndent

internal/jsonrpc2/messages.go:161–172  ·  view source on GitHub ↗

EncodeIndent is like EncodeMessage, but honors indents. TODO(rfindley): refactor so that this concern is handled independently. Perhaps we should pass in a json.Encoder?

(msg Message, prefix, indent string)

Source from the content-addressed store, hash-verified

159// TODO(rfindley): refactor so that this concern is handled independently.
160// Perhaps we should pass in a json.Encoder?
161func EncodeIndent(msg Message, prefix, indent string) ([]byte, error) {
162 wire := wireCombined{VersionTag: wireVersion}
163 msg.marshal(&wire)
164 var buf bytes.Buffer
165 enc := json.NewEncoder(&buf)
166 enc.SetEscapeHTML(false)
167 enc.SetIndent(prefix, indent)
168 if err := enc.Encode(&wire); err != nil {
169 return nil, fmt.Errorf("marshaling jsonrpc message: %w", err)
170 }
171 return bytes.TrimRight(buf.Bytes(), "\n"), nil
172}
173
174func DecodeMessage(data []byte) (Message, error) {
175 msg := wireCombined{}

Callers 1

runServerTestFunction · 0.92

Calls 2

BytesMethod · 0.80
marshalMethod · 0.65

Tested by 1

runServerTestFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…