Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2/wire.go

raw symbols SCIP
1 // Copyright 2018 The Go MCP SDK Authors. All rights reserved.
2 // Use of this source code is governed by an MIT-style
3 // license that can be found in the LICENSE file.
4
5 package jsonrpc2
6
7 import (
8 "encoding/json"
9 )
10
11 // This file contains the go forms of the wire specification.
12 // see http://www.jsonrpc.org/specification for details
13
14 var (
15 // ErrParse is used when invalid JSON was received by the server.
16 ErrParse = NewError(-32700, "parse error")
17 // ErrInvalidRequest is used when the JSON sent is not a valid Request object.
18 ErrInvalidRequest = NewError(-32600, "invalid request")
19 // ErrMethodNotFound should be returned by the handler when the method does
20 // not exist / is not available.
21 ErrMethodNotFound = NewError(-32601, "method not found")
22 // ErrInvalidParams should be returned by the handler when method
23 // parameter(s) were invalid.
24 ErrInvalidParams = NewError(-32602, "invalid params")
25 // ErrInternal indicates a failure to process a call correctly
26 ErrInternal = NewError(-32603, "internal error")
27
28 // The following errors are not part of the json specification, but
29 // compliant extensions specific to this implementation.
30
31 // ErrUnknown should be used for all non coded errors.
32 ErrUnknown = NewError(-32001, "unknown error")
33 // ErrServerClosing is returned for calls that arrive while the server is closing.
34 ErrServerClosing = NewError(-32004, "server is closing")
35 // ErrClientClosing is a dummy error returned for calls initiated while the client is closing.
36 ErrClientClosing = NewError(-32003, "client is closing")
37
38 // The following errors have special semantics for MCP transports
39
40 // ErrRejected may be wrapped to return errors from calls to Writer.Write
41 // that signal that the request was rejected by the transport layer as
42 // invalid.
43 //
44 // Such failures do not indicate that the connection is broken, but rather
45 // should be returned to the caller to indicate that the specific request is
46 // invalid in the current context.
47 ErrRejected = NewError(-32005, "rejected by transport")
48 )
49
50 const wireVersion = "2.0"
51
52 // wireCombined has all the fields of both Request and Response.
53 // We can decode this and then work out which it is.
54 type wireCombined struct {
55 VersionTag string `json:"jsonrpc"`
56 ID any `json:"id,omitempty"`
57 Method string `json:"method,omitempty"`
58 Params json.RawMessage `json:"params,omitempty"`
59 Result json.RawMessage `json:"result,omitempty"`
60 Error *WireError `json:"error,omitempty"`
61 }
62
63 // WireError represents a structured error in a Response.
64 type WireError struct {
65 // Code is an error code indicating the type of failure.
66 Code int64 `json:"code"`
67 // Message is a short description of the error.
68 Message string `json:"message"`
69 // Data is optional structured data containing additional information about the error.
70 Data json.RawMessage `json:"data,omitempty"`
71 }
72
73 // NewError returns an error that will encode on the wire correctly.
74 // The standard codes are made available from this package, this function should
75 // only be used to build errors for application specific codes as allowed by the
76 // specification.
77 func NewError(code int64, message string) error {
78 return &WireError{
79 Code: code,
80 Message: message,
81 }
82 }
83
84 func (err *WireError) Error() string {
85 return err.Message
86 }
87
88 func (err *WireError) Is(other error) bool {
89 w, ok := other.(*WireError)
90 if !ok {
91 return false
92 }
93 return err.Code == w.Code
94 }