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

Function DecodeMessage

internal/jsonrpc2/messages.go:174–207  ·  view source on GitHub ↗
(data []byte)

Source from the content-addressed store, hash-verified

172}
173
174func DecodeMessage(data []byte) (Message, error) {
175 msg := wireCombined{}
176 if err := internaljson.Unmarshal(data, &msg); err != nil {
177 return nil, fmt.Errorf("unmarshaling jsonrpc message: %w", err)
178 }
179 if msg.VersionTag != wireVersion {
180 return nil, fmt.Errorf("invalid message version tag %q; expected %q", msg.VersionTag, wireVersion)
181 }
182 id, err := MakeID(msg.ID)
183 if err != nil {
184 return nil, err
185 }
186 if msg.Method != "" {
187 // has a method, must be a call
188 return &Request{
189 Method: msg.Method,
190 ID: id,
191 Params: msg.Params,
192 }, nil
193 }
194 // no method, should be a response
195 if !id.IsValid() {
196 return nil, ErrInvalidRequest
197 }
198 resp := &Response{
199 ID: id,
200 Result: msg.Result,
201 }
202 // we have to check if msg.Error is nil to avoid a typed error
203 if msg.Error != nil {
204 resp.Error = msg.Error
205 }
206 return resp, nil
207}
208
209func marshalToRaw(obj any) (json.RawMessage, error) {
210 if obj == nil {

Callers 8

TestWireMessageFunction · 0.92
doMethod · 0.92
readBatchFunction · 0.92
loadConformanceTestFunction · 0.92
ServeHTTPMethod · 0.92
ReadMethod · 0.92
DecodeMessageFunction · 0.92

Calls 2

IsValidMethod · 0.80
MakeIDFunction · 0.70

Tested by 4

TestWireMessageFunction · 0.74
doMethod · 0.74
loadConformanceTestFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…