switch-chain
Switch the blockchain network in MetaMask by specifying chain ID and parameters like chain name, RPC URLs, and native currency details, enabling seamless AI-powered interactions with MCPilot.
Instructions
Switch the target chain
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| addEthereumChainParameter | No | ||
| chainId | Yes |
Implementation Reference
- The main handler function that executes the chain switch using wagmi's switchChain action. It extracts chainId and addEthereumChainParameter from args, calls switchChain, 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, }) return { content: [ { type: "text", text: JSONStringify(result), }, ], } },
- Zod schema defining the input parameters for the switch-chain tool: required chainId (number) and optional addEthereumChainParameter object for adding new chains.parameters: z.object({ chainId: z.coerce.number(), addEthereumChainParameter: z.object({ chainName: z.string(), nativeCurrency: z.object({ name: z.string(), symbol: z.string(), decimals: z.coerce.number(), }).optional(), rpcUrls: z.string().array(), blockExplorerUrls: z.string().array().optional(), iconUrls: z.string().array().optional(), }).optional() }),
- packages/metamask-mcp/src/tools/switch-chain.ts:7-42 (registration)The registration function registerSwitchChainTools that adds the switch-chain tool to the FastMCP server, including name, description, parameters schema, and execute handler.export function registerSwitchChainTools(server: FastMCP): void { server.addTool({ name: "switch-chain", description: "Switch the target chain", parameters: z.object({ chainId: z.coerce.number(), addEthereumChainParameter: z.object({ chainName: z.string(), nativeCurrency: z.object({ name: z.string(), symbol: z.string(), decimals: z.coerce.number(), }).optional(), rpcUrls: z.string().array(), blockExplorerUrls: z.string().array().optional(), iconUrls: z.string().array().optional(), }).optional() }), execute: async (args) => { const chainId = args.chainId as typeof wagmiConfig['chains'][number]['id'] const addEthereumChainParameter = args.addEthereumChainParameter const result = await switchChain(wagmiConfig, { chainId, addEthereumChainParameter, }) return { content: [ { type: "text", text: JSONStringify(result), }, ], } }, }); };
- packages/metamask-mcp/src/index.ts:57-57 (registration)Invocation of registerSwitchChainTools on the main MCP server instance to register the tool.registerSwitchChainTools(server);