decode_tx
Decode Bitcoin transaction data by inputting raw hexadecimal data to extract transaction details using the Bitcoin MCP Server. Simplify analysis of Bitcoin network activities.
Instructions
Decode a Bitcoin transaction
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| rawHex | Yes | Transaction hex |
Implementation Reference
- src/server/tools.ts:107-128 (handler)MCP tool handler for decode_tx: validates input with DecodeTxSchema, calls bitcoinService.decodeTx(rawHex), formats response as text content with TXID, version, input/output counts, locktime.export async function handleDecodeTx( bitcoinService: BitcoinService, args: unknown ) { const result = DecodeTxSchema.safeParse(args); if (!result.success) { throw new McpError( ErrorCode.InvalidParams, `Invalid parameters: ${result.error.message}` ); } const tx = bitcoinService.decodeTx(result.data.rawHex); return { content: [ { type: "text", text: `Decoded transaction:\nTXID: ${tx.txid}\nVersion: ${tx.version}\nInputs: ${tx.inputs.length}\nOutputs: ${tx.outputs.length}\nLocktime: ${tx.locktime}`, }, ] as TextContent[], }; }
- src/types.ts:122-124 (schema)Zod schema for decode_tx input validation: requires 'rawHex' as non-empty string.export const DecodeTxSchema = z.object({ rawHex: z.string().min(1, "Raw transaction hex is required"), });
- src/server/base.ts:136-145 (registration)Tool registration metadata in listToolsRequestHandler: name, description, inputSchema for decode_tx.name: "decode_tx", description: "Decode a Bitcoin transaction", inputSchema: { type: "object", properties: { rawHex: { type: "string", description: "Transaction hex" }, }, required: ["rawHex"], }, } as Tool,
- src/server/base.ts:206-208 (registration)Dispatch to handleDecodeTx in the CallToolRequestHandler switch statement.case "decode_tx": { return handleDecodeTx(this.bitcoinService, args); }
- src/services/bitcoin.ts:137-163 (helper)BitcoinService.decodeTx implementation: parses rawHex with bitcoinjs-lib Transaction.fromHex, extracts txid, version, inputs, outputs (with addresses), locktime; throws BitcoinError on failure.decodeTx(rawHex: string): DecodedTx { try { const tx = bitcoin.Transaction.fromHex(rawHex); return { txid: tx.getId(), version: tx.version, inputs: tx.ins.map((input) => ({ txid: Buffer.from(input.hash).reverse().toString("hex"), vout: input.index, sequence: input.sequence, })), outputs: tx.outs.map((output) => ({ value: output.value, scriptPubKey: output.script.toString("hex"), address: this.tryGetAddress(output.script), })), locktime: tx.locktime, }; } catch (error) { logger.error({ error, rawHex }, "Failed to decode transaction"); throw new BitcoinError( "Failed to decode transaction", BitcoinErrorCode.DECODE_ERROR ); } }