arbitrum-mcp-tools
Version:
A comprehensive collection of Model Context Protocol (MCP) tools for interacting with the Arbitrum blockchain. Enables AI assistants like Claude, Cursor, and Windsurf to perform blockchain operations including account analysis, contract interaction, cross
174 lines (173 loc) • 6.82 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { z } from "zod";
import { formatEther } from "ethers";
import { handleError } from "../common.js";
import { alchemy } from "../../index.js";
export function registerChainDataTools(server) {
// 1. Block Number
server.tool("getBlockNumber", "Get the latest block number on Arbitrum", {}, // no input needed
() => __awaiter(this, void 0, void 0, function* () {
try {
const blockNumber = yield alchemy.core.getBlockNumber();
return {
content: [
{ type: "text", text: `Latest block number: ${blockNumber}` },
],
};
}
catch (error) {
return {
content: [{ type: "text", text: `Error: ${handleError(error)}` }],
};
}
}));
// 2. Block Details
server.tool("getBlock", "Get details of a block by number or hash", {
block: z
.string()
.describe("Block number (as a string), block hash, or one of the following tags: 'latest', 'pending', 'earliest'"),
}, (_a) => __awaiter(this, [_a], void 0, function* ({ block }) {
try {
// Convert decimal block numbers to hex format
let formattedBlock = block;
if (/^\d+$/.test(block)) {
// If block is a pure decimal number, convert to hex
formattedBlock = "0x" + parseInt(block, 10).toString(16);
}
const blockData = yield alchemy.core.getBlock(formattedBlock);
if (!blockData) {
return {
content: [{ type: "text", text: "Block not found." }],
};
}
return {
content: [
{
type: "text",
text: `Block details:\n${JSON.stringify(blockData, null, 2)}`,
},
],
};
}
catch (error) {
return {
content: [{ type: "text", text: `Error: ${handleError(error)}` }],
};
}
}));
// 3. Gas Metrics
server.tool("getGasParameters", "Get detailed Arbitrum gas price metrics", {}, () => __awaiter(this, void 0, void 0, function* () {
try {
const [gasPrice, feeData] = yield Promise.all([
alchemy.core.getGasPrice(),
alchemy.core.getFeeData(),
]);
return {
content: [
{
type: "text",
text: `Current gas metrics:
- Base Fee: ${formatEther(feeData.lastBaseFeePerGas
? feeData.lastBaseFeePerGas.toString()
: "0")} ETH
- Max Priority Fee: ${formatEther(feeData.maxPriorityFeePerGas
? feeData.maxPriorityFeePerGas.toString()
: "0")} ETH
- Max Fee: ${formatEther(feeData.maxFeePerGas ? feeData.maxFeePerGas.toString() : "0")} ETH
- Gas Price: ${formatEther(gasPrice.toString())} ETH`,
},
],
};
}
catch (error) {
return {
content: [{ type: "text", text: `Error: ${handleError(error)}` }],
};
}
}));
// 4. Get Transaction Details
server.tool("getTransaction", "Get details of a transaction by hash", { txHash: z.string().describe("Transaction hash") }, (_a) => __awaiter(this, [_a], void 0, function* ({ txHash }) {
try {
const txData = yield alchemy.core.getTransaction(txHash);
if (!txData) {
return {
content: [
{
type: "text",
text: "Transaction not found or not yet indexed.",
},
],
};
}
return {
content: [
{
type: "text",
text: `Transaction details:\n${JSON.stringify(txData, null, 2)}`,
},
],
};
}
catch (error) {
return {
content: [{ type: "text", text: `Error: ${handleError(error)}` }],
};
}
}));
// 5. Get Transaction Receipt
server.tool("getTransactionReceipt", "Get the transaction receipt for a given transaction hash", { txHash: z.string().describe("Transaction hash") }, (_a) => __awaiter(this, [_a], void 0, function* ({ txHash }) {
try {
const receipt = yield alchemy.core.getTransactionReceipt(txHash);
if (!receipt) {
return {
content: [
{
type: "text",
text: "Transaction receipt not found or not yet indexed.",
},
],
};
}
return {
content: [
{
type: "text",
text: `Transaction Receipt:\n${JSON.stringify(receipt, null, 2)}`,
},
],
};
}
catch (error) {
return {
content: [{ type: "text", text: `Error: ${handleError(error)}` }],
};
}
}));
// 6. Get Gas Price
server.tool("getGasPrice", "Get the current gas price on Arbitrum", {}, () => __awaiter(this, void 0, void 0, function* () {
try {
const gasPrice = yield alchemy.core.getGasPrice();
return {
content: [
{
type: "text",
text: `Current gas price: ${gasPrice.toString()} wei`,
},
],
};
}
catch (error) {
return {
content: [{ type: "text", text: `Error: ${handleError(error)}` }],
};
}
}));
}