contract_call_raw_function
Execute a raw function call on a NEAR smart contract. Specify the signer, contract, method, and arguments to submit the transaction.
Instructions
Call a function of a contract as a raw function call action. This tool creates a function call as a transaction which costs gas and NEAR.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| accountId | Yes | The account id of the signer. | |
| contractAccountId | 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. | |
| gas | No | The amount of gas to use for the function call in yoctoNEAR (default to 30TGas). | |
| attachedDeposit | No | The amount to attach to the function call (default to 1 yoctoNEAR). Can be specified as a number (in NEAR) or as a bigint (in yoctoNEAR). |
Implementation Reference
- src/services.ts:2055-2149 (registration)Registration of the 'contract_call_raw_function' MCP tool using mcp.tool(), defining its name, description, and input schema.
mcp.tool( 'contract_call_raw_function', noLeadingWhitespace` Call a function of a contract as a raw function call action. This tool creates a function call as a transaction which costs gas and NEAR.`, { accountId: z.string().describe('The account id of the signer.'), contractAccountId: 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.'), gas: z .bigint() .optional() .describe( 'The amount of gas to use for the function call in yoctoNEAR (default to 30TGas).', ), attachedDeposit: z .union([ z.number().describe('The amount of NEAR tokens (in NEAR)'), z.bigint().describe('The amount in yoctoNEAR'), ]) .default(NearToken.parse_yocto_near('1').as_near()) .describe( 'The amount to attach to the function call (default to 1 yoctoNEAR). Can be specified as a number (in NEAR) or as a bigint (in yoctoNEAR).', ), }, async (args, _) => { const connection = await connect({ networkId: args.networkId, keyStore: keystore, nodeUrl: getEndpointsByNetwork(args.networkId)[0]!, }); const contractAccountResult: Result<Account, Error> = await getAccount( args.contractAccountId, connection, ); if (!contractAccountResult.ok) { return { content: [ { type: 'text', text: `Error: ${contractAccountResult.error}`, }, ], }; } const contractAccount = contractAccountResult.value; const functionCallResult: Result<unknown, Error> = await (async () => { try { const deposit = typeof args.attachedDeposit === 'number' ? NearToken.parse_near( args.attachedDeposit.toString(), ).as_yocto_near() : args.attachedDeposit; const signerAccount = await connection.account(args.accountId); return { ok: true, value: await signerAccount.functionCall({ contractId: contractAccount.accountId, methodName: args.methodName, args: args.args, gas: args.gas || DEFAULT_GAS, attachedDeposit: deposit, }), }; } catch (e) { return { ok: false, error: new Error(e as string) }; } })(); if (!functionCallResult.ok) { return { content: [ { type: 'text', text: `Error: ${functionCallResult.error}`, }, ], }; } return { content: [ { type: 'text', text: `Function call result: ${stringify_bigint(functionCallResult.value)}`, }, ], }; }, ); - src/services.ts:2060-2083 (schema)Input schema definition using Zod for the contract_call_raw_function tool, specifying accountId, contractAccountId, methodName, networkId, args, gas, and attachedDeposit.
{ accountId: z.string().describe('The account id of the signer.'), contractAccountId: 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.'), gas: z .bigint() .optional() .describe( 'The amount of gas to use for the function call in yoctoNEAR (default to 30TGas).', ), attachedDeposit: z .union([ z.number().describe('The amount of NEAR tokens (in NEAR)'), z.bigint().describe('The amount in yoctoNEAR'), ]) .default(NearToken.parse_yocto_near('1').as_near()) .describe( 'The amount to attach to the function call (default to 1 yoctoNEAR). Can be specified as a number (in NEAR) or as a bigint (in yoctoNEAR).', ), }, - src/services.ts:2084-2149 (handler)Handler function for contract_call_raw_function. Connects to NEAR, verifies the contract account, converts deposit to yoctoNEAR if needed, and executes the function call via signerAccount.functionCall().
async (args, _) => { const connection = await connect({ networkId: args.networkId, keyStore: keystore, nodeUrl: getEndpointsByNetwork(args.networkId)[0]!, }); const contractAccountResult: Result<Account, Error> = await getAccount( args.contractAccountId, connection, ); if (!contractAccountResult.ok) { return { content: [ { type: 'text', text: `Error: ${contractAccountResult.error}`, }, ], }; } const contractAccount = contractAccountResult.value; const functionCallResult: Result<unknown, Error> = await (async () => { try { const deposit = typeof args.attachedDeposit === 'number' ? NearToken.parse_near( args.attachedDeposit.toString(), ).as_yocto_near() : args.attachedDeposit; const signerAccount = await connection.account(args.accountId); return { ok: true, value: await signerAccount.functionCall({ contractId: contractAccount.accountId, methodName: args.methodName, args: args.args, gas: args.gas || DEFAULT_GAS, attachedDeposit: deposit, }), }; } catch (e) { return { ok: false, error: new Error(e as string) }; } })(); if (!functionCallResult.ok) { return { content: [ { type: 'text', text: `Error: ${functionCallResult.error}`, }, ], }; } return { content: [ { type: 'text', text: `Function call result: ${stringify_bigint(functionCallResult.value)}`, }, ], }; }, ); - src/utils.ts:6-6 (helper)DEFAULT_GAS constant used as the default gas value for function calls, imported and used in the contract_call_raw_function handler.
export const DEFAULT_GAS = DEFAULT_FUNCTION_CALL_GAS * BigInt(10);