Skip to content

Proposal: jsonrpc: expose Error (fka WireError) #452

@findleyr

Description

@findleyr

Some MCP handlers may need to return jsonrpc errors (for example, if arguments are invalid). Currently, if an error wraps a jsonrpc2.WireError, it is promoted to a jsonrpc2 error by the connection, with the associated error code.

We should expose this type in the SDK API. I think 'WireError' is too much, and therefore suggest:

// An Error represents a JSON-RPC level error.
//
// If JSON-RPC handlers return an Error, it will be used as the wire-level error object.
// https://www.jsonrpc.org/specification#error_object
type Error struct {
  // Code is an error code indicating the type of failure.
  Code int64 `json:"code"`
  // Message is a short description of the error.
  Message string `json:"message"`
  // Data is optional structured data containing additional information about the error.
  Data json.RawMessage `json:"data,omitempty"`
}

func (err *Error) Error() string {
  return err.Message
}

func (err *Error) Is(other error) bool {
  w, ok := other.(*Error)
  if !ok {
    return false
  } 
  return err.Code == w.Code
}

Metadata

Metadata

Assignees

Labels

proposalA proposal for an a new API or behavior. See CONTRIBUTING.md.proposal-acceptedProposals that have been accepted.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions