Skip to main content
Glama

get_chain_context

Retrieve DeFi context for blockchain networks including TVL, dominance, trends, top protocols, and market share movement. Supports major chains like Ethereum, Solana, Base, Arbitrum, and others.

Instructions

Get DeFi context for a specific blockchain: TVL, dominance, trend, top protocols, and whether the chain is gaining or losing market share. Supports: ethereum, solana, base, arbitrum, optimism, polygon, avalanche, bsc, tron, bitcoin.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
chainYesChain name (e.g. "ethereum", "solana", "base", "arbitrum")

Implementation Reference

  • The main handler for the 'get_chain_context' tool, which fetches chain TVL, dominance, and protocol data.
    export async function getChainContext(cache: CacheService, chain: string): Promise<ChainContextOutput | ErrorOutput> {
      const chainName = resolveChainName(chain);
      const cacheKey = `chain_context_${chainName.toLowerCase()}`;
      const cached = cache.get<ChainContextOutput>(cacheKey);
      if (cached) return cached.data;
    
      try {
        const [chains, protocols, history] = await Promise.all([
          getChains(),
          getProtocols(),
          getChainHistoricalTvl(chainName).catch(() => []),
        ]);
    
        const chainData = chains.find(c => c.name.toLowerCase() === chainName.toLowerCase());
        if (!chainData) {
          return {
            error: true, error_source: 'get_chain_context',
            agent_guidance: 'Chain not found. Verify the chain name and retry.',
            last_known_data: null, data_warnings: ['Chain not found in DeFiLlama data.'],
          };
        }
    
        const totalTvl = chains.reduce((sum, c) => sum + c.tvl, 0);
        const dominancePct = totalTvl > 0 ? Math.round((chainData.tvl / totalTvl) * 10000) / 100 : 0;
    
        // TVL changes from history
        let tvlChange7d = 0;
        let tvlChange30d = 0;
        if (history.length > 7) {
          const current = history[history.length - 1]?.tvl ?? chainData.tvl;
          const weekAgo = history[history.length - 8]?.tvl ?? current;
          tvlChange7d = weekAgo > 0 ? Math.round(((current - weekAgo) / weekAgo) * 10000) / 100 : 0;
          if (history.length > 30) {
            const monthAgo = history[history.length - 31]?.tvl ?? current;
            tvlChange30d = monthAgo > 0 ? Math.round(((current - monthAgo) / monthAgo) * 10000) / 100 : 0;
          }
        }
    
        let tvlTrend: ChainContextOutput['tvl_trend'] = 'stable';
        if (tvlChange7d > 10) tvlTrend = 'expanding';
        else if (tvlChange7d > 3) tvlTrend = 'expanding';
        else if (tvlChange7d < -10) tvlTrend = 'collapsing';
        else if (tvlChange7d < -3) tvlTrend = 'contracting';
    
        // Top protocols on this chain
        const chainProtocols = protocols
          .filter(p => p.chain.toLowerCase() === chainName.toLowerCase() || p.chain === '-')
          .sort((a, b) => b.tvl - a.tvl)
          .slice(0, 10);
    
        const topProtocols = chainProtocols.map(p => ({
          name: p.name,
          tvl_usd: p.tvl,
          category: p.category,
          dominance_on_chain: chainData.tvl > 0 ? Math.round((p.tvl / chainData.tvl) * 10000) / 100 : 0,
        }));
    
        // Dominance shift
        let dominanceShift: ChainContextOutput['chain_dominance_shift'] = 'stable';
        if (tvlChange7d > 5 && tvlChange7d > tvlChange30d / 4) dominanceShift = 'gaining';
        else if (tvlChange7d < -5) dominanceShift = 'losing';
    
        const guidance = generateChainGuidance(chainName, chainData.tvl, dominancePct, tvlTrend, dominanceShift, tvlChange7d);
    
        const result: ChainContextOutput = {
          chain: chainName,
          chain_tvl_usd: chainData.tvl,
          chain_dominance_pct: dominancePct,
          tvl_change_7d: tvlChange7d,
          tvl_change_30d: tvlChange30d,
          tvl_trend: tvlTrend,
          top_protocols: topProtocols,
          protocol_count: chainProtocols.length,
          chain_dominance_shift: dominanceShift,
          agent_guidance: guidance,
        };
    
        cache.set(cacheKey, result, getCacheTtl(BASE_TTL));
        return result;
      } catch {
        return {
          error: true, error_source: 'get_chain_context',
          agent_guidance: 'Chain context temporarily unavailable. Retry shortly.',
          last_known_data: null, data_warnings: ['Chain data source temporarily unavailable.'],
        };
      }
    }
  • Type definition for the output of 'get_chain_context'.
    export interface ChainContextOutput {
      chain: string;
      chain_tvl_usd: number;
      chain_dominance_pct: number;
      tvl_change_7d: number;
      tvl_change_30d: number;
      tvl_trend: 'expanding' | 'stable' | 'contracting' | 'collapsing';
      top_protocols: Array<{ name: string; tvl_usd: number; category: string; dominance_on_chain: number }>;
      protocol_count: number;
      chain_dominance_shift: 'gaining' | 'stable' | 'losing';
      agent_guidance: string;
    }
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 of behavioral disclosure. It adequately describes the conceptual data returned (compensating for the missing output schema), but omits operational details like rate limits, data freshness, error behaviors for unsupported chains, or whether results are cached vs real-time.

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?

Every sentence earns its place: the first clause defines the action and resource, the colon-delimited list specifies return values, and the final fragment enumerates valid inputs. Zero waste, appropriately front-loaded.

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?

Given the simple single-parameter input and lack of output schema, the description compensates well by detailing the expected return content (TVL, dominance, etc.) and listing all valid chain values. It is complete for a read-only data retrieval tool, though error scenarios could be mentioned.

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

Parameters4/5

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

With 100% schema coverage, the schema already documents the parameter well. However, the description adds significant value by providing the complete enumeration of supported chains ('ethereum, solana, base...'), effectively serving as an enum constraint that the schema lacks.

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 uses a specific verb ('Get') and clear resource ('DeFi context for a specific blockchain'), then enumerates exactly what metrics are returned (TVL, dominance, trend, top protocols, market share direction). This clearly distinguishes it from sibling tools like get_asset_context (asset-level) and get_defi_health (health-focused).

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 the description implies usage through specificity of returned metrics, it lacks explicit guidance on when to prefer this over siblings like get_onchain_pulse or get_defi_health. No alternatives or exclusion criteria are mentioned, leaving selection logic implicit.

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/0xHashy/fathom-fyi'

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