switch-chain
Change the active blockchain network in MetaMask by specifying the desired chain ID or adding new chains with required parameters for Ethereum wallet integration.
Instructions
Switch the target chain.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| addEthereumChainParameter | No | Add not configured chains to Ethereum wallets. | |
| chainId | Yes | ID of chain to switch to. |
Implementation Reference
- src/tools/switch-chain.ts:25-44 (handler)The execute handler for the switch-chain tool. It calls wagmi's switchChain function with the provided chainId and optional addEthereumChainParameter, updates the chains state, and returns the result as text content.execute: async (args) => { const chainId = args.chainId as typeof wagmiConfig["chains"][number]["id"]; const addEthereumChainParameter = args.addEthereumChainParameter; const result = await switchChain(wagmiConfig, { chainId, addEthereumChainParameter, }); wagmiConfig._internal.chains.setState(x => [...x, result]); return { content: [ { type: "text", text: JSONStringify(result), }, ], }; },
- src/tools/switch-chain.ts:11-24 (schema)Zod schema defining the input parameters for the switch-chain tool: chainId (number) and optional addEthereumChainParameter object for adding new chains.parameters: z.object({ chainId: z.coerce.number().describe("ID of chain to switch to."), addEthereumChainParameter: z.object({ chainName: z.string(), nativeCurrency: z.object({ name: z.string(), symbol: z.string(), decimals: z.coerce.number(), }), rpcUrls: z.string().array().min(1), blockExplorerUrls: z.string().array().optional(), iconUrls: z.string().array().optional(), }).optional().describe("Add not configured chains to Ethereum wallets."), }),
- src/tools/switch-chain.ts:7-46 (registration)The registerSwitchChainTools function that registers the 'switch-chain' tool on the FastMCP server using server.addTool, including name, description, schema, and handler.export function registerSwitchChainTools(server: FastMCP, wagmiConfig: Config): void { server.addTool({ name: "switch-chain", description: "Switch the target chain.", parameters: z.object({ chainId: z.coerce.number().describe("ID of chain to switch to."), addEthereumChainParameter: z.object({ chainName: z.string(), nativeCurrency: z.object({ name: z.string(), symbol: z.string(), decimals: z.coerce.number(), }), rpcUrls: z.string().array().min(1), blockExplorerUrls: z.string().array().optional(), iconUrls: z.string().array().optional(), }).optional().describe("Add not configured chains to Ethereum wallets."), }), execute: async (args) => { const chainId = args.chainId as typeof wagmiConfig["chains"][number]["id"]; const addEthereumChainParameter = args.addEthereumChainParameter; const result = await switchChain(wagmiConfig, { chainId, addEthereumChainParameter, }); wagmiConfig._internal.chains.setState(x => [...x, result]); return { content: [ { type: "text", text: JSONStringify(result), }, ], }; }, }); };
- src/tools/register-tools.ts:54-54 (registration)Call to registerSwitchChainTools within the central registerTools function, which is invoked during server setup.registerSwitchChainTools(server, wagmiConfig);