get_transaction
Retrieve Bitcoin transaction details by providing a transaction ID to access blockchain data.
Instructions
Get transaction details
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| txid | Yes | Transaction ID |
Implementation Reference
- src/index.ts:268-323 (handler)The main execution handler for the 'get-transaction' tool. Fetches raw transaction data via helper and formats a detailed Markdown-like text response including basic info, status, inputs, and outputs.async ({ txid }) => { try { const tx = await getTransaction(txid); let result = `**Transaction: ${tx.txid}**\n\n`; result += `**Basic Info:**\n`; result += `- Version: ${tx.version}\n`; result += `- Size: ${tx.size} bytes\n`; result += `- Weight: ${tx.weight} WU\n`; result += `- Fee: ${tx.fee} sats\n`; result += `- Locktime: ${tx.locktime}\n\n`; result += `**Status:**\n`; if (tx.status.confirmed) { result += `- Confirmed in block ${tx.status.block_height}\n`; result += `- Block hash: ${tx.status.block_hash}\n`; result += `- Block time: ${new Date(tx.status.block_time! * 1000).toISOString()}\n`; } else { result += `- Unconfirmed (in mempool)\n`; } result += "\n"; result += `**Inputs (${tx.vin.length}):**\n`; tx.vin.forEach((input, index) => { if (input.prevout) { result += `${index + 1}. ${input.txid.substring(0, 16)}...:${input.vout} - ${(input.prevout.value / 100000000).toFixed(8)} BTC\n`; } else { result += `${index + 1}. Coinbase transaction\n`; } }); result += "\n"; result += `**Outputs (${tx.vout.length}):**\n`; tx.vout.forEach((output, index) => { result += `${index + 1}. ${(output.value / 100000000).toFixed(8)} BTC to ${output.scriptpubkey_address || "Unknown address"}\n`; }); return { content: [ { type: "text", text: result, }, ], }; } catch (error: any) { return { content: [ { type: "text", text: `Error fetching transaction data: ${error.message || "Unknown error"}`, }, ], }; } },
- src/index.ts:265-267 (schema)Zod input schema defining the 'txid' parameter for the tool.{ txid: z.string().describe("Transaction ID (hash) to query"), },
- src/index.ts:262-324 (registration)Registration of the 'get-transaction' tool on the MCP server using server.tool() with name, description, schema, and handler.server.tool( "get-transaction", "Get detailed information about a Bitcoin transaction", { txid: z.string().describe("Transaction ID (hash) to query"), }, async ({ txid }) => { try { const tx = await getTransaction(txid); let result = `**Transaction: ${tx.txid}**\n\n`; result += `**Basic Info:**\n`; result += `- Version: ${tx.version}\n`; result += `- Size: ${tx.size} bytes\n`; result += `- Weight: ${tx.weight} WU\n`; result += `- Fee: ${tx.fee} sats\n`; result += `- Locktime: ${tx.locktime}\n\n`; result += `**Status:**\n`; if (tx.status.confirmed) { result += `- Confirmed in block ${tx.status.block_height}\n`; result += `- Block hash: ${tx.status.block_hash}\n`; result += `- Block time: ${new Date(tx.status.block_time! * 1000).toISOString()}\n`; } else { result += `- Unconfirmed (in mempool)\n`; } result += "\n"; result += `**Inputs (${tx.vin.length}):**\n`; tx.vin.forEach((input, index) => { if (input.prevout) { result += `${index + 1}. ${input.txid.substring(0, 16)}...:${input.vout} - ${(input.prevout.value / 100000000).toFixed(8)} BTC\n`; } else { result += `${index + 1}. Coinbase transaction\n`; } }); result += "\n"; result += `**Outputs (${tx.vout.length}):**\n`; tx.vout.forEach((output, index) => { result += `${index + 1}. ${(output.value / 100000000).toFixed(8)} BTC to ${output.scriptpubkey_address || "Unknown address"}\n`; }); return { content: [ { type: "text", text: result, }, ], }; } catch (error: any) { return { content: [ { type: "text", text: `Error fetching transaction data: ${error.message || "Unknown error"}`, }, ], }; } }, );
- src/index.ts:89-95 (helper)Helper function that fetches the raw Transaction data from the mempool.space API.async function getTransaction(txid: string): Promise<Transaction> { const res = await superagent .get(`${MEMPOOL_API_BASE}/tx/${txid}`) .set("User-Agent", USER_AGENT); return res.body; }
- src/index.ts:36-51 (schema)TypeScript interface defining the structure of a Transaction object used by the tool.type Transaction = { txid: string; version: number; locktime: number; vin: any[]; vout: any[]; size: number; weight: number; fee: number; status: { confirmed: boolean; block_height?: number; block_hash?: string; block_time?: number; }; };