contract_call_raw_function_as_read_only
Execute a read-only function call on a NEAR smart contract to retrieve data without modifying its state. Specify contract ID, method name, and arguments.
Instructions
Call a function of a contract as a read-only call. This is equivalent to saying we are calling a view method of the contract.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| args | Yes | The arguments to pass to the method. | |
| contractId | Yes | The account id of the contract. | |
| methodName | Yes | The name of the method to call. | |
| networkId | No | mainnet |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"args": {
"additionalProperties": {},
"description": "The arguments to pass to the method.",
"type": "object"
},
"contractId": {
"description": "The account id of the contract.",
"type": "string"
},
"methodName": {
"description": "The name of the method to call.",
"type": "string"
},
"networkId": {
"default": "mainnet",
"enum": [
"testnet",
"mainnet"
],
"type": "string"
}
},
"required": [
"contractId",
"methodName",
"args"
],
"type": "object"
}
Implementation Reference
- src/services.ts:2009-2052 (handler)Executes a read-only (view) function call on a NEAR smart contract. Connects to the network, verifies the contract account, calls viewFunction with provided method and args, handles errors, and returns the result.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}` }], }; } const account = accountResult.value; const viewCallResult: Result<unknown, Error> = await (async () => { try { return { ok: true, value: await account.viewFunction({ contractId: args.contractId, methodName: args.methodName, args: args.args, }), }; } catch (e) { return { ok: false, error: new Error(e as string) }; } })(); if (!viewCallResult.ok) { return { content: [{ type: 'text', text: `Error: ${viewCallResult.error}` }], }; } return { content: [ { type: 'text', text: `View call result: ${stringify_bigint(viewCallResult.value)}`, }, ], }; },
- src/services.ts:2001-2007 (schema)Zod schema defining input parameters: contractId (string), methodName (string), networkId (enum: testnet/mainnet, default mainnet), args (record of string to any).contractId: z.string().describe('The account id of the contract.'), methodName: z.string().describe('The name of the method to call.'), networkId: z.enum(['testnet', 'mainnet']).default('mainnet'), args: z .record(z.string(), z.any()) .describe('The arguments to pass to the method.'), },
- src/services.ts:1996-2053 (registration)Registers the MCP tool 'contract_call_raw_function_as_read_only' with description, input schema, and handler function.'contract_call_raw_function_as_read_only', noLeadingWhitespace` Call a function of a contract as a read-only call. This is equivalent to saying we are calling a view method of the contract.`, { contractId: z.string().describe('The account id of the contract.'), methodName: z.string().describe('The name of the method to call.'), networkId: z.enum(['testnet', 'mainnet']).default('mainnet'), args: z .record(z.string(), z.any()) .describe('The arguments to pass to the method.'), }, 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}` }], }; } const account = accountResult.value; const viewCallResult: Result<unknown, Error> = await (async () => { try { return { ok: true, value: await account.viewFunction({ contractId: args.contractId, methodName: args.methodName, args: args.args, }), }; } catch (e) { return { ok: false, error: new Error(e as string) }; } })(); if (!viewCallResult.ok) { return { content: [{ type: 'text', text: `Error: ${viewCallResult.error}` }], }; } return { content: [ { type: 'text', text: `View call result: ${stringify_bigint(viewCallResult.value)}`, }, ], }; }, );