getBalance
Check the SOL balance of a specific Solana account address using a simple RPC endpoint provided by the Solana MCP Server.
Instructions
Get balance for a Solana address
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | Yes | Solana account address |
Implementation Reference
- src/index.ts:62-87 (handler)The main execution logic for the getBalance tool. Parses the address into a PublicKey, retrieves the lamports balance from Solana RPC, converts to SOL, and returns formatted response or error.async ({ address }) => { try { const publicKey = new PublicKey(address); const balance = await connection.getBalance(publicKey); const solBalance = balance / LAMPORTS_PER_SOL; // Using LAMPORTS_PER_SOL constant return { content: [ { type: "text", text: `Balance for ${address}:\n${solBalance} SOL`, }, ], }; } catch (err) { const error = err as Error; return { content: [ { type: "text", text: `Failed to retrieve balance for address: ${error.message}`, }, ], }; } }
- src/index.ts:59-61 (schema)Zod input schema validating the 'address' parameter as a string with description.{ address: z.string().describe("Solana account address"), },
- src/index.ts:56-88 (registration)Calls server.tool() to register the getBalance tool with its name, description, schema, and handler function.server.tool( "getBalance", "Get balance for a Solana address", { address: z.string().describe("Solana account address"), }, async ({ address }) => { try { const publicKey = new PublicKey(address); const balance = await connection.getBalance(publicKey); const solBalance = balance / LAMPORTS_PER_SOL; // Using LAMPORTS_PER_SOL constant return { content: [ { type: "text", text: `Balance for ${address}:\n${solBalance} SOL`, }, ], }; } catch (err) { const error = err as Error; return { content: [ { type: "text", text: `Failed to retrieve balance for address: ${error.message}`, }, ], }; } } );