net_version
Identify the current network ID for EVM-compatible blockchains like Ethereum, Polygon, and Arbitrum to verify which blockchain network you're connected to.
Instructions
Returns the current network id
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:848-875 (handler)Inline asynchronous handler function for the 'net_version' MCP tool. It calls makeRPCCall('net_version') to fetch the network ID via RPC, formats the result using formatResponse, and returns it in the expected MCP content structure. Handles errors by returning an error message.server.tool("net_version", "Returns the current network id", {}, async () => { try { const result = await makeRPCCall("net_version"); return { content: [ { type: "text", text: formatResponse( { network_id: result, }, "Network Version", ), }, ], }; } catch (error: any) { return { content: [ { type: "text", text: `Error: ${error.message}`, }, ], }; } });
- src/index.ts:848-875 (registration)Registration of the 'net_version' tool on the MCP server using server.tool(). Includes tool name, description 'Returns the current network id', empty input schema {}, and the inline handler function.server.tool("net_version", "Returns the current network id", {}, async () => { try { const result = await makeRPCCall("net_version"); return { content: [ { type: "text", text: formatResponse( { network_id: result, }, "Network Version", ), }, ], }; } catch (error: any) { return { content: [ { type: "text", text: `Error: ${error.message}`, }, ], }; } });
- src/index.ts:89-96 (helper)makeRPCCall utility function that wraps ethers.JsonRpcProvider.send() to perform RPC calls. Directly used in the net_version handler to invoke the 'net_version' RPC method.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}`); } }
- src/index.ts:32-57 (helper)formatResponse utility function that converts data objects into formatted Markdown strings for tool responses. Used in net_version handler to format {network_id: result} with title 'Network Version'.function formatResponse(data: any, title: string): string { let result = `**${title}**\n\n`; if (typeof data === "object" && data !== null) { if (Array.isArray(data)) { // Handle arrays result += `**Count:** ${data.length}\n\n`; data.forEach((item, index) => { result += `**${index + 1}.**\n`; if (typeof item === "object" && item !== null) { result += formatObject(item, " "); } else { result += ` ${item}\n`; } result += "\n"; }); } else { // Handle objects result += formatObject(data, ""); } } else { result += `${data}\n`; } return result; }