Skip to main content
Glama

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
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • 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}`,
            },
          ],
        };
      }
    });
  • 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}`);
      }
    }
  • 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;
    }

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/JamesANZ/evm-mcp'

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