Skip to main content

Signature

Parameters

mcpServer

Your MCP server instance created with @modelcontextprotocol/sdk:
The SDK is compatible with any object that has:
  • A .server property (the internal low-level server)
  • The .server.setRequestHandler() method

config

A PaymentConfig object with your settings:
See Configuration for details on each option.

Return Value

Returns the same server instance with payment logic injected. The server’s type is preserved for TypeScript compatibility.

Basic Usage

How It Works

withPayments() modifies your server by:
  1. Creating a Proxy - Intercepts the .server property
  2. Wrapping setRequestHandler - Catches tool handler registration
  3. Injecting Payment Logic - Wraps tools/call and tools/list handlers
When a tool is called:

Pricing Configuration

The pricing object maps tool names to USD prices:
Tools not in the pricing object are treated as free by default.

Validations

withPayments() validates your configuration at startup:

Error Handling

If initialization fails, withPayments() throws synchronously:
Runtime payment errors (during tool calls) are handled by the wrapper and returned to agents as tool errors.

Multiple Servers

You can wrap multiple servers independently:

Chaining with Other Wrappers

If you have other wrappers/middleware, apply withPayments() last so payment happens first:

TypeScript

Full type inference is preserved: