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
| Name | Required | Description | Default |
|---|---|---|---|
| contractId | Yes | ||
| networkId | No | mainnet |
Implementation Reference
- src/services.ts:1795-1841 (handler)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)}`, }, ], }; }, ); - src/services.ts:1795-1841 (registration)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)}`, }, ], }; }, ); - src/services.ts:1799-1802 (schema)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'), }, - src/services.ts:153-206 (helper)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 }; };