Help us shape the future of Go! Take the 2026 Go Developer Survey

jsonrpc

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2026 License: MIT, Apache-2.0, MIT Imports: 7 Imported by: 0

Documentation

Overview

Package jsonrpc charges for MCP tool calls handled as raw JSON-RPC messages, without an MCP SDK, matching mppx's raw MCP server transport (Transport.mcp()):

  • Credentials are read from params._meta["org.paymentauth/credential"].
  • Payment errors are JSON-RPC error responses with the request's id and codes -32042 (payment required), -32043 (verification failed), -32602 (malformed credential) or -32603 (internal error), carrying {httpStatus, challenges, problem} as data.
  • Receipts are attached as result._meta["org.paymentauth/receipt"]; error responses are returned unchanged.

For servers built on the official MCP Go SDK, use pkg/mcp/server instead.

res, err := jsonrpc.Charge(ctx, payment, req, server.ChargeParams{Amount: "0.01"})
if err != nil {
	return nil, err
}
if res.IsChallenge() {
	return res.Challenge, nil
}
return res.WithReceipt(&jsonrpc.Response{JSONRPC: "2.0", ID: req.ID, Result: result}), nil

Index

Constants

View Source
const Version = "2.0"

Version is the JSON-RPC protocol version.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Code    int64           `json:"code"`
	Message string          `json:"message"`
	Data    json.RawMessage `json:"data,omitempty"`
}

Error is a JSON-RPC error object. For payment errors Data holds mpp.MCPPaymentRequiredData.

func (*Error) PaymentRequired

func (e *Error) PaymentRequired() (*mpp.MCPPaymentRequiredData, bool)

PaymentRequired decodes the payment data of a payment error, reporting false for other errors.

type Request

type Request struct {
	JSONRPC string          `json:"jsonrpc,omitempty"`
	ID      json.RawMessage `json:"id,omitempty"`
	Method  string          `json:"method"`
	Params  map[string]any  `json:"params,omitempty"`
}

Request is a JSON-RPC request carrying an MCP method call, such as tools/call. Params holds the method's params, including _meta.

func (*Request) Meta

func (r *Request) Meta() map[string]any

Meta returns the request's params._meta, or nil when it has none.

func (*Request) UnmarshalJSON

func (r *Request) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes numbers as json.Number, so integers above 2^53 in the Credential survive re-encoding and still match the Challenge they echo.

type Response

type Response struct {
	JSONRPC string          `json:"jsonrpc,omitempty"`
	ID      json.RawMessage `json:"id,omitempty"`
	Result  map[string]any  `json:"result,omitempty"`
	Error   *Error          `json:"error,omitempty"`
}

Response is a JSON-RPC response: Result on success, Error on failure.

type Result

type Result struct {
	// Challenge is the JSON-RPC error response to send when the client still
	// needs to pay or its Credential was rejected. It carries the request's id.
	Challenge *Response
	// Credential is the verified client Credential on success.
	Credential *mpp.Credential
	// Receipt acknowledges the verified payment on success.
	Receipt *mpp.Receipt
	// contains filtered or unexported fields
}

Result is either a payment error response to send or a verified (Credential, Receipt) pair.

func Charge

func Charge(ctx context.Context, payment *server.Mpp, req *Request, params server.ChargeParams) (*Result, error)

Charge runs payment.Charge for a JSON-RPC request, reading the Credential from params._meta instead of HTTP headers. Leave the credential fields of params empty. It returns an error only when no Challenge could be issued, such as for invalid params.

func Compose

func Compose(ctx context.Context, req *Request, configs ...server.ComposeConfig) (*Result, error)

Compose offers several payment options for one request, like mppx's compose: without a Credential the error lists every configured offer, and a Credential is verified against the offer it echoes, so only that offer is settled.

func (*Result) IsChallenge

func (r *Result) IsChallenge() bool

IsChallenge reports whether the server must send Challenge.

func (*Result) WithReceipt

func (r *Result) WithReceipt(response *Response) *Response

WithReceipt returns a copy of response with the payment receipt attached as result._meta["org.paymentauth/receipt"], keeping its other metadata. Error responses are returned unchanged, as in mppx.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL