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

server

package
v0.6.0 Latest Latest
Warning

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

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

Documentation

Overview

Package server charges for MCP tools served with the official MCP Go SDK (github.com/modelcontextprotocol/go-sdk), matching the mppx/mcp/server transport (Transport.mcpSdk):

  • Credentials are read from the tool call's _meta["org.paymentauth/credential"].
  • Payment errors become JSON-RPC errors with the specification-defined codes (-32042 payment required, -32043 verification failed, -32602 malformed credential, -32603 internal error) carrying {httpStatus, challenges, problem} as data.
  • Receipts are attached as _meta["org.paymentauth/receipt"] on the tool result.

A tool handler charges like this:

res, err := mcpserver.Charge(ctx, payment, req, mppserver.ChargeParams{Amount: "0.01"})
if err != nil {
	return nil, err
}
if res.IsChallenge() {
	return nil, res.Challenge
}
return res.WithReceipt(&mcp.CallToolResult{Content: content}), nil

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Result

type Result struct {
	// Challenge is the JSON-RPC error to return from the tool handler when the
	// client still needs to pay or its Credential was rejected. The SDK sends
	// it to the client unchanged.
	Challenge *jsonrpc.Error
	// 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 Challenge to return from the tool handler or a verified (Credential, Receipt) pair.

func Charge

func Charge(
	ctx context.Context,
	payment *mppserver.Mpp,
	req *mcp.CallToolRequest,
	params mppserver.ChargeParams,
) (*Result, error)

Charge runs payment.Charge for an MCP tool call, reading the Credential from the request's _meta instead of HTTP headers. Leave the credential fields of params empty. It returns a Result whose Challenge the handler returns when payment is still required or was rejected, and an error only when no Challenge could be issued, such as for invalid params.

func Compose

func Compose(
	ctx context.Context,
	req *mcp.CallToolRequest,
	configs ...mppserver.ComposeConfig,
) (*Result, error)

Compose offers several payment options on one tool, like server.ComposeMiddleware for HTTP and mppx's compose: without a Credential the Challenge 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 tool handler must return Challenge.

func (*Result) WithReceipt

func (r *Result) WithReceipt(result *mcp.CallToolResult) *mcp.CallToolResult

WithReceipt returns a copy of result with the payment receipt attached as _meta["org.paymentauth/receipt"], keeping its other metadata. A nil result becomes an empty tool result.

Jump to

Keyboard shortcuts

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