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
| Name | Required | Description | Default |
|---|---|---|---|
| contractId | Yes | ||
| networkId | No | mainnet |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"contractId": {
"type": "string"
},
"networkId": {
"default": "mainnet",
"enum": [
"testnet",
"mainnet"
],
"type": "string"
}
},
"required": [
"contractId"
],
"type": "object"
}
Implementation Reference
- src/services.ts:1796-1841 (registration)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)}`, }, ], }; }, );
- src/services.ts:1804-1840 (handler)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)}`, }, ], }; },
- src/services.ts:1800-1802 (schema)Input schema for contract_view_functions tool defining contractId and networkId parameters.contractId: z.string(), networkId: z.enum(['testnet', 'mainnet']).default('mainnet'), },
- src/services.ts:153-206 (helper)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 }; };