Signature
Parameters
mcpServer
Your MCP server instance created with@modelcontextprotocol/sdk:
- A
.serverproperty (the internal low-level server) - The
.server.setRequestHandler()method
config
APaymentConfig object with your settings:
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:
- Creating a Proxy - Intercepts the
.serverproperty - Wrapping setRequestHandler - Catches tool handler registration
- Injecting Payment Logic - Wraps
tools/callandtools/listhandlers
Pricing Configuration
Thepricing 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:
Multiple Servers
You can wrap multiple servers independently:Chaining with Other Wrappers
If you have other wrappers/middleware, applywithPayments() last so payment happens first: