account_export_account
Export a NEAR account from the local keystore to a specified file path, enabling easy backup or transfer. Supports testnet and mainnet.
Instructions
Export a NEAR account from the local keystore to a file.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| accountId | Yes | ||
| filePath | No | The path to the file to write the account to. If not provided, the account will be written to the current working directory. | |
| networkId | No | mainnet |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"accountId": {
"type": "string"
},
"filePath": {
"description": "The path to the file to write the account to. If not provided, the account will be written to the current working directory.",
"type": "string"
},
"networkId": {
"default": "mainnet",
"enum": [
"testnet",
"mainnet"
],
"type": "string"
}
},
"required": [
"accountId"
],
"type": "object"
}
Implementation Reference
- src/services.ts:872-930 (handler)Handler function for 'account_export_account' tool. Retrieves the account and keypair from keystore, constructs a JSON payload with account_id, public_key, and private_key, and writes it to a file (default: <accountId>.<networkId>.json). Returns success message or error.async (args, _) => { const connection = await connect({ networkId: args.networkId, 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 keypairResult: Result<KeyPair, Error> = await getAccountKeyPair( args.accountId, args.networkId, keystore, ); if (!keypairResult.ok) { return { content: [{ type: 'text', text: `Error: ${keypairResult.error}` }], }; } const keypair = keypairResult.value; const writeKeyFileResult: Result<void, Error> = await (async () => { try { const filePayload = { account_id: args.accountId, public_key: keypair.getPublicKey().toString(), private_key: keypair.toString(), }; const filePath = args.filePath || `${args.accountId}.${args.networkId}.json`; await writeFile(filePath, JSON.stringify(filePayload, null, 2)); return { ok: true, value: undefined }; } catch (e) { return { ok: false, error: new Error(e as string) }; } })(); if (!writeKeyFileResult.ok) { return { content: [ { type: 'text', text: `Error: ${writeKeyFileResult.error}` }, ], }; } return { content: [ { type: 'text', text: `Account ${args.accountId} exported to ${args.filePath}`, }, ], }; },
- src/services.ts:863-871 (schema)Input schema using Zod: accountId (string, required), networkId (enum testnet/mainnet, default mainnet), filePath (optional string).accountId: z.string(), networkId: z.enum(['testnet', 'mainnet']).default('mainnet'), filePath: z .string() .optional() .describe( 'The path to the file to write the account to. If not provided, the account will be written to the current working directory.', ), },
- src/services.ts:859-931 (registration)MCP tool registration for 'account_export_account': provides description, input schema, and references the handler function.'account_export_account', noLeadingWhitespace` Export a NEAR account from the local keystore to a file.`, { accountId: z.string(), networkId: z.enum(['testnet', 'mainnet']).default('mainnet'), filePath: z .string() .optional() .describe( 'The path to the file to write the account to. If not provided, the account will be written to the current working directory.', ), }, async (args, _) => { const connection = await connect({ networkId: args.networkId, 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 keypairResult: Result<KeyPair, Error> = await getAccountKeyPair( args.accountId, args.networkId, keystore, ); if (!keypairResult.ok) { return { content: [{ type: 'text', text: `Error: ${keypairResult.error}` }], }; } const keypair = keypairResult.value; const writeKeyFileResult: Result<void, Error> = await (async () => { try { const filePayload = { account_id: args.accountId, public_key: keypair.getPublicKey().toString(), private_key: keypair.toString(), }; const filePath = args.filePath || `${args.accountId}.${args.networkId}.json`; await writeFile(filePath, JSON.stringify(filePayload, null, 2)); return { ok: true, value: undefined }; } catch (e) { return { ok: false, error: new Error(e as string) }; } })(); if (!writeKeyFileResult.ok) { return { content: [ { type: 'text', text: `Error: ${writeKeyFileResult.error}` }, ], }; } return { content: [ { type: 'text', text: `Account ${args.accountId} exported to ${args.filePath}`, }, ], }; }, );