contract_call_raw_function_as_read_only
Invoke a contract's view method to read data without changing state, requiring only the 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 |
|---|---|---|---|
| contractId | Yes | The account id of the contract. | |
| methodName | Yes | The name of the method to call. | |
| networkId | No | mainnet | |
| args | Yes | The arguments to pass to the method. |
Implementation Reference
- src/services.ts:1995-2053 (registration)Tool 'contract_call_raw_function_as_read_only' is registered using mcp.tool() with name 'contract_call_raw_function_as_read_only' and description about calling a contract function as read-only.
mcp.tool( '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)}`, }, ], }; }, ); - src/services.ts:2000-2007 (schema)Input schema for the tool: contractId (string), methodName (string), networkId (enum), args (record of 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:2008-2053 (handler)Handler function: connects to NEAR network, gets the contract account, calls viewFunction() on it (read-only call), and returns the result as text content.
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)}`, }, ], }; }, );