get_latest_block
Retrieve the most recent Bitcoin block data using the Bitcoin MCP Server, enabling efficient access to blockchain information for analysis or integration.
Instructions
Get the latest block
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/server/tools.ts:142-152 (handler)The handler function that executes the get_latest_block tool. It fetches the latest block via BitcoinService and returns formatted text content.export async function handleGetLatestBlock(bitcoinService: BitcoinService) { const block = await bitcoinService.getLatestBlock(); return { content: [ { type: "text", text: `Latest block:\nHash: ${block.hash}\nHeight: ${block.height}\nTimestamp: ${block.timestamp}\nTransactions: ${block.txCount}`, }, ] as TextContent[], }; }
- src/server/base.ts:147-150 (registration)Tool registration in the listToolsRequestSchema handler, defining the tool name, description, and empty input schema (no parameters required).name: "get_latest_block", description: "Get the latest block", inputSchema: { type: "object", properties: {} }, } as Tool,
- src/server/base.ts:209-211 (registration)Tool handling registration in the CallToolRequestSchema switch statement, dispatching to the handleGetLatestBlock function.case "get_latest_block": { return handleGetLatestBlock(this.bitcoinService); }
- src/services/bitcoin.ts:173-202 (helper)Supporting helper method in BitcoinService that fetches the latest block hash and details from Blockstream API and returns BlockInfo.async getLatestBlock(): Promise<BlockInfo> { try { const hashRes = await fetch(`${this.apiBase}/blocks/tip/hash`); if (!hashRes.ok) { throw new Error("Failed to fetch latest block hash"); } const hash = await hashRes.text(); const blockRes = await fetch(`${this.apiBase}/block/${hash}`); if (!blockRes.ok) { throw new Error("Failed to fetch block data"); } const block = (await blockRes.json()) as BlockstreamBlock; return { hash: block.id, height: block.height, timestamp: block.timestamp, txCount: block.tx_count, size: block.size, weight: block.weight, }; } catch (error) { logger.error({ error }, "Failed to fetch latest block"); throw new BitcoinError( "Failed to fetch latest block", BitcoinErrorCode.BLOCKCHAIN_ERROR ); } }