get-balance
Check your current SOL wallet balance to monitor funds and manage payments within the AI42-MCP X402 Payment Server.
Instructions
Check the current SOL balance in your wallet
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:37-71 (handler)The handler function for the 'get-balance' tool. It creates a signer, connects to Solana devnet RPC, derives the public key from the private key in SOLANA_PRIVATE_KEY env var, fetches the balance, converts lamports to SOL, and returns formatted JSON or error.async () => { try { const signer = await createSigner("solana-devnet", process.env.SOLANA_PRIVATE_KEY || " "); const connection = new Connection("https://api.devnet.solana.com", "confirmed"); const privateKeyBytes = bs58.decode(process.env.SOLANA_PRIVATE_KEY || ""); const keypair = Keypair.fromSecretKey(privateKeyBytes); const publicKey = keypair.publicKey; const balance = await connection.getBalance(publicKey); const solBalance = balance / LAMPORTS_PER_SOL; return { content: [{ type: "text", text: JSON.stringify({ balance: solBalance, unit: "SOL", network: "devnet", address: publicKey }, null, 2) }] }; } catch (err: any) { return { content: [{ type: "text", text: `Error: ${err.message || "Failed to get balance"}` }], isError: true }; } }
- src/index.ts:32-36 (schema)Schema definition for the 'get-balance' tool, including title, description, and empty input schema (no parameters required).{ title: "Get Wallet Balance", description: "Check the current SOL balance in your wallet", inputSchema: {}, },
- src/index.ts:30-72 (registration)Full registration of the 'get-balance' tool via server.registerTool, including name, schema, and inline handler function.server.registerTool( "get-balance", { title: "Get Wallet Balance", description: "Check the current SOL balance in your wallet", inputSchema: {}, }, async () => { try { const signer = await createSigner("solana-devnet", process.env.SOLANA_PRIVATE_KEY || " "); const connection = new Connection("https://api.devnet.solana.com", "confirmed"); const privateKeyBytes = bs58.decode(process.env.SOLANA_PRIVATE_KEY || ""); const keypair = Keypair.fromSecretKey(privateKeyBytes); const publicKey = keypair.publicKey; const balance = await connection.getBalance(publicKey); const solBalance = balance / LAMPORTS_PER_SOL; return { content: [{ type: "text", text: JSON.stringify({ balance: solBalance, unit: "SOL", network: "devnet", address: publicKey }, null, 2) }] }; } catch (err: any) { return { content: [{ type: "text", text: `Error: ${err.message || "Failed to get balance"}` }], isError: true }; } } );