@sanlim/mempool-mcp-server
Version:
A sample of MCP implementation using DDD structure with some APIs call.
74 lines (73 loc) • 3.33 kB
JavaScript
import { z } from "zod";
import { BaseToolsController } from "./base/BaseToolsController.js";
export class BlocksToolsController extends BaseToolsController {
blocksService;
constructor(server, blocksService) {
super(server);
this.blocksService = blocksService;
}
registerTools() {
this.registerGetBlocksHandler();
this.registerGetBlockTxidsHandler();
this.registerGetBlockTxsHandler();
this.registerGetBlockStatusHandler();
this.registerGetBlockRawHandler();
this.registerGetBlockTxidByIndexHandler();
this.registerGetBlockHeaderHandler();
}
registerGetBlocksHandler() {
this.server.tool("get-blocks", "Returns the latest blocks", {}, async () => {
const text = await this.blocksService.getBlocks();
return { content: [{ type: "text", text }] };
});
}
registerGetBlockTxidsHandler() {
this.server.tool("get-block-txids", "Returns txids for a block", {
hash: z.string().length(64).describe("The block hash to get txids for"),
}, async ({ hash }) => {
const text = await this.blocksService.getBlockTxids({ hash });
return { content: [{ type: "text", text }] };
});
}
registerGetBlockTxsHandler() {
this.server.tool("get-block-txs", "Returns transactions for a block", {
hash: z.string().length(64).describe("The block hash to get txs for"),
}, async ({ hash }) => {
const text = await this.blocksService.getBlockTxs({ hash });
return { content: [{ type: "text", text }] };
});
}
registerGetBlockStatusHandler() {
this.server.tool("get-block-status", "Returns status for a block", {
hash: z.string().length(64).describe("The block hash to get status for"),
}, async ({ hash }) => {
const text = await this.blocksService.getBlockStatus({ hash });
return { content: [{ type: "text", text }] };
});
}
registerGetBlockRawHandler() {
this.server.tool("get-block-raw", "Returns raw hex for a block", {
hash: z.string().length(64).describe("The block hash to get raw hex for"),
}, async ({ hash }) => {
const text = await this.blocksService.getBlockRaw({ hash });
return { content: [{ type: "text", text }] };
});
}
registerGetBlockTxidByIndexHandler() {
this.server.tool("get-block-txid-by-index", "Returns txid for a block at a specific index", {
hash: z.string().length(64).describe("The block hash to get txid for"),
index: z.number().int().describe("The index of the txid in the block"),
}, async ({ hash, index }) => {
const text = await this.blocksService.getBlockTxidByIndex({ hash, index });
return { content: [{ type: "text", text }] };
});
}
registerGetBlockHeaderHandler() {
this.server.tool("get-block-header", "Returns the block header in hex", {
hash: z.string().length(64).describe("The block hash to get header for"),
}, async ({ hash }) => {
const text = await this.blocksService.getBlockHeader({ hash });
return { content: [{ type: "text", text }] };
});
}
}