get-balance
Check your current SOL wallet balance to monitor funds and manage payments within the X402 payment system.
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)Handler function that creates a Solana connection on devnet, derives the public key from the environment private key, fetches the balance in lamports, converts to SOL, and returns formatted text response 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)Tool schema with 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)Registers the 'get-balance' tool on the MCP server with 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 }; } } );