get_msol_balance
Check mSOL token balance for any Solana wallet address or the environment wallet to monitor liquid staking positions in Marinade Finance.
Instructions
Check the mSOL token balance of the environment wallet or any specified Solana wallet address.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| walletAddress | No | Optional: The Solana wallet address to check. If not provided, checks the environment wallet balance. |
Implementation Reference
- src/tools.ts:185-297 (handler)The callback handler that executes the get_msol_balance tool logic: creates Solana config, initializes Marinade instance, gets mSolMint, resolves target wallet, fetches or notes absence of associated token account, computes and returns balance in mSOL and lamports.callback: async ({ walletAddress }: { walletAddress?: string }) => { try { const { wallet, connection } = createSolanaConfig() const config = new MarinadeConfig({ connection, publicKey: wallet.publicKey, }) const marinade = new Marinade(config) const marinadeState = await marinade.getMarinadeState(); const mSolMint = marinadeState.mSolMintAddress; let targetPublicKey: PublicKey; let isOwnWallet = true; if (walletAddress) { try { targetPublicKey = new PublicKey(walletAddress); isOwnWallet = targetPublicKey.toString() === wallet.publicKey.toString(); } catch (err) { return { content: [ { type: "text", text: JSON.stringify({ error: "Invalid wallet address", reason: "The provided wallet address is not a valid Solana public key" }, null, 2), }, ], }; } } else { targetPublicKey = wallet.publicKey; } try { const tokenAccount = await getOrCreateAssociatedTokenAccount( connection, wallet, mSolMint, targetPublicKey, false ); const balance = Number(tokenAccount.amount); const balanceInMSol = balance / LAMPORTS_PER_SOL; return { content: [ { type: "text", text: JSON.stringify({ success: true, wallet: targetPublicKey.toString(), tokenAccount: tokenAccount.address.toString(), balance: balanceInMSol, balanceLamports: balance.toString(), mSolMint: mSolMint.toString(), explorerUrl: `https://solscan.io/account/${tokenAccount.address.toString()}${process.env.ENVIRONMENT === 'MAINNET' ? '' : '?cluster=devnet'}` }, null, 2), }, ], }; } catch (err) { if ((err as Error)?.message?.includes("could not find") || (err as Error)?.message?.includes("Invalid account")) { return { content: [ { type: "text", text: JSON.stringify({ success: true, wallet: targetPublicKey.toString(), balance: 0, balanceLamports: "0", mSolMint: mSolMint.toString(), note: "No mSOL token account found for this wallet (balance is 0)" }, null, 2), }, ], }; } throw err; } } catch (err) { const isAbort = (err as Error)?.name === "AbortError"; const isTimeout = (err as Error)?.message?.includes("timeout"); McpLogger.error("Error in get_msol_balance tool:", String(err)); return { content: [ { type: "text", text: JSON.stringify( { error: isAbort || isTimeout ? "Request timed out" : "Failed to get mSOL balance", reason: String((err as Error)?.message ?? err), suggestion: isAbort || isTimeout ? "Try again with a different RPC endpoint." : "Please check the wallet address and network connection." }, null, 2 ), }, ], }; } }
- src/tools.ts:182-184 (schema)Zod input schema defining the optional 'walletAddress' parameter for the tool.inputSchema: { walletAddress: z.string().optional().describe("Optional: The Solana wallet address to check. If not provided, checks the environment wallet balance."), },
- src/server.ts:25-43 (registration)Registers all tools from marinadeFinanceTools array (including get_msol_balance) to the MCP server using server.registerTool, passing name, metadata/schema, and wrapped callback.for (const t of marinadeFinanceTools) { server.registerTool( t.name, { title: t.title, description: t.description, inputSchema: t.inputSchema }, async (args) => { const result = await t.callback(args); return { content: result.content.map(item => ({ ...item, type: "text" as const })) }; } ); }
- src/tools.ts:12-31 (helper)Helper function to create Solana wallet and connection based on environment variables, used by get_msol_balance and other on-chain tools.function createSolanaConfig() { const isMainnet = process.env.ENVIRONMENT === "MAINNET"; const privateKey = process.env.PRIVATE_KEY || ''; const rpcUrlMainnet = process.env.SOLANA_RPC_URL const rpcUrlDevnet = process.env.SOLANA_RPC_URL_DEVNET; const rpcUrl = isMainnet ? rpcUrlMainnet : rpcUrlDevnet; if (!privateKey || !rpcUrlMainnet || !rpcUrlDevnet || !rpcUrl) { throw new Error("PRIVATE_KEY, SOLANA_RPC_URL, SOLANA_RPC_URL_DEVNET environment variables are required"); } const wallet = Keypair.fromSecretKey(bs58.decode(privateKey)); const connection = new Connection(rpcUrl, { commitment: "confirmed", }); return { wallet, connection }; }