create-meterkit 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -2
- package/dist/cli.d.ts +11 -1
- package/dist/cli.js +104 -7
- package/dist/cli.js.map +1 -1
- package/dist/cli.test.js +59 -0
- package/dist/cli.test.js.map +1 -1
- package/dist/plan.d.ts +1 -0
- package/dist/plan.js +26 -4
- package/dist/plan.js.map +1 -1
- package/dist/templates.integration.test.js +79 -0
- package/dist/templates.integration.test.js.map +1 -1
- package/dist/write.d.ts +14 -0
- package/dist/write.js +95 -1
- package/dist/write.js.map +1 -1
- package/dist/write.test.d.ts +1 -0
- package/dist/write.test.js +48 -0
- package/dist/write.test.js.map +1 -0
- package/package.json +2 -2
- package/templates/express/.env.example +2 -0
- package/templates/express/README.md +58 -2
- package/templates/express/package.json +9 -4
- package/templates/express/src/payment-store.ts +20 -0
- package/templates/express/src/server.ts +4 -2
- package/templates/hono/.env.example +2 -0
- package/templates/hono/README.md +36 -2
- package/templates/hono/package.json +10 -4
- package/templates/hono/src/payment-store.ts +20 -0
- package/templates/hono/src/server.ts +40 -7
- package/templates/mcp/.env.example +2 -0
- package/templates/mcp/README.md +29 -2
- package/templates/mcp/package.json +11 -4
- package/templates/mcp/src/payment-store.ts +20 -0
- package/templates/mcp/src/server.ts +90 -8
- package/templates/next-route/.env.example +2 -0
- package/templates/next-route/README.md +36 -2
- package/templates/next-route/app/api/premium/route.ts +47 -12
- package/templates/next-route/app/payment-store.ts +22 -0
- package/templates/next-route/package.json +11 -6
|
@@ -1,12 +1,94 @@
|
|
|
1
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
|
+
import { SOLANA_DEVNET, SolanaSettlementValidator } from "@usemeterkit/sdk";
|
|
4
|
+
import { HTTPFacilitatorClient } from "@x402/core/server";
|
|
5
|
+
import { createPaymentWrapper, x402ResourceServer } from "@x402/mcp";
|
|
6
|
+
import { ExactSvmScheme } from "@x402/svm/exact/server";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { createPaymentStore } from "./payment-store.js";
|
|
9
|
+
|
|
1
10
|
const wallet = process.env.MERCHANT_WALLET;
|
|
2
11
|
if (!wallet)
|
|
3
12
|
throw new Error("Set MERCHANT_WALLET to a disposable devnet address");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
const USDC_DEVNET = "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU";
|
|
14
|
+
const rpcUrl = process.env.SOLANA_RPC_URL ?? "https://api.devnet.solana.com";
|
|
15
|
+
const settlementValidator = new SolanaSettlementValidator(rpcUrl);
|
|
16
|
+
// One process keeps one set of spent signatures. Persist this in a database
|
|
17
|
+
// before serving anything that matters: an in-memory set forgets every replay
|
|
18
|
+
// it has already rejected as soon as the process restarts.
|
|
19
|
+
const paymentStore = await createPaymentStore();
|
|
20
|
+
|
|
21
|
+
const resourceServer = new x402ResourceServer(
|
|
22
|
+
new HTTPFacilitatorClient({
|
|
23
|
+
url: process.env.X402_FACILITATOR_URL ?? "https://x402.org/facilitator",
|
|
24
|
+
}),
|
|
25
|
+
)
|
|
26
|
+
.register(SOLANA_DEVNET, new ExactSvmScheme({}))
|
|
27
|
+
.onAfterSettle(async ({ result, requirements }) => {
|
|
28
|
+
if (!result.success || !result.transaction || !result.payer)
|
|
29
|
+
throw new Error("PAYMENT_SETTLEMENT_INCOMPLETE");
|
|
30
|
+
if (await paymentStore.has(result.transaction))
|
|
31
|
+
throw new Error("PAYMENT_PROOF_ALREADY_USED");
|
|
32
|
+
await settlementValidator.validate({
|
|
33
|
+
signature: result.transaction,
|
|
34
|
+
payer: result.payer,
|
|
35
|
+
payTo: requirements.payTo,
|
|
36
|
+
mint: requirements.asset,
|
|
37
|
+
amountAtomic: requirements.amount,
|
|
38
|
+
});
|
|
39
|
+
await paymentStore.save({
|
|
40
|
+
id: crypto.randomUUID(),
|
|
41
|
+
productId: "premium_tool",
|
|
42
|
+
payer: result.payer,
|
|
43
|
+
payTo: requirements.payTo,
|
|
44
|
+
mint: requirements.asset,
|
|
45
|
+
amountAtomic: requirements.amount,
|
|
46
|
+
network: SOLANA_DEVNET,
|
|
47
|
+
signature: result.transaction,
|
|
48
|
+
settledAt: new Date().toISOString(),
|
|
49
|
+
status: "confirmed",
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
await resourceServer.initialize();
|
|
53
|
+
|
|
54
|
+
const accepts = await resourceServer.buildPaymentRequirements({
|
|
55
|
+
scheme: "exact",
|
|
56
|
+
network: SOLANA_DEVNET,
|
|
57
|
+
payTo: wallet,
|
|
58
|
+
price: { amount: "10000", asset: USDC_DEVNET },
|
|
59
|
+
maxTimeoutSeconds: 300,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
if (process.argv.includes("--unpaid")) {
|
|
63
|
+
process.stdout.write(`${JSON.stringify({ accepts }, null, 2)}\n`);
|
|
64
|
+
process.exit(0);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const paid = createPaymentWrapper(resourceServer, {
|
|
68
|
+
accepts,
|
|
69
|
+
resource: {
|
|
70
|
+
url: "mcp://tool/premium_tool",
|
|
71
|
+
description: "A protected MCP tool result",
|
|
72
|
+
mimeType: "application/json",
|
|
73
|
+
serviceName: "MeterKit paid MCP tool",
|
|
74
|
+
tags: ["meterkit"],
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const server = new McpServer({
|
|
79
|
+
name: "meterkit-paid-mcp",
|
|
80
|
+
version: "0.1.0",
|
|
81
|
+
});
|
|
82
|
+
server.registerTool(
|
|
83
|
+
"premium_tool",
|
|
84
|
+
{
|
|
85
|
+
title: "Premium tool",
|
|
86
|
+
description: "Returns a protected result for 0.01 devnet USDC.",
|
|
87
|
+
inputSchema: { query: z.string().describe("What to look up") },
|
|
88
|
+
},
|
|
89
|
+
paid(async ({ query }) => ({
|
|
90
|
+
content: [{ type: "text", text: `Protected result for: ${query}` }],
|
|
91
|
+
})),
|
|
12
92
|
);
|
|
93
|
+
|
|
94
|
+
await server.connect(new StdioServerTransport());
|
|
@@ -1,3 +1,37 @@
|
|
|
1
|
-
# MeterKit Next route
|
|
1
|
+
# MeterKit Next route — devnet beta
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The initializer writes the public provider configuration to `.env.local`.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
{{INSTALL_COMMAND}}
|
|
7
|
+
{{DEV_COMMAND}}
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
In a second terminal:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
{{CHECK_COMMAND}}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Expected: `HTTP 402` with x402 v2, Solana devnet, test-USDC, amount `10000`,
|
|
17
|
+
your recipient and `http://localhost:3000/api/premium`.
|
|
18
|
+
|
|
19
|
+
Run `{{VERIFY_COMMAND}}` to enforce the exact policy. Payment remains an explicit
|
|
20
|
+
state-changing step: place only a disposable funded devnet keypair (mode `0600`)
|
|
21
|
+
at `.local-wallets/payer.json`, then run `{{PAY_COMMAND}}`. The command caps the
|
|
22
|
+
request and session at `10000` atomic test-USDC and checks replay rejection.
|
|
23
|
+
|
|
24
|
+
This route uses `MemoryPaymentStore`; restart erases replay history. It is a
|
|
25
|
+
local discovery example, not a deployment-ready paid endpoint. Configure the
|
|
26
|
+
documented PostgreSQL mode and pass restart/replay validation before accepting
|
|
27
|
+
payments.
|
|
28
|
+
|
|
29
|
+
For recovery, rerun the install command, check `.env.local`, and confirm port
|
|
30
|
+
3000 is available. External RPC/facilitator outages remain unavailable or
|
|
31
|
+
unknown. Reset by stopping Next and removing `.next`/`node_modules`; preserve
|
|
32
|
+
your source before removing the generated directory.
|
|
33
|
+
Run `{{DOCTOR_COMMAND}}` for the non-mutating diagnostic report. If remediation
|
|
34
|
+
still fails, share only sanitized output at
|
|
35
|
+
https://github.com/JuanTorchia/meterkit/issues.
|
|
36
|
+
|
|
37
|
+
Never commit `.env.local`, signing material, payment headers or full proofs.
|
|
@@ -1,17 +1,52 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createMeterKitResourceServer, SOLANA_DEVNET } from "@usemeterkit/sdk";
|
|
2
|
+
import { withX402 } from "@x402/next";
|
|
3
|
+
import { NextResponse, type NextRequest } from "next/server";
|
|
4
|
+
import { paymentStore } from "../../payment-store";
|
|
2
5
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
async function createPremiumRoute(wallet: string) {
|
|
7
|
+
const port = process.env.PORT ?? "3000";
|
|
8
|
+
const product = {
|
|
9
|
+
id: "premium",
|
|
10
|
+
name: "Premium API",
|
|
11
|
+
description: "Protected data",
|
|
12
|
+
resource: `http://localhost:${port}/api/premium`,
|
|
13
|
+
priceAtomic: "10000",
|
|
14
|
+
assetMint: "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",
|
|
15
|
+
payTo: wallet,
|
|
16
|
+
network: SOLANA_DEVNET,
|
|
17
|
+
} as const;
|
|
18
|
+
const server = createMeterKitResourceServer({
|
|
19
|
+
product,
|
|
20
|
+
store: await paymentStore,
|
|
21
|
+
rpcUrl: process.env.SOLANA_RPC_URL ?? "https://api.devnet.solana.com",
|
|
22
|
+
});
|
|
23
|
+
const handler = async () => NextResponse.json({ protected: true });
|
|
24
|
+
return withX402(
|
|
25
|
+
handler,
|
|
10
26
|
{
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
27
|
+
accepts: {
|
|
28
|
+
scheme: "exact",
|
|
29
|
+
price: { amount: product.priceAtomic, asset: product.assetMint },
|
|
30
|
+
network: SOLANA_DEVNET,
|
|
31
|
+
payTo: product.payTo,
|
|
32
|
+
maxTimeoutSeconds: 300,
|
|
33
|
+
},
|
|
34
|
+
description: product.description,
|
|
35
|
+
mimeType: "application/json",
|
|
14
36
|
},
|
|
15
|
-
|
|
37
|
+
server,
|
|
16
38
|
);
|
|
17
39
|
}
|
|
40
|
+
|
|
41
|
+
let configuredRoute: Awaited<ReturnType<typeof createPremiumRoute>> | undefined;
|
|
42
|
+
|
|
43
|
+
export async function GET(request: NextRequest) {
|
|
44
|
+
const wallet = process.env.MERCHANT_WALLET;
|
|
45
|
+
if (!wallet)
|
|
46
|
+
return NextResponse.json(
|
|
47
|
+
{ error: "server_not_configured", required: "MERCHANT_WALLET" },
|
|
48
|
+
{ status: 503 },
|
|
49
|
+
);
|
|
50
|
+
configuredRoute ??= await createPremiumRoute(wallet);
|
|
51
|
+
return configuredRoute(request);
|
|
52
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { StandalonePostgresPaymentStore } from "@usemeterkit/database";
|
|
2
|
+
import { MemoryPaymentStore, type PaymentStore } from "@usemeterkit/sdk";
|
|
3
|
+
|
|
4
|
+
async function initialize(): Promise<PaymentStore> {
|
|
5
|
+
const mode = process.env.DURABILITY_MODE ?? "memory";
|
|
6
|
+
if (mode === "memory") {
|
|
7
|
+
process.stderr.write(
|
|
8
|
+
"WARNING: memory payment store is non-durable; do not serve paid production traffic.\n",
|
|
9
|
+
);
|
|
10
|
+
return new MemoryPaymentStore();
|
|
11
|
+
}
|
|
12
|
+
if (mode !== "postgres")
|
|
13
|
+
throw new Error("DURABILITY_MODE must be memory or postgres");
|
|
14
|
+
const url = process.env.DATABASE_URL;
|
|
15
|
+
if (!url)
|
|
16
|
+
throw new Error("DATABASE_URL is required when DURABILITY_MODE=postgres");
|
|
17
|
+
const store = StandalonePostgresPaymentStore.connect(url);
|
|
18
|
+
await store.migrate();
|
|
19
|
+
return store;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const paymentStore = initialize();
|
|
@@ -4,16 +4,21 @@
|
|
|
4
4
|
"scripts": {
|
|
5
5
|
"dev": "next dev",
|
|
6
6
|
"build": "next build",
|
|
7
|
-
"check:unpaid": "
|
|
7
|
+
"check:unpaid": "meterkit check http://localhost:3000/api/premium --allow-localhost",
|
|
8
|
+
"doctor": "meterkit doctor --project . --url http://localhost:3000/api/premium --allow-localhost",
|
|
9
|
+
"verify": "meterkit verify http://localhost:3000/api/premium --network solana-devnet --mint 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU --recipient {{RECIPIENT}} --max-amount 10000 --allow-localhost",
|
|
10
|
+
"pay:test": "meterkit pay http://localhost:3000/api/premium --keypair .local-wallets/payer.json --mint 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU --recipient {{RECIPIENT}} --max-amount 10000 --max-session 10000 --rpc-url https://api.devnet.solana.com --replay --yes"
|
|
8
11
|
},
|
|
9
12
|
"dependencies": {
|
|
10
|
-
"@usemeterkit/
|
|
11
|
-
"@
|
|
12
|
-
"next": "
|
|
13
|
-
"
|
|
14
|
-
"react
|
|
13
|
+
"@usemeterkit/database": "0.3.0",
|
|
14
|
+
"@usemeterkit/sdk": "0.3.0",
|
|
15
|
+
"@x402/next": "2.22.0",
|
|
16
|
+
"next": "16.3.1",
|
|
17
|
+
"react": "19.2.8",
|
|
18
|
+
"react-dom": "19.2.8"
|
|
15
19
|
},
|
|
16
20
|
"devDependencies": {
|
|
21
|
+
"@usemeterkit/cli": "0.3.0",
|
|
17
22
|
"@types/node": "^24.3.0",
|
|
18
23
|
"@types/react": "^19.1.10",
|
|
19
24
|
"@types/react-dom": "^19.1.7",
|