Learn how to protect your MCP servers with Nevermined Payments Library.
withPaywall
to automatically verify Authorization
tokens and check for valid subscriptions.attach
method to register and protect your tools in a single, clean step.McpServer
or Python’s FastMCP
) and custom low-level ASGI/Express routers.city="Paris"
) and the server executes the action.weather://today/Paris
) provides the raw, structured data (like a JSON object) that an agent can parse and use for further tasks.Authorization
header to verify they have a valid subscription and sufficient credits through the Nevermined protocol. If they don’t, the request is blocked. If they do, the request proceeds, and after your handler successfully completes, the paywall automatically deducts the configured number of credits.
This integration allows you to build a sustainable business model around your AI services. You can offer different subscription tiers (plans), charge dynamically based on usage, and maintain a complete audit trail of every transaction, all without cluttering your core application logic with complex payment code.
@modelcontextprotocol/sdk
)@nevermined-io/payments
(Nevermined SDK)weather.today
.sandbox
or live
).
withPaywall
(TS) or with_paywall
(PY) decorator is the core of the integration. It wraps your business logic, checks for authentication, and burns credits.
First, define your handler. It should contain only your business logic, returning a standard MCP content
object.
extra
object, containing request headers, is passed automatically by the MCP server to the handler. The paywall uses this to extract the Authorization
token.credits
option. It receives a context object (ctx
) with request args
, the handler’s result
, and extra
metadata.
attach
withPaywall
is useful, it can be repetitive. The attach
method provides a more streamlined alternative by combining registration and protection into a single call. It takes your server instance and returns a registrar object with protected registerTool
, registerResource
, and registerPrompt
methods.
extra
object (containing request headers) to the protected handler.
accessToken
and include it in the Authorization
header of every MCP request.
-32003
-32002