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

client

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: 15 Imported by: 0

Documentation

Overview

Package client pays for MCP tool calls made with the official MCP Go SDK (github.com/modelcontextprotocol/go-sdk), matching mppx's McpClient.wrap.

Wrap installs sending middleware on an *mcp.Client, so every session it connects, and every CallTool on them, handles payment challenges whether they arrive as payment-required JSON-RPC errors (-32042 or -32043) or as tool results carrying _meta["org.paymentauth/payment-required"]. The client pays with a configured method and retries the call with the Credential in _meta["org.paymentauth/credential"]; Receipt reads the receipt from the result:

c := mcp.NewClient(&mcp.Implementation{Name: "my-client", Version: "1.0.0"}, nil)
mcpclient.Wrap(c, mcpclient.Config{Methods: []client.Method{method}})
session, err := c.Connect(ctx, transport, nil)
...
result, err := session.CallTool(ctx, &mcp.CallToolParams{Name: "premium_tool"})
receipt, err := mcpclient.Receipt(result)

Index

Constants

This section is empty.

Variables

View Source
var ErrPaymentDeclined = errors.New("mpp: payment declined")

ErrPaymentDeclined is returned when the approval hook declines a payment.

Functions

func IsPaymentRequiredError

func IsPaymentRequiredError(err error) bool

IsPaymentRequiredError reports whether err is an MCP payment-required or payment-verification-failed error that carries at least one Challenge.

func Receipt

func Receipt(result *mcp.CallToolResult) (*mpp.MCPReceipt, error)

Receipt returns the payment receipt in a tool result's _meta["org.paymentauth/receipt"], or nil when it has none.

func WithOnPaymentRequired

func WithOnPaymentRequired(ctx context.Context, fn OnPaymentRequired) context.Context

WithOnPaymentRequired returns a context whose tool calls use fn instead of Config.OnPaymentRequired. A nil fn pays without asking.

func Wrap

func Wrap(c *mcp.Client, config Config) *mcp.Client

Wrap adds automatic payment handling to tool calls made through sessions of c and returns c. Sessions connected before and after Wrap both use it. Calling Wrap again on the same client replaces its payment configuration.

Types

type Config

type Config struct {
	// Methods are the payment methods the client can pay with.
	Methods []mppclient.Method
	// OnPaymentRequired, when set, approves every payment before it is made.
	OnPaymentRequired OnPaymentRequired
	// OrderChallenges, when set, filters and sorts payable Challenges.
	OrderChallenges OrderChallenges
	// PaymentPreferences ranks Challenges by "method/intent" q-values.
	PaymentPreferences mppclient.PaymentPreferences
	// TrustedTempoProofRealms lists the realms the client signs zero-amount
	// Tempo proofs for. A zero-amount tempo/charge Challenge authenticates the
	// payer at the realm it names, and an MCP server has no origin to compare
	// that realm with, so other realms are refused, like the HTTP Transport
	// refuses realms its origin does not own.
	TrustedTempoProofRealms []string
}

Config configures payment handling for an MCP client.

type OnPaymentRequired

type OnPaymentRequired func(ctx context.Context, challenge *mpp.Challenge) (bool, error)

OnPaymentRequired approves a payment before a Credential is created for challenge. Returning false declines it.

type OrderChallenges

type OrderChallenges func(
	ctx context.Context,
	candidates []mppclient.ChallengeCandidate,
) ([]mppclient.ChallengeCandidate, error)

OrderChallenges filters and sorts the payable Challenges before one is paid; the first one returned is paid.

Jump to

Keyboard shortcuts

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