Skip to main content
Glama

getPortfolio

Retrieve a wallet's complete asset portfolio including native and ERC-20 tokens with USD values and allocation percentages across multiple EVM chains.

Instructions

지갑의 전체 자산 포트폴리오(네이티브 + ERC-20 토큰, USD 가치, 비율)를 한 번에 조회합니다

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
addressYes조회할 지갑 주소 (0x...)
chainNoEVM 체인ethereum

Implementation Reference

  • The main handler function for the getPortfolio tool. It fetches native and ERC-20 balances for a given address on a specific chain, calculates USD values, and structures the response data.
    async function handler(args: z.infer<typeof inputSchema>): Promise<ToolResult<PortfolioData>> {
      const { address, chain } = args;
    
      if (!isAddress(address)) {
        return makeError(`Invalid address: ${address}`, "INVALID_INPUT");
      }
    
      const cacheKey = `portfolio:${chain}:${address}`;
      const cached = cache.get<PortfolioData>(cacheKey);
      if (cached.hit) return makeSuccess(chain, cached.data, true);
    
      try {
        const client = getClient(chain);
        const chainConfig = chains[chain];
        const assets: PortfolioAsset[] = [];
    
        // 네이티브 토큰 잔고 조회
        const nativeBalanceWei = await client.getBalance({ address: address as Address });
        const nativeFormatted = formatUnits(nativeBalanceWei, 18);
        const nativeSymbol = chainConfig?.nativeCurrency?.symbol ?? "ETH";
        const nativeName = chainConfig?.nativeCurrency?.name ?? "Ether";
    
        let nativeValueUsd = 0;
        try {
          const nativeId = chainConfig?.nativeCurrency?.coingeckoId;
          if (nativeId) {
            const price = await getPrice(nativeId);
            nativeValueUsd = parseFloat(nativeFormatted) * price.priceUsd;
          }
        } catch {
          // 가격 조회 실패 무시
        }
    
        if (nativeBalanceWei > 0n) {
          assets.push({
            symbol: nativeSymbol,
            name: nativeName,
            balance: nativeFormatted,
            balanceUsd: Math.round(nativeValueUsd * 100) / 100,
            percentage: 0, // 나중에 계산
            type: "native",
          });
        }
    
        // 해당 체인에 주소가 있는 모든 토큰의 잔고 조회
        for (const tokenInfo of tokensData) {
          const tokenAddress = tokenInfo.addresses[chain];
          if (!tokenAddress) continue;
    
          try {
            const balance = await client.readContract({
              address: tokenAddress as Address,
              abi: ERC20_BALANCE_ABI,
              functionName: "balanceOf",
              args: [address as Address],
            });
    
            // 잔고가 0인 토큰은 건너뜀
            if (balance === 0n) continue;
    
            const formatted = formatUnits(balance, tokenInfo.decimals);
    
            let valueUsd = 0;
            try {
              const price = await getPrice(tokenInfo.coingeckoId);
              valueUsd = parseFloat(formatted) * price.priceUsd;
            } catch {
              // 가격 조회 실패 무시
            }
    
            assets.push({
              symbol: tokenInfo.symbol,
              name: tokenInfo.name,
              balance: formatted,
              balanceUsd: Math.round(valueUsd * 100) / 100,
              percentage: 0, // 나중에 계산
              type: "erc20",
              address: tokenAddress,
            });
          } catch {
            // 개별 토큰 조회 실패는 건너뜀
          }
        }
    
        // balanceUsd 내림차순 정렬
        assets.sort((a, b) => b.balanceUsd - a.balanceUsd);
    
        // 총 포트폴리오 가치 합산
        const totalValueUsd =
          Math.round(assets.reduce((sum, a) => sum + a.balanceUsd, 0) * 100) / 100;
    
        // 비율 계산
        if (totalValueUsd > 0) {
          for (const asset of assets) {
            asset.percentage = Math.round((asset.balanceUsd / totalValueUsd) * 10000) / 100;
          }
        }
    
        const data: PortfolioData = {
          address,
          chain,
          totalValueUsd,
          assets,
          assetCount: assets.length,
        };
    
        cache.set(cacheKey, data, PORTFOLIO_CACHE_TTL);
        return makeSuccess(chain, data, false);
      } catch (err) {
        const message = sanitizeError(err);
        return makeError(`Failed to fetch portfolio: ${message}`, "RPC_ERROR");
      }
    }
  • Input schema definition using Zod for the getPortfolio tool parameters.
    const inputSchema = z.object({
      address: z.string().describe("조회할 지갑 주소 (0x...)"),
      chain: z.enum(SUPPORTED_CHAINS).default("ethereum").describe("EVM 체인"),
    });
  • Registration function for the getPortfolio tool, which adds the tool to the MCP server.
    export function register(server: McpServer) {
      server.tool(
        "getPortfolio",
        "지갑의 전체 자산 포트폴리오(네이티브 + ERC-20 토큰, USD 가치, 비율)를 한 번에 조회합니다",
        inputSchema.shape,
        async (args) => {
          const result = await handler(args as z.infer<typeof inputSchema>);
          return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
        },
      );
    }
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden. It usefully discloses data composition (native + ERC-20, USD value, ratios) and aggregation behavior ('한 번에' / at once), but lacks operational details like read-only nature, rate limits, authentication requirements, or error handling for invalid addresses.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Single well-constructed sentence with efficient parenthetical specification of return data components. Every element earns its place—no fluff, no tautology, front-loaded with the core action and resource.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Despite lacking an output schema, the description compensates by explicitly enumerating return data components (native tokens, ERC-20, USD value, ratios). For a read-only portfolio aggregation tool, this provides sufficient completeness, though specific response format or pagination details could strengthen it further.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 100% schema description coverage (address and chain fully documented), the description appropriately does not redundantly explain parameters. It meets the baseline score of 3 where structured schema already provides complete semantic coverage, though it adds no supplementary parameter guidance.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action (query/retrieve) and resource (wallet's total asset portfolio), including detailed scope (native + ERC-20 tokens, USD value, ratios). It effectively distinguishes from siblings like getBalance (single token) or getTokenInfo (metadata) by emphasizing 'total portfolio' and 'at once' aggregation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

While it doesn't explicitly name alternatives, the description implies usage through scope specification—'total asset portfolio' signals this is for comprehensive aggregation rather than individual token lookups. However, it lacks explicit when-to-use guidance comparing against getBalance or getTokenInfo siblings.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/calintzy/evmscope'

If you have feedback or need assistance with the MCP directory API, please join our Discord server