Here’s how to initialize the agent with a keypair wallet:
Copy
import { SolanaAgentKit, createVercelAITools, KeypairWallet } from "solana-agent-kit";import TokenPlugin from "@solana-agent-kit/plugin-token";import NFTPlugin from "@solana-agent-kit/plugin-nft";import DefiPlugin from "@solana-agent-kit/plugin-defi";import MiscPlugin from "@solana-agent-kit/plugin-misc";import BlinksPlugin from "@solana-agent-kit/plugin-blinks";import { Keypair } from "@solana/web3.js";import bs58 from "bs58";// Create a keypair from a private keyconst keyPair = Keypair.fromSecretKey(bs58.decode("YOUR_SECRET_KEY"));const wallet = new KeypairWallet(keyPair);// Initialize with wallet and optional RPC URLconst agent = new SolanaAgentKit( wallet, "YOUR_RPC_URL", { OPENAI_API_KEY: "YOUR_OPENAI_API_KEY", }) .use(TokenPlugin) .use(NFTPlugin) .use(DefiPlugin) .use(MiscPlugin) .use(BlinksPlugin);// Create Vercel AI tools (or use createLangchainTools for LangChain)const tools = createVercelAITools(agent, agent.actions);
For a better user experience, you can integrate with embedded wallets like Privy instead of directly handling private keys. Here’s how to use Solana Agent Kit with the Solana wallet adapter:
Copy
import { SolanaAgentKit, createVercelAITools } from "solana-agent-kit";import TokenPlugin from "@solana-agent-kit/plugin-token";import DefiPlugin from "@solana-agent-kit/plugin-defi";import BlinksPlugin from "@solana-agent-kit/plugin-blinks";import MiscPlugin from "@solana-agent-kit/plugin-misc";import { Connection, PublicKey } from "@solana/web3.js";// Assuming you have a wallet from Privy or another wallet adapterconst wallet = wallets[0]; // From useSolanaWallets() or similar// Create an agent with the wallet adapterconst agent = new SolanaAgentKit( { publicKey: new PublicKey(wallet.address), signTransaction: async (tx) => { const signed = await wallet.signTransaction(tx); return signed; }, signMessage: async (msg) => { const signed = await wallet.signMessage(msg); return signed; }, sendTransaction: async (tx) => { const connection = new Connection( "YOUR_RPC_URL", "confirmed" ); return await wallet.sendTransaction(tx, connection); }, signAllTransactions: async (txs) => { const signed = await wallet.signAllTransactions(txs); return signed; }, signAndSendTransaction: async (tx) => { const signed = await wallet.signTransaction(tx); const connection = new Connection( "YOUR_RPC_URL", "confirmed" ); const sig = await wallet.sendTransaction(signed, connection); return { signature: sig }; }, }, "YOUR_RPC_URL", {}) .use(TokenPlugin) .use(DefiPlugin) .use(BlinksPlugin) .use(MiscPlugin);// Create tools for your AI modelconst tools = createVercelAITools(agent, agent.actions);
The Model Context Protocol (MCP) adapter allows you to create an MCP server that Claude Desktop can use to interact with the Solana blockchain. This provides a standardized way for Claude to access Solana functionality.
Restart Claude Desktop after updating the configuration.
This integration allows Claude to interact directly with your Solana agent through the MCP protocol, providing seamless blockchain functionality within conversations.