wallet_get_chain_id
Retrieve the chain ID of the Ethereum or EVM-compatible network connected to your wallet, ensuring accurate blockchain interactions and transaction processing.
Instructions
Get the chain ID the wallet is connected to
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": "wallet_get_chain_id"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| wallet | No | The wallet (private key, mnemonic, or JSON). If not provided, uses PRIVATE_KEY environment variable if set. |
Implementation Reference
- src/handlers/wallet.ts:236-253 (handler)The getChainIdHandler function implements the core logic for the wallet_get_chain_id tool. It retrieves the wallet, checks for a provider, fetches the chain ID using wallet.getChainId(), and returns a formatted success response.
export const getChainIdHandler = async (input: any): Promise<ToolResultSchema> => { try { const wallet = await getWallet(input.wallet, input.password); if (!wallet.provider) { return createErrorResponse("Provider is required to get chain ID, please set the provider URL"); } const chainId = await wallet.getChainId(); return createSuccessResponse( `Chain ID retrieved successfully Chain ID: ${chainId.toString()} `); } catch (error) { return createErrorResponse(`Failed to get chain ID: ${(error as Error).message}`); } }; - src/tools.ts:188-197 (schema)The input schema definition for the wallet_get_chain_id tool, specifying an optional wallet parameter.
{ name: "wallet_get_chain_id", description: "Get the chain ID the wallet is connected to", inputSchema: { type: "object", properties: { wallet: { type: "string", description: "The wallet (private key, mnemonic, or JSON). If not provided, uses PRIVATE_KEY environment variable if set." } }, required: [] } - src/tools.ts:573-573 (registration)The registration mapping the 'wallet_get_chain_id' tool name to its handler function getChainIdHandler in the handlers dictionary.
"wallet_get_chain_id": getChainIdHandler, - src/tools.ts:188-197 (registration)The tool definition in the tools array, registering the name, description, and schema for wallet_get_chain_id.
{ name: "wallet_get_chain_id", description: "Get the chain ID the wallet is connected to", inputSchema: { type: "object", properties: { wallet: { type: "string", description: "The wallet (private key, mnemonic, or JSON). If not provided, uses PRIVATE_KEY environment variable if set." } }, required: [] } - src/handlers/wallet.ts:1-3 (helper)Imports for getWallet and getProvider helpers used in the handler.
import { ethers, providers } from "ethers"; import { ToolResultSchema } from "../types.js"; import { createSuccessResponse, createErrorResponse, getProvider, getWallet, setProvider } from "./utils.js";