Skip to main content
Glama

contract_view_functions

View the available functions of a NEAR smart contract to understand its interface for interaction.

Instructions

View available functions on a NEAR smart contract.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contractIdYes
networkIdNomainnet

Implementation Reference

  • The 'contract_view_functions' MCP tool registration and handler function. It connects to the NEAR blockchain, validates the contract account exists, then calls getContractMethods() to retrieve the list of exported functions from the deployed WebAssembly contract code.
    mcp.tool(
      '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)}`,
            },
          ],
        };
      },
    );
  • The tool is registered via mcp.tool() with the name 'contract_view_functions' and accepts 'contractId' (string) and 'networkId' (enum: testnet/mainnet) as input schema parameters.
    mcp.tool(
      '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)}`,
            },
          ],
        };
      },
    );
  • Input schema for the tool: contractId (z.string()) and networkId (z.enum(['testnet', 'mainnet']).default('mainnet')).
    {
      contractId: z.string(),
      networkId: z.enum(['testnet', 'mainnet']).default('mainnet'),
    },
  • The getContractMethods helper function that fetches the contract's Wasm code via RPC query (view_code request type), decodes it from base64, compiles it with WebAssembly, and extracts the 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 };
    };
Behavior2/5

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

No annotations provided, and the description does not disclose behavioral traits like read-only nature, error handling, or authentication requirements. For a tool with no annotations, this is insufficient.

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 sentence, concise, no fluff. Front-loaded with the action and resource.

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?

No output schema and minimal description. Does not explain return format, possible errors, or provide examples. Incomplete for a tool with 2 parameters and no annotations.

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

Parameters2/5

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

Schemas have 0% description coverage, and the description adds no meaning beyond parameter names. Does not explain what contractId or networkId represent or how to use them.

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 'View' and the resource 'available functions on a NEAR smart contract', distinguishing it from sibling tools like contract_call_raw_function.

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?

No guidance on when to use this tool versus alternatives such as contract_get_function_args or contract_call_raw_function. Missing context on prerequisites or network selection.

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