Skip to main content
Glama

contract_view_functions

Retrieve and analyze available functions on a NEAR smart contract by specifying the contract ID and network, enabling efficient interaction with blockchain data.

Instructions

View available functions on a NEAR smart contract.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contractIdYes
networkIdNomainnet

Implementation Reference

  • Registration of the 'contract_view_functions' MCP tool, including inline schema and handler function.
      'contract_view_functions',
      noLeadingWhitespace`
      View available functions on a NEAR smart contract.`,
      {
        contractId: z.string(),
        networkId: z.enum(['testnet', 'mainnet']).default('mainnet'),
      },
      async (args, _) => {
        const connection = await connect({
          networkId: args.networkId,
          nodeUrl: getEndpointsByNetwork(args.networkId)[0]!,
        });
    
        const accountResult: Result<Account, Error> = await getAccount(
          args.contractId,
          connection,
        );
        if (!accountResult.ok) {
          return {
            content: [{ type: 'text', text: `Error: ${accountResult.error}` }],
          };
        }
    
        // fallback to downloading the wasm code and parsing functions
        const contractMethodsResult: Result<string[], Error> =
          await getContractMethods(args.contractId, connection);
        if (!contractMethodsResult.ok) {
          return {
            content: [
              {
                type: 'text',
                text: `Error: ${contractMethodsResult.error}`,
              },
            ],
          };
        }
        return {
          content: [
            {
              type: 'text',
              text: `Contract ${args.contractId} methods: ${stringify_bigint(contractMethodsResult.value)}`,
            },
          ],
        };
      },
    );
  • Handler implementation for contract_view_functions tool that connects to NEAR, verifies contract account, and uses getContractMethods to list functions.
      const connection = await connect({
        networkId: args.networkId,
        nodeUrl: getEndpointsByNetwork(args.networkId)[0]!,
      });
    
      const accountResult: Result<Account, Error> = await getAccount(
        args.contractId,
        connection,
      );
      if (!accountResult.ok) {
        return {
          content: [{ type: 'text', text: `Error: ${accountResult.error}` }],
        };
      }
    
      // fallback to downloading the wasm code and parsing functions
      const contractMethodsResult: Result<string[], Error> =
        await getContractMethods(args.contractId, connection);
      if (!contractMethodsResult.ok) {
        return {
          content: [
            {
              type: 'text',
              text: `Error: ${contractMethodsResult.error}`,
            },
          ],
        };
      }
      return {
        content: [
          {
            type: 'text',
            text: `Contract ${args.contractId} methods: ${stringify_bigint(contractMethodsResult.value)}`,
          },
        ],
      };
    },
  • Input schema for contract_view_functions tool defining contractId and networkId parameters.
      contractId: z.string(),
      networkId: z.enum(['testnet', 'mainnet']).default('mainnet'),
    },
  • getContractMethods helper: Fetches contract WASM code via RPC and parses it with WebAssembly to extract exported function names.
    const getContractMethods = async (
      contractAccountId: string,
      connection: Near,
    ): Promise<Result<string[], Error>> => {
      const contractCodeResult: Result<string, Error> = await (async () => {
        try {
          const view_code =
            await connection.connection.provider.query<ContractCodeView>({
              account_id: contractAccountId,
              finality: 'final',
              request_type: 'view_code',
            });
    
          return {
            ok: true,
            value: view_code.code_base64,
          };
        } catch (e) {
          return { ok: false, error: new Error(e as string) };
        }
      })();
      if (!contractCodeResult.ok) {
        return contractCodeResult;
      }
    
      // Decode the base64 contract code
      const contractCodeBase64 = contractCodeResult.value;
      const contractCodeBuffer = Buffer.from(contractCodeBase64, 'base64');
    
      // Parse the contract code using WebAssembly
      const contractMethodsResult: Result<string[], Error> = await (async () => {
        try {
          const wasmModule = await WebAssembly.compile(contractCodeBuffer);
          const exports = WebAssembly.Module.exports(wasmModule)
            .filter((exp) => exp.kind === 'function')
            .map((exp) => exp.name);
          return {
            ok: true,
            value: exports,
          };
        } catch (e) {
          return {
            ok: false,
            error: new Error(
              `Failed to parse WebAssembly: ${e instanceof Error ? e.message : String(e)}`,
            ),
          };
        }
      })();
      if (!contractMethodsResult.ok) {
        return contractMethodsResult;
      }
      return { ok: true, value: contractMethodsResult.value };
    };

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