Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(mcp): mint an agent even when the atomic tx exceeds Solana's size cap

nirholase09f072ecc69
diff --git a/packages/metaplex-agent-mcp/src/lib/mint.js b/packages/metaplex-agent-mcp/src/lib/mint.js
index 719a2e931..6213a5822 100644
--- a/packages/metaplex-agent-mcp/src/lib/mint.js
+++ b/packages/metaplex-agent-mcp/src/lib/mint.js
@@ -168,7 +168,77 @@ export function buildAgentMint(umi, {
const registerArgs = { asset: assetSigner.publicKey, agentRegistrationUri };
if (collection) registerArgs.collection = umiPublicKey(collection);
- const builder = create(umi, createArgs).add(registerIdentityV1(umi, registerArgs));
+ const createBuilder = create(umi, createArgs);
+ const registerBuilder = registerIdentityV1(umi, registerArgs);
+ const combinedBuilder = createBuilder.add(registerBuilder);
- return { builder, assetSigner, assetMetadata, metadataUri: uri, registration, registrationUri: agentRegistrationUri };
+ // Solana caps a transaction at 1232 bytes, and two data: URIs plus two
+ // instructions can exceed it. Atomic when it fits (small/hosted URIs);
+ // otherwise the mint runs as create followed by register, exactly how the
+ // Genesis 333 landed (their register txs are separate on-chain).
+ const atomic = combinedBuilder.fitsInOneTransaction(umi);
+
+ return {
+ atomic,
+ builders: atomic ? [combinedBuilder] : [createBuilder, registerBuilder],
+ createBuilder,
+ registerBuilder,
+ combinedBuilder,
+ assetSigner,
+ assetMetadata,
+ metadataUri: uri,
+ registration,
+ registrationUri: agentRegistrationUri,
+ };
+}
+
+const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
+
+/**
+ * Wait until the asset account is visible to the RPC. A freshly-minted asset
+ * can lag the node that simulates the register tx; the identity program then
+ * reads an empty account and rejects with `InvalidCoreAsset` (0x4). Bounded
+ * poll + retry closes that race for the split create-then-register path.
+ */
+export async function waitForAsset(umi, assetPk, { tries = 12, delayMs = 1500 } = {}) {
+ for (let i = 0; i < tries; i++) {
+ const acct = await umi.rpc.getAccount(assetPk).catch(() => null);
+ if (acct?.exists && acct.data?.length) return true;
+ await sleep(delayMs);
+ }
+ return false;
+}
+
+/** True when the error is the propagation race, not a real rejection. */
+export function isAssetPropagationError(err) {
+ return /Invalid Core Asset|custom program error: 0x4/i.test(err?.message || '');
+}
+
+/**
+ * Send a mint end-to-end with a keypair identity: one atomic transaction when
+ * it fits, otherwise create then register with the propagation race handled.
+ * @returns {Promise<{ signatures: string[], atomic: boolean }>}
+ */
+export async function sendAgentMint(umi, mint, { toBase58Signature }) {
+ const confirmOpts = { confirm: { commitment: 'confirmed' } };
+ if (mint.atomic) {
+ const result = await mint.combinedBuilder.sendAndConfirm(umi, confirmOpts);
+ return { signatures: [toBase58Signature(result.signature)], atomic: true };
+ }
+ const created = await mint.createBuilder.sendAndConfirm(umi, confirmOpts);
+ await waitForAsset(umi, mint.assetSigner.publicKey);
+ let registered;
+ for (let attempt = 0; ; attempt++) {
+ try {
+ registered = await mint.registerBuilder.sendAndConfirm(umi, confirmOpts);
+ break;
+ } catch (err) {
+ if (!isAssetPropagationError(err) || attempt >= 4) throw err;
+ await sleep(2000);
+ }
+ }
+ return {
+ signatures: [toBase58Signature(created.signature), toBase58Signature(registered.signature)],
+ atomic: false,
+ };
}
diff --git a/packages/metaplex-agent-mcp/src/tools/mint-onchain-agent.js b/packages/metaplex-agent-mcp/src/tools/mint-onchain-agent.js
index 83f51ebeb..57a4d8ccc 100644
--- a/packages/metaplex-agent-mcp/src/tools/mint-onchain-agent.js
+++ b/packages/metaplex-agent-mcp/src/tools/mint-onchain-agent.js
@@ -6,7 +6,7 @@
import { z } from 'zod';
import { NETWORK, REQUIRE_CONFIRM } from '../config.js';
-import { buildAgentMint } from '../lib/mint.js';
+import { buildAgentMint, sendAgentMint } from '../lib/mint.js';
import {
buildUmi,
solBalance,
@@ -27,9 +27,10 @@ export const def = {
title: 'Mint an on-chain agent into the Metaplex Agent Registry',
annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: true },
description:
- 'Deploy an AI agent on-chain, Genesis-333 style: one atomic Solana transaction mints a Metaplex Core asset ' +
- '(data: URI metadata, verified creator, royalties, immutable metadata) AND registers its EIP-8004 Agent ' +
- 'Identity, so it appears on metaplex.com/agents with its own built-in wallet. Signs with the configured ' +
+ 'Deploy an AI agent on-chain, Genesis-333 style: mints a Metaplex Core asset (data: URI metadata, verified ' +
+ 'creator, royalties, immutable metadata) AND registers its EIP-8004 Agent Identity, so it appears on ' +
+ 'metaplex.com/agents with its own built-in wallet. Runs as ONE atomic transaction when it fits Solana\'s ' +
+ '1232-byte limit, otherwise as create followed by register (how the Genesis 333 landed). Signs with the configured ' +
'SOLANA_SECRET_KEY (or a per-call secret) and spends ~0.007 SOL in rent + fees. Without confirm:true it ' +
'returns a full preview (both JSON documents, the paying wallet, the cost) and broadcasts NOTHING. ' +
'For Phantom/Solflare users, use prepare_agent_mint instead.',
@@ -70,15 +71,15 @@ export const def = {
);
}
- const result = await mint.builder.sendAndConfirm(umi, { confirm: { commitment: 'confirmed' } });
- const signature = toBase58Signature(result.signature);
+ const { signatures, atomic } = await sendAgentMint(umi, mint, { toBase58Signature });
return {
ok: true,
network,
asset,
- signature,
- tx: txLink(signature, network),
+ atomic,
+ signatures,
+ txs: signatures.map((s) => txLink(s, network)),
owner: args.owner || wallet,
agent_wallet: assetSignerAddress(umi, asset),
metadata_uri: mint.metadataUri,
diff --git a/packages/metaplex-agent-mcp/src/tools/prepare-agent-mint.js b/packages/metaplex-agent-mcp/src/tools/prepare-agent-mint.js
index 615c348a3..31969d66e 100644
--- a/packages/metaplex-agent-mcp/src/tools/prepare-agent-mint.js
+++ b/packages/metaplex-agent-mcp/src/tools/prepare-agent-mint.js
@@ -24,12 +24,13 @@ export const def = {
title: 'Prepare an agent mint for a Phantom/Solflare wallet to sign',
annotations: { readOnlyHint: true, idempotentHint: false, openWorldHint: true },
description:
- 'Build the exact Genesis-style mint+register transaction (Metaplex Core asset + Agent Identity, one atomic ' +
- 'tx) for an EXTERNAL Solana wallet: Phantom, Solflare, Backpack, Ledger, anything. Needs no secret key. ' +
- 'Returns the unsigned transaction base64 (already co-signed by the new asset keypair) with `wallet` as the ' +
- 'fee payer; the wallet signs it and broadcasts, or passes the signed bytes to send_signed_transaction. ' +
- 'Broadcasts nothing itself. The blockhash expires after roughly a minute, so sign promptly and re-prepare ' +
- 'if a wallet reports an expired transaction.',
+ 'Build the exact Genesis-style mint+register flow (Metaplex Core asset + Agent Identity) for an EXTERNAL ' +
+ 'Solana wallet: Phantom, Solflare, Backpack, Ledger, anything. Needs no secret key. Returns unsigned ' +
+ 'transactions base64 in txs_base64 (already co-signed by the new asset keypair) with `wallet` as the fee ' +
+ 'payer: one atomic tx when it fits Solana\'s size limit, else create + register to sign together via ' +
+ 'signAllTransactions. The wallet signs and broadcasts, or hands the signed array to ' +
+ 'send_signed_transaction. Broadcasts nothing itself. The blockhash expires after roughly a minute, so sign ' +
+ 'promptly and re-prepare if a wallet reports an expired transaction.',
inputSchema: {
...mintShape,
wallet: z.string().min(32).max(44).describe('The base58 address of the wallet that will sign, pay, and own the agent.'),
@@ -42,8 +43,11 @@ export const def = {
const mint = buildAgentMint(umi, mintParams(args, { network, creator: args.wallet }));
const asset = mint.assetSigner.publicKey.toString();
- const tx = await mint.builder.buildAndSign(umi);
- const txBase64 = Buffer.from(umi.transactions.serialize(tx)).toString('base64');
+ const txsBase64 = [];
+ for (const builder of mint.builders) {
+ const tx = await builder.buildAndSign(umi);
+ txsBase64.push(Buffer.from(umi.transactions.serialize(tx)).toString('base64'));
+ }
return {
ok: true,
@@ -51,12 +55,16 @@ export const def = {
wallet: args.wallet,
asset,
agent_wallet: assetSignerAddress(umi, asset),
- tx_base64: txBase64,
+ atomic: mint.atomic,
+ txs_base64: txsBase64,
estimated_cost_sol: (EST_MINT_LAMPORTS + EST_REGISTER_LAMPORTS) / LAMPORTS_PER_SOL,
asset_metadata: mint.assetMetadata,
registration: mint.registration,
links: agentLinks(asset, network),
- next: 'Have the wallet sign tx_base64 (signTransaction on a VersionedTransaction/legacy tx from these bytes) and broadcast it, or call send_signed_transaction with the signed base64.',
+ next:
+ txsBase64.length === 1
+ ? 'Have the wallet sign txs_base64[0] and broadcast it, or call send_signed_transaction with the signed base64.'
+ : 'The mint exceeds one transaction (large data: URIs), so it is create + register IN ORDER. Have the wallet signAllTransactions over txs_base64, then call send_signed_transaction with the signed array; it broadcasts sequentially and absorbs the propagation race.',
};
},
};