Skip to main content
Glama

switch-chain

Switch blockchain networks in MetaMask by specifying chain ID or adding new chains with required parameters for Ethereum wallet operations.

Instructions

Switch the target chain.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
chainIdYesID of chain to switch to.
addEthereumChainParameterNoAdd not configured chains to Ethereum wallets.

Implementation Reference

  • The execute handler that switches to the specified chainId using wagmi's switchChain function. Optionally adds chain configuration if provided. Updates the internal chains state and returns the result as a JSON string.
    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), }, ], }; },
  • Zod input schema defining required chainId and optional addEthereumChainParameter for configuring 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."), }),
  • The registration function that adds the "switch-chain" tool to the FastMCP server, including 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), }, ], }; }, }); };
  • Invocation of the switch-chain registration as part of the overall tools registration.
    registerSwitchChainTools(server, wagmiConfig);
  • src/index.ts:15-15 (registration)
    Top-level call to register all MCP tools, which includes switch-chain.
    registerTools(server, wagmiConfig);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Xiawpohr/metamask-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server