eth_chainId
Identify the current blockchain network by retrieving its unique chain ID, enabling proper network verification and compatibility checks for EVM-based applications.
Instructions
Returns the chain ID of the current network
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:797-846 (handler)Full implementation of the 'eth_chainId' MCP tool handler. Registers the tool with no input parameters and provides an async handler that calls the 'eth_chainId' RPC method via makeRPCCall, parses the hex result to decimal, maps it to a known chain name, and returns a formatted text response.server.tool( "eth_chainId", "Returns the chain ID of the current network", {}, async () => { try { const result = await makeRPCCall("eth_chainId"); const chainId = parseInt(result, 16); const chainNames: { [key: number]: string } = { 1: "Ethereum Mainnet", 11155111: "Sepolia Testnet", 5: "Goerli Testnet", 137: "Polygon Mainnet", 80001: "Polygon Mumbai Testnet", 42161: "Arbitrum One", 421614: "Arbitrum Sepolia", 10: "Optimism", 420: "Optimism Sepolia", 56: "BNB Smart Chain", 97: "BNB Smart Chain Testnet", }; return { content: [ { type: "text", text: formatResponse( { chain_id_hex: result, chain_id_decimal: chainId, chain_name: chainNames[chainId] || "Unknown Network", }, "Network Chain ID", ), }, ], }; } catch (error: any) { return { content: [ { type: "text", text: `Error: ${error.message}`, }, ], }; } }, );
- src/index.ts:89-96 (helper)Generic helper function used by eth_chainId (and other tools) to perform RPC calls to the Ethers provider.async function makeRPCCall(method: string, params: any[] = []): Promise<any> { try { const result = await provider.send(method, params); return result; } catch (error: any) { throw new Error(`RPC call failed: ${error.message}`); } }