|
| 1 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js" |
| 2 | +import type { Hash } from "viem" |
| 3 | +import { z } from "zod" |
| 4 | + |
| 5 | +import * as services from "@/evm/services/index.js" |
| 6 | +import { defaultNetworkParam } from "../common/types" |
| 7 | + |
| 8 | +export function registerBlockTools(server: McpServer) { |
| 9 | + // Get block by hash for a specific network |
| 10 | + server.tool( |
| 11 | + "get_block_by_hash", |
| 12 | + "Get a block by hash", |
| 13 | + { |
| 14 | + blockHash: z.string().describe("The block hash to look up"), |
| 15 | + network: defaultNetworkParam |
| 16 | + }, |
| 17 | + async ({ network, blockHash }) => { |
| 18 | + try { |
| 19 | + const block = await services.getBlockByHash(blockHash as Hash, network) |
| 20 | + return { |
| 21 | + content: [ |
| 22 | + { |
| 23 | + type: "text", |
| 24 | + text: services.helpers.formatJson(block) |
| 25 | + } |
| 26 | + ] |
| 27 | + } |
| 28 | + } catch (error) { |
| 29 | + return { |
| 30 | + content: [ |
| 31 | + { |
| 32 | + type: "text", |
| 33 | + text: `Error fetching block with hash: ${error instanceof Error ? error.message : String(error)}` |
| 34 | + } |
| 35 | + ] |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | + // Get block by number for a specific network |
| 42 | + server.tool( |
| 43 | + "get_block_by_number", |
| 44 | + "Get a block by number", |
| 45 | + { |
| 46 | + blockNumber: z.string().describe("The block number to look up"), |
| 47 | + network: defaultNetworkParam |
| 48 | + }, |
| 49 | + async ({ network, blockNumber }) => { |
| 50 | + try { |
| 51 | + const block = await services.getBlockByNumber( |
| 52 | + parseInt(blockNumber), |
| 53 | + network |
| 54 | + ) |
| 55 | + return { |
| 56 | + content: [ |
| 57 | + { |
| 58 | + type: "text", |
| 59 | + text: services.helpers.formatJson(block) |
| 60 | + } |
| 61 | + ] |
| 62 | + } |
| 63 | + } catch (error) { |
| 64 | + return { |
| 65 | + content: [ |
| 66 | + { |
| 67 | + type: "text", |
| 68 | + text: `Error fetching block: ${error instanceof Error ? error.message : String(error)}` |
| 69 | + } |
| 70 | + ] |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + // Get latest block for a specific network |
| 77 | + server.tool( |
| 78 | + "get_latest_block", |
| 79 | + "Get the latest block", |
| 80 | + { |
| 81 | + network: defaultNetworkParam |
| 82 | + }, |
| 83 | + async ({ network }) => { |
| 84 | + try { |
| 85 | + const block = await services.getLatestBlock(network) |
| 86 | + return { |
| 87 | + content: [ |
| 88 | + { |
| 89 | + type: "text", |
| 90 | + text: services.helpers.formatJson(block) |
| 91 | + } |
| 92 | + ] |
| 93 | + } |
| 94 | + } catch (error) { |
| 95 | + return { |
| 96 | + content: [ |
| 97 | + { |
| 98 | + type: "text", |
| 99 | + text: `Error fetching latest block: ${error instanceof Error ? error.message : String(error)}` |
| 100 | + } |
| 101 | + ] |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + ) |
| 106 | +} |
0 commit comments