transfer_tokens
Transfer SPL tokens between Solana wallets by specifying sender, recipient address, token mint, and amount.
Instructions
Transfer SPL tokens between wallets
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fromWallet | Yes | Name of the sender wallet | |
| toAddress | Yes | Recipient address | |
| tokenMint | Yes | Token mint address | |
| amount | Yes | Amount of tokens to transfer |
Implementation Reference
- src/index.ts:671-724 (handler)The handler function that executes the SPL token transfer. It retrieves the sender's wallet, computes associated token accounts, optionally creates the recipient's ATA, builds and signs a transaction with transfer instruction, and sends it.async function handleTransferTokens(args: any) { const { fromWallet, toAddress, tokenMint, amount } = args; const wallet = wallets.get(fromWallet); if (!wallet) { throw new Error(`Wallet '${fromWallet}' not found`); } ensureConnection(); const tokenMintPubkey = new PublicKey(tokenMint); const toPubkey = new PublicKey(toAddress); const fromTokenAccount = await getAssociatedTokenAddress(tokenMintPubkey, wallet.keypair.publicKey); const toTokenAccount = await getAssociatedTokenAddress(tokenMintPubkey, toPubkey); const transaction = new Transaction(); // Check if recipient has token account, create if not try { await getAccount(connection, toTokenAccount); } catch { transaction.add( createAssociatedTokenAccountInstruction( wallet.keypair.publicKey, toTokenAccount, toPubkey, tokenMintPubkey ) ); } transaction.add( createTransferInstruction( fromTokenAccount, toTokenAccount, wallet.keypair.publicKey, BigInt(Math.floor(amount)) ) ); const { blockhash } = await connection.getLatestBlockhash(); transaction.recentBlockhash = blockhash; transaction.feePayer = wallet.keypair.publicKey; transaction.sign(wallet.keypair); const signature = await connection.sendTransaction(transaction, [wallet.keypair]); return { success: true, signature, explorerUrl: `https://explorer.solana.com/tx/${signature}?cluster=${currentNetwork}` }; }
- src/index.ts:168-192 (schema)The input schema definition and tool metadata for transfer_tokens, part of the tools array used for registration and validation.{ name: "transfer_tokens", description: "Transfer SPL tokens between wallets", inputSchema: { type: "object", properties: { fromWallet: { type: "string", description: "Name of the sender wallet" }, toAddress: { type: "string", description: "Recipient address" }, tokenMint: { type: "string", description: "Token mint address" }, amount: { type: "number", description: "Amount of tokens to transfer" } }, required: ["fromWallet", "toAddress", "tokenMint", "amount"] }
- src/index.ts:1303-1304 (registration)The dispatch case in the MCP CallToolRequest handler that routes calls to the transfer_tokens handler function.case "transfer_tokens": result = await handleTransferTokens(args);