getMiningStatus.ts•2.02 kB
import { z } from "zod";
import { CallToolResult } from "@modelcontextprotocol/sdk/types";
import { INFURA_CHAIN_URLS } from "../../config/chains.js";
import { JsonRpcResponse } from "../../types/rpc.js";
export const getMiningStatus = {
name: "eth_get_mining_status",
description:
"Returns true if the client is actively mining new blocks.",
schema: {
chainid: z
.number()
.optional()
.describe("EVM chain ID (default: 1 for Ethereum mainnet)"),
},
handler: async ({
chainid,
}: {
chainid?: number;
}): Promise<CallToolResult> => {
try {
const selectedChainId = chainid ?? 1;
const url = INFURA_CHAIN_URLS[selectedChainId];
if (!url) {
return {
content: [
{ type: "text", text: `Unsupported chain ID: ${selectedChainId}` },
],
};
}
const body = {
jsonrpc: "2.0",
method: "eth_mining",
params: [],
id: 1,
};
const res = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
const data: JsonRpcResponse = await res.json();
if (typeof data.result !== "boolean") {
return {
content: [
{
type: "text",
text: `Error fetching mining status: ${
data.error?.message || "Unknown error"
}`,
},
],
};
}
return {
content: [
{
type: "text",
text: `Mining status on chainid ${selectedChainId}: ${data.result}`,
},
],
};
} catch (err: unknown) {
const message =
err instanceof Error
? err.message
: typeof err === "string"
? err
: JSON.stringify(err);
return {
content: [
{ type: "text", text: `Error fetching mining status: ${message}` },
],
};
}
},
};