getAccountInfo.tsā¢3.1 kB
import { Tool } from "@modelcontextprotocol/sdk/types.js";
import { getAptosClient } from "../../config.js";
import { formatAddress } from "../../utils/format.js";
export const GET_ACCOUNT_INFO: Tool = {
name: "get_account_info",
description: "Get detailed information about an Aptos account including sequence number and authentication key. This is used for checking account status and transaction readiness. Returns comprehensive account information.",
inputSchema: {
type: "object",
properties: {
account_address: {
type: "string",
description: "Aptos account address, e.g., 0x1 or 0x742d35Cc6634C0532925a3b8D6Ac0C4db9c8b3",
},
},
required: ["account_address"],
},
};
/**
* Gets detailed information about an Aptos account
* @param args The arguments containing the account address
* @returns The account information
*/
export async function getAccountInfoHandler(args: Record<string, any> | undefined) {
if (!isGetAccountInfoArgs(args)) {
throw new Error("Invalid arguments for get_account_info");
}
const { account_address } = args;
try {
const results = await performGetAccountInfo(account_address);
return {
content: [{ type: "text", text: results }],
isError: false,
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Error getting account info: ${error instanceof Error ? error.message : String(error)}`,
},
],
isError: true,
};
}
}
/**
* Gets detailed information about an Aptos account
* @param accountAddress The Aptos account address
* @returns The account information as a formatted string
*/
export async function performGetAccountInfo(accountAddress: string): Promise<string> {
try {
const aptos = getAptosClient();
// Get account info
const accountInfo = await aptos.getAccountInfo({ accountAddress });
return `Account Information:
Address: ${accountAddress}
Sequence Number: ${accountInfo.sequence_number}
Authentication Key: ${accountInfo.authentication_key}
Formatted Address: ${formatAddress(accountAddress)}`;
} catch (error) {
console.error('Error getting account info:', error);
// Check if account doesn't exist
if (error instanceof Error && error.message.includes('not found')) {
return `Account Information:
Address: ${accountAddress}
Status: Account does not exist or has not been initialized
Note: Account needs to receive at least one transaction to be initialized on-chain`;
}
throw new Error(`Failed to get account info: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Checks if the provided arguments are valid for the getAccountInfo tool
* @param args The arguments to check
* @returns True if the arguments are valid, false otherwise
*/
export function isGetAccountInfoArgs(args: unknown): args is { account_address: string } {
return (
typeof args === "object" &&
args !== null &&
"account_address" in args &&
typeof (args as any).account_address === "string"
);
}