Skip to main content
Glama

ref_finance_get_pools

Search for liquidity pools on Ref Finance by token pair, returning pools prioritized by higher liquidity and better rates.

Instructions

Search for liquidity pools on the Ref Finance exchange contract based on two tokens. Prioritize pools with higher liquidity and better rates for the user.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tokenAYes
tokenBYes
networkIdNomainnet

Implementation Reference

  • Registration of the 'ref_finance_get_pools' MCP tool. Uses mcp.tool() with name 'ref_finance_get_pools', defines input schema (tokenA, tokenB, networkId), and the handler callback.
    mcp.tool(
      'ref_finance_get_pools',
      noLeadingWhitespace`
      Search for liquidity pools on the Ref Finance exchange contract based on two tokens.
      Prioritize pools with higher liquidity and better rates for the user.`,
      {
        tokenA: z.object({
          contractId: z.string().describe('The first token contract id'),
          symbol: z.string().describe('The first token symbol'),
        }),
        tokenB: z.object({
          contractId: z.string().describe('The second token contract id'),
          symbol: z.string().describe('The second token symbol'),
        }),
        networkId: z.enum(['testnet', 'mainnet']).default('mainnet'),
      },
      async (args, _) => {
        const connection = await connect({
          networkId: args.networkId,
          keyStore: keystore,
          nodeUrl: getEndpointsByNetwork(args.networkId)[0]!,
        });
    
        const tokenAContractAccountResult = await getAccount(
          args.tokenA.contractId,
          connection,
        );
        if (!tokenAContractAccountResult.ok) {
          return {
            content: [
              {
                type: 'text',
                text: `Error: ${tokenAContractAccountResult.error}`,
              },
            ],
          };
        }
        const tokenA = tokenAContractAccountResult.value;
    
        const tokenBContractAccountResult = await getAccount(
          args.tokenB.contractId,
          connection,
        );
        if (!tokenBContractAccountResult.ok) {
          return {
            content: [
              {
                type: 'text',
                text: `Error: ${tokenBContractAccountResult.error}`,
              },
            ],
          };
        }
        const tokenB = tokenBContractAccountResult.value;
    
        const poolsInfoResult = await refFinanceGetPoolsInfo(connection);
        if (!poolsInfoResult.ok) {
          return {
            content: [{ type: 'text', text: `Error: ${poolsInfoResult.error}` }],
          };
        }
    
        const filteredPools = poolsInfoResult.value.filter(
          (pool) =>
            pool.tokenIds.includes(tokenA.accountId) &&
            pool.tokenIds.includes(tokenB.accountId),
        );
        if (filteredPools.length === 0) {
          return {
            content: [
              {
                type: 'text',
                text: `No pool found for ${args.tokenA.contractId} and ${args.tokenB.contractId}`,
              },
            ],
          };
        }
    
        return {
          content: [{ type: 'text', text: stringify_bigint(filteredPools) }],
        };
      },
    );
  • Handler for 'ref_finance_get_pools' tool. Connects to NEAR, validates token contracts, calls refFinanceGetPoolsInfo(), filters pools matching both token IDs, and returns the result.
      async (args, _) => {
        const connection = await connect({
          networkId: args.networkId,
          keyStore: keystore,
          nodeUrl: getEndpointsByNetwork(args.networkId)[0]!,
        });
    
        const tokenAContractAccountResult = await getAccount(
          args.tokenA.contractId,
          connection,
        );
        if (!tokenAContractAccountResult.ok) {
          return {
            content: [
              {
                type: 'text',
                text: `Error: ${tokenAContractAccountResult.error}`,
              },
            ],
          };
        }
        const tokenA = tokenAContractAccountResult.value;
    
        const tokenBContractAccountResult = await getAccount(
          args.tokenB.contractId,
          connection,
        );
        if (!tokenBContractAccountResult.ok) {
          return {
            content: [
              {
                type: 'text',
                text: `Error: ${tokenBContractAccountResult.error}`,
              },
            ],
          };
        }
        const tokenB = tokenBContractAccountResult.value;
    
        const poolsInfoResult = await refFinanceGetPoolsInfo(connection);
        if (!poolsInfoResult.ok) {
          return {
            content: [{ type: 'text', text: `Error: ${poolsInfoResult.error}` }],
          };
        }
    
        const filteredPools = poolsInfoResult.value.filter(
          (pool) =>
            pool.tokenIds.includes(tokenA.accountId) &&
            pool.tokenIds.includes(tokenB.accountId),
        );
        if (filteredPools.length === 0) {
          return {
            content: [
              {
                type: 'text',
                text: `No pool found for ${args.tokenA.contractId} and ${args.tokenB.contractId}`,
              },
            ],
          };
        }
    
        return {
          content: [{ type: 'text', text: stringify_bigint(filteredPools) }],
        };
      },
    );
  • Helper function refFinanceGetPoolsInfo() that fetches all pools from the Ref Finance contract using paginated view calls to 'get_pools' and 'get_number_of_pools' methods.
    export const refFinanceGetPoolsInfo = async (
      connection: Near,
    ): Promise<Result<Pool[], Error>> => {
      try {
        const refConfig = refGetConfig(connection.connection.networkId);
        const refAccount = await connection.account(refConfig.REF_FI_CONTRACT_ID);
    
        // get the total number of pools
        const numberOfPools = (await refAccount.viewFunction({
          contractId: refConfig.REF_FI_CONTRACT_ID,
          methodName: 'get_number_of_pools',
          args: {},
        })) as unknown as number;
    
        // Concurrently get all pools
        const perPage = 1024;
        const allResults = (
          await mapSemaphore(
            Array.from(
              { length: Math.ceil(numberOfPools / perPage) },
              (_, i) => i * perPage,
            ),
            4,
            async (index) => {
              const contractResult = (await refAccount.viewFunction({
                contractId: refConfig.REF_FI_CONTRACT_ID,
                methodName: 'get_pools',
                args: { from_index: index, limit: perPage },
              })) as unknown as PoolRPCView[];
              return contractResult.map((rawPool, i) =>
                parsePool(rawPool, i + index),
              );
            },
          )
        ).flat();
    
        return {
          ok: true,
          value: allResults,
        };
      } catch (e) {
        return { ok: false, error: new Error(e as string) };
      }
    };
  • Input schema for the 'ref_finance_get_pools' tool: tokenA (contractId, symbol), tokenB (contractId, symbol), and optional networkId.
    {
      tokenA: z.object({
        contractId: z.string().describe('The first token contract id'),
        symbol: z.string().describe('The first token symbol'),
      }),
      tokenB: z.object({
        contractId: z.string().describe('The second token contract id'),
        symbol: z.string().describe('The second token symbol'),
      }),
      networkId: z.enum(['testnet', 'mainnet']).default('mainnet'),
    },
  • Helper parsePool() function that converts raw pool RPC view data into the Pool interface used by the tool.
    export const parsePool = (pool: PoolRPCView, id?: number): Pool => ({
      id: Number(typeof id === 'number' ? id : pool.id),
      tokenIds: pool.token_account_ids,
      supplies: pool.amounts.reduce(
        (acc: Record<string, string>, amount: string, i: number) => {
          if (pool.token_account_ids[i] !== undefined) {
            acc[pool.token_account_ids[i]] = amount;
          }
          return acc;
        },
        {},
      ),
      fee: pool.total_fee,
      shareSupply: pool.shares_total_supply,
      tvl: pool.tvl,
      token0_ref_price: pool.token0_ref_price,
      pool_kind: pool.pool_kind,
    });
Behavior2/5

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

No annotations are provided, so the description carries full burden. It states it 'searches' and 'prioritizes' but does not disclose whether the tool is read-only, how many pools are returned, how prioritization works, or any side effects. This lacks sufficient behavioral context.

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?

Two concise sentences with no fluff. Front-loaded with the primary action. Every sentence adds value.

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

Completeness2/5

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

Despite having 3 parameters and no output schema, the description does not cover what the tool returns, how prioritization works, or any limitations. It is insufficient for an agent to reliably use the tool without additional context.

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

Parameters1/5

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

With 0% schema description coverage, the description must explain parameters. It only says 'based on two tokens', adding no meaning beyond the schema itself. It does not explain the structure of tokenA/tokenB or the networkId parameter.

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 verb 'search' and the resource 'liquidity pools on the Ref Finance exchange contract', and distinguishes it from sibling tools like ref_finance_execute_swap and ref_finance_get_swap_estimate by specifying it is based on two tokens.

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

Usage Guidelines2/5

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

The description says to use it for searching pools based on tokens and prioritizes liquidity, but provides no explicit guidance on when to use this tool vs alternatives (e.g., when to use get_swap_estimate instead), nor any conditions or exclusions.

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/nearai/near-mcp'

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