Skip to main content
Glama

getBlockInfo

Retrieve detailed blockchain information including timestamp, transaction count, gas usage, and validator for any block number or the latest block across multiple EVM chains.

Instructions

블록 번호 또는 'latest'로 블록 상세 정보(타임스탬프, 트랜잭션 수, gas 사용량, 검증자)를 조회합니다

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
blockNumberNo블록 번호 또는 'latest'latest
chainNoEVM 체인ethereum

Implementation Reference

  • The handler function for 'getBlockInfo' tool, which fetches block data from RPC, processes it, and returns the formatted response.
    async function handler(args: z.infer<typeof inputSchema>): Promise<ToolResult<BlockInfoData>> {
      const { blockNumber, chain } = args;
    
      // "latest"가 아닌 경우 숫자 검증
      const isLatest = blockNumber === "latest";
      let blockNum: bigint | undefined;
      if (!isLatest) {
        const parsed = Number(blockNumber);
        if (!Number.isInteger(parsed) || parsed < 0) {
          return makeError(`Invalid block number: ${blockNumber}`, "INVALID_INPUT");
        }
        blockNum = BigInt(parsed);
      }
    
      // 특정 블록 번호인 경우 캐시 확인
      const cacheKey = `block:${chain}:${blockNumber}`;
      const cached = cache.get<BlockInfoData>(cacheKey);
      if (cached.hit) return makeSuccess(chain, cached.data, true);
    
      try {
        const client = getClient(chain);
    
        const block = isLatest
          ? await client.getBlock({ blockTag: "latest" })
          : await client.getBlock({ blockNumber: blockNum });
    
        const data: BlockInfoData = {
          chain,
          blockNumber: Number(block.number),
          timestamp: Number(block.timestamp),
          datetime: new Date(Number(block.timestamp) * 1000).toISOString(),
          hash: block.hash ?? "",
          parentHash: block.parentHash,
          gasUsed: block.gasUsed.toString(),
          gasLimit: block.gasLimit.toString(),
          baseFeePerGas: block.baseFeePerGas != null ? formatGwei(block.baseFeePerGas) : null,
          transactionCount: block.transactions.length,
          miner: block.miner ?? "",
        };
    
        // latest 요청은 짧은 TTL, 특정 블록은 긴 TTL
        const ttl = isLatest ? LATEST_BLOCK_TTL : CONFIRMED_BLOCK_TTL;
        cache.set(cacheKey, data, ttl);
    
        return makeSuccess(chain, data, false);
      } catch (err) {
        const message = sanitizeError(err);
        return makeError(`Failed to fetch block info: ${message}`, "RPC_ERROR");
      }
    }
  • Input schema validation for the getBlockInfo tool.
    const inputSchema = z.object({
      blockNumber: z
        .string()
        .default("latest")
        .describe("블록 번호 또는 'latest'"),
      chain: z.enum(SUPPORTED_CHAINS).default("ethereum").describe("EVM 체인"),
    });
  • Tool registration function for 'getBlockInfo'.
    export function register(server: McpServer) {
      server.tool(
        "getBlockInfo",
        "블록 번호 또는 'latest'로 블록 상세 정보(타임스탬프, 트랜잭션 수, gas 사용량, 검증자)를 조회합니다",
        inputSchema.shape,
        async (args) => {
          const result = await handler(args as z.infer<typeof inputSchema>);
          return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
        },
      );
    }

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/calintzy/evmscope'

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