@agentwonderland/mcp 0.1.23 → 0.1.24
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/dist/core/__tests__/card-setup.test.d.ts +1 -0
- package/dist/core/__tests__/card-setup.test.js +99 -0
- package/dist/core/__tests__/formatters.test.d.ts +1 -0
- package/dist/core/__tests__/formatters.test.js +15 -0
- package/dist/core/__tests__/passes.test.d.ts +1 -0
- package/dist/core/__tests__/passes.test.js +82 -0
- package/dist/core/__tests__/payments.test.d.ts +1 -0
- package/dist/core/__tests__/payments.test.js +52 -0
- package/dist/core/__tests__/principal.test.d.ts +1 -0
- package/dist/core/__tests__/principal.test.js +67 -0
- package/dist/core/api-client.d.ts +9 -4
- package/dist/core/api-client.js +52 -22
- package/dist/core/card-setup.d.ts +20 -13
- package/dist/core/card-setup.js +85 -29
- package/dist/core/config.d.ts +3 -0
- package/dist/core/config.js +24 -2
- package/dist/core/formatters.d.ts +2 -0
- package/dist/core/formatters.js +5 -1
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +2 -0
- package/dist/core/ows-adapter.d.ts +10 -2
- package/dist/core/ows-adapter.js +54 -10
- package/dist/core/passes.d.ts +40 -0
- package/dist/core/passes.js +32 -0
- package/dist/core/payments.d.ts +8 -0
- package/dist/core/payments.js +97 -13
- package/dist/core/principal.d.ts +2 -0
- package/dist/core/principal.js +109 -0
- package/dist/core/solana-charge.d.ts +9 -0
- package/dist/core/solana-charge.js +95 -0
- package/dist/core/types.d.ts +10 -0
- package/dist/index.js +8 -3
- package/dist/prompts/index.js +1 -1
- package/dist/resources/wallet.js +8 -1
- package/dist/tools/__tests__/_payment-confirmation.test.d.ts +1 -0
- package/dist/tools/__tests__/_payment-confirmation.test.js +30 -0
- package/dist/tools/_payment-confirmation.d.ts +6 -0
- package/dist/tools/_payment-confirmation.js +28 -0
- package/dist/tools/agent-info.js +14 -0
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +1 -0
- package/dist/tools/passes.d.ts +2 -0
- package/dist/tools/passes.js +157 -0
- package/dist/tools/run.js +113 -51
- package/dist/tools/solve.js +102 -44
- package/dist/tools/wallet.js +85 -50
- package/package.json +3 -1
- package/src/core/__tests__/card-setup.test.ts +118 -0
- package/src/core/__tests__/formatters.test.ts +17 -0
- package/src/core/__tests__/passes.test.ts +94 -0
- package/src/core/__tests__/payments.test.ts +60 -0
- package/src/core/__tests__/principal.test.ts +87 -0
- package/src/core/api-client.ts +70 -23
- package/src/core/card-setup.ts +109 -34
- package/src/core/config.ts +29 -3
- package/src/core/formatters.ts +7 -1
- package/src/core/index.ts +2 -0
- package/src/core/ows-adapter.ts +74 -8
- package/src/core/passes.ts +74 -0
- package/src/core/payments.ts +113 -13
- package/src/core/principal.ts +128 -0
- package/src/core/solana-charge.ts +149 -0
- package/src/core/types.ts +10 -0
- package/src/index.ts +8 -3
- package/src/prompts/index.ts +1 -1
- package/src/resources/wallet.ts +8 -1
- package/src/tools/__tests__/_payment-confirmation.test.ts +45 -0
- package/src/tools/_payment-confirmation.ts +52 -0
- package/src/tools/agent-info.ts +23 -0
- package/src/tools/index.ts +1 -0
- package/src/tools/passes.ts +234 -0
- package/src/tools/run.ts +171 -55
- package/src/tools/solve.ts +149 -56
- package/src/tools/wallet.ts +102 -52
package/src/core/payments.ts
CHANGED
|
@@ -19,9 +19,23 @@ import {
|
|
|
19
19
|
getApiUrl,
|
|
20
20
|
type WalletEntry,
|
|
21
21
|
} from "./config.js";
|
|
22
|
+
import type { AgentRecord } from "./types.js";
|
|
22
23
|
|
|
23
24
|
// Cache per wallet+chain combo to avoid re-initializing
|
|
24
25
|
const fetchCache = new Map<string, typeof fetch>();
|
|
26
|
+
const REGISTRY_METHOD_MAP: Record<string, string> = {
|
|
27
|
+
tempo: "tempo_usdc",
|
|
28
|
+
base: "base_usdc",
|
|
29
|
+
solana: "solana_usdc",
|
|
30
|
+
card: "stripe_card",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const METHOD_REGISTRY_MAP: Record<string, string> = {
|
|
34
|
+
tempo_usdc: "tempo",
|
|
35
|
+
base_usdc: "base",
|
|
36
|
+
solana_usdc: "solana",
|
|
37
|
+
stripe_card: "card",
|
|
38
|
+
};
|
|
25
39
|
|
|
26
40
|
// ── Helpers ─────────────────────────────────────────────────────
|
|
27
41
|
|
|
@@ -33,6 +47,20 @@ function cacheKey(walletId: string, chain: string): string {
|
|
|
33
47
|
return `${walletId}:${chain}`;
|
|
34
48
|
}
|
|
35
49
|
|
|
50
|
+
function cardCacheKey(): string | null {
|
|
51
|
+
const card = getCardConfig();
|
|
52
|
+
if (!card) return null;
|
|
53
|
+
return `card:${getApiUrl()}:${card.consumerToken}:${card.paymentMethodId ?? ""}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function clearStaleCardCache(activeKey?: string): void {
|
|
57
|
+
for (const key of fetchCache.keys()) {
|
|
58
|
+
if (key.startsWith("card:") && key !== activeKey) {
|
|
59
|
+
fetchCache.delete(key);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
36
64
|
// ── Per-protocol initializers ───────────────────────────────────
|
|
37
65
|
|
|
38
66
|
async function initMpp(wallet: WalletEntry): Promise<typeof fetch | null> {
|
|
@@ -58,6 +86,23 @@ async function initMpp(wallet: WalletEntry): Promise<typeof fetch | null> {
|
|
|
58
86
|
}
|
|
59
87
|
}
|
|
60
88
|
|
|
89
|
+
async function initSolanaMpp(wallet: WalletEntry): Promise<typeof fetch | null> {
|
|
90
|
+
try {
|
|
91
|
+
if (wallet.keyType !== "ows" && !wallet.key) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const { Mppx } = await import("mppx/client");
|
|
96
|
+
const { solanaChargeClient } = await import("./solana-charge.js");
|
|
97
|
+
const mppx = Mppx.create({
|
|
98
|
+
methods: [solanaChargeClient({ wallet })] as any,
|
|
99
|
+
});
|
|
100
|
+
return mppx.fetch.bind(mppx) as typeof fetch;
|
|
101
|
+
} catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
61
106
|
async function initCard(): Promise<typeof fetch | null> {
|
|
62
107
|
const cardConfig = getCardConfig();
|
|
63
108
|
if (!cardConfig) return null;
|
|
@@ -101,7 +146,10 @@ async function initCard(): Promise<typeof fetch | null> {
|
|
|
101
146
|
/**
|
|
102
147
|
* Initialize a payment-aware fetch for a given wallet + chain.
|
|
103
148
|
*/
|
|
104
|
-
async function initForChain(wallet: WalletEntry,
|
|
149
|
+
async function initForChain(wallet: WalletEntry, chain: string): Promise<typeof fetch | null> {
|
|
150
|
+
if (chain === "solana") {
|
|
151
|
+
return initSolanaMpp(wallet);
|
|
152
|
+
}
|
|
105
153
|
return initMpp(wallet);
|
|
106
154
|
}
|
|
107
155
|
|
|
@@ -116,7 +164,11 @@ async function initForChain(wallet: WalletEntry, _chain: string): Promise<typeof
|
|
|
116
164
|
export async function getPaymentFetch(method?: string): Promise<typeof fetch> {
|
|
117
165
|
// Card payment
|
|
118
166
|
if (method === "card") {
|
|
119
|
-
const ck =
|
|
167
|
+
const ck = cardCacheKey();
|
|
168
|
+
clearStaleCardCache(ck ?? undefined);
|
|
169
|
+
if (!ck) {
|
|
170
|
+
throw new Error('Payment method "card" is not configured. Use the wallet_setup tool to configure a wallet');
|
|
171
|
+
}
|
|
120
172
|
if (fetchCache.has(ck)) return fetchCache.get(ck)!;
|
|
121
173
|
const pf = await initCard();
|
|
122
174
|
if (pf) {
|
|
@@ -148,7 +200,16 @@ export async function getPaymentFetch(method?: string): Promise<typeof fetch> {
|
|
|
148
200
|
|
|
149
201
|
for (const m of configured) {
|
|
150
202
|
if (m === "card") {
|
|
151
|
-
const ck =
|
|
203
|
+
const ck = cardCacheKey();
|
|
204
|
+
clearStaleCardCache(ck ?? undefined);
|
|
205
|
+
if (!ck) {
|
|
206
|
+
if (m === defaultMethod) {
|
|
207
|
+
const others = configured.filter((x) => x !== m);
|
|
208
|
+
const altText = others.length > 0 ? ` Available alternatives: ${others.join(", ")}` : "";
|
|
209
|
+
throw new Error(`Card payment failed to initialize. Check your card with wallet_status.${altText}`);
|
|
210
|
+
}
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
152
213
|
if (fetchCache.has(ck)) return fetchCache.get(ck)!;
|
|
153
214
|
const pf = await initCard();
|
|
154
215
|
if (pf) {
|
|
@@ -230,6 +291,16 @@ export function getConfiguredMethods(): string[] {
|
|
|
230
291
|
return methods;
|
|
231
292
|
}
|
|
232
293
|
|
|
294
|
+
/**
|
|
295
|
+
* Normalize a requested payment method into the MCP-visible method identifier.
|
|
296
|
+
* Accepts chain names, wallet IDs, or "card".
|
|
297
|
+
*/
|
|
298
|
+
export function normalizePaymentMethod(method: string): string | null {
|
|
299
|
+
if (method === "card") return "card";
|
|
300
|
+
const resolved = resolveWalletAndChain(method);
|
|
301
|
+
return resolved?.chain ?? null;
|
|
302
|
+
}
|
|
303
|
+
|
|
233
304
|
/**
|
|
234
305
|
* Human-friendly display name for a payment method identifier.
|
|
235
306
|
*/
|
|
@@ -250,13 +321,33 @@ export function paymentMethodDisplayName(method: string): string {
|
|
|
250
321
|
*/
|
|
251
322
|
export function getAcceptedPaymentMethods(): string[] {
|
|
252
323
|
const methods = getConfiguredMethods();
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
324
|
+
return methods
|
|
325
|
+
.map((m) => REGISTRY_METHOD_MAP[m])
|
|
326
|
+
.filter(Boolean);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export function toRegistryPaymentMethod(method: string): string | null {
|
|
330
|
+
const normalized = normalizePaymentMethod(method);
|
|
331
|
+
if (!normalized) return null;
|
|
332
|
+
return REGISTRY_METHOD_MAP[normalized] ?? null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function getCompatiblePaymentMethods(
|
|
336
|
+
agent: Pick<AgentRecord, "payment"> | null | undefined,
|
|
337
|
+
configuredMethods = getConfiguredMethods(),
|
|
338
|
+
): string[] {
|
|
339
|
+
const acceptedPayments = agent?.payment?.accepted_payments;
|
|
340
|
+
if (!Array.isArray(acceptedPayments) || acceptedPayments.length === 0) {
|
|
341
|
+
return [...configuredMethods];
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const acceptedMethods = new Set(
|
|
345
|
+
acceptedPayments
|
|
346
|
+
.map((payment) => METHOD_REGISTRY_MAP[payment])
|
|
347
|
+
.filter(Boolean),
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
return configuredMethods.filter((method) => acceptedMethods.has(method));
|
|
260
351
|
}
|
|
261
352
|
|
|
262
353
|
/**
|
|
@@ -270,13 +361,16 @@ export function hasWalletConfigured(): boolean {
|
|
|
270
361
|
* Get address for a specific method, or the first configured one.
|
|
271
362
|
*/
|
|
272
363
|
export async function getWalletAddress(method?: string): Promise<string | null> {
|
|
364
|
+
let chain: string | undefined;
|
|
273
365
|
let wallet: WalletEntry | undefined;
|
|
274
366
|
|
|
275
367
|
if (method && method !== "card") {
|
|
276
368
|
const resolved = resolveWalletAndChain(method);
|
|
277
369
|
wallet = resolved?.wallet;
|
|
370
|
+
chain = resolved?.chain;
|
|
278
371
|
} else if (!method) {
|
|
279
372
|
wallet = getDefaultWallet();
|
|
373
|
+
chain = wallet?.defaultChain ?? wallet?.chains[0];
|
|
280
374
|
}
|
|
281
375
|
|
|
282
376
|
if (!wallet) return null;
|
|
@@ -284,14 +378,20 @@ export async function getWalletAddress(method?: string): Promise<string | null>
|
|
|
284
378
|
// OWS-managed wallet: derive address via the adapter
|
|
285
379
|
if (wallet.keyType === "ows" && wallet.owsWalletId) {
|
|
286
380
|
try {
|
|
287
|
-
const {
|
|
288
|
-
|
|
289
|
-
|
|
381
|
+
const { getOwsWalletAddress } = await import("./ows-adapter.js");
|
|
382
|
+
return await getOwsWalletAddress(
|
|
383
|
+
wallet.owsWalletId,
|
|
384
|
+
chain === "solana" ? "solana" : "evm",
|
|
385
|
+
);
|
|
290
386
|
} catch {
|
|
291
387
|
return null;
|
|
292
388
|
}
|
|
293
389
|
}
|
|
294
390
|
|
|
391
|
+
if (chain === "solana") {
|
|
392
|
+
return null;
|
|
393
|
+
}
|
|
394
|
+
|
|
295
395
|
// Raw EVM key path
|
|
296
396
|
if (!wallet.key) return null;
|
|
297
397
|
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { addWallet, getDefaultWallet, getWallets, type WalletEntry } from "./config.js";
|
|
2
|
+
import { getWalletAddress } from "./payments.js";
|
|
3
|
+
import {
|
|
4
|
+
createOwsWallet,
|
|
5
|
+
isOwsAvailable,
|
|
6
|
+
listOwsWallets,
|
|
7
|
+
} from "./ows-adapter.js";
|
|
8
|
+
|
|
9
|
+
const BASE_CHAIN_ID = "8453";
|
|
10
|
+
const SOLANA_CHAIN_ID = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
|
|
11
|
+
const AUTO_IDENTITY_LABEL = "AW Identity";
|
|
12
|
+
|
|
13
|
+
function principalFromAddress(address: string, type: "evm" | "solana"): string {
|
|
14
|
+
if (type === "evm") {
|
|
15
|
+
return `did:pkh:eip155:${BASE_CHAIN_ID}:${address.toLowerCase()}`;
|
|
16
|
+
}
|
|
17
|
+
return `did:pkh:${SOLANA_CHAIN_ID}:${address}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function walletSupportsEvm(wallet: WalletEntry): boolean {
|
|
21
|
+
return wallet.chains.some((chain) => chain !== "solana");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function walletSupportsSolana(wallet: WalletEntry): boolean {
|
|
25
|
+
return wallet.chains.includes("solana");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function walletPrincipal(wallet: WalletEntry): Promise<string | null> {
|
|
29
|
+
if (walletSupportsEvm(wallet)) {
|
|
30
|
+
const address = await getWalletAddress(wallet.id);
|
|
31
|
+
return address ? principalFromAddress(address, "evm") : null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (walletSupportsSolana(wallet)) {
|
|
35
|
+
const address = await getWalletAddress(wallet.id);
|
|
36
|
+
return address ? principalFromAddress(address, "solana") : null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function preferredWallets(): WalletEntry[] {
|
|
43
|
+
const wallets = getWallets();
|
|
44
|
+
const defaultWallet = getDefaultWallet();
|
|
45
|
+
const ordered = defaultWallet
|
|
46
|
+
? [defaultWallet, ...wallets.filter((wallet) => wallet.id !== defaultWallet.id)]
|
|
47
|
+
: wallets;
|
|
48
|
+
|
|
49
|
+
return ordered.sort((a, b) => {
|
|
50
|
+
const aScore = walletSupportsEvm(a) ? 0 : walletSupportsSolana(a) ? 1 : 2;
|
|
51
|
+
const bScore = walletSupportsEvm(b) ? 0 : walletSupportsSolana(b) ? 1 : 2;
|
|
52
|
+
return aScore - bScore;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function createFallbackIdentityWallet(): Promise<WalletEntry> {
|
|
57
|
+
const { generatePrivateKey } = await import("viem/accounts");
|
|
58
|
+
const walletName = `aw-identity-${Date.now()}`;
|
|
59
|
+
const entry: WalletEntry = {
|
|
60
|
+
id: walletName,
|
|
61
|
+
keyType: "evm",
|
|
62
|
+
key: generatePrivateKey(),
|
|
63
|
+
chains: ["tempo", "base"],
|
|
64
|
+
defaultChain: "base",
|
|
65
|
+
label: AUTO_IDENTITY_LABEL,
|
|
66
|
+
};
|
|
67
|
+
addWallet(entry);
|
|
68
|
+
return entry;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function ensureIdentityWallet(): Promise<WalletEntry> {
|
|
72
|
+
const wallets = preferredWallets();
|
|
73
|
+
const existing = wallets.find((wallet) => walletSupportsEvm(wallet) || walletSupportsSolana(wallet));
|
|
74
|
+
if (existing) return existing;
|
|
75
|
+
|
|
76
|
+
if (await isOwsAvailable()) {
|
|
77
|
+
const existingOwsWallets = await listOwsWallets();
|
|
78
|
+
const linked = existingOwsWallets[0];
|
|
79
|
+
if (linked) {
|
|
80
|
+
const entry: WalletEntry = {
|
|
81
|
+
id: linked.name,
|
|
82
|
+
keyType: "ows",
|
|
83
|
+
owsWalletId: linked.id,
|
|
84
|
+
chains: ["tempo", "base"],
|
|
85
|
+
defaultChain: "base",
|
|
86
|
+
label: linked.name,
|
|
87
|
+
};
|
|
88
|
+
addWallet(entry);
|
|
89
|
+
return entry;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const walletName = `aw-identity-${Date.now()}`;
|
|
93
|
+
const created = await createOwsWallet(walletName, "evm");
|
|
94
|
+
const entry: WalletEntry = {
|
|
95
|
+
id: walletName,
|
|
96
|
+
keyType: "ows",
|
|
97
|
+
owsWalletId: created.walletId,
|
|
98
|
+
chains: ["tempo", "base"],
|
|
99
|
+
defaultChain: "base",
|
|
100
|
+
label: AUTO_IDENTITY_LABEL,
|
|
101
|
+
};
|
|
102
|
+
addWallet(entry);
|
|
103
|
+
return entry;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return createFallbackIdentityWallet();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export async function getConsumerPrincipal(): Promise<string | null> {
|
|
110
|
+
for (const wallet of preferredWallets()) {
|
|
111
|
+
const principal = await walletPrincipal(wallet);
|
|
112
|
+
if (principal) return principal;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function ensureConsumerPrincipal(): Promise<string> {
|
|
119
|
+
const existing = await getConsumerPrincipal();
|
|
120
|
+
if (existing) return existing;
|
|
121
|
+
|
|
122
|
+
const identityWallet = await ensureIdentityWallet();
|
|
123
|
+
const principal = await walletPrincipal(identityWallet);
|
|
124
|
+
if (!principal) {
|
|
125
|
+
throw new Error("Could not derive a consumer principal from the configured identity wallet.");
|
|
126
|
+
}
|
|
127
|
+
return principal;
|
|
128
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom MPP payment method: Solana USDC charge (client-side).
|
|
3
|
+
*
|
|
4
|
+
* Sends an SPL Token transfer on Solana mainnet and returns the transaction
|
|
5
|
+
* signature as the payment credential.
|
|
6
|
+
*/
|
|
7
|
+
import { Credential, Method, z } from "mppx";
|
|
8
|
+
import { Connection, Keypair, PublicKey, Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
|
|
9
|
+
import {
|
|
10
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
11
|
+
TOKEN_PROGRAM_ID,
|
|
12
|
+
createTransferCheckedInstruction,
|
|
13
|
+
getAssociatedTokenAddressSync,
|
|
14
|
+
} from "@solana/spl-token";
|
|
15
|
+
import type { WalletEntry } from "./config.js";
|
|
16
|
+
|
|
17
|
+
export const SOLANA_USDC_MINT = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" as const;
|
|
18
|
+
export const SOLANA_CHAIN_ID = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp" as const;
|
|
19
|
+
const SOLANA_RPC = "https://api.mainnet-beta.solana.com";
|
|
20
|
+
|
|
21
|
+
const solanaChargeMethod = Method.from({
|
|
22
|
+
name: "solana" as const,
|
|
23
|
+
intent: "charge" as const,
|
|
24
|
+
schema: {
|
|
25
|
+
credential: {
|
|
26
|
+
payload: z.object({
|
|
27
|
+
signature: z.string(),
|
|
28
|
+
type: z.literal("signature"),
|
|
29
|
+
}),
|
|
30
|
+
},
|
|
31
|
+
request: z.pipe(
|
|
32
|
+
z.object({
|
|
33
|
+
amount: z.string(),
|
|
34
|
+
currency: z.string(),
|
|
35
|
+
recipient: z.string(),
|
|
36
|
+
chainId: z.optional(z.string()),
|
|
37
|
+
decimals: z.optional(z.number()),
|
|
38
|
+
}),
|
|
39
|
+
z.transform((value: any) => ({
|
|
40
|
+
...value,
|
|
41
|
+
methodDetails: { chainId: value.chainId ?? SOLANA_CHAIN_ID },
|
|
42
|
+
})),
|
|
43
|
+
),
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
function keypairFromPrivateKeyHex(privateKeyHex: string): Keypair {
|
|
48
|
+
const normalized = privateKeyHex.startsWith("0x")
|
|
49
|
+
? privateKeyHex.slice(2)
|
|
50
|
+
: privateKeyHex;
|
|
51
|
+
const bytes = Uint8Array.from(Buffer.from(normalized, "hex"));
|
|
52
|
+
if (bytes.length === 32) {
|
|
53
|
+
return Keypair.fromSeed(bytes);
|
|
54
|
+
}
|
|
55
|
+
if (bytes.length === 64) {
|
|
56
|
+
return Keypair.fromSecretKey(bytes);
|
|
57
|
+
}
|
|
58
|
+
throw new Error(`Unsupported Solana private key length: ${bytes.length} bytes.`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function getKeypair(wallet: WalletEntry): Promise<Keypair> {
|
|
62
|
+
if (wallet.keyType === "ows" && wallet.owsWalletId) {
|
|
63
|
+
const { owsSolanaKeypairFromWalletId } = await import("./ows-adapter.js");
|
|
64
|
+
return owsSolanaKeypairFromWalletId(wallet.owsWalletId);
|
|
65
|
+
}
|
|
66
|
+
if (wallet.key) {
|
|
67
|
+
return keypairFromPrivateKeyHex(wallet.key);
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`Wallet "${wallet.id}" cannot sign Solana transactions.`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface SolanaChargeClientConfig {
|
|
73
|
+
wallet: WalletEntry;
|
|
74
|
+
rpcUrl?: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function resolveRecipientTokenAccount(
|
|
78
|
+
connection: Connection,
|
|
79
|
+
mint: PublicKey,
|
|
80
|
+
recipient: PublicKey,
|
|
81
|
+
): Promise<PublicKey> {
|
|
82
|
+
const accountInfo = await connection.getParsedAccountInfo(recipient, "confirmed");
|
|
83
|
+
const owner = accountInfo.value?.owner;
|
|
84
|
+
if (owner && owner.toBase58() === TOKEN_PROGRAM_ID.toBase58()) {
|
|
85
|
+
return recipient;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return getAssociatedTokenAddressSync(
|
|
89
|
+
mint,
|
|
90
|
+
recipient,
|
|
91
|
+
false,
|
|
92
|
+
TOKEN_PROGRAM_ID,
|
|
93
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function solanaChargeClient(config: SolanaChargeClientConfig) {
|
|
98
|
+
return Method.toClient(solanaChargeMethod as any, {
|
|
99
|
+
async createCredential({ challenge }: any) {
|
|
100
|
+
const { request } = challenge;
|
|
101
|
+
const amount = BigInt(request.amount);
|
|
102
|
+
const decimals = request.decimals ?? 6;
|
|
103
|
+
const mint = new PublicKey(request.currency ?? SOLANA_USDC_MINT);
|
|
104
|
+
const recipient = new PublicKey(request.recipient);
|
|
105
|
+
const keypair = await getKeypair(config.wallet);
|
|
106
|
+
const owner = keypair.publicKey;
|
|
107
|
+
const connection = new Connection(config.rpcUrl ?? SOLANA_RPC, "confirmed");
|
|
108
|
+
|
|
109
|
+
const sourceTokenAccount = getAssociatedTokenAddressSync(
|
|
110
|
+
mint,
|
|
111
|
+
owner,
|
|
112
|
+
false,
|
|
113
|
+
TOKEN_PROGRAM_ID,
|
|
114
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
115
|
+
);
|
|
116
|
+
const destinationTokenAccount = await resolveRecipientTokenAccount(
|
|
117
|
+
connection,
|
|
118
|
+
mint,
|
|
119
|
+
recipient,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const instruction = createTransferCheckedInstruction(
|
|
123
|
+
sourceTokenAccount,
|
|
124
|
+
mint,
|
|
125
|
+
destinationTokenAccount,
|
|
126
|
+
owner,
|
|
127
|
+
amount,
|
|
128
|
+
decimals,
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const { blockhash } = await connection.getLatestBlockhash("confirmed");
|
|
132
|
+
|
|
133
|
+
const transaction = new Transaction({
|
|
134
|
+
feePayer: owner,
|
|
135
|
+
recentBlockhash: blockhash,
|
|
136
|
+
}).add(instruction);
|
|
137
|
+
|
|
138
|
+
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair], {
|
|
139
|
+
commitment: "confirmed",
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
return Credential.serialize({
|
|
143
|
+
challenge,
|
|
144
|
+
payload: { signature, type: "signature" as const },
|
|
145
|
+
source: `did:pkh:${SOLANA_CHAIN_ID}:${owner.toBase58()}`,
|
|
146
|
+
});
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
}
|
package/src/core/types.ts
CHANGED
|
@@ -22,6 +22,16 @@ export interface AgentRecord {
|
|
|
22
22
|
};
|
|
23
23
|
payment?: {
|
|
24
24
|
pricing?: Record<string, unknown>;
|
|
25
|
+
accepted_payments?: string[];
|
|
26
|
+
credit_packs?: {
|
|
27
|
+
unit_type?: string;
|
|
28
|
+
packs?: Array<{
|
|
29
|
+
key?: string;
|
|
30
|
+
name?: string;
|
|
31
|
+
included_units?: number;
|
|
32
|
+
price_usd?: string;
|
|
33
|
+
}>;
|
|
34
|
+
} | null;
|
|
25
35
|
[key: string]: unknown;
|
|
26
36
|
};
|
|
27
37
|
[key: string]: unknown;
|
package/src/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { registerRateTools } from "./tools/rate.js";
|
|
|
13
13
|
import { registerWalletTools } from "./tools/wallet.js";
|
|
14
14
|
import { registerFavoriteTools } from "./tools/favorites.js";
|
|
15
15
|
import { registerTipTools } from "./tools/tip.js";
|
|
16
|
+
import { registerPassTools } from "./tools/passes.js";
|
|
16
17
|
|
|
17
18
|
// ── Resources ────────────────────────────────────────────────────
|
|
18
19
|
import { registerAgentResources } from "./resources/agents.js";
|
|
@@ -40,14 +41,17 @@ export async function startMcpServer(): Promise<void> {
|
|
|
40
41
|
"1. search_agents() or solve() — find agents for the task",
|
|
41
42
|
"2. get_agent() — check required input fields before running",
|
|
42
43
|
"3. run_agent() or solve() with ALL required fields",
|
|
43
|
-
"
|
|
44
|
+
"3a. If the agent offers discounted credit packs, use buy_agent_credit_pack() and list_agent_credit_packs() when helpful",
|
|
45
|
+
"4. If no payment method is set up, run_agent returns a setup page to connect a credit card — present it to the user",
|
|
44
46
|
"5. Ask user to rate or tip after a successful run",
|
|
45
47
|
"",
|
|
46
48
|
"PAYMENT:",
|
|
47
|
-
"- Credit/debit card is the default and easiest way to pay —
|
|
48
|
-
"- Crypto wallets (Tempo USDC, Base USDC) are available for advanced users.",
|
|
49
|
+
"- Credit/debit card is the default and easiest way to pay — open the setup page to connect, no funding needed.",
|
|
50
|
+
"- Crypto wallets (Tempo USDC, Base USDC, Solana USDC) are available for advanced users.",
|
|
49
51
|
"- Card and crypto are SEPARATE payment methods. Card charges a credit card; crypto sends USDC on-chain.",
|
|
50
52
|
"- Card is set as the default payment method when configured.",
|
|
53
|
+
"- If multiple valid payment methods are available for a run and pay_with is not specified, ask the user which one to use.",
|
|
54
|
+
"- For fully agentic execution, include pay_with explicitly.",
|
|
51
55
|
"- Payment is automatic once configured. Users are never charged for failed runs.",
|
|
52
56
|
"- Do NOT ask the user to set up payment before they try to run an agent. Let them explore freely.",
|
|
53
57
|
"- If a specific payment method fails, report the error clearly. Do NOT silently fall back to a different method.",
|
|
@@ -74,6 +78,7 @@ export async function startMcpServer(): Promise<void> {
|
|
|
74
78
|
registerWalletTools(server);
|
|
75
79
|
registerFavoriteTools(server);
|
|
76
80
|
registerTipTools(server);
|
|
81
|
+
registerPassTools(server);
|
|
77
82
|
|
|
78
83
|
// Register resources
|
|
79
84
|
registerAgentResources(server);
|
package/src/prompts/index.ts
CHANGED
|
@@ -18,7 +18,7 @@ export function registerPrompts(server: McpServer) {
|
|
|
18
18
|
"1. Check my wallet status with wallet_status",
|
|
19
19
|
"2. If no wallet is configured, help me create one with wallet_setup",
|
|
20
20
|
"3. Show me some popular agents with search_agents",
|
|
21
|
-
"4. Briefly explain how solve, run_agent, rating, and tipping work",
|
|
21
|
+
"4. Briefly explain how solve, run_agent, buy_agent_credit_pack, rating, and tipping work",
|
|
22
22
|
].join("\n"),
|
|
23
23
|
},
|
|
24
24
|
}],
|
package/src/resources/wallet.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
-
import { getWallets, getCardConfig } from "../core/config.js";
|
|
2
|
+
import { getWallets, getCardConfig, getPendingCardSetupToken } from "../core/config.js";
|
|
3
|
+
import { getCardCapabilities } from "../core/card-setup.js";
|
|
3
4
|
import { getWalletAddress, getConfiguredMethods } from "../core/payments.js";
|
|
4
5
|
|
|
5
6
|
export function registerWalletResources(server: McpServer) {
|
|
6
7
|
server.resource("wallet-config", "aw://wallet", async () => {
|
|
7
8
|
const wallets = getWallets();
|
|
8
9
|
const card = getCardConfig();
|
|
10
|
+
const pendingCardSetupToken = getPendingCardSetupToken();
|
|
9
11
|
const methods = getConfiguredMethods();
|
|
10
12
|
|
|
11
13
|
const lines = ["Wallet Configuration", ""];
|
|
@@ -16,6 +18,11 @@ export function registerWalletResources(server: McpServer) {
|
|
|
16
18
|
}
|
|
17
19
|
if (card) {
|
|
18
20
|
lines.push(`Card: ${card.brand} ****${card.last4}`);
|
|
21
|
+
const capabilities = await getCardCapabilities();
|
|
22
|
+
lines.push(`Card MPP: ${capabilities.spt_status}${capabilities.message ? ` — ${capabilities.message}` : ""}`);
|
|
23
|
+
}
|
|
24
|
+
if (pendingCardSetupToken) {
|
|
25
|
+
lines.push("Card setup: pending confirmation");
|
|
19
26
|
}
|
|
20
27
|
lines.push("", `Configured methods: ${methods.join(", ") || "none"}`);
|
|
21
28
|
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
formatPaymentChoicePrompt,
|
|
5
|
+
formatRunConfirmationCommand,
|
|
6
|
+
formatSolveConfirmationCommand,
|
|
7
|
+
makeSolvePendingKey,
|
|
8
|
+
resolveConfirmationMethod,
|
|
9
|
+
} from "../_payment-confirmation.js";
|
|
10
|
+
|
|
11
|
+
describe("payment confirmation helpers", () => {
|
|
12
|
+
it("prefers the explicit method, then pending, then configured default", () => {
|
|
13
|
+
expect(resolveConfirmationMethod("card", "tempo", ["tempo", "card"])).toBe("card");
|
|
14
|
+
expect(resolveConfirmationMethod(undefined, "card", ["tempo", "card"])).toBe("card");
|
|
15
|
+
expect(resolveConfirmationMethod(undefined, undefined, ["card", "tempo"])).toBe("card");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("includes pay_with in run confirmation commands when present", () => {
|
|
19
|
+
expect(formatRunConfirmationCommand("agent_123", "card")).toContain('pay_with: "card"');
|
|
20
|
+
expect(formatRunConfirmationCommand("agent_123", undefined)).not.toContain("pay_with");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("includes pay_with in solve confirmation commands when present", () => {
|
|
24
|
+
expect(formatSolveConfirmationCommand("translate", 1, "card")).toContain('pay_with: "card"');
|
|
25
|
+
expect(formatSolveConfirmationCommand("translate", 1, undefined)).not.toContain("pay_with");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("uses a stable solve pending key for the same request", () => {
|
|
29
|
+
const input = { text: "hello", target_language: "es" };
|
|
30
|
+
expect(makeSolvePendingKey("translate", input, 1)).toBe(
|
|
31
|
+
makeSolvePendingKey("translate", input, 1),
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("formats a payment choice prompt with explicit options", () => {
|
|
36
|
+
const prompt = formatPaymentChoicePrompt("Echo Agent", ["card", "tempo"], [
|
|
37
|
+
' run_agent({ agent_id: "agent_123", input: <same>, pay_with: "card" })',
|
|
38
|
+
' run_agent({ agent_id: "agent_123", input: <same>, pay_with: "tempo" })',
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
expect(prompt).toContain("Multiple payment methods are available for Echo Agent.");
|
|
42
|
+
expect(prompt).toContain('Available methods: "card", "tempo"');
|
|
43
|
+
expect(prompt).toContain('pay_with: "card"');
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export function resolveConfirmationMethod(
|
|
2
|
+
requestedMethod: string | undefined,
|
|
3
|
+
pendingMethod: string | undefined,
|
|
4
|
+
compatibleMethods: string[],
|
|
5
|
+
): string | undefined {
|
|
6
|
+
return requestedMethod ?? pendingMethod ?? compatibleMethods[0];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function formatPaymentLabel(method: string | undefined): string {
|
|
10
|
+
return method ?? "auto";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function formatRunConfirmationCommand(
|
|
14
|
+
agentId: string,
|
|
15
|
+
method: string | undefined,
|
|
16
|
+
): string {
|
|
17
|
+
const payWith = method ? `, pay_with: "${method}"` : "";
|
|
18
|
+
return ` run_agent({ agent_id: "${agentId}", input: <same>${payWith}, confirmed: true })`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function formatSolveConfirmationCommand(
|
|
22
|
+
intent: string,
|
|
23
|
+
budget: number,
|
|
24
|
+
method: string | undefined,
|
|
25
|
+
): string {
|
|
26
|
+
const payWith = method ? `, pay_with: "${method}"` : "";
|
|
27
|
+
return ` solve({ intent: "${intent}", input: <same>, budget: ${budget}${payWith}, confirmed: true })`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function makeSolvePendingKey(
|
|
31
|
+
intent: string,
|
|
32
|
+
input: Record<string, unknown>,
|
|
33
|
+
budget: number,
|
|
34
|
+
): string {
|
|
35
|
+
return JSON.stringify({ intent, input, budget });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function formatPaymentChoicePrompt(
|
|
39
|
+
subject: string,
|
|
40
|
+
methods: string[],
|
|
41
|
+
commands: string[],
|
|
42
|
+
): string {
|
|
43
|
+
return [
|
|
44
|
+
`Multiple payment methods are available for ${subject}.`,
|
|
45
|
+
"",
|
|
46
|
+
"Ask the user which payment method they want to use, then call one of:",
|
|
47
|
+
...commands,
|
|
48
|
+
"",
|
|
49
|
+
`Available methods: ${methods.map((method) => `"${method}"`).join(", ")}`,
|
|
50
|
+
"For fully agentic execution, include pay_with explicitly.",
|
|
51
|
+
].join("\n");
|
|
52
|
+
}
|