Skip to main content
Glama

account_add_access_key

Add an access key to a NEAR account to grant full or function-specific contract access. Specify account ID, network, and key permissions for secure account management.

Instructions

Add an access key to an account. This can be used to grant full access to an account, or allow the specified account to have specific function call access to a contract.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
accessKeyArgsYes
accountIdYes
networkIdNomainnet

Implementation Reference

  • Registration of the 'account_add_access_key' MCP tool, including name, description, input schema using Zod, and the handler function.
      'account_add_access_key',
      noLeadingWhitespace`
      Add an access key to an account. This can be used to grant full access to an account,
      or allow the specified account to have specific function call access to a contract.`,
      {
        accountId: z.string(),
        networkId: z.enum(['testnet', 'mainnet']).default('mainnet'),
        accessKeyArgs: z.object({
          permission: z.union([
            z.object({
              type: z.literal('FullAccess'),
              publicKey: z.string().describe('The public key of the access key.'),
            }),
            z.object({
              type: z.literal('FunctionCall'),
              publicKey: z.string().describe('The public key of the access key.'),
              FunctionCall: z.object({
                contractId: z.string(),
                allowance: 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 allowance of the function call access key.'),
                methodNames: z.array(z.string()),
              }),
            }),
          ]),
        }),
      },
      async (args, _) => {
        const connection = await connect({
          networkId: args.networkId,
          keyStore: keystore,
          nodeUrl: getEndpointsByNetwork(args.networkId)[0]!,
        });
    
        const accountResult: Result<Account, Error> = await getAccount(
          args.accountId,
          connection,
        );
        if (!accountResult.ok) {
          return {
            content: [{ type: 'text', text: `Error: ${accountResult.error}` }],
          };
        }
        const account = accountResult.value;
    
        const addAccessKeyResult: Result<FinalExecutionOutcome, Error> =
          await (async () => {
            try {
              switch (args.accessKeyArgs.permission.type) {
                case 'FullAccess':
                  return {
                    ok: true,
                    value: await account.addKey(
                      args.accessKeyArgs.permission.publicKey,
                    ),
                  };
                case 'FunctionCall':
                  const allowance =
                    typeof args.accessKeyArgs.permission.FunctionCall
                      .allowance === 'number'
                      ? NearToken.parse_near(
                          args.accessKeyArgs.permission.FunctionCall.allowance.toString(),
                        ).as_yocto_near()
                      : args.accessKeyArgs.permission.FunctionCall.allowance;
    
                  return {
                    ok: true,
                    value: await account.addKey(
                      args.accessKeyArgs.permission.publicKey,
                      args.accessKeyArgs.permission.FunctionCall.contractId,
                      args.accessKeyArgs.permission.FunctionCall.methodNames,
                      allowance,
                    ),
                  };
              }
            } catch (e) {
              return { ok: false, error: new Error(e as string) };
            }
          })();
        if (!addAccessKeyResult.ok) {
          return {
            content: [
              {
                type: 'text',
                text: `Error: ${addAccessKeyResult.error}\n\nFailed to add access key to account ${args.accountId}`,
              },
            ],
          };
        }
    
        return {
          content: [
            {
              type: 'text',
              text: `Add access key transaction result: ${stringify_bigint({
                final_execution_status:
                  addAccessKeyResult.value.final_execution_status,
                status: addAccessKeyResult.value.status,
                transaction_outcome: addAccessKeyResult.value.transaction_outcome,
                receipts_outcome: addAccessKeyResult.value.receipts_outcome,
              })}`,
            },
            {
              type: 'text',
              text: `Access key added: ${args.accessKeyArgs.permission.publicKey}`,
            },
          ],
        };
      },
    );
  • Zod schema defining the input parameters for the tool: accountId, networkId, and accessKeyArgs with permission (FullAccess or FunctionCall).
      accountId: z.string(),
      networkId: z.enum(['testnet', 'mainnet']).default('mainnet'),
      accessKeyArgs: z.object({
        permission: z.union([
          z.object({
            type: z.literal('FullAccess'),
            publicKey: z.string().describe('The public key of the access key.'),
          }),
          z.object({
            type: z.literal('FunctionCall'),
            publicKey: z.string().describe('The public key of the access key.'),
            FunctionCall: z.object({
              contractId: z.string(),
              allowance: 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 allowance of the function call access key.'),
              methodNames: z.array(z.string()),
            }),
          }),
        ]),
      }),
    },
  • The handler function that connects to NEAR, retrieves the account, and calls account.addKey with appropriate parameters for FullAccess or FunctionCall permissions, returning transaction results.
    async (args, _) => {
      const connection = await connect({
        networkId: args.networkId,
        keyStore: keystore,
        nodeUrl: getEndpointsByNetwork(args.networkId)[0]!,
      });
    
      const accountResult: Result<Account, Error> = await getAccount(
        args.accountId,
        connection,
      );
      if (!accountResult.ok) {
        return {
          content: [{ type: 'text', text: `Error: ${accountResult.error}` }],
        };
      }
      const account = accountResult.value;
    
      const addAccessKeyResult: Result<FinalExecutionOutcome, Error> =
        await (async () => {
          try {
            switch (args.accessKeyArgs.permission.type) {
              case 'FullAccess':
                return {
                  ok: true,
                  value: await account.addKey(
                    args.accessKeyArgs.permission.publicKey,
                  ),
                };
              case 'FunctionCall':
                const allowance =
                  typeof args.accessKeyArgs.permission.FunctionCall
                    .allowance === 'number'
                    ? NearToken.parse_near(
                        args.accessKeyArgs.permission.FunctionCall.allowance.toString(),
                      ).as_yocto_near()
                    : args.accessKeyArgs.permission.FunctionCall.allowance;
    
                return {
                  ok: true,
                  value: await account.addKey(
                    args.accessKeyArgs.permission.publicKey,
                    args.accessKeyArgs.permission.FunctionCall.contractId,
                    args.accessKeyArgs.permission.FunctionCall.methodNames,
                    allowance,
                  ),
                };
            }
          } catch (e) {
            return { ok: false, error: new Error(e as string) };
          }
        })();
      if (!addAccessKeyResult.ok) {
        return {
          content: [
            {
              type: 'text',
              text: `Error: ${addAccessKeyResult.error}\n\nFailed to add access key to account ${args.accountId}`,
            },
          ],
        };
      }
    
      return {
        content: [
          {
            type: 'text',
            text: `Add access key transaction result: ${stringify_bigint({
              final_execution_status:
                addAccessKeyResult.value.final_execution_status,
              status: addAccessKeyResult.value.status,
              transaction_outcome: addAccessKeyResult.value.transaction_outcome,
              receipts_outcome: addAccessKeyResult.value.receipts_outcome,
            })}`,
          },
          {
            type: 'text',
            text: `Access key added: ${args.accessKeyArgs.permission.publicKey}`,
          },
        ],
      };
    },
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It states the tool performs an 'Add' operation (implying mutation/write) but doesn't mention required permissions, whether this is reversible (can keys be deleted?), rate limits, or what happens on success/failure. For a mutation tool with zero annotation coverage, this leaves significant behavioral gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately concise with two sentences that directly state the tool's purpose and two use cases. It's front-loaded with the core action and wastes no words on unnecessary details. However, it could be slightly more structured by explicitly separating the two permission types.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given this is a mutation tool with 3 parameters, nested objects, 0% schema description coverage, no annotations, and no output schema, the description is insufficiently complete. It doesn't explain the complex permission structure (FullAccess vs FunctionCall with allowance, contractId, methodNames), doesn't mention the networkId parameter or its default, and provides no information about return values or error conditions.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the description must compensate for undocumented parameters. It mentions 'full access' and 'specific function call access to a contract' which hints at the permission parameter's purpose, but doesn't explain the three parameters (accountId, accessKeyArgs, networkId) or their relationships. The description adds some context but doesn't adequately cover the parameter semantics given the schema's lack of descriptions.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Add an access key') and target resource ('to an account'), providing a specific verb+resource combination. It distinguishes the tool's purpose from siblings like account_list_access_keys (list) and account_delete_access_keys (delete), though it doesn't explicitly differentiate from account_create_account which creates accounts rather than adding keys.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It mentions two use cases (grant full access or specific function call access) but doesn't specify prerequisites, when not to use it, or refer to sibling tools like account_list_access_keys for checking existing keys first. There's no explicit comparison to other account management tools.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/nearai/near-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server