switch-chain
Change blockchain networks in MetaMask for MCPilot server, enabling AI to interact with different chains while maintaining wallet security.
Instructions
Switch the target chain
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"chainId": "<chainId>"
},
"name": "switch-chain"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chainId | Yes | ||
| addEthereumChainParameter | No |
Implementation Reference
- The main handler function that executes the chain switch using the wagmi switchChain function. It takes chainId and optional addEthereumChainParameter, calls switchChain, and returns the result as a text content block.
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 a new 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() }), - packages/metamask-mcp/src/tools/switch-chain.ts:7-42 (registration)The registration function that adds the switch-chain tool to the FastMCP server, including name, description, schema, and 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)Top-level call to register the switch-chain tools during server initialization.
registerSwitchChainTools(server);