generateWallet
Create a new Ethereum wallet with a private key, optionally saving it to environment variables for secure access in blockchain operations.
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "generateWallet"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| saveToEnv | No | Optional. If true, the private key will be saved to the server's environment variables for future use. Default is false. |
Implementation Reference
- src/tools/core.ts:136-176 (registration)Full registration of the generateWallet MCP tool, including Zod input schema, async handler function that creates a random Ethereum wallet using ethers.Wallet.createRandom(), optionally saves private key to env, and returns address and private key.
server.tool( "generateWallet", { saveToEnv: z.boolean().optional().describe( "Optional. If true, the private key will be saved to the server's environment variables for future use. Default is false." ) }, async ({ saveToEnv = false }) => { try { const wallet = ethers.Wallet.createRandom(); if (saveToEnv) { process.env.WALLET_PRIVATE_KEY = wallet.privateKey; // Update the ethersService with the new wallet const signer = new ethers.Wallet(wallet.privateKey, ethersService.provider); ethersService.setSigner(signer); } return { content: [{ type: "text", text: ` New wallet generated: Address: ${wallet.address} Private Key: ${wallet.privateKey} ${saveToEnv ? "Private key has been saved to environment variables for this session." : ""} ` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error generating wallet: ${error instanceof Error ? error.message : String(error)}` }] }; } } ); - src/tools/core.ts:139-141 (schema)Zod schema for generateWallet tool input: optional boolean saveToEnv.
saveToEnv: z.boolean().optional().describe( "Optional. If true, the private key will be saved to the server's environment variables for future use. Default is false." ) - src/tools/core.ts:143-175 (handler)Handler function for generateWallet tool: generates random wallet, optionally persists to env and updates service, returns formatted response.
async ({ saveToEnv = false }) => { try { const wallet = ethers.Wallet.createRandom(); if (saveToEnv) { process.env.WALLET_PRIVATE_KEY = wallet.privateKey; // Update the ethersService with the new wallet const signer = new ethers.Wallet(wallet.privateKey, ethersService.provider); ethersService.setSigner(signer); } return { content: [{ type: "text", text: ` New wallet generated: Address: ${wallet.address} Private Key: ${wallet.privateKey} ${saveToEnv ? "Private key has been saved to environment variables for this session." : ""} ` }] }; } catch (error) { return { isError: true, content: [{ type: "text", text: `Error generating wallet: ${error instanceof Error ? error.message : String(error)}` }] }; } } - src/tools/index.ts:23-23 (registration)Registration of core tools group (including generateWallet) via registerCoreTools call in top-level tools index.
registerCoreTools(server, ethersService);