The Unified Identity & Payment Model
Our platform uses a unified identity and payment model that securely supports both interactive and programmatic (machine-to-machine) use cases. Think of it like a debit card system for the decentralized web. A user’s Principal is their bank account, which holds their funds. They authorize services to draw from this account using two types of credentials: a short-lived JWT for interactive sessions, and a long-lived API Key for automated services. The entire system is built on a single, non-custodial principle:- Identity is the Principal: The user’s on-chain identity is always their Principal ID.
- Payment is the Allowance: A user grants a spending allowance (via
icrc2_approve
) from their Principal to your service. This allowance is the single source of truth for all payments.
Step 1: Activate Monetization in Your Code
Your project template contains all the necessary code for monetization, but it’s commented out by default. In this step, you’ll activate it. Open thesrc/main.mo
file in your editor. You will need to uncomment three sections of code.
1
Update the Tools to Require Payment
Find the
tools
array and uncomment the payment
block inside the get_weather
tool definition. This tells the MCP SDK that calling this tool requires a valid payment from an authenticated Principal.src/main.mo
2
Configure the Allowance Management URL
Uncomment the
allowanceUrl
in the mcpConfig
. This URL is returned to clients and AI agents, directing users to the web dashboard where they can manage their token allowances and API keys for your service.src/main.mo
3
Initialize the Authentication Context
Finally, find and uncomment the large block of code that initializes the
authContext
. This is the core logic that integrates your server with the Prometheus identity layer, enabling it to validate access tokens (both JWTs and API Keys) on incoming requests.src/main.mo
Step 2: Deploy the Updated Server
Now that the code is activated, save thesrc/main.mo
file and deploy the new logic to your local replica.
Step 3: Generate and Test an API Key
With the authentication context active, your service can now issue and validate API keys out of the box. You can test this programmatic access flow by calling your canister directly withdfx
to generate a key. The key will be linked to your current dfx
identity.
1
Generate the Key
Run the following command, replacing The command will return your new API key.
<your_canister_id>
with the ID from the npm run deploy
output.Save this key immediately! This is the only time it will be visible.
2
Test the Key in MCP Inspector
You can now use this key to call a protected tool. The key must be passed in via the
x-api-key
header.-
Open the MCP Inspector if it’s not already open:
npx @modelcontextprotocol/inspector
-
Set transport type to Streamable HTTP, and enter the URL of your local server (e.g.,
http://127.0.0.1:4943/mcp?canisterId=tnszz-sh777-77774-qaasa-cai
). -
Click the Authentication button, and set API Token Authentication using
x-api-key
as the header name and your generated API key as the value. - Click Connect. If successful, your server will return the weather data, proving that your API key authentication and payment logic is working correctly.
🎉 Congratulations! Your server is now a fully monetized service. You’ve enabled and tested programmatic access via API Keys and you know how to add browser-based login for interactive web apps. Next, you’ll learn how to manage your service post-launch, including withdrawing funds from your treasury and enabling usage analytics.