Skip to main content
Glama

eth_getBlockByNumber

Retrieve detailed block information from EVM-compatible blockchains using block number or special tags like 'latest'. Get transaction data, timestamps, and other block metadata for blockchain analysis.

Instructions

Returns information about a block by block number

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
blockNumberYesBlock number (hex) or 'latest', 'earliest', 'pending'
includeTransactionsNoInclude full transaction objects

Implementation Reference

  • The async handler function that performs the RPC call to eth_getBlockByNumber, processes the block information, and formats the response using helpers.
    async ({ blockNumber, includeTransactions }) => {
      try {
        const result = await makeRPCCall("eth_getBlockByNumber", [
          blockNumber,
          includeTransactions,
        ]);
        if (!result) {
          return {
            content: [
              {
                type: "text",
                text: `Block not found: ${blockNumber}`,
              },
            ],
          };
        }
    
        const blockInfo = {
          number: result.number,
          hash: result.hash,
          parentHash: result.parentHash,
          timestamp: result.timestamp,
          gasLimit: result.gasLimit,
          gasUsed: result.gasUsed,
          transactionCount: result.transactions.length,
          baseFeePerGas: result.baseFeePerGas,
        };
    
        return {
          content: [
            {
              type: "text",
              text: formatResponse(blockInfo, "Block Information"),
            },
          ],
        };
      } catch (error: any) {
        return {
          content: [
            {
              type: "text",
              text: `Error: ${error.message}`,
            },
          ],
        };
      }
    },
  • Zod schema defining the input parameters: blockNumber (string) and optional includeTransactions (boolean, default false).
    {
      blockNumber: z
        .string()
        .describe("Block number (hex) or 'latest', 'earliest', 'pending'"),
      includeTransactions: z
        .boolean()
        .optional()
        .default(false)
        .describe("Include full transaction objects"),
    },
  • src/index.ts:288-348 (registration)
    The server.tool registration that defines the tool name, description, schema, and handler function.
    server.tool(
      "eth_getBlockByNumber",
      "Returns information about a block by block number",
      {
        blockNumber: z
          .string()
          .describe("Block number (hex) or 'latest', 'earliest', 'pending'"),
        includeTransactions: z
          .boolean()
          .optional()
          .default(false)
          .describe("Include full transaction objects"),
      },
      async ({ blockNumber, includeTransactions }) => {
        try {
          const result = await makeRPCCall("eth_getBlockByNumber", [
            blockNumber,
            includeTransactions,
          ]);
          if (!result) {
            return {
              content: [
                {
                  type: "text",
                  text: `Block not found: ${blockNumber}`,
                },
              ],
            };
          }
    
          const blockInfo = {
            number: result.number,
            hash: result.hash,
            parentHash: result.parentHash,
            timestamp: result.timestamp,
            gasLimit: result.gasLimit,
            gasUsed: result.gasUsed,
            transactionCount: result.transactions.length,
            baseFeePerGas: result.baseFeePerGas,
          };
    
          return {
            content: [
              {
                type: "text",
                text: formatResponse(blockInfo, "Block Information"),
              },
            ],
          };
        } catch (error: any) {
          return {
            content: [
              {
                type: "text",
                text: `Error: ${error.message}`,
              },
            ],
          };
        }
      },
    );
  • Generic helper function used to make RPC calls to the Ethereum provider, invoked within the handler.
    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}`);
      }
    }
  • Helper function to format the response data into a markdown-friendly string, used in the handler.
    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